generated from 32blit/32blit-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
track.cpp
284 lines (205 loc) · 7.32 KB
/
track.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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include "track.hpp"
#include "graphics/surface.hpp"
#include "graphics/tilemap.hpp"
#include "camera.hpp"
#include "kart.hpp"
using namespace blit;
static Mat3 mode7_scanline_transform(const Camera &cam, float fog, uint8_t y) {
float top = cam.viewport.h / 2;
float left = -cam.viewport.w / 2;
// pitch
float pitch_c = cam.up.y;
float pitch_s = -cam.forward.y;
float yb = (y - top) * pitch_c + cam.focal_distance * pitch_s;
float zb = (y - top) * pitch_s - cam.focal_distance * pitch_c;
float l = cam.pos.y / yb;
// yaw
float scaled_yaw_c = l * cam.right.x;
float scaled_yaw_s = l * cam.right.z;
// final translation
float tx = cam.pos.x + scaled_yaw_c * left - scaled_yaw_s * zb;
float ty = cam.pos.z + scaled_yaw_s * left + scaled_yaw_c * zb;
Mat3 mat = Mat3::identity();
mat.v00 = scaled_yaw_c; mat.v10 = scaled_yaw_s;
mat.v11 = 0.0f;
mat.v02 = tx; mat.v12 = ty;
screen.alpha = 255 - std::max(0.0f, std::min(255.0f, (l - 0.2f) * fog));
return mat;
}
TrackObject::TrackObject(ObjectType type) : type(type) {}
TrackObject::TrackObject(const TrackObjectInfo &info, blit::Surface *spritesheet) : type(info.type) {
sprite.spritesheet = spritesheet;
sprite.world_pos.x = info.pos_x;
sprite.world_pos.z = info.pos_y;
sprite.scale = info.scale / 16.0f;
sprite.sheet_x = info.sprite_x;
sprite.sheet_y = info.sprite_y;
sprite.size_w = info.sprite_w;
sprite.size_h = info.sprite_h;
sprite.origin_x = info.origin_x;
sprite.origin_y = info.origin_y;
}
void TrackObject::update() {
if(respawn_timer)
respawn_timer -= 10;
if(type == ObjectType::Projectile) {
sprite.world_pos += vel * 0.01f;
auto &track_info = track->get_info();
// collide with track obstacles
// TODO: mostly copy/pasted from kart
for(size_t i = 0; i < track_info.num_collision_rects; i++) {
auto &rect = track_info.collision_rects[i];
if(rect.empty())
continue;
Vec2 pos_2d(sprite.world_pos.x, sprite.world_pos.z);
float radius = sprite.size_w * 4.0f;
Vec2 obstacle_pos(pos_2d);
if(pos_2d.x < rect.x)
obstacle_pos.x = rect.x;
else if(pos_2d.x >= rect.x + rect.w)
obstacle_pos.x = rect.x + rect.w;
if(pos_2d.y < rect.y)
obstacle_pos.y = rect.y;
else if(pos_2d.y >= rect.y + rect.h)
obstacle_pos.y = rect.y + rect.h;
auto vec = pos_2d - obstacle_pos;
float dist = vec.length();
if(dist > radius)
continue;
vec /= dist;
float penetration = radius - dist;
sprite.world_pos += Vec3(vec.x, 0.0f, vec.y) * penetration;
// boing
Vec3 vec3(vec.x, 0.0f, vec.y);
vel -= vec3 * vec3.dot(vel) * 2.0f;
}
// TODO: collide with other objects
}
}
void TrackObject::collide(Kart &kart) {
if(!is_active())
return;
float sprite_radius = sprite.size_w * 4.0f * sprite.scale;
float kart_radius = kart.get_radius();
auto vec = kart.get_2d_pos() - Vec2(sprite.world_pos.x, sprite.world_pos.z);
float dist = vec.x * vec.x + vec.y * vec.y; // squared length
if(dist >= (kart_radius + sprite_radius) * (kart_radius + sprite_radius))
return;
if(type == ObjectType::Item) {
kart.collect_item();
respawn_timer = 10000; // 10s
return;
}
// do collision
dist = std::sqrt(dist);
vec /= dist;
float penetration = kart_radius + sprite_radius - dist;
kart.sprite.world_pos += Vec3(vec.x, 0.0f, vec.y) * penetration;
if(type == ObjectType::DroppedItem || type == ObjectType::Projectile) {
// assume hitting this is bad (okay unless we allow dropped boosts)
kart.disable();
type = ObjectType::Removed;
}
}
bool TrackObject::is_active() const {
return type != ObjectType::Removed && respawn_timer == 0;
}
Track::Track(const TrackInfo &info) : info(info) {
tiles = Surface::load(info.tiles_asset);
load_tilemap();
// some extra for dynamic objects
objects.reserve(info.num_sprites + 16);
reset_objects(); // hmm, this will always be called by setup_race
}
Track::~Track() {
delete map;
}
void Track::render(const Camera &cam) {
using namespace std::placeholders;
int horizon = (screen.bounds.h / 2) - (((cam.far * -cam.forward.y) - cam.pos.y) * cam.focal_distance) / (cam.far * cam.up.y);
map->draw(&screen, Rect(cam.viewport.x, cam.viewport.y + horizon, cam.viewport.w, cam.viewport.h - horizon), std::bind(mode7_scanline_transform, cam, fog, _1));
//map->transform = /*Mat3::translation(Vec2(cam.look_at.x - screen.bounds.w / 2, cam.look_at.z - screen.bounds.h / 2)) */ Mat3::scale(Vec2(0.5f, 0.5f));
//map->draw(&screen, Rect({0, 0}, screen.bounds));
screen.alpha = 255;
}
unsigned int Track::find_closest_route_segment(Vec2 pos, float &segment_t) const {
unsigned int ret = 0;
float min_dist = INFINITY;
for(size_t i = 0; i < info.route_len - 1; i++) {
Vec2 a(info.route[i]), b(info.route[i + 1]);
// intersection
Vec2 r = b - a;
Vec2 dir(r.y, -r.x);
float rxs = (r.x * dir.y) - (r.y * dir.x);
float t = ((pos.x - a.x) * dir.y - (pos.y - a.y) * dir.x) / rxs; // distance along route segment
// clamp to segment
Vec2 route_point;
if(t < 0.0f)
route_point = a;
else if(t > 1.0f)
route_point = b;
else
route_point = a + r * t;
float dist = (pos - route_point).length();
if(dist < min_dist) {
ret = i;
segment_t = t;
min_dist = dist;
}
}
return ret;
}
float Track::get_friction(blit::Vec2 pos) {
Point tile_coord(pos / Vec2(8.0f, 8.0f));
if(tile_coord.x < 0 || tile_coord.y < 0 || tile_coord.x >= map->bounds.w || tile_coord.y >= map->bounds.h)
return 0.0f;
auto tile_id = map->tile_at(tile_coord);
// default
if(tile_id >= info.tile_friction_len)
return 1.0f;
return info.tile_friction[tile_id];
}
Vec2 Track::get_starting_dir() const {
Vec2 dir(info.route[1] - info.route[0]);
dir.normalize();
return dir;
}
void Track::set_fog(float fog) {
this->fog = fog;
}
const TrackInfo &Track::get_info() const {
return info;
}
blit::Surface *Track::get_tiles() {
return tiles;
}
TileMap &Track::get_map() {
return *map;
}
void Track::reset_objects() {
objects.clear();
for(size_t i = 0; i < info.num_sprites; i++) {
objects.emplace_back(info.sprites[i], tiles);
objects.back().track = this;
}
}
std::vector<TrackObject> &Track::get_objects() {
return objects;
}
void Track::add_object(TrackObject object) {
if(!object.sprite.spritesheet)
object.sprite.spritesheet = tiles;
object.track = this;
// attempt reuse
for(auto it = objects.begin() + info.num_sprites; it != objects.end(); ++it) {
if(it->type == ObjectType::Removed) {
*it = object;
return;
}
}
objects.emplace_back(std::move(object));
}
void Track::load_tilemap() {
map = TileMap::load_tmx(info.map_asset, tiles, 0, 0);
map->repeat_mode = TileMap::CLAMP_TO_EDGE;
}