-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayground.cpp
More file actions
332 lines (291 loc) · 8.72 KB
/
Playground.cpp
File metadata and controls
332 lines (291 loc) · 8.72 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
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
#include "include/Playground.hpp"
#include "include/Platform.hpp"
#include <iostream>
#include "include/globals.hpp"
Playground::Playground(Texture *texture) : texture(texture)
{
}
void Playground::CheckPlayerCollisions(Player *player)
{
for (std::vector<Platform *>::iterator it = platforms.begin(); it != platforms.end(); ++it)
{
if (player->Collided(*it) and player->velY > 0)
{
player->velY = -10;
}
}
}
void Playground::CheckEnemyCollisions(std::vector<Enemy *> bombs)
{
for (std::vector<Enemy *>::iterator enemy = bombs.begin(); enemy != bombs.end(); ++enemy)
{
for (std::vector<Platform *>::iterator platform = platforms.begin(); platform != platforms.end(); ++platform)
{
if ((*enemy)->state == Enemy::Falling and (*enemy)->Collided(*platform))
{
(*enemy)->velY = 0;
(*enemy)->state = Enemy::Stopped;
// fix bomb position to be in line with platform
if ((*enemy)->posY + (*enemy)->sizeY > (*platform)->posY)
{
(*enemy)->posY = (*platform)->posY - (*enemy)->sizeY;
}
break;
}
}
}
}
void Playground::CheckPickupCollisions(Pickup *pickup)
{
for (std::vector<Platform *>::iterator platform = platforms.begin(); platform != platforms.end(); ++platform)
{
if (pickup->state == Pickup::Falling and pickup->Collided(*platform))
{
pickup->velY = 0;
pickup->state = Pickup::Stopped;
// fix pickup position to be in line with platform
if (pickup->posY + pickup->sizeY > (*platform)->posY)
{
pickup->posY = (*platform)->posY - pickup->sizeY;
}
break;
}
}
}
void Playground::Playground::Update()
{
for (std::vector<Platform *>::iterator platform = platforms.begin(); platform != platforms.end(); ++platform)
{
(*platform)->Update();
}
}
void Playground::Render(sf::RenderWindow &window)
{
for (std::vector<Platform *>::iterator platform = platforms.begin(); platform != platforms.end(); ++platform)
{
(*platform)->Render(window);
}
}
void Playground::MoveDown(float dy)
{
for (std::vector<Platform *>::iterator platform = platforms.begin(); platform != platforms.end(); ++platform)
{
(*platform)->posY = (*platform)->posY - dy;
}
for (std::vector<Platform *>::iterator platform = platforms.begin(); platform != platforms.end(); ++platform)
{
if ((*platform)->posY > 800)
{
removePlatform((*platform));
generateMore(1);
break;
}
}
}
void Playground::GeneratePlatforms()
{
// Let's split the screen into 12 boxes, each 200x200
int boxW = 200, boxH = 200;
int rows = 4, cols = 3;
bool doGenerate = 0;
do
{
for (int i = 0; i < cols; i++)
{
for (int j = 0; j < rows; j++)
{
doGenerate = rand() % 2;
// Let's try to generate a platform in given box
if (doGenerate and platforms.size() < 10)
{
generateRandomGameObjectsWithFixingOnGivenArea(i * boxW, j * boxH, i * boxW + boxW, j * boxH + boxH, 1, 0);
}
}
}
} while (platforms.size() < 10);
generateMore(1); // oh, well, let's try creating one more platform if there's any space
}
void Playground::generateRandomGameObjectsWithFixingOnGivenArea(int startX, int startY, int endX, int endY, int n, int offsetY)
{
int randX, randY, tryFixCnt;
GameObject *closestObj;
Platform *platform;
closestObj = nullptr;
for (int i = 0; i < n; i++)
{
// move on random position in given area and put platform
randX = randomWithStep(startX, endX - 100, 5);
randY = randomWithStep(startY, endY - 20, 5);
randY += offsetY;
platform = new Platform(randX, randY, texture, 2, 1);
closestObj = getClosestObjInRange(platform, 160);
if (closestObj == nullptr)
{
// if there is no platform in close range => great, we are done!
platforms.push_back(platform);
}
else
{
// if there's another platform in close range ...
tryFixCnt = 0;
do
{
// ... well, let's try moving the new platform slightly and check if it's ok then
fixPos(platform, closestObj);
closestObj = getClosestObjInRange(platform, 160);
tryFixCnt++;
// oh, and let's do it 50 times!
if (tryFixCnt > 50)
{
break;
}
} while (closestObj != nullptr);
if (closestObj == nullptr)
{
// if there is no platform in close range after fixing => thats what we wanted and we will use it in the game!
platforms.push_back(platform);
}
}
}
}
int Playground::randomWithStep(int min, int max, int step)
{
int range = (max - min) / step;
return min + (rand() % range) * step;
}
bool Playground::isValidPosition(GameObject *newObj)
{
if (newObj->posX > SCREEN_WIDTH - 100)
{
return false; // too close to the right
}
if (newObj->posY > SCREEN_HEIGHT - 2 * 32)
{
return false; // too close to the bottom
}
if (newObj->posY < 2 * 32)
{
return false; // too close to the top
}
for (std::vector<Platform *>::iterator it = platforms.begin(); it != platforms.end(); ++it)
{
float dist = newObj->distanceBetweenObjects(*it);
if (dist < 150)
{
return false; // too close to other platform
}
}
return true;
}
GameObject *Playground::getClosestObjInRange(GameObject *newObj, int range)
{
int dist;
int closestDistance = 1000;
GameObject *closestObj = nullptr;
// just iterate platforms and find closest one
for (std::vector<Platform *>::iterator it = platforms.begin(); it != platforms.end(); ++it)
{
dist = newObj->distanceBetweenObjects(*it);
if (dist < range)
{
if (dist < closestDistance)
{
closestObj = *it;
closestDistance = dist;
}
}
}
return closestObj;
}
void Playground::fixPos(GameObject *newObj, GameObject *obj)
{
// Depending on the situation, let's try moving the new platform slightly to see if it fits better
if (obj->posX > newObj->posX)
{
// on the right side
if (obj->posY > newObj->posY)
{
// on the top
newObj->posY -= 1;
newObj->posX -= 1;
}
else
{
// must be bottom
newObj->posY += 1;
newObj->posX -= 1;
}
}
else
{
// must be on the left side
if (obj->posY > newObj->posY)
{
// on the top
newObj->posY -= 1;
newObj->posX += 1;
}
else
{
// must be bottom
newObj->posY -= 1;
newObj->posX += 1;
}
}
// fit to the screen goes next
if (newObj->posX < 0)
{
newObj->posX = 0;
}
if (newObj->posX + 100 > SCREEN_WIDTH)
{
newObj->posX = SCREEN_WIDTH - 100;
}
if (newObj->posY + 20 > SCREEN_HEIGHT)
{
newObj->posY = SCREEN_HEIGHT - 20;
}
}
void Playground::generateMore(int num)
{
// This one is very similar to GeneratePlatforms but it operates only on the top part of the game screen
// TODO probably should be merged with GeneratePlatforms
int boxW = 200;
int cols = 4;
bool doGenerate = 0;
generateRandomGameObjectsWithFixingOnGivenArea(200, 0, 400, 30, 1, -30);
if (platforms.size() < 10)
{
do
{
for (int i = 0; i < cols; i++)
{
doGenerate = rand() % 2;
if (doGenerate and platforms.size() < 10)
{
generateRandomGameObjectsWithFixingOnGivenArea(i * boxW, 0, i * boxW + boxW, 200, 1, -200);
}
}
} while (platforms.size() < 10);
}
}
void Playground::removePlatform(Platform *block)
{
for (std::vector<Platform *>::iterator it = platforms.begin(); it != platforms.end(); ++it)
{
if (*it == block)
{
delete *it;
platforms.erase(it);
break;
}
}
}
Playground::~Playground()
{
for (std::vector<Platform *>::iterator it = platforms.begin(); it != platforms.end(); ++it)
{
delete *it;
}
platforms.clear();
}