-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLevel.cpp
431 lines (402 loc) · 16.6 KB
/
Level.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
#include "stdafx.h"
#include "Level.h"
#include "Map.h"
#include <iostream>
#include "Constants.h"
#include "Player.h"
#include "Enumerations.h"
#include "Engine.h"
#include "Enemy.h"
#include "GeneralFunctions.h"
Level::Level()
{
}
/*
// Applies map's textures
void Level::attachTexture()
{
background.setTexture(&selectedMap.txu_BackgroundImage);
levelStructure.setTexture(selectedMap.txu_LevelImage);
}*/
/*
// Assigns tile information to tiles
void Level::attachMap(Map map)
{
// Attach input map to be the selected map
selectedMap = map;
}*/
/*
// Checks if player is colliding with any surface
void Level::detectCollisionPlayer(Player &player)
{
// Cycle through all surfaces
for (int count = 0; count < MAXCOLLISIONS; count++)
{
// Detect surface type and apply effects
switch (selectedMap.col_CollisionData[count].type)
{
case PT_FLOOR: //Set player position to top of surface, zero y-velocity, stop falling and allow jumping.
if (selectedMap.col_CollisionData[count].collisionArea.getGlobalBounds().intersects(player.getSprite().getGlobalBounds())
&& player.jumping == false)
{
player.position.y = selectedMap.col_CollisionData[count].collisionArea.getPosition().y - 32;
player.velocity.y = 0;
player.falling = false;
player.canJump = true;
}
break;
case PT_CEILING: //Set player position to bottom of surface
if (selectedMap.col_CollisionData[count].collisionArea.getGlobalBounds().intersects(player.getSprite().getGlobalBounds()))
{
player.position.y = selectedMap.col_CollisionData[count].collisionArea.getPosition().y + 34;
player.velocity.y = 0;
}
break;
case PT_LEFT_WALL: //Set player position to right of surface
if (selectedMap.col_CollisionData[count].collisionArea.getGlobalBounds().intersects(player.getSprite().getGlobalBounds())
&& (player.velocity.y < 2 || player.velocity.y > -2))
{
player.position.x = selectedMap.col_CollisionData[count].collisionArea.getPosition().x + 34;
}
break;
case PT_RIGHT_WALL: //Set player position to left of surface
if (selectedMap.col_CollisionData[count].collisionArea.getGlobalBounds().intersects(player.getSprite().getGlobalBounds())
&& (player.velocity.y < 2 || player.velocity.y > -2))
{
player.position.x = selectedMap.col_CollisionData[count].collisionArea.getPosition().x - 34;
}
break;
}
}
// Cycle through all platforms
for (int count = 0; count < MAXPLATFORMS; count++)
{
// If player is colliding with floor surface of platform and is not jumping
// --Set Position to proper
// --Set velocity to 0
// --Allow player to jump and not be in falling state
if (selectedMap.pla_PlatformData[count].topCollision.collisionArea.getGlobalBounds().intersects(player.getSprite().getGlobalBounds())
&& player.jumping == false)
{
player.position.y = selectedMap.pla_PlatformData[count].topCollision.collisionArea.getPosition().y - 32;
player.velocity.y = 0;
player.falling = false;
player.canJump = true;
}
// If player is colliding with ceiling surface
// --Set Position to proper
// --Set Velocity to 0
if (selectedMap.pla_PlatformData[count].bottomCollision.collisionArea.getGlobalBounds().intersects(player.getSprite().getGlobalBounds()))
{
player.position.y = selectedMap.pla_PlatformData[count].bottomCollision.collisionArea.getPosition().y + 34;
player.velocity.y = 0;
}
// If player is colliding with left surface
// --Set Position to proper
if (selectedMap.pla_PlatformData[count].leftCollision.collisionArea.getGlobalBounds().intersects(player.getSprite().getGlobalBounds())
&& (player.velocity.y < 2 || player.velocity.y > -2))
{
player.position.x = selectedMap.pla_PlatformData[count].leftCollision.collisionArea.getPosition().x + 34;
}
// If player is colliding with right surface
// --Set Position to proper
if (selectedMap.pla_PlatformData[count].rightCollision.collisionArea.getGlobalBounds().intersects(player.getSprite().getGlobalBounds())
&& (player.velocity.y < 2 || player.velocity.y > -2))
{
player.position.x = selectedMap.pla_PlatformData[count].rightCollision.collisionArea.getPosition().x - 34;
}
}
// Cycle through all slopes
for (int count = 0; count < MAXSLOPES; count++)
{
// Placeholders
sf::Vector2f startPoint;
sf::Vector2f playerPoint;
float difference;
float angle = selectedMap.slo_SlopeData[count].slope;
// Assign values for the focal point of slope detection of player
startPoint.x = selectedMap.slo_SlopeData[count].topCollision.collisionArea.getGlobalBounds().left + 8;
startPoint.y = selectedMap.slo_SlopeData[count].bottomCollision.collisionArea.getGlobalBounds().top - 8;
// If the player is colliding with slope and is not jumping
if ((selectedMap.slo_SlopeData[count].topCollision.collisionArea.getGlobalBounds().intersects(player.getSprite().getGlobalBounds())
|| selectedMap.slo_SlopeData[count].bottomCollision.collisionArea.getGlobalBounds().intersects(player.getSprite().getGlobalBounds())
|| selectedMap.slo_SlopeData[count].leftCollision.collisionArea.getGlobalBounds().intersects(player.getSprite().getGlobalBounds())
|| selectedMap.slo_SlopeData[count].rightCollision.collisionArea.getGlobalBounds().intersects(player.getSprite().getGlobalBounds()))
&& player.jumping == false )
{
// Right Slope
if (selectedMap.slo_SlopeData[count].inverted == false &&
selectedMap.slo_SlopeData[count].left_right == RIGHT)
{
// Assign focal point for player detection of slope
playerPoint.x = player.position.x + 34;
playerPoint.y = player.position.y + 34;
// Calculate how far player is in the slope's tile
difference = playerPoint.x - startPoint.x;
// If the player is within a certain units of the focal point,
// and the player's position is below where it should be:
// --Set position to proper
// --Set Velocity to 0
// --Allow player to jump and not be in falling state.
if ((difference > 0 && difference < (64 * angle) + 4)
&& player.position.y > startPoint.y - difference - 34)
{
player.position.y = startPoint.y - (difference / angle) - 34;
player.velocity.y = 0;
player.falling = false;
player.canJump = true;
}
}
//Left Slope
if (selectedMap.slo_SlopeData[count].inverted == false &&
selectedMap.slo_SlopeData[count].left_right == LEFT)
{
// Assign focal point for player detection of slope
playerPoint.x = player.position.x - 34;
playerPoint.y = player.position.y + 34;
// Calculate how far player is in the slope's tile
difference = playerPoint.x - startPoint.x;
// If the player is within a certain units of the focal point,
// and the player's position is below where it should be:
// --Set position to proper
// --Set Velocity to 0
// --Allow player to jump and not be in falling state.
if ((difference > 0 && difference < (64 * angle) + 4)
&& player.position.y > startPoint.y + difference - 96)
{
player.position.y = startPoint.y + (difference / angle) - 96;
player.velocity.y = 0;
player.falling = false;
player.canJump = true;
}
}
//Right Inverted Slope
if (selectedMap.slo_SlopeData[count].inverted == true &&
selectedMap.slo_SlopeData[count].left_right == RIGHT)
{
// Assign focal point for player detection of slope
playerPoint.x = player.position.x + 34;
playerPoint.y = player.position.y - 34;
// Calculate how far player is in the slope's tile
difference = playerPoint.x - startPoint.x;
// If the player is within a certain units of the focal point,
// and the player's position is below where it should be:
// --Set position to proper
// --Set Velocity to 0
if ((difference > 0 && difference < 68)
&& player.position.y < startPoint.y - difference + 96)
{
player.position.y = startPoint.y - difference + 96;
player.velocity.y = 0;
}
}
//Left Inverted Slope
if (selectedMap.slo_SlopeData[count].inverted == true &&
selectedMap.slo_SlopeData[count].left_right == LEFT)
{
// Assign focal point for player detection of slope
playerPoint.x = player.position.x - 34;
playerPoint.y = player.position.y - 34;
// Calculate how far player is in the slope's tile
difference = playerPoint.x - startPoint.x;
// If the player is within a certain units of the focal point,
// and the player's position is below where it should be:
// --Set position to proper
// --Set Velocity to 0
if ((difference > 0 && difference < 68)
&& player.position.y < startPoint.y + difference + 34)
{
player.position.y = startPoint.y + difference + 34;
player.velocity.y = 0;
}
}
}
}
}
// Checks if player is colliding with any surface
void Level::detectCollisionEnemy(EnemyObject &enemy)
{
// Cycle through all surfaces
for (int count = 0; count < MAXCOLLISIONS; count++)
{
// Detect surface type and apply effects
switch (selectedMap.col_CollisionData[count].type)
{
case PT_FLOOR: //Set player position to top of surface, zero y-velocity, stop falling and allow jumping.
if (selectedMap.col_CollisionData[count].collisionArea.getGlobalBounds().intersects(enemy.getSprite().getGlobalBounds()))
//&& enemy.jumping == false)
{
enemy.position.y = selectedMap.col_CollisionData[count].collisionArea.getPosition().y - 32;
enemy.velocity.y = 0;
enemy.falling = false;
//enemy.canJump = true;
}
break;
case PT_CEILING: //Set player position to bottom of surface
if (selectedMap.col_CollisionData[count].collisionArea.getGlobalBounds().intersects(enemy.getSprite().getGlobalBounds()))
{
enemy.position.y = selectedMap.col_CollisionData[count].collisionArea.getPosition().y + 34;
enemy.velocity.y = 0;
}
break;
case PT_LEFT_WALL: //Set player position to right of surface
if (selectedMap.col_CollisionData[count].collisionArea.getGlobalBounds().intersects(enemy.getSprite().getGlobalBounds())
&& (enemy.velocity.y < 2 || enemy.velocity.y > -2))
{
enemy.position.x = selectedMap.col_CollisionData[count].collisionArea.getPosition().x + 34;
}
break;
case PT_RIGHT_WALL: //Set player position to left of surface
if (selectedMap.col_CollisionData[count].collisionArea.getGlobalBounds().intersects(enemy.getSprite().getGlobalBounds())
&& (enemy.velocity.y < 2 || enemy.velocity.y > -2))
{
enemy.position.x = selectedMap.col_CollisionData[count].collisionArea.getPosition().x - 34;
}
break;
}
}
// Cycle through all platforms
for (int count = 0; count < MAXPLATFORMS; count++)
{
// If player is colliding with floor surface of platform and is not jumping
// --Set Position to proper
// --Set velocity to 0
// --Allow player to jump and not be in falling state
if (selectedMap.pla_PlatformData[count].topCollision.collisionArea.getGlobalBounds().intersects(enemy.getSprite().getGlobalBounds()))
//&& player.jumping == false)
{
enemy.position.y = selectedMap.pla_PlatformData[count].topCollision.collisionArea.getPosition().y - 32;
enemy.velocity.y = 0;
enemy.falling = false;
//enemy.canJump = true;
}
// If player is colliding with ceiling surface
// --Set Position to proper
// --Set Velocity to 0
if (selectedMap.pla_PlatformData[count].bottomCollision.collisionArea.getGlobalBounds().intersects(enemy.getSprite().getGlobalBounds()))
{
enemy.position.y = selectedMap.pla_PlatformData[count].bottomCollision.collisionArea.getPosition().y + 34;
enemy.velocity.y = 0;
}
// If player is colliding with left surface
// --Set Position to proper
if (selectedMap.pla_PlatformData[count].leftCollision.collisionArea.getGlobalBounds().intersects(enemy.getSprite().getGlobalBounds())
&& (enemy.velocity.y < 2 || enemy.velocity.y > -2))
{
enemy.position.x = selectedMap.pla_PlatformData[count].leftCollision.collisionArea.getPosition().x + 34;
}
// If player is colliding with right surface
// --Set Position to proper
if (selectedMap.pla_PlatformData[count].rightCollision.collisionArea.getGlobalBounds().intersects(enemy.getSprite().getGlobalBounds())
&& (enemy.velocity.y < 2 || enemy.velocity.y > -2))
{
enemy.position.x = selectedMap.pla_PlatformData[count].rightCollision.collisionArea.getPosition().x - 34;
}
}
// Cycle through all slopes
for (int count = 0; count < MAXSLOPES; count++)
{
// Placeholders
sf::Vector2f startPoint;
sf::Vector2f playerPoint;
float difference;
float angle = selectedMap.slo_SlopeData[count].slope;
// Assign values for the focal point of slope detection of player
startPoint.x = selectedMap.slo_SlopeData[count].topCollision.collisionArea.getGlobalBounds().left + 8;
startPoint.y = selectedMap.slo_SlopeData[count].bottomCollision.collisionArea.getGlobalBounds().top - 8;
// If the player is colliding with slope and is not jumping
if ((selectedMap.slo_SlopeData[count].topCollision.collisionArea.getGlobalBounds().intersects(enemy.getSprite().getGlobalBounds())
|| selectedMap.slo_SlopeData[count].bottomCollision.collisionArea.getGlobalBounds().intersects(enemy.getSprite().getGlobalBounds())
|| selectedMap.slo_SlopeData[count].leftCollision.collisionArea.getGlobalBounds().intersects(enemy.getSprite().getGlobalBounds())
|| selectedMap.slo_SlopeData[count].rightCollision.collisionArea.getGlobalBounds().intersects(enemy.getSprite().getGlobalBounds())))
//&& player.jumping == false)
{
// Right Slope
if (selectedMap.slo_SlopeData[count].inverted == false &&
selectedMap.slo_SlopeData[count].left_right == RIGHT)
{
// Assign focal point for player detection of slope
playerPoint.x = enemy.position.x + 34;
playerPoint.y = enemy.position.y + 34;
// Calculate how far player is in the slope's tile
difference = playerPoint.x - startPoint.x;
// If the player is within a certain units of the focal point,
// and the player's position is below where it should be:
// --Set position to proper
// --Set Velocity to 0
// --Allow player to jump and not be in falling state.
if ((difference > 0 && difference < (64 * angle) + 4)
&& enemy.position.y > startPoint.y - difference - 34)
{
enemy.position.y = startPoint.y - (difference / angle) - 34;
enemy.velocity.y = 0;
enemy.falling = false;
//enemy.canJump = true;
}
}
//Left Slope
if (selectedMap.slo_SlopeData[count].inverted == false &&
selectedMap.slo_SlopeData[count].left_right == LEFT)
{
// Assign focal point for player detection of slope
playerPoint.x = enemy.position.x - 34;
playerPoint.y = enemy.position.y + 34;
// Calculate how far player is in the slope's tile
difference = playerPoint.x - startPoint.x;
// If the player is within a certain units of the focal point,
// and the player's position is below where it should be:
// --Set position to proper
// --Set Velocity to 0
// --Allow player to jump and not be in falling state.
if ((difference > 0 && difference < (64 * angle) + 4)
&& enemy.position.y > startPoint.y + difference - 96)
{
enemy.position.y = startPoint.y + (difference / angle) - 96;
enemy.velocity.y = 0;
enemy.falling = false;
//enemy.canJump = true;
}
}
//Right Inverted Slope
if (selectedMap.slo_SlopeData[count].inverted == true &&
selectedMap.slo_SlopeData[count].left_right == RIGHT)
{
// Assign focal point for player detection of slope
playerPoint.x = enemy.position.x + 34;
playerPoint.y = enemy.position.y - 34;
// Calculate how far player is in the slope's tile
difference = playerPoint.x - startPoint.x;
// If the player is within a certain units of the focal point,
// and the player's position is below where it should be:
// --Set position to proper
// --Set Velocity to 0
if ((difference > 0 && difference < 68)
&& enemy.position.y < startPoint.y - difference + 96)
{
enemy.position.y = startPoint.y - difference + 96;
enemy.velocity.y = 0;
}
}
//Left Inverted Slope
if (selectedMap.slo_SlopeData[count].inverted == true &&
selectedMap.slo_SlopeData[count].left_right == LEFT)
{
// Assign focal point for player detection of slope
playerPoint.x = enemy.position.x - 34;
playerPoint.y = enemy.position.y - 34;
// Calculate how far player is in the slope's tile
difference = playerPoint.x - startPoint.x;
// If the player is within a certain units of the focal point,
// and the player's position is below where it should be:
// --Set position to proper
// --Set Velocity to 0
if ((difference > 0 && difference < 68)
&& enemy.position.y < startPoint.y + difference + 34)
{
enemy.position.y = startPoint.y + difference + 34;
enemy.velocity.y = 0;
}
}
}
}
}*/