-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplatforming_scene.cpp
74 lines (65 loc) · 2.25 KB
/
platforming_scene.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 "platforming_scene.h"
#include <chrono>
#include "home_scene.h"
#include "platform.h"
#include "screen_config.h"
#include "you_died.h"
#include "score.h"
extern Scene* current_scene;
extern std::unique_ptr<You_died> you_died;
extern std::unique_ptr<Home_scene> home;
decltype(std::chrono::high_resolution_clock::now()) die_time;
Platforming_scene::Platforming_scene(sdlxx::Sdl_renderer& renderer)
: _renderer(renderer)
{
}
void Platforming_scene::update()
{
if (player == nullptr) {
for (auto& object : scene_objects) {
if (object->isPlayer) {
player = dynamic_cast<Player*>(object.get());
player_spawn.x = player->position.x;
player_spawn.y = player->position.y;
}
if (object->isPupper) {
pupper = dynamic_cast<Pupper*>(object.get());
pup_spawn = pupper->position;
}
if (object->isGoal) { mailbox = dynamic_cast<Goal*>(object.get()); }
}
}
for (auto& object : scene_objects) { object->update(); }
for (auto a = scene_objects.begin(); a < scene_objects.end(); ++a) {
for (auto b = scene_objects.begin(); b < scene_objects.end(); ++b) {
// Don't collide with yourself
if (a == b) { continue; }
if ((**a).collides_with(**b)) { (**a).collide(**b); }
}
}
for (auto iter = scene_objects.begin(); iter != scene_objects.end();) {
if ((**iter).should_be_destroyed() == true)
iter = scene_objects.erase(iter);
else
++iter;
}
if (player->position.y > window_height) {
// Dead!
die_time = std::chrono::high_resolution_clock::now();
current_scene = you_died.get();
player->position.x = player_spawn.x;
player->position.y = player_spawn.y;
pupper->position = pup_spawn;
pupper->held = false;
}
if (player->collides_with(*mailbox)) {
if (pupper->isHeld()) {
player->position.x = player_spawn.x;
player->position.y = player_spawn.y;
pupper->position = pup_spawn;
pupper->held = false;
current_scene = home.get();
Score::sharedInstance().increment();
}
}
}