You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
5.5 KiB

  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-2022 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.2f, 0.4f, 0.2f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f, 0 };
  27. Image imMap = LoadImage("resources/cubicmap.png"); // Load cubicmap image (RAM)
  28. Texture2D cubicmap = LoadTextureFromImage(imMap); // Convert image to texture to display (VRAM)
  29. Mesh mesh = GenMeshCubicmap(imMap, (Vector3){ 1.0f, 1.0f, 1.0f });
  30. Model model = LoadModelFromMesh(mesh);
  31. // NOTE: By default each cube is mapped to one part of texture atlas
  32. Texture2D texture = LoadTexture("resources/cubicmap_atlas.png"); // Load map texture
  33. model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture; // Set map diffuse texture
  34. // Get map image data to be used for collision detection
  35. Color *mapPixels = LoadImageColors(imMap);
  36. UnloadImage(imMap); // Unload image from RAM
  37. Vector3 mapPosition = { -16.0f, 0.0f, -8.0f }; // Set model position
  38. SetCameraMode(camera, CAMERA_FIRST_PERSON); // Set camera mode
  39. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  40. //--------------------------------------------------------------------------------------
  41. // Main game loop
  42. while (!WindowShouldClose()) // Detect window close button or ESC key
  43. {
  44. // Update
  45. //----------------------------------------------------------------------------------
  46. Vector3 oldCamPos = camera.position; // Store old camera position
  47. UpdateCamera(&camera);
  48. // Check player collision (we simplify to 2D collision detection)
  49. Vector2 playerPos = { camera.position.x, camera.position.z };
  50. float playerRadius = 0.1f; // Collision radius (player is modelled as a cilinder for collision)
  51. int playerCellX = (int)(playerPos.x - mapPosition.x + 0.5f);
  52. int playerCellY = (int)(playerPos.y - mapPosition.z + 0.5f);
  53. // Out-of-limits security check
  54. if (playerCellX < 0) playerCellX = 0;
  55. else if (playerCellX >= cubicmap.width) playerCellX = cubicmap.width - 1;
  56. if (playerCellY < 0) playerCellY = 0;
  57. else if (playerCellY >= cubicmap.height) playerCellY = cubicmap.height - 1;
  58. // Check map collisions using image data and player position
  59. // TODO: Improvement: Just check player surrounding cells for collision
  60. for (int y = 0; y < cubicmap.height; y++)
  61. {
  62. for (int x = 0; x < cubicmap.width; x++)
  63. {
  64. if ((mapPixels[y*cubicmap.width + x].r == 255) && // Collision: white pixel, only check R channel
  65. (CheckCollisionCircleRec(playerPos, playerRadius,
  66. (Rectangle){ mapPosition.x - 0.5f + x*1.0f, mapPosition.z - 0.5f + y*1.0f, 1.0f, 1.0f })))
  67. {
  68. // Collision detected, reset camera position
  69. camera.position = oldCamPos;
  70. }
  71. }
  72. }
  73. //----------------------------------------------------------------------------------
  74. // Draw
  75. //----------------------------------------------------------------------------------
  76. BeginDrawing();
  77. ClearBackground(RAYWHITE);
  78. BeginMode3D(camera);
  79. DrawModel(model, mapPosition, 1.0f, WHITE); // Draw maze map
  80. EndMode3D();
  81. DrawTextureEx(cubicmap, (Vector2){ GetScreenWidth() - cubicmap.width*4.0f - 20, 20.0f }, 0.0f, 4.0f, WHITE);
  82. DrawRectangleLines(GetScreenWidth() - cubicmap.width*4 - 20, 20, cubicmap.width*4, cubicmap.height*4, GREEN);
  83. // Draw player position radar
  84. DrawRectangle(GetScreenWidth() - cubicmap.width*4 - 20 + playerCellX*4, 20 + playerCellY*4, 4, 4, RED);
  85. DrawFPS(10, 10);
  86. EndDrawing();
  87. //----------------------------------------------------------------------------------
  88. }
  89. // De-Initialization
  90. //--------------------------------------------------------------------------------------
  91. UnloadImageColors(mapPixels); // Unload color array
  92. UnloadTexture(cubicmap); // Unload cubicmap texture
  93. UnloadTexture(texture); // Unload map texture
  94. UnloadModel(model); // Unload map model
  95. CloseWindow(); // Close window and OpenGL context
  96. //--------------------------------------------------------------------------------------
  97. return 0;
  98. }