-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathModulePlayer2.cpp
574 lines (463 loc) · 18.5 KB
/
ModulePlayer2.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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/*#include "Globals.h"
#include "Application.h"
#include "ModuleTextures.h"
#include "ModuleInput.h"
#include "ModuleRender.h"
#include "ModulePlayer2.h"
#include "ModuleCollision.h"
#include "ModulePlayer2Unit.h"
#include "ModuleParticles.h"
#include "ModuleAudio.h"
#include "ModuleFadeToBlack.h"
ModulePlayer2::ModulePlayer2()
{
position.x = 40;
position.y = 80;
//spawn anim
spawnAnim.PushBack({ 0,122,111,1 }); //0,79
spawnAnim.PushBack({ 6,125,105,2 }); //13,80
spawnAnim.PushBack({ 1,127,76,4 }); //12,79
spawnAnim.PushBack({ 0,131,74,8 }); //12,76
spawnAnim.PushBack({ 2,142,62,15 }); //8,72
spawnAnim.PushBack({ 2,171,62,15 }); //8,73
spawnAnim.PushBack({ 13,193,51,16 }); //16,72
spawnAnim.PushBack({ 13,219,51,16 }); //16,72
spawnAnim.PushBack({ 64,143,64,16 }); //8,72
spawnAnim.PushBack({ 64,164,64,25 }); //8,67
spawnAnim.PushBack({ 71,188,57,25 }); //15,67
spawnAnim.PushBack({ 72,214,56,25 }); //16,67
spawnAnim.PushBack({ 156,143,36,19 }); //36,71
spawnAnim.PushBack({ 160,171,32,15 }); //40,73
spawnAnim.speed = 0.25f;
spawnAnim.repeat = false;
//movement anim
playerAnim.PushBack({ 0,3,32,13 }); //up2 - 0
playerAnim.PushBack({ 32,3,32,13 }); //up1 - 1
playerAnim.PushBack({ 64,3,32,12 }); //idle - 2
playerAnim.PushBack({ 96,3,32,12 }); //down1 - 3
playerAnim.PushBack({ 128,4,32,11 }); //down2 - 4
//Player Dying animation
playerDyingAnim.PushBack({ 18,20,37,16 });
playerDyingAnim.PushBack({ 14,37,41,16 });
playerDyingAnim.PushBack({ 9,53,46,17 });
playerDyingAnim.PushBack({ 3,70,52,17 });
playerDyingAnim.PushBack({ 2,87,53,15 });
playerDyingAnim.PushBack({ 0,104,55,16 });
playerDyingAnim.PushBack({ 57,21,53,15 });
playerDyingAnim.PushBack({ 58,36,52,16 });
playerDyingAnim.PushBack({ 55,53,55,17 });
playerDyingAnim.PushBack({ 56,70,54,17 });
playerDyingAnim.PushBack({ 55,88,55,16 });
playerDyingAnim.PushBack({ 57,104,53,17 });
playerDyingAnim.PushBack({ 112,21,53,16 });
playerDyingAnim.PushBack({ 114,38,51,16 });
playerDyingAnim.PushBack({ 118,54,47,15 });
playerDyingAnim.PushBack({ 118,70,47,16 });
playerDyingAnim.PushBack({ 124,90,41,12 });
playerDyingAnim.PushBack({ 128,108,37,10 });
playerDyingAnim.speed = 0.3f;
playerDyingAnim.repeat = false;
//beam flash smoke
beamSmoke.PushBack({ 128,126,10,9 });
beamSmoke.PushBack({ 115,124,13,12 });
beamSmoke.PushBack({ 0,0,0,0 });
beamSmoke.PushBack({ 0,0,0,0 });
beamSmoke.PushBack({ 0,0,0,0 });
beamSmoke.PushBack({ 128,126,10,9 });
beamSmoke.PushBack({ 115,124,13,12 });
beamSmoke.PushBack({ 115,124,13,12 });
beamSmoke.speed = 0.5f;
beamSmoke.repeat = false;
//boost animation
boostAnim.PushBack({ 0,0,32,31 });
boostAnim.PushBack({ 33,0,32,32 });
boostAnim.PushBack({ 64,2,32,26 });
boostAnim.PushBack({ 96,7,32,18 });
boostAnim.PushBack({ 0,33,32,13 });
boostAnim.PushBack({ 33,36,32,7 }); //42,36,22,7
boostAnim.PushBack({ 64,36,32,7 }); //71,36,25,7
boostAnim.PushBack({ 96,36,32,6 }); //42,36,22,7
boostAnim.PushBack({ 0,53,32,6 });
boostAnim.PushBack({ 33,52,32,6 });
boostAnim.speed = 0.25f;
boostAnim.repeat = false;
//laser powerUp flash
laserFlash.PushBack({ 1,1,13,13 });
laserFlash.PushBack({ 16,1,15,15 });
laserFlash.PushBack({ 33,1,27,9 });
laserFlash.PushBack({ 32,18,31,7 });
laserFlash.PushBack({ 1,27,31,5 });
laserFlash.PushBack({ 1,39,32,3 });
laserFlash.PushBack({ 1,34,32,3 });
laserFlash.PushBack({ 1,18,30,3 });
laserFlash.speed = 0.25f;
laserFlash.repeat = false;
}
ModulePlayer2::~ModulePlayer2()
{}
// Load assets
bool ModulePlayer2::Start()
{
LOG("Loading player textures");
bool ret = true;
player2 = App->textures->Load("assets/Graphics/Player/player1_incomplete.png");
powerUpTextures = App->textures->Load("assets/Graphics/Player/PowerUps.png");
laserFlashTexture = App->textures->Load("assets/Graphics/Player/laserFlash.png");
//restart player positions for next time playerModule call
position.x = 40;
position.y = 80;
//player collision rect
//playerCollider = App->collision->AddCollider({ position.x, position.y, 32, 12 }, COLLIDER_PLAYER,this);
frameIncrement = 2; //initializes speed positions frame rect array incrementer at "center" -> idle
ignitionSpeed = 0.2f; //speed when accelerate
releaseSpeed = 0.1f; //speed when releases direction keys to return to idle
//resets player score on every start
playerScore = 0;
destroyed = false;
//load necessary fx wavs
App->audio->LoadAudio("assets/Audio/SFX/Player/speedUpgrade.wav", "speedUP", SFX);
App->audio->LoadAudio("assets/Audio/SFX/Player/speedDowngrade.wav", "speedDN", SFX);
App->audio->LoadAudio("assets/Audio/SFX/Player/spawn.wav", "spawn", SFX);
App->audio->LoadAudio("assets/Audio/SFX/Player/death.wav", "death", SFX);
// ---------------------
//for new gameLoops
//checks if the player state is normal, if is, spawn condition
if (player_step == player_state::normal || player_step == player_state::died)
player_step = player_state::spawn;
//and play FX, for the moment, right here is good
App->audio->ControlAudio("spawn", SFX, PLAY);
//defines times for buffs
powerUpTime = 5000; //in ms
return ret;
}
update_status ModulePlayer2::PreUpdate()
{
//FUNCTIONALITY POWERUPS, assigns speeds, animations, etc
if (player_step == player_state::normal)
{
now = SDL_GetTicks() - start_time; //time events/states general counter
//BOOST LOGIC ---------------------------------------------------------
if (powerUpActive == powerUpTypes::BOOST)
{
//play FX
App->audio->ControlAudio("speedUP", SFX, PLAY);
start_time = SDL_GetTicks(); //start time of the boost buff
speed = boostPlayerSpeed;
activebuff.boost = true; //actives bool condition
activebuff.boostAnim = true;
powerUpActive = powerUpTypes::NONE; //resets the enumerator to free the way for another
//if we are on brake
activebuff.brake = false;
}
//float speed = 1.4f; //player position speed
if (!activebuff.boost && !activebuff.brake) //if the time for boost go out... and the boost brake is not active, normal speed
{
playerSpeed = speed = normalPlayerSpeed; //resets the temporal incrementer to always Nicolas at the desired incrementer count
//new change direction starts incrementer at speed correct value (always same distances)
}
else if (activebuff.brake)
speed = brakePlayerSpeed;
if (powerUpActive == powerUpTypes::BRAKE)
{
//activebuff.boost = false; //its finish automatic when its animation finish
activebuff.brake = true;
powerUpActive = powerUpTypes::NONE;
}
if (powerUpActive == powerUpTypes::LASER)
{
activebuff.laser = true;
powerUpActive = powerUpTypes::NONE;
}
// --------------------------------------------------------------------
}
return UPDATE_CONTINUE;
}
// Update: draw background
update_status ModulePlayer2::Update()
{
current_animation = &playerAnim;
SDL_Rect r;
if (player_step == player_state::spawn)
{
current_animation = &spawnAnim;
r = current_animation->GetCurrentFrame();
if (current_animation->finish)
{
player_step = player_state::normal;
current_animation->finish = false; //and resets condition
current_animation->current_frame = 0;
//activate collider
playerCollider = App->collision->AddCollider({ position.x, position.y, 32, 12 }, COLLIDER_PLAYER, this);
}
}
else if (player_step == player_state::normal)
{
//for now activates playerUnit for testing here
//if (!App->player2Unit->IsEnabled())
//{
// //App->playerUnit->Enable();
//}
/*if (powerUpActive == powerUpTypes::BOOST)
{
speed = boostPlayerSpeed;
now = SDL_GetTicks() - start_time;
if (now >= poweUpTime)
{
start_time = SDL_GetTicks();
powerUpActive = powerUpTypes::NONE;
}
}
//float speed = 1.4f; //player position speed
else
playerSpeed = speed = normalPlayerSpeed; //resets the temporal incrementer to always Nicolas at the desired incrementer count
//new change direction starts incrementer at speed correct value (always same distances)
//App->render->Blit(player,300, 50, &spawnAnim.GetCurrentFrame());
//Animation* current_animation = &playerAnim;
//animation logic ---------------------------------------------------------------
if (App->input->keyboard[SDL_SCANCODE_DOWN] == KEY_STATE::KEY_REPEAT) //while press down
{
frameIncrement += ignitionSpeed;
if (frameIncrement >= 5)
frameIncrement = 4.99f;
}
//for quickly directions change, release to turn idle speed incrementer doesn't affect
//yet, and go to top up animation (or wherever we are) at ignition speed (normal/fast direction change)
//and only if the two keys are released, return at release speed to idle position
//we check on separate for fast direction change (no key release = to estabilization spaceship)
//----ship estabilization check (return to idle when release up or down) ---
if (App->input->keyboard[SDL_SCANCODE_DOWN] == KEY_STATE::KEY_IDLE && (int)frameIncrement != 2)
{
if ((int)frameIncrement >= 2)
frameIncrement -= releaseSpeed;
}
if (App->input->keyboard[SDL_SCANCODE_UP] == KEY_STATE::KEY_IDLE && (int)frameIncrement != 2)
{
if (frameIncrement < 2)
frameIncrement += releaseSpeed;
}
//---------------------------------------------------------------------------
//already, double check to imitate original animation behaviour (emulator)
if (App->input->keyboard[SDL_SCANCODE_UP] == KEY_STATE::KEY_REPEAT &&
App->input->keyboard[SDL_SCANCODE_DOWN] == KEY_STATE::KEY_IDLE) //while press up
{
frameIncrement -= ignitionSpeed;
if (frameIncrement <= 0)
frameIncrement = 0;
}
//--------------------------------------------------------------------------------
r = current_animation->frames[(int)frameIncrement]; //idle
//--------------------------------------------------------------------------------
//move player position (this section must be code optimized ----------------------
//down move position
if (App->input->keyboard[SDL_SCANCODE_DOWN] == KEY_STATE::KEY_REPEAT)
{
playerSpeed += speed;
position.y += (int)playerSpeed;
if (playerSpeed >= speed + 1) //returns correct value to cast incrementer
playerSpeed = speed;
if (position.y + (int)playerSpeed >= SCREEN_HEIGHT - 8) // 8 = player height/2 + required offset
position.y = 216; //target max down player position (original game pixel)
}
//up move position, checks if not down to imitate original game (emulator) behaviour
if (App->input->keyboard[SDL_SCANCODE_UP] == KEY_STATE::KEY_REPEAT &&
App->input->keyboard[SDL_SCANCODE_DOWN] == KEY_STATE::KEY_IDLE)
{
playerSpeed += speed;
position.y -= (int)playerSpeed;
if (playerSpeed >= speed + 1) //checks and assign correct value to int cast float incrementer
playerSpeed = speed; //and next cycle
if (position.y + (int)playerSpeed <= 0 + 8) //0 = min y coordinate, 8 = texture height/2 + offset
position.y = 8; //target min y player position
}
//---------------------------------------------------------------------------------
//right move, doble check for imitate original (emulator) behaviour
if (App->input->keyboard[SDL_SCANCODE_RIGHT] == KEY_STATE::KEY_REPEAT &&
App->input->keyboard[SDL_SCANCODE_LEFT] == KEY_STATE::KEY_IDLE)
{
playerSpeed += speed;
position.x += (int)playerSpeed;
if (playerSpeed >= speed + 1) //checks and assign correct value to int cast float incrementer
playerSpeed = speed; //and next cycle
//playerSpeed at if is for assure the limits independent of speed value
//if (position.x + (int)playerSpeed >= 256) //0 = min y coordinate, 9=texture height/2 + offset
//position.x = 256; //target min y player position
if (position.x > (abs(App->render->camera.x) / SCREEN_SIZE) + SCREEN_WIDTH - (32 + 16)) //player text width + offset
{
position.x = (abs(App->render->camera.x) / SCREEN_SIZE) + SCREEN_WIDTH - (32 + 16);// 16 = 1/2 player width + 16 offset original game
}
}
//left move
if (App->input->keyboard[SDL_SCANCODE_LEFT] == KEY_STATE::KEY_REPEAT)
{
playerSpeed += speed;
position.x -= (int)playerSpeed;
if (playerSpeed >= speed + 1) //checks and assign correct value to int cast float incrementer
playerSpeed = speed; //and next cycle
/*if (position.x + (int)playerSpeed <= 0 + 16) //0 = min y coordinate, 9=texture height/2 + offset
position.x = 16; //target min y player position
if (position.x < abs(App->render->camera.x) / SCREEN_SIZE + 16)
{
position.x = abs(App->render->camera.x) / SCREEN_SIZE + 16;// 16 = 1/2 player width + 16 offset original game
}
}
cameraPosition = abs(App->render->camera.x / 3);
//--------------------------------------------------------------------------------
//if player press keys of particles emitters
if (App->input->keyboard[SDL_SCANCODE_M] == KEY_STATE::KEY_DOWN)
{
LOG("Beam!");
if (activebuff.laser)
{
shootingLaser = true;
App->particles->AddParticle(App->particles->laser, position.x + 32, position.y - 2, COLLIDER_PLAYER_SHOT); //"shot");
}
shooting = true;
App->particles->AddParticle(App->particles->beam, position.x + 32, position.y - 4, COLLIDER_PLAYER_SHOT); //, "shot");
}
//checks animation cycle --------------------------------------------------------------------------------------------------
if (shooting)
{
if (beamSmoke.current_frame == beamSmoke.last_frame) //resets animation cycle
{
beamSmoke.current_frame = 0;
beamSmoke.finish = false;
shooting = false;
}
App->render->Blit(player2, position.x + 32, position.y - 6, &beamSmoke.GetCurrentFrame());
}
if (shootingLaser)
{
Animation* currentLaserFlash = &laserFlash;
SDL_Rect currentLaserFlashRect = currentLaserFlash->GetCurrentFrame();
if (App->input->keyboard[SDL_SCANCODE_M] == KEY_DOWN) //force restart animation for new shot
currentLaserFlash->current_frame = 0;
if (currentLaserFlash->current_frame >= currentLaserFlash->last_frame) //resets animation cycle
{
laserFlash.current_frame = 0;
laserFlash.finish = false;
shootingLaser = false;
}
App->render->Blit(laserFlashTexture, position.x + 32, position.y - 1 - (currentLaserFlashRect.h / 2), ¤tLaserFlashRect);
}
// -------------------------------------------------------------------------------------------------------------------------
//CHECK POWERUP ANIMATIONS etc ---------------------------------------------------------------------------------------------
if (activebuff.boost)
{
now = SDL_GetTicks() - start_time;
if (now >= powerUpTime) //checks the timer counter and deactivate the boost time
{
activebuff.boost = false;
//PLAY SFX
App->audio->ControlAudio("speedDN", SFX, PLAY);
}
if (activebuff.boostAnim)
{
current_animation = &boostAnim;
SDL_Rect boostAnimRect = current_animation->GetCurrentFrame();
if (current_animation->finish)
{
activebuff.boostAnim = false;
current_animation->current_frame = 0; //and resets its values for next boost
current_animation->finish = false;
}
else
App->render->Blit(powerUpTextures, position.x - 32, position.y - (boostAnimRect.h / 2), &boostAnimRect);
}
//if (activebuff.laser)
//{
//current_animation = &laserFlash;
//}
}
// --------------------------------------------------------------------------------------------------------------------------
//update player collider to its position -----------------------------------------
playerCollider->SetPos(position.x, position.y - 6);
//DEBUG FUNCTIONALITIES -------------------------------------------------------------------------------------------------
//DEBUG: GODMODE F2 ---------------------------------
if (App->input->keyboard[SDL_SCANCODE_F2] == KEY_DOWN)
{
if (playerCollider != nullptr && !godMode)
{
this->playerCollider->to_delete = true;
playerCollider = nullptr;
playerCollider = App->collision->AddCollider({ position.x, position.y - 6, 32, 12 }, COLLIDER_PLAYER_GOD, this);
godMode = true;
}
else
{
this->playerCollider->to_delete = true;
playerCollider = nullptr;
playerCollider = App->collision->AddCollider({ position.x, position.y - 6, 32, 12 }, COLLIDER_PLAYER, this);
godMode = false;
}
}
} // ----------------------------------------------------------------------------------------------------------------------
//DRAW PLAYER ---------------------------------------------------------------------------
//draw SPAWN ANIMATION --------
if (player_step == player_state::spawn)
App->render->Blit(player2, position.x - pivotsSpawnX[(int)current_animation->current_frame], position.y - (r.h / 2), &r, 1.0f);
else if (player_step == player_state::normal)
//draw player NORMAL STATE --------------------------------------------------------------
App->render->Blit(player2, position.x, position.y - (r.h / 2), &r, 1.0f);
// --------------------------------------------------------------------------------------
else if (player_step == player_state::died && destroyed)
{
current_animation = &playerDyingAnim;
r = current_animation->GetCurrentFrame();
App->render->Blit(player2, position.x, position.y - (r.h / 2), &r);
if (current_animation->finish)
{
current_animation->current_frame = 0;
current_animation->finish = false;
destroyed = false;
if (lives <= 1)
{
//resets lives counter for next gameLoop
lives = 3;
App->fade->FadeToBlack((Module*)App->scene_lvl1, (Module*)App->gameOverScreen);
}
else
{
App->fade->FadeToBlack((Module*)App->scene_lvl1, (Module*)App->continueScreen, 1.0f);
--lives;
}
}
}
return UPDATE_CONTINUE;
}
void ModulePlayer2::OnCollision(Collider* collider1, Collider* collider2)
{
//deactivate player active powerUps
//if (App->player2Unit->IsEnabled())
// App->player2Unit->Disable();
player_step = player_state::died;
destroyed = true;
//PLAY FX
App->audio->ControlAudio("death", SFX, PLAY);
if (playerCollider != nullptr)
this->playerCollider->to_delete = true;
}
bool ModulePlayer2::CleanUp()
{
//freeing textures
if (player2 != nullptr)
App->textures->Unload(player2);
if (powerUpTextures != nullptr)
App->textures->Unload(player2);
if (playerCollider != nullptr)
{
playerCollider->to_delete = true;
}
//if we are on super sayan GOD
godMode = false;
//unloading SFX
App->audio->UnloadAudio("speedDN", SFX);
//App->audio->UnloadAudio("speedUP", SFX);
App->audio->UnloadAudio("spawn", SFX);
App->audio->UnloadAudio("death", SFX);
//deactivate all possible active buffs
activebuff.bombs = false;
activebuff.boost = false;
activebuff.brake = false;
activebuff.laser = false;
return true;
}*/