-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhome_scene.cpp
86 lines (76 loc) · 2.44 KB
/
home_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
75
76
77
78
79
80
81
82
83
84
85
86
#include "home_scene.h"
#include "player.h"
#include "platforming_scene.h"
#include "score.h"
#include "moverpup.h"
extern Scene * current_scene;
extern std::vector<Platforming_scene> scenes;
extern std::vector<Platforming_scene>::iterator platscene_iter;
static double starting_position_x = 0;
static double starting_position_y = 0;
Home_scene::Home_scene(sdlxx::Sdl_renderer& renderer) : renderer{renderer} {
}
void Home_scene::player_died()
{
player_was_dead = true;
}
void Home_scene::removePupper() {
std::vector<std::unique_ptr<Game_object>>::iterator it;
for (it = scene_objects.begin(); it < scene_objects.end(); ++it) {
if ((*it)->isPupper) {
scene_objects.erase(it);
--pups;
break;
}
}
}
void Home_scene::update()
{
Player* player = nullptr;
if (player == nullptr) {
for (auto& object : scene_objects) {
if (object->isPlayer) {
player = dynamic_cast<Player*>(object.get());
if (starting_position_x == 0) {
starting_position_x = player->position.x;
starting_position_y = player->position.y;
}
}
}
}
if (pups < Score::sharedInstance().get()) {
addObject(std::make_unique<MoverPup>(
renderer, player->position.x, player->position.y + 100));
++pups;
}
if (pups > Score::sharedInstance().get()) {
removePupper();
}
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.x < 0) {
if (player_was_dead) {
--platscene_iter;
player_was_dead = false;
}
current_scene = &(*platscene_iter);
++platscene_iter;
if (platscene_iter == scenes.end()) {
platscene_iter = scenes.begin();
}
player->position.x = starting_position_x;
player->position.y = starting_position_y;
}
}