aboutsummaryrefslogtreecommitdiffstats
path: root/src/map.c
blob: 3b763f0d108800d4fe657957dce6f2b1c53c7666 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#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);
}