2019-04-24 00:28:11 +03:00
|
|
|
/*******************************************************************************************
|
|
|
|
*
|
|
|
|
* raylib [models] example - first person maze
|
|
|
|
*
|
|
|
|
* This example has been created using raylib 2.5 (www.raylib.com)
|
|
|
|
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2019 Ramon Santamaria (@raysan5)
|
|
|
|
*
|
|
|
|
********************************************************************************************/
|
|
|
|
|
|
|
|
#include "raylib.h"
|
|
|
|
|
2019-05-02 14:24:02 +03:00
|
|
|
#include <stdlib.h> // Required for: free()
|
2019-05-02 14:15:05 +03:00
|
|
|
|
2019-05-20 17:36:42 +03:00
|
|
|
int main(void)
|
2019-04-24 00:28:11 +03:00
|
|
|
{
|
|
|
|
// Initialization
|
|
|
|
//--------------------------------------------------------------------------------------
|
2019-05-20 17:36:42 +03:00
|
|
|
const int screenWidth = 800;
|
|
|
|
const int screenHeight = 450;
|
2019-04-24 00:28:11 +03:00
|
|
|
|
|
|
|
InitWindow(screenWidth, screenHeight, "raylib [models] example - first person maze");
|
|
|
|
|
|
|
|
// Define the camera to look into our 3d world
|
2019-05-27 01:18:15 +03:00
|
|
|
Camera camera = { { 0.2f, 0.4f, 0.2f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f, 0 };
|
2019-04-24 00:28:11 +03:00
|
|
|
|
2019-05-02 13:22:15 +03:00
|
|
|
Image imMap = LoadImage("resources/cubicmap.png"); // Load cubicmap image (RAM)
|
|
|
|
Texture2D cubicmap = LoadTextureFromImage(imMap); // Convert image to texture to display (VRAM)
|
|
|
|
Mesh mesh = GenMeshCubicmap(imMap, (Vector3){ 1.0f, 1.0f, 1.0f });
|
2019-04-24 00:28:11 +03:00
|
|
|
Model model = LoadModelFromMesh(mesh);
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2019-04-24 00:28:11 +03:00
|
|
|
// NOTE: By default each cube is mapped to one part of texture atlas
|
|
|
|
Texture2D texture = LoadTexture("resources/cubicmap_atlas.png"); // Load map texture
|
|
|
|
model.materials[0].maps[MAP_DIFFUSE].texture = texture; // Set map diffuse texture
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2019-05-02 13:22:15 +03:00
|
|
|
// Get map image data to be used for collision detection
|
|
|
|
Color *mapPixels = GetImageData(imMap);
|
|
|
|
UnloadImage(imMap); // Unload image from RAM
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2019-04-24 00:28:11 +03:00
|
|
|
Vector3 mapPosition = { -16.0f, 0.0f, -8.0f }; // Set model position
|
|
|
|
Vector3 playerPosition = camera.position; // Set player position
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2019-04-24 00:28:11 +03:00
|
|
|
SetCameraMode(camera, CAMERA_FIRST_PERSON); // Set camera mode
|
|
|
|
|
|
|
|
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Main game loop
|
|
|
|
while (!WindowShouldClose()) // Detect window close button or ESC key
|
|
|
|
{
|
|
|
|
// Update
|
|
|
|
//----------------------------------------------------------------------------------
|
2019-05-02 13:22:15 +03:00
|
|
|
Vector3 oldCamPos = camera.position; // Store old camera position
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2019-04-24 00:28:11 +03:00
|
|
|
UpdateCamera(&camera); // Update camera
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2019-05-02 13:22:15 +03:00
|
|
|
// Check player collision (we simplify to 2D collision detection)
|
|
|
|
Vector2 playerPos = { camera.position.x, camera.position.z };
|
|
|
|
float playerRadius = 0.1f; // Collision radius (player is modelled as a cilinder for collision)
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2019-05-02 13:22:15 +03:00
|
|
|
int playerCellX = (int)(playerPos.x - mapPosition.x + 0.5f);
|
|
|
|
int playerCellY = (int)(playerPos.y - mapPosition.z + 0.5f);
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2019-05-02 13:22:15 +03:00
|
|
|
// Out-of-limits security check
|
|
|
|
if (playerCellX < 0) playerCellX = 0;
|
|
|
|
else if (playerCellX >= cubicmap.width) playerCellX = cubicmap.width - 1;
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2019-05-02 13:22:15 +03:00
|
|
|
if (playerCellY < 0) playerCellY = 0;
|
|
|
|
else if (playerCellY >= cubicmap.height) playerCellY = cubicmap.height - 1;
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2019-05-02 13:22:15 +03:00
|
|
|
// Check map collisions using image data and player position
|
|
|
|
// TODO: Improvement: Just check player surrounding cells for collision
|
|
|
|
for (int y = 0; y < cubicmap.height; y++)
|
|
|
|
{
|
|
|
|
for (int x = 0; x < cubicmap.width; x++)
|
|
|
|
{
|
|
|
|
if ((mapPixels[y*cubicmap.width + x].r == 255) && // Collision: white pixel, only check R channel
|
2019-05-20 17:36:42 +03:00
|
|
|
(CheckCollisionCircleRec(playerPos, playerRadius,
|
2019-05-02 13:22:15 +03:00
|
|
|
(Rectangle){ mapPosition.x - 0.5f + x*1.0f, mapPosition.z - 0.5f + y*1.0f, 1.0f, 1.0f })))
|
|
|
|
{
|
|
|
|
// Collision detected, reset camera position
|
|
|
|
camera.position = oldCamPos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-24 00:28:11 +03:00
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Draw
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
BeginDrawing();
|
|
|
|
|
|
|
|
ClearBackground(RAYWHITE);
|
|
|
|
|
|
|
|
BeginMode3D(camera);
|
|
|
|
|
|
|
|
DrawModel(model, mapPosition, 1.0f, WHITE); // Draw maze map
|
|
|
|
//DrawCubeV(playerPosition, (Vector3){ 0.2f, 0.4f, 0.2f }, RED); // Draw player
|
|
|
|
|
|
|
|
EndMode3D();
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2019-04-24 00:28:11 +03:00
|
|
|
DrawTextureEx(cubicmap, (Vector2){ GetScreenWidth() - cubicmap.width*4 - 20, 20 }, 0.0f, 4.0f, WHITE);
|
|
|
|
DrawRectangleLines(GetScreenWidth() - cubicmap.width*4 - 20, 20, cubicmap.width*4, cubicmap.height*4, GREEN);
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2019-05-02 13:22:15 +03:00
|
|
|
// Draw player position radar
|
|
|
|
DrawRectangle(GetScreenWidth() - cubicmap.width*4 - 20 + playerCellX*4, 20 + playerCellY*4, 4, 4, RED);
|
2019-04-24 00:28:11 +03:00
|
|
|
|
|
|
|
DrawFPS(10, 10);
|
|
|
|
|
|
|
|
EndDrawing();
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
}
|
|
|
|
|
|
|
|
// De-Initialization
|
|
|
|
//--------------------------------------------------------------------------------------
|
2019-05-02 13:22:15 +03:00
|
|
|
free(mapPixels); // Unload color array
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2019-04-24 00:28:11 +03:00
|
|
|
UnloadTexture(cubicmap); // Unload cubicmap texture
|
|
|
|
UnloadTexture(texture); // Unload map texture
|
|
|
|
UnloadModel(model); // Unload map model
|
|
|
|
|
|
|
|
CloseWindow(); // Close window and OpenGL context
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|