-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathObjects.cpp
More file actions
155 lines (129 loc) · 3.19 KB
/
Objects.cpp
File metadata and controls
155 lines (129 loc) · 3.19 KB
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
#include "Objects.hpp"
bool Objects::crash = false;
float Objects::velocity = 0.;
const float Objects::jumpVelocity = 27.5;
const float Objects::gravity = 4.;
bool Objects::flag = false;
// deleting objects created in the destructor
Objects::~Objects()
{ }
// drawing all game objects
void Objects::drawObjects()
{
base->draw();
for (auto i = L.begin(); i != L.end();)
{
Obstacles *u = *i;
u->draw();
u->move();
// checking for collision with each obstacle
if (u->collision(S))
{
S->destroy();
crash = true;
}
// checking if the obstacle has left the game screen and needs to be deleted
if (u->delete_obstacle())
{
delete u;
u = nullptr;
i = L.erase(i);
}
else
++i;
}
// drawing the sprite if it has not collided
if (!crash)
S->draw();
}
// creates new objects randomly
void Objects::createObstacles()
{
int z = rand() % 6;
if (z == 0)
L.emplace_back(new Spike());
else if (z == 1)
L.emplace_back(new DoubleSpike());
else if (z == 2)
L.emplace_back(new TripleSpike());
else if (z == 3)
L.emplace_back(new BlockSpike());
else if (z == 4)
{
L.emplace_back(new DoubleSpike());
L.emplace_back(new SpikeDSpike());
L.emplace_back(new SpikeDSpike2());
}
else
{
L.emplace_back(new HangingBase2());
L.emplace_back(new JumpDJump());
L.emplace_back(new JumpDJump2());
}
// keeping track of last created object
Prev = L.back();
}
// function to create base and sprite
void Objects::createEssentials()
{
S = new Sprite();
base = new platform();
starting_x = S->getMoverRect().x;
starting_y = S->getMoverRect().y;
}
// updating the sprite position when it has to jump
void Objects::update(SDL_Event &e)
{
if (e.type == SDL_KEYDOWN && e.key.keysym.sym == SDLK_SPACE && !flag)
{
if (!flag)
{
velocity = -jumpVelocity;
flag = true;
}
}
if (flag)
{
SDL_Rect &obstacleRect = S->getMoverRect();
velocity += gravity;
obstacleRect.y += velocity;
if (obstacleRect.y >= starting_y)
{
obstacleRect.y = starting_y;
velocity = 0;
flag = false;
}
}
}
// function to check if new obstacle should be introduced
bool Objects::addObstacle() const
{
if (Prev)
return (Prev->getMoverRect().x <= S->getMoverRect().x + S->getMoverRect().w);
return true;
}
// tells if the game needs to end by checking if a collision has occurred
bool Objects::EndGame() const
{
return crash;
}
void Objects::reset(){
for (Obstacles *u : L)
{
delete u;
u = nullptr;
}
L.clear();//this is an inbuilt function that clears all the vector
// Reset sprite and base
delete S;
S = new Sprite();
delete base;
base = new platform();
// Reset other variables
crash = false;
flag = false;
velocity = 0.0;
Prev = nullptr;
starting_x = S->getMoverRect().x;
starting_y = S->getMoverRect().y;
}