diff options
| author | nathan <nathansmith@disroot.org> | 2025-11-24 15:47:25 +0000 |
|---|---|---|
| committer | nathan <nathansmith@disroot.org> | 2025-11-24 15:47:25 +0000 |
| commit | 05624b044e59e70d10b786538f55e77c19cc3c8c (patch) | |
| tree | b51187d58403bcb2c00fc24fbca7643f176d7961 /src/map.c | |
| parent | eff6d7308d69ece8b89fe22e529b815f67a893e2 (diff) | |
| download | FindThings-05624b044e59e70d10b786538f55e77c19cc3c8c.tar.gz FindThings-05624b044e59e70d10b786538f55e77c19cc3c8c.tar.bz2 FindThings-05624b044e59e70d10b786538f55e77c19cc3c8c.zip | |
Improved map
Diffstat (limited to 'src/map.c')
| -rw-r--r-- | src/map.c | 138 |
1 files changed, 70 insertions, 68 deletions
@@ -1,112 +1,109 @@ #include "map.h" #include "game.h" #include "world.h" +#include "player.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, + .render = LoadRenderTexture(settings->mapPreviewWidth, + settings->mapPreviewHeight), + .camera = (Camera2D){ + .offset = (Vector2){settings->mapPreviewWidth / 2.0, + settings->mapPreviewHeight / 2.0}, + .target = Vector2Zero(), + .rotation = 0.0, + .zoom = settings->mapPreviewZoomDefault + }, .playerPosition = Vector2Zero(), - .playerRotation = 0.0 + .isEnabled = settings->isMapPreviewEnabledDefault, + .isFullSize = false }; - zoomMap(map, settings->mapPreviewZoomDefault); repositionMap(map); } -void updateMapPreview(Map* map, Game* game) +void drawMapPlayer(Map* map, Player* player, Vector2 position, + float width, float height) { - const Settings* settings = &game->settings; - const World* world = &game->world; - - float mouseWheel = GetMouseWheelMove(); + Vector2 playerPreview[3] = { + (Vector2){0.0, height}, + (Vector2){width, -height}, + (Vector2){-width, -height} + }; - if (mouseWheel != 0.0) + // Move player to position. + for (int index = 0; index < 3; ++index) { - zoomMap(map, Clamp(map->zoom + mouseWheel, MAP_ZOOM_MIN, MAP_ZOOM_MAX)); + playerPreview[index] = Vector2Rotate(playerPreview[index], + player->cameraAngle.x); + playerPreview[index] = Vector2Add(playerPreview[index], position); } + // Draw triangles. + DrawTriangle(playerPreview[0], playerPreview[1], playerPreview[2], BLACK); + DrawTriangleLines(playerPreview[0], playerPreview[1], playerPreview[2], + WHITE); +} + +void updateMapPreview(Map* map, Game* game) +{ + const Settings* settings = &game->settings; + const World* world = &game->world; + // 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}); + // Zoom. + float mouseScroll = GetMouseWheelMove(); - 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) + if (mouseScroll != 0.0) { - 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; + map->camera.zoom = expf(logf(map->camera.zoom) + + ((float)GetMouseWheelMove() * 0.1f)); + map->camera.zoom = Clamp(map->camera.zoom, MAP_ZOOM_MIN, MAP_ZOOM_MAX); } - 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); - } + // Camera follow player. + map->camera.target = map->playerPosition; + + // Draw map scene. + BeginTextureMode(map->render); + BeginMode2D(map->camera); + ClearBackground(BLANK); + + // Draw height map. + Color color = GREEN; + color.a = settings->mapAlpha; + DrawTexture(game->world.heightmapTexture, 0.0, 0.0, color); - // Draw map. - DrawTexturePro(world->heightmapTexture, - map->heightmapRect, + EndMode2D(); + EndTextureMode(); + + // Draw render texture. + DrawTexturePro(map->render.texture, + (Rectangle){0.0, 0.0, map->render.texture.width, + -map->render.texture.height}, map->rect, (Vector2){0.0, 0.0}, 0.0, - settings->mapColor); + WHITE); // Draw player. - DrawCircleV(playerPreviewPosition, 3.0, BLUE); - - // Outline. + Vector2 mapCenter = (Vector2){map->rect.x + (map->rect.width / 2.0), + map->rect.y + (map->rect.height / 2.0)}; + drawMapPlayer(map, &game->player, mapCenter, 3.0, 6.0); + DrawRectangleLinesEx(map->rect, 3.0, BLUE); } @@ -133,3 +130,8 @@ void updateMap(Map* map, Game* game) updateMapPreview(map, game); } + +void closeMap(Map* map) +{ + UnloadRenderTexture(map->render); +} |
