aboutsummaryrefslogtreecommitdiffstats
path: root/src/map.c
blob: 9d91202adc56d8f54bbace5abcd65f7a5432ef20 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#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,
    .zoom = settings->mapPreviewZoomDefault,
    .playerPosition = Vector2Zero(),
    .playerRotation = 0.0
  };

  zoomMap(map, 10.0);
  repositionMap(map);
}

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
  };

  map->heightmapRect.x = map->playerPosition.x -
    (map->heightmapRect.width / 2.0);
  map->heightmapRect.y = map->playerPosition.y -
    (map->heightmapRect.height / 2.0);

  // Draw map.
  DrawTexturePro(world->heightmapTexture,
                 map->heightmapRect,
                 map->rect,
                 (Vector2){0.0, 0.0},
                 0.0,
                 settings->mapColor);

  // Draw player.
  DrawCircleV(Vector2Add((Vector2){map->rect.x, map->rect.y},
                         (Vector2){map->rect.width / 2.0,
                                   map->rect.height / 2.0}),
              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);
}