aboutsummaryrefslogtreecommitdiffstats
path: root/src/map.c
blob: 976cc4d5d78e6313df3bba034866182ac3eb2394 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include "map.h"
#include "game.h"
#include "world.h"
#include "player.h"
#include "entity.h"
#include "entitiesInclude.h"

void repositionMap(Map* map)
{
  map->rect.x = GetRenderWidth() - map->rect.width;
}

void renderMapEntityPreview(Image* map, Entity entity, const World* world)
{
  Vector2 position = (Vector2){
    WORLD_IMAGE_WIDTH / world->size.x * entity.position.x,
    WORLD_IMAGE_HEIGHT / world->size.z * entity.position.z
  };
  
  switch (entity.id)
  {
  case POND:
  {
    float pondSize = roundf(WORLD_IMAGE_WIDTH / world->size.x * POND_SIZE);
    position = Vector2SubtractValue(position, pondSize);
    pondSize *= 2.0;
    
    ImageDrawRectangle(map, position.x, position.y, pondSize, pondSize, BLUE);
    ImageDrawText(map, "Pond", position.x, position.y, 20, BLACK);
  }
    break;
  default:
    break;
  }
}

void initMapTexture(Map* map, const World* world, const Settings* settings)
{
  Image image = LoadImageFromTexture(world->heightmapTexture);
  Color* colors = LoadImageColors(image);
  UnloadImage(image);

  // Color filter the texture.
  for (int index = 0; index < WORLD_IMAGE_WIDTH * WORLD_IMAGE_HEIGHT; ++index)
  {
    float height = (float)GRAY_VALUE(colors[index]) / 255.0;

    // Limit color acount.
    height *= settings->mapColorCount;
    height = floorf(height);
    height /= settings->mapColorCount;

    Color lowColor = settings->mapLowColor;
    Color highColor = settings->mapHighColor;

    // Lerp between low and high colors.
    colors[index].r = Lerp(lowColor.r, highColor.r, height);
    colors[index].g = Lerp(lowColor.g, highColor.g, height);
    colors[index].b = Lerp(lowColor.b, highColor.b, height);
  }

  image = (Image){
    .data = colors,
    .width = WORLD_IMAGE_WIDTH,
    .height = WORLD_IMAGE_HEIGHT,
    .format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8,
    .mipmaps = 1
  };

  // Draw entities.
  for (int index = 0; index < WORLD_ENTITY_MAX; ++index)
  {
    renderMapEntityPreview(&image, world->entities[index], world);
  }
  
  map->texture = LoadTextureFromImage(image);
  UnloadImage(image);
}

void initMap(Map* map, const World* world, const Settings* settings)
{
  *map = (Map){
    .rect = (Rectangle){0.0, 0.0, settings->mapPreviewWidth,
                        settings->mapPreviewHeight},
    .render = LoadRenderTexture(settings->mapPreviewWidth,
                                settings->mapPreviewHeight),
    .blackout = LoadRenderTexture(BLACKOUT_WIDTH, BLACKOUT_HEIGHT),
    .camera = (Camera2D){
      .offset = (Vector2){settings->mapPreviewWidth / 2.0,
                          settings->mapPreviewHeight / 2.0},
      .target = Vector2Zero(),
      .rotation = 0.0,
      .zoom = settings->mapPreviewZoomDefault
    },
    .playerPosition = Vector2Zero(),
    .isEnabled = settings->isMapPreviewEnabledDefault,
    .isFullSize = false
  };

  // Init blackout texture.
  BeginTextureMode(map->blackout);
  ClearBackground(BLACK);
  EndTextureMode();

  initMapTexture(map, world, settings);
  repositionMap(map);
}

void drawMapPlayer(Map* map, Player* player, Vector2 position,
                   float width, float height)
{
  Vector2 playerPreview[3] = {
    (Vector2){0.0, height},
    (Vector2){width, -height},
    (Vector2){-width, -height}
  };

  // Rotate and move player.
  for (int index = 0; index < 3; ++index)
  {
    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 updateMapBlackout(Map* map, Game* game)
{
  float x = (float)BLACKOUT_WIDTH / WORLD_IMAGE_WIDTH
    * map->playerPosition.x;
  float y = (float)BLACKOUT_HEIGHT / WORLD_IMAGE_HEIGHT
    * map->playerPosition.y;

  x -= (float)BLACKOUT_AREA / 2.0;
  y -= (float)BLACKOUT_AREA / 2.0;

  // Clear the part of the screen the player is on.
  BeginTextureMode(map->blackout);
  BeginScissorMode(x, y, BLACKOUT_AREA, BLACKOUT_AREA);
  
  ClearBackground(BLANK);
  
  EndScissorMode();
  EndTextureMode();
}

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

  // Zoom.
  float mouseScroll = GetMouseWheelMove();

  if (mouseScroll != 0.0)
  {
    map->camera.zoom = expf(
      logf(map->camera.zoom) +
      ((float)GetMouseWheelMove() * settings->mapZoomSpeed));
    map->camera.zoom = Clamp(map->camera.zoom, MAP_ZOOM_MIN, MAP_ZOOM_MAX);
  }

  if (IsKeyPressed(settings->defaultMapZoomKey))
  {
    map->camera.zoom = settings->mapPreviewZoomDefault;
  }

  // Camera follow player.
  map->camera.target = map->playerPosition;

  updateMapBlackout(map, game);

  // Draw map scene.
  BeginTextureMode(map->render);
  BeginMode2D(map->camera);
  ClearBackground(BLANK);

  // Draw height map.
  DrawTexture(map->texture, 0.0, 0.0, WHITE);


  // Draw blackout.
  DrawTexturePro(map->blackout.texture,
                 (Rectangle){0.0, 0.0, BLACKOUT_WIDTH, -BLACKOUT_HEIGHT},
                 (Rectangle){0.0, 0.0, WORLD_IMAGE_WIDTH, WORLD_IMAGE_HEIGHT},
                 (Vector2){0.0, 0.0},
                 0.0,
                 WHITE);

  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,
                 (Color){255, 255, 255, settings->mapAlpha});

  // Draw player.
  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);
}

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

void closeMap(Map* map)
{
  UnloadRenderTexture(map->render);
  UnloadRenderTexture(map->blackout);
  UnloadTexture(map->texture);
}