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.

126 line
5.4 KiB

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