forked from Themaister/Dinothawr
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathgame.cpp
443 lines (358 loc) · 13.5 KB
/
game.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
#include "game.hpp"
#include "utils.hpp"
#include <iostream>
#include <stdexcept>
using namespace Blit;
using namespace std;
namespace Icy
{
EdgeDetector::EdgeDetector(bool init) : pos(init)
{}
bool EdgeDetector::set(bool state)
{
bool ret = state && !pos;
pos = state;
return ret;
}
Game::Game(const string& level_path, unsigned chapter, unsigned level, unsigned best_pushes, Blit::FontCluster& font)
: map(level_path), target(fb_width, fb_height), font(&font),
camera(target, player.rect(), Pos(map.pix_width(), map.pix_height())),
won_frame_cnt(0), is_sliding(false), best_pushes(best_pushes), pushes(0),
chapter(chapter), level(level), push(true)
{
m_won_early = false;
set_initial_pos(level_path);
bg = NULL;
}
Game::Game(const string& level_path)
: map(level_path), target(fb_width, fb_height), font(NULL),
camera(target, player.rect(), Pos(map.pix_width(), map.pix_height())),
won_frame_cnt(0), is_sliding(false), push(true)
{
m_won_early = false;
set_initial_pos(level_path);
bg = NULL;
}
void Game::set_bg(const Blit::Surface& bg)
{
this->bg = &bg;
}
void Game::set_initial_pos(const string& level)
{
Blit::Tilemap::Layer *layer = map.find_layer("floor");
if (!layer)
throw runtime_error("Floor layer not found.");
std::basic_string<char> sprite_path = Utils::find_or_default(layer->attr, "player_sprite", "");
if (sprite_path.empty())
player = cache.from_sprite(Utils::join(level, ".sprite"));
else
player = cache.from_sprite(Utils::join(Utils::basedir(level), "/", sprite_path));
int x = Utils::stoi(Utils::find_or_default(layer->attr, "start_x", "1"));
int y = Utils::stoi(Utils::find_or_default(layer->attr, "start_y", "1"));
int off_x = Utils::stoi(Utils::find_or_default(layer->attr, "player_offset_x", "0"));
int off_y = Utils::stoi(Utils::find_or_default(layer->attr, "player_offset_y", "0"));
std::basic_string<char> face = Utils::find_or_default(layer->attr, "start_facing", "right");
player.rect().pos = Pos(x * map.tile_width(), y * map.tile_height());
player_off = Pos(off_x, off_y);
facing = string_to_input(face);
player.active_alt(face);
}
void Game::iterate()
{
update_player();
if (bg)
target.blit(*bg, Rect());
else
target.clear(Pixel::ARGB(0x00, 0x00, 0x00, 0x00));
camera.update();
map.render(target);
target.blit_offset(player, Rect(), player_off);
if (font)
{
font->set_id("lime");
font->render_msg(target,
Utils::join((chapter + 1), "-", (level + 1)), 314, 184, Font::RenderAlignment::Right);
if (!best_pushes)
font->render_msg(target, Utils::join(" Pushes:", pushes), 2, 184);
else
font->render_msg(target, Utils::join(" Pushes:", pushes, " Best:", best_pushes), 2, 184);
}
if (m_video_cb)
m_video_cb(target.buffer(), target.width(), target.height(), target.width() * sizeof(Pixel));
}
vector<reference_wrapper<SurfaceCluster::Elem>> Game::get_tiles_with_attr(const string& name,
const string& attr, const string& val)
{
vector<reference_wrapper<SurfaceCluster::Elem>> surfs;
Blit::Tilemap::Layer *layer = map.find_layer(name);
if (!layer)
return surfs;
copy_if(layer->cluster.vec().begin(),
layer->cluster.vec().end(),
back_inserter(surfs), [&attr, &val](const SurfaceCluster::Elem& surf) -> bool {
if (val.empty())
return surf.surf.attr().find(attr) != surf.surf.attr().end();
else
return Utils::find_or_default(surf.surf.attr(), attr, "") == val;
});
return surfs;
}
bool Game::win_animation_stepper()
{
won_frame_cnt++;
std::vector<std::reference_wrapper<Blit::SurfaceCluster::Elem> > goal_blocks = get_tiles_with_attr("blocks", "goal", "true");
const unsigned frame_per_iter = 24;
std::string state = "frozen";
if (won_frame_cnt >= 3 * frame_per_iter)
{
bool jump = ((won_frame_cnt / frame_per_iter - 3) >> 1) & 1;
unsigned last_jump = (((won_frame_cnt - 1) / frame_per_iter - 3) >> 1) & 1;
state = jump ? "cheer" : "down";
player.active_alt(state);
if (jump && !last_jump)
get_sfx().play_sfx("dino_jump", 0.4);
}
else if (won_frame_cnt >= 2 * frame_per_iter)
state = "defrost2";
else if (won_frame_cnt >= 1 * frame_per_iter)
state = "defrost1";
for (auto& block : goal_blocks)
{
block.get().surf.active_alt(state);
// Shift defrosted block same way player sprite is (16x17, etc), but only when defrost kicks in.
if (won_frame_cnt >= 1 * frame_per_iter)
block.get().offset = player_off;
}
m_won_early = (won_frame_cnt >= frame_per_iter * 3) && push.set(m_input_cb(Input::Push));
return true;
}
void Game::prepare_won_animation()
{
won_frame_cnt = 1;
player_walking = false;
push.set(true); // Avoid exiting win animation early.
m_won_early = false;
stepper = bind(&Game::win_animation_stepper, this);
get_sfx().play_sfx("frozen_dino_melt", 0.25);
}
bool Game::won() const
{
return m_won_early || (won_frame_cnt >= won_frame_cnt_limit);
}
static bool is_game_won(const Blit::SurfaceCluster::Elem& a, const Blit::SurfaceCluster::Elem& b)
{
return a.surf.rect().pos == b.surf.rect().pos;
}
static bool sort_blocks(const SurfaceCluster::Elem& a, const SurfaceCluster::Elem& b)
{
return a.surf.rect().pos < b.surf.rect().pos;
}
// Checks if all goals on floor and blocks are aligned with each other.
bool Game::won_condition()
{
std::vector<std::reference_wrapper<Blit::SurfaceCluster::Elem> > goal_floor = get_tiles_with_attr("floor", "goal", "true");
std::vector<std::reference_wrapper<Blit::SurfaceCluster::Elem> > goal_blocks = get_tiles_with_attr("blocks", "goal", "true");
if (goal_floor.size() != goal_blocks.size())
throw logic_error("Number of goal floors and goal blocks do not match.");
if (goal_floor.empty() || goal_blocks.empty())
throw logic_error("Goal floor or blocks are empty.");
sort(goal_floor.begin(), goal_floor.end(), sort_blocks);
sort(goal_blocks.begin(), goal_blocks.end(), sort_blocks);
return equal(goal_floor.begin(), goal_floor.end(),
goal_blocks.begin(), is_game_won);
}
void Game::update_player()
{
if (!m_input_cb)
return;
bool had_stepper = static_cast<bool>(stepper);
run_stepper();
if (won_frame_cnt)
return;
if (!stepper)
update_input();
else
update_triggers();
// Reset animation.
if (!had_stepper && stepper)
frame_cnt = 0;
else if (!stepper)
{
frame_cnt = 0;
player.active_alt_index(0);
}
if (stepper && player_walking)
update_animation();
if (won_condition())
prepare_won_animation();
}
void Game::update_animation()
{
frame_cnt++;
// Animation from index 1 to 4, "neutral position" in 0. "Slippery" animations in 5 and 6.
unsigned anim_index;
if (is_sliding)
anim_index = (frame_cnt / 10) % 2 + 5;
else
anim_index = (frame_cnt / 10) % 4 + 1;
player.active_alt_index(anim_index);
}
void Game::update_triggers()
{
push.set(m_input_cb(Input::Push));
}
void Game::update_input()
{
bool push_trigger = push.set(m_input_cb(Input::Push));
if (push_trigger)
push_block();
else if (m_input_cb(Input::Up))
move_if_no_collision(Input::Up);
else if (m_input_cb(Input::Down))
move_if_no_collision(Input::Down);
else if (m_input_cb(Input::Left))
move_if_no_collision(Input::Left);
else if (m_input_cb(Input::Right))
move_if_no_collision(Input::Right);
}
string Game::input_to_string(Input input)
{
switch (input)
{
case Input::Up: return "up";
case Input::Left: return "left";
case Input::Right: return "right";
case Input::Down: return "down";
default: return "";
}
}
Blit::Pos Game::input_to_offset(Input input)
{
switch (input)
{
case Input::Up: return Pos(0, -1);
case Input::Left: return Pos(-1, 0);
case Input::Right: return Pos(1, 0);
case Input::Down: return Pos(0, 1);
default: return Pos();
}
}
Input Game::string_to_input(const string& dir)
{
if (dir == "up") return Input::Up;
if (dir == "down") return Input::Down;
if (dir == "left") return Input::Left;
if (dir == "right") return Input::Right;
return Input::None;
}
bool Game::is_offset_collision(Surface& surf, Pos offset)
{
Blit::Rect new_rect = surf.rect() + offset;
// Always assume that the rect in question is inside a single tile.
// This is needed as the dino sprite can be slightly larger than 16x16, but it's
// *assumed* from a collition detection POV that a surface is tile sized to simplify things.
new_rect.w = map.tile_width();
new_rect.h = map.tile_height();
bool outside_grid = surf.rect().pos.x % map.tile_width() || surf.rect().pos.y % map.tile_height();
if (outside_grid)
throw logic_error("Offset collision check was performed outside tile grid.");
int current_x = surf.rect().pos.x / map.tile_width();
int current_y = surf.rect().pos.y / map.tile_height();
int min_tile_x = new_rect.pos.x / map.tile_width();
int max_tile_x = (new_rect.pos.x + new_rect.w - 1) / map.tile_width();
int min_tile_y = new_rect.pos.y / map.tile_height();
int max_tile_y = (new_rect.pos.y + new_rect.h - 1) / map.tile_height();
for (int y = min_tile_y; y <= max_tile_y; y++)
for (int x = min_tile_x; x <= max_tile_x; x++)
if (Pos(x, y) != Pos(current_x, current_y) && map.collision(Pos(x, y))) // Can't collide against ourselves.
return true;
return false;
}
void Game::push_block()
{
Blit::Pos offset = input_to_offset(facing);
Blit::Pos dir = offset * Pos(map.tile_width(), map.tile_height());
Blit::Surface *tile = map.find_tile("blocks", player.rect().pos + dir);
if (!tile)
return;
int tile_x = player.rect().pos.x / map.tile_width();
int tile_y = player.rect().pos.y / map.tile_height();
Pos tile_pos(tile_x, tile_y);
if (!map.collision(tile_pos + (2 * offset)))
{
stepper = bind(&Game::tile_stepper, this, ref(*tile), offset);
stepper_cnt = 0;
player_walking = false;
player.active_alt_index(0);
get_sfx().play_sfx("dino_push", 1.0);
pushes++;
}
}
void Game::move_if_no_collision(Input input)
{
facing = input;
player.active_alt(input_to_string(facing));
Blit::Pos offset = input_to_offset(input);
if (!is_offset_collision(player, offset))
{
stepper = bind(&Game::tile_stepper, this, ref(player), offset);
player_walking = true;
}
}
bool Game::tile_stepper(Surface& surf, Pos step_dir)
{
surf.rect() += 2 * step_dir;
if (!player_walking)
{
unsigned alt = stepper_cnt <= 6 ? 7 : 0;
player.active_alt_index(alt);
stepper_cnt++;
}
if (surf.rect().pos.x % map.tile_width() || surf.rect().pos.y % map.tile_height())
return true;
if (is_offset_collision(surf, step_dir))
{
is_sliding = false;
if (&surf != &player)
get_sfx().play_sfx("ice_bump", 0.25);
return false;
}
//cerr << "Player: " << player.rect().pos << " Surf: " << surf->rect().pos << endl;
Blit::Surface *surface = map.find_tile("floor", surf.rect().pos);
bool slippery = surface && Utils::find_or_default(surface->attr(),
&surf == &player ? "slippery_player" : "slippery_block", "") == "true";
is_sliding = slippery;
return slippery;
}
void Game::run_stepper()
{
if (stepper && !stepper())
stepper = {};
}
CameraManager::CameraManager(RenderTarget& target, const Rect& rect, Blit::Pos map_size)
: target(&target), rect(&rect), map_size(map_size)
{}
void CameraManager::update()
{
// Map can fit completely inside our rect, just center it.
if (target->width() >= map_size.x && target->height() >= map_size.y)
target->camera_set((map_size - Pos(target->width(), target->height())) / 2);
else // Center around player, but clamp if player isn't near walls.
{
Blit::Pos pos = rect->pos;
pos += Pos(rect->w, rect->h) / 2;
Pos target_size(target->width(), target->height());
Blit::Pos pos_base = pos - target_size / 2;
Blit::Pos pos_max = pos_base + target_size;
if (pos_base.x < 0)
pos_base.x = 0;
else if (pos_max.x > map_size.x)
pos_base.x -= pos_max.x - map_size.x;
if (pos_base.y < 0)
pos_base.y = 0;
else if (pos_max.y > map_size.y)
pos_base.y -= pos_max.y - map_size.y;
target->camera_set(pos_base);
}
}
}