|
|
@ -23,16 +23,19 @@ int main() |
|
|
|
// Define the camera to look into our 3d world |
|
|
|
Camera camera = {{ 0.2f, 0.4f, 0.2f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f, 0 }; |
|
|
|
|
|
|
|
Image image = LoadImage("resources/cubicmap.png"); // Load cubicmap image (RAM) |
|
|
|
Texture2D cubicmap = LoadTextureFromImage(image); // Convert image to texture to display (VRAM) |
|
|
|
Mesh mesh = GenMeshCubicmap(image, (Vector3){ 1.0f, 1.0f, 1.0f }); |
|
|
|
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 }); |
|
|
|
Model model = LoadModelFromMesh(mesh); |
|
|
|
UnloadImage(image); // Unload cubesmap image from RAM, already uploaded to VRAM |
|
|
|
|
|
|
|
// 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 |
|
|
|
|
|
|
|
// Get map image data to be used for collision detection |
|
|
|
Color *mapPixels = GetImageData(imMap); |
|
|
|
UnloadImage(imMap); // Unload image from RAM |
|
|
|
|
|
|
|
Vector3 mapPosition = { -16.0f, 0.0f, -8.0f }; // Set model position |
|
|
|
Vector3 playerPosition = camera.position; // Set player position |
|
|
|
|
|
|
@ -46,7 +49,39 @@ int main() |
|
|
|
{ |
|
|
|
// Update |
|
|
|
//---------------------------------------------------------------------------------- |
|
|
|
Vector3 oldCamPos = camera.position; // Store old camera position |
|
|
|
|
|
|
|
UpdateCamera(&camera); // Update camera |
|
|
|
|
|
|
|
// 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) |
|
|
|
|
|
|
|
int playerCellX = (int)(playerPos.x - mapPosition.x + 0.5f); |
|
|
|
int playerCellY = (int)(playerPos.y - mapPosition.z + 0.5f); |
|
|
|
|
|
|
|
// Out-of-limits security check |
|
|
|
if (playerCellX < 0) playerCellX = 0; |
|
|
|
else if (playerCellX >= cubicmap.width) playerCellX = cubicmap.width - 1; |
|
|
|
|
|
|
|
if (playerCellY < 0) playerCellY = 0; |
|
|
|
else if (playerCellY >= cubicmap.height) playerCellY = cubicmap.height - 1; |
|
|
|
|
|
|
|
// 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 |
|
|
|
(CheckCollisionCircleRec(playerPos, playerRadius, |
|
|
|
(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; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
//---------------------------------------------------------------------------------- |
|
|
|
|
|
|
|
// Draw |
|
|
@ -64,6 +99,9 @@ int main() |
|
|
|
|
|
|
|
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); |
|
|
|
|
|
|
|
// Draw player position radar |
|
|
|
DrawRectangle(GetScreenWidth() - cubicmap.width*4 - 20 + playerCellX*4, 20 + playerCellY*4, 4, 4, RED); |
|
|
|
|
|
|
|
DrawFPS(10, 10); |
|
|
|
|
|
|
@ -73,6 +111,8 @@ int main() |
|
|
|
|
|
|
|
// De-Initialization |
|
|
|
//-------------------------------------------------------------------------------------- |
|
|
|
free(mapPixels); // Unload color array |
|
|
|
|
|
|
|
UnloadTexture(cubicmap); // Unload cubicmap texture |
|
|
|
UnloadTexture(texture); // Unload map texture |
|
|
|
UnloadModel(model); // Unload map model |
|
|
|