#include "map.h" #include "game.h" #include "world.h" void repositionMap(Map* map) { map->rect.x = GetRenderWidth() - map->rect.width; } void zoomMap(Map* map, float zoom) { map->zoom = zoom; map->heightmapRect.width = WORLD_IMAGE_WIDTH / zoom; map->heightmapRect.height = WORLD_IMAGE_HEIGHT / zoom; } void initMap(Map* map, const Settings* settings) { *map = (Map){ .rect = (Rectangle){0.0, 0.0, settings->mapPreviewWidth, settings->mapPreviewHeight}, .isEnabled = settings->isMapPreviewEnabledDefault, .isFullSize = false, .playerPosition = Vector2Zero(), .playerRotation = 0.0 }; zoomMap(map, settings->mapPreviewZoomDefault); repositionMap(map); } void updateMapPreview(Map* map, Game* game) { const Settings* settings = &game->settings; const World* world = &game->world; float mouseWheel = GetMouseWheelMove(); if (mouseWheel != 0.0) { zoomMap(map, Clamp(map->zoom + mouseWheel, MAP_ZOOM_MIN, MAP_ZOOM_MAX)); } // Player position. map->playerPosition = (Vector2){ WORLD_IMAGE_WIDTH / world->size.x * game->player.position.x, WORLD_IMAGE_HEIGHT / world->size.z * game->player.position.z }; Vector2 playerPreviewPosition = Vector2Add( (Vector2){map->rect.x, map->rect.y}, (Vector2){map->rect.width / 2.0, map->rect.height / 2.0}); map->heightmapRect.x = map->playerPosition.x - (map->heightmapRect.width / 2.0); map->heightmapRect.y = map->playerPosition.y - (map->heightmapRect.height / 2.0); // Preview out of bounds. bool outOfX = false; bool outOfY = false; float oldX = map->heightmapRect.x; float oldY = map->heightmapRect.y; if (map->heightmapRect.x + map->heightmapRect.width > WORLD_IMAGE_WIDTH) { map->heightmapRect.x = WORLD_IMAGE_WIDTH - map->heightmapRect.width; outOfX = true; } else if (map->heightmapRect.x < 0.0) { map->heightmapRect.x = 0.0; outOfX = true; } if (map->heightmapRect.y + map->heightmapRect.height > WORLD_IMAGE_HEIGHT) { map->heightmapRect.y = WORLD_IMAGE_HEIGHT - map->heightmapRect.height; outOfY = true; } else if (map->heightmapRect.y < 0.0) { map->heightmapRect.y = 0.0; outOfY = true; } if (outOfX) { playerPreviewPosition.x += (oldX - map->heightmapRect.x) * (map->rect.width / map->heightmapRect.width); } if (outOfY) { playerPreviewPosition.y += (oldY - map->heightmapRect.y) * (map->rect.height / map->heightmapRect.height); } // Draw map. DrawTexturePro(world->heightmapTexture, map->heightmapRect, map->rect, (Vector2){0.0, 0.0}, 0.0, settings->mapColor); // Draw player. DrawCircleV(playerPreviewPosition, 3.0, BLUE); // Outline. DrawRectangleLinesEx(map->rect, 3.0, BLUE); } void updateMap(Map* map, Game* game) { const Settings* settings = &game->settings; // Handle window resize. if (IsWindowResized()) { repositionMap(map); } // Toggle preview. if (IsKeyPressed(settings->toggleMapPreviewKey)) { map->isEnabled = !map->isEnabled; } if (!map->isEnabled) { return; } updateMapPreview(map, game); }