Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

133 строки
5.9 KiB

2 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
  1. /*******************************************************************************************
  2. *
  3. * raylib [models] example - first person maze
  4. *
  5. * Example originally created with raylib 2.5, last time updated with raylib 3.5
  6. *
  7. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  8. * BSD-like license that allows static linking with closed source software
  9. *
  10. * Copyright (c) 2019-2023 Ramon Santamaria (@raysan5)
  11. *
  12. ********************************************************************************************/
  13. #include "raylib.h"
  14. #include <stdlib.h> // Required for: free()
  15. //------------------------------------------------------------------------------------
  16. // Program main entry point
  17. //------------------------------------------------------------------------------------
  18. int main(void)
  19. {
  20. // Initialization
  21. //--------------------------------------------------------------------------------------
  22. const int screenWidth = 800;
  23. const int screenHeight = 450;
  24. InitWindow(screenWidth, screenHeight, "raylib [models] example - first person maze");
  25. // Define the camera to look into our 3d world
  26. Camera camera = { 0 };
  27. camera.position = (Vector3){ 0.2f, 0.4f, 0.2f }; // Camera position
  28. camera.target = (Vector3){ 0.185f, 0.4f, 0.0f }; // Camera looking at point
  29. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  30. camera.fovy = 45.0f; // Camera field-of-view Y
  31. camera.projection = CAMERA_PERSPECTIVE; // Camera projection type
  32. Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position
  33. Image imMap = LoadImage("resources/cubicmap.png"); // Load cubicmap image (RAM)
  34. Texture2D cubicmap = LoadTextureFromImage(imMap); // Convert image to texture to display (VRAM)
  35. Mesh mesh = GenMeshCubicmap(imMap, (Vector3){ 1.0f, 1.0f, 1.0f });
  36. Model model = LoadModelFromMesh(mesh);
  37. // NOTE: By default each cube is mapped to one part of texture atlas
  38. Texture2D texture = LoadTexture("resources/cubicmap_atlas.png"); // Load map texture
  39. model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture; // Set map diffuse texture
  40. // Get map image data to be used for collision detection
  41. Color *mapPixels = LoadImageColors(imMap);
  42. UnloadImage(imMap); // Unload image from RAM
  43. Vector3 mapPosition = { -16.0f, 0.0f, -8.0f }; // Set model position
  44. DisableCursor(); // Limit cursor to relative movement inside the window
  45. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  46. //--------------------------------------------------------------------------------------
  47. // Main game loop
  48. while (!WindowShouldClose()) // Detect window close button or ESC key
  49. {
  50. // Update
  51. //----------------------------------------------------------------------------------
  52. Vector3 oldCamPos = camera.position; // Store old camera position
  53. UpdateCamera(&camera, CAMERA_FIRST_PERSON);
  54. // Check player collision (we simplify to 2D collision detection)
  55. Vector2 playerPos = { camera.position.x, camera.position.z };
  56. float playerRadius = 0.1f; // Collision radius (player is modelled as a cilinder for collision)
  57. int playerCellX = (int)(playerPos.x - mapPosition.x + 0.5f);
  58. int playerCellY = (int)(playerPos.y - mapPosition.z + 0.5f);
  59. // Out-of-limits security check
  60. if (playerCellX < 0) playerCellX = 0;
  61. else if (playerCellX >= cubicmap.width) playerCellX = cubicmap.width - 1;
  62. if (playerCellY < 0) playerCellY = 0;
  63. else if (playerCellY >= cubicmap.height) playerCellY = cubicmap.height - 1;
  64. // Check map collisions using image data and player position
  65. // TODO: Improvement: Just check player surrounding cells for collision
  66. for (int y = 0; y < cubicmap.height; y++)
  67. {
  68. for (int x = 0; x < cubicmap.width; x++)
  69. {
  70. if ((mapPixels[y*cubicmap.width + x].r == 255) && // Collision: white pixel, only check R channel
  71. (CheckCollisionCircleRec(playerPos, playerRadius,
  72. (Rectangle){ mapPosition.x - 0.5f + x*1.0f, mapPosition.z - 0.5f + y*1.0f, 1.0f, 1.0f })))
  73. {
  74. // Collision detected, reset camera position
  75. camera.position = oldCamPos;
  76. }
  77. }
  78. }
  79. //----------------------------------------------------------------------------------
  80. // Draw
  81. //----------------------------------------------------------------------------------
  82. BeginDrawing();
  83. ClearBackground(RAYWHITE);
  84. BeginMode3D(camera);
  85. DrawModel(model, mapPosition, 1.0f, WHITE); // Draw maze map
  86. EndMode3D();
  87. DrawTextureEx(cubicmap, (Vector2){ GetScreenWidth() - cubicmap.width*4.0f - 20, 20.0f }, 0.0f, 4.0f, WHITE);
  88. DrawRectangleLines(GetScreenWidth() - cubicmap.width*4 - 20, 20, cubicmap.width*4, cubicmap.height*4, GREEN);
  89. // Draw player position radar
  90. DrawRectangle(GetScreenWidth() - cubicmap.width*4 - 20 + playerCellX*4, 20 + playerCellY*4, 4, 4, RED);
  91. DrawFPS(10, 10);
  92. EndDrawing();
  93. //----------------------------------------------------------------------------------
  94. }
  95. // De-Initialization
  96. //--------------------------------------------------------------------------------------
  97. UnloadImageColors(mapPixels); // Unload color array
  98. UnloadTexture(cubicmap); // Unload cubicmap texture
  99. UnloadTexture(texture); // Unload map texture
  100. UnloadModel(model); // Unload map model
  101. CloseWindow(); // Close window and OpenGL context
  102. //--------------------------------------------------------------------------------------
  103. return 0;
  104. }