您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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