| 12 | #include "raylib-cpp.hpp" |
| 13 | |
| 14 | int main(void) |
| 15 | { |
| 16 | // Initialization |
| 17 | //-------------------------------------------------------------------------------------- |
| 18 | const int screenWidth = 800; |
| 19 | const int screenHeight = 450; |
| 20 | |
| 21 | raylib::Window window(screenWidth, screenHeight, "raylib [models] example - first person maze"); |
| 22 | |
| 23 | // Define the camera to look into our 3d world |
| 24 | raylib::Camera camera({ 0.2f, 0.4f, 0.2f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f); |
| 25 | |
| 26 | raylib::Image imMap("resources/cubicmap.png"); // Load cubicmap image (RAM) |
| 27 | raylib::Texture cubicmap(imMap); // Convert image to texture to display (VRAM) |
| 28 | raylib::MeshUnmanaged mesh = raylib::MeshUnmanaged::Cubicmap(imMap, Vector3{ 1.0f, 1.0f, 1.0f }); // Use MeshUnmanaged, Mesh will be handled by Model |
| 29 | raylib::Model model(mesh); |
| 30 | |
| 31 | // NOTE: By default each cube is mapped to one part of texture atlas |
| 32 | raylib::Texture texture("resources/cubicmap_atlas.png"); // Load map texture |
| 33 | model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture; // Set map diffuse texture |
| 34 | |
| 35 | // Get map image data to be used for collision detection |
| 36 | Color *mapPixels = imMap.LoadColors(); |
| 37 | |
| 38 | imMap.Unload(); // Unload image from RAM |
| 39 | |
| 40 | raylib::Vector3 mapPosition(-16.0f, 0.0f, -8.0f); // Set model position |
| 41 | raylib::Vector3 playerPosition(camera.position); // Set player position |
| 42 | |
| 43 | SetTargetFPS(60); // Set our game to run at 60 frames-per-second |
| 44 | //-------------------------------------------------------------------------------------- |
| 45 | |
| 46 | // Main game loop |
| 47 | while (!window.ShouldClose()) { // Detect window close button or ESC key |
| 48 | // Update |
| 49 | //---------------------------------------------------------------------------------- |
| 50 | raylib::Vector3 oldCamPos(camera.position); // Store old camera position |
| 51 | |
| 52 | camera.Update(CAMERA_FIRST_PERSON); // Update camera |
| 53 | |
| 54 | // Check player collision (we simplify to 2D collision detection) |
| 55 | raylib::Vector2 playerPos(camera.position.x, camera.position.z); |
| 56 | float playerRadius = 0.1f; // Collision radius (player is modelled as a cilinder for collision) |
| 57 | |
| 58 | int playerCellX = static_cast<int>(playerPos.x - mapPosition.x + 0.5f); |
| 59 | int playerCellY = static_cast<int>(playerPos.y - mapPosition.z + 0.5f); |
| 60 | |
| 61 | // Out-of-limits security check |
| 62 | if (playerCellX < 0) playerCellX = 0; |
| 63 | else if (playerCellX >= cubicmap.width) playerCellX = cubicmap.width - 1; |
| 64 | |
| 65 | if (playerCellY < 0) playerCellY = 0; |
| 66 | else if (playerCellY >= cubicmap.height) playerCellY = cubicmap.height - 1; |
| 67 | |
| 68 | // Check map collisions using image data and player position |
| 69 | // TODO: Improvement: Just check player surrounding cells for collision |
| 70 | for (int y = 0; y < cubicmap.height; y++) |
| 71 | { |
nothing calls this directly
no test coverage detected