generated from 32blit/32blit-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
minimap.cpp
74 lines (57 loc) · 1.55 KB
/
minimap.cpp
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
#include "minimap.hpp"
#include "engine/engine.hpp"
#include "graphics/tilemap.hpp"
#include "track.hpp"
using namespace blit;
static const blit::Pen minimap_palette[] {
{ 0, 0, 0, 0},
{ 0, 0, 0, 128},
{128, 128, 128, 128},
{255, 255, 255, 128}
};
Minimap::Minimap() {
surface.palette = const_cast<Pen *>(minimap_palette);
}
void Minimap::update(Point center_pos) {
Point half(size.w / 2, size.h / 2);
Point min = center_pos - half;
Point max = center_pos + half;
// clamp
if(min.x < 0) {
max.x += -min.x;
min.x = 0;
}
if(min.y < 0) {
max.y += -min.y;
min.y = 0;
}
auto &map = track->get_map();
// assuming map size >= minimap size
if(max.x > map.bounds.w) {
min.x -= (max.x - map.bounds.w);
max.x = map.bounds.w;
}
if(max.y > map.bounds.h) {
min.y -= (max.y - map.bounds.h);
max.y = map.bounds.h;
}
viewport = Rect(min, max);
int out_i = 0;
for(int y = min.y; y < max.y; y++) {
for(int x = min.x; x < max.x; x++) {
float friction = track->get_friction(Vec2(x, y) * 8.0f);
if(friction == 0.0f)
data[out_i++] = 1;
else if(friction > 1.0f)
data[out_i++] = 2;
else
data[out_i++] = 3;
}
}
}
void Minimap::render() {
screen.blit(&surface, Rect(Point(0, 0), size), Point(screen.bounds.w - size.w, screen.bounds.h - size.h));
}
void Minimap::set_track(Track *track) {
this->track = track;
}