-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathModulePowerUp.cpp
276 lines (224 loc) · 7.12 KB
/
ModulePowerUp.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
#include "Globals.h"
#include "Application.h"
#include "ModuleTextures.h"
#include "ModuleRender.h"
//#include "ModuleParticles.h"
#include "Animation.h"
#include "ModuleAudio.h"
//#include "ModulePlayer.h"
#include "ModuleCollision.h"
#include "ModulePowerUp.h"
//#include "ModuleEnemies.h"
#include "ModulePlayerUnit.h"
ModulePowerUp::ModulePowerUp()
{
for (uint i = 0; i < MAX_TYPES; ++i)
active_powerUps[i] = nullptr;
//testing purposes rect
testRect.x = 0;
testRect.y = 0;
testRect.w = 31;
testRect.h = 16;
//powerUp Animation rects
powerUpG.anim.PushBack({ 18,96,31,16 });
powerUpG.anim.PushBack({ 49,96,31,16 });
powerUpG.anim.speed = 0.01f;
powerUpG.type = powerUpTypes::BOMBS;
powerUpH.anim.PushBack({18,64,31,16});
powerUpH.anim.PushBack({ 49,64,31,16 });
powerUpH.anim.speed = 0.01f;
powerUpH.type = powerUpTypes::MISSILES;
powerUpL.anim.PushBack({ 18, 80, 31, 16 });
powerUpL.anim.PushBack({ 49, 80, 31, 16 });
powerUpL.anim.speed = 0.01f;
powerUpL.type = powerUpTypes::LASER;
powerUpS.anim.PushBack({ 0,64,18,21 });
powerUpS.type = powerUpTypes::BOOST;
powerUpZ.anim.PushBack({ 0,85,18,21 });
powerUpZ.type = powerUpTypes::BRAKE;
}
ModulePowerUp::~ModulePowerUp() {}
bool ModulePowerUp::Start()
{
powerUpTextures = App->textures->Load("assets/Graphics/Player/PowerUps.png");
//Audio
App->audio->LoadAudio("assets/Audio/SFX/Player/taking_Unit.wav", "Taking_Unit", SFX);
App->audio->LoadAudio("assets/Audio/SFX/Player/pickPowerUp.wav", "pickPowerUp", SFX);
return true;
}
update_status ModulePowerUp::Update()
{
for (uint i = 0; i < MAX_ACTIVE_POWERUPS; ++i)
{
PowerUp* p = active_powerUps[i];
if (p == nullptr)
continue;
if (p->Update() == false)
{
delete p;
active_powerUps[i] = nullptr;
}
App->render->Blit(powerUpTextures, p->position.x, p->position.y, &(p->anim.GetCurrentFrame()));
if (p->fx_played == false && p->fx != nullptr)
{
p->fx_played = true;
// PLAY SPAWN power up effect
App->audio->ControlAudio(p->fx, SFX, PLAY);
}
}
return UPDATE_CONTINUE;
}
bool ModulePowerUp::CleanUp()
{
//remove powerups and its colliders
for (uint i = 0; i < MAX_ACTIVE_POWERUPS; ++i)
{
if (active_powerUps[i] != nullptr)
{
active_powerUps[i]->collider->to_delete = true; //delete colliders
delete active_powerUps[i]; //delete poweUp instance
active_powerUps[i] = nullptr;
}
}
//unload textures
if (powerUpTextures != nullptr)
App->textures->Unload(powerUpTextures);
//unload audio
App->audio->UnloadAudio("Taking_Unit", SFX);
App->audio->UnloadAudio("pickPowerUp", SFX);
return true;
}
void ModulePowerUp::SpawnPowerUp(iPoint position, powerUpTypes type)
{
if (type != powerUpTypes::NONE)
{
LOG("Spawning powerUP");
for (uint i = 0; i < MAX_ACTIVE_POWERUPS; ++i)
{
if (active_powerUps[i] == nullptr) //find room
{
PowerUp* p = nullptr;
switch (type)
{
case powerUpTypes::BOMBS:
p = new PowerUp(powerUpG);
break;
case powerUpTypes::LASER:
p = new PowerUp(powerUpL);
break;
case powerUpTypes::MISSILES:
p = new PowerUp(powerUpH);
break;
case powerUpTypes::BOOST:
p = new PowerUp(powerUpS);
break;
case powerUpTypes::BRAKE:
p = new PowerUp(powerUpZ);
break;
default:
//untill we got all powerup working, for avoid memory violations, assigns default powerup
p = new PowerUp(powerUpG);//continue; //if we send invalid type, or not contempled, do nothing
break;
}
p->position = position;
p->type = type;
if (p->collider == nullptr)
p->collider = App->collision->AddCollider(p->anim.GetCurrentFrame(), COLLIDER_POWER_UP, this);
active_powerUps[i] = p;
break;
}
}
}
}
void ModulePowerUp::OnCollision(Collider* c1, Collider* c2)
{
//check what player are picking the powerUp
int playerIndex;
if (c2->callback == App->player[0]) playerIndex = 0;
else playerIndex = 1;
for (uint i = 0; i < MAX_ACTIVE_POWERUPS; ++i)
{
// and destroy power up when pick up
if (active_powerUps[i] != nullptr && active_powerUps[i]->collider == c1) //and assure if is the powerup who tells he is
{
//assigns the color of the pickup item, if not is a brake or boost wich only has one frame per anim
//
if (active_powerUps[i]->anim.current_frame < 1)// &&
/*active_powerUps[i]->type != powerUpTypes::BOOST ||
active_powerUps[i]->type != powerUpTypes::BRAKE)*/ color = powerUpColor::ORANGE;
else if (active_powerUps[i]->anim.current_frame > 1)// &&
/*active_powerUps[i]->type != powerUpTypes::BOOST ||
active_powerUps[i]->type != powerUpTypes::BRAKE)*/ color = powerUpColor::BLUE;
//add particle or fx, etc when pickup
switch (active_powerUps[i]->type)
{
case powerUpTypes::BOMBS:
//call the functionality of the bombs powerup
App->player[playerIndex]->powerUpActive = powerUpTypes::BOMBS;
App->audio->ControlAudio("pickPowerUp", SFX, PLAY);
break;
case powerUpTypes::BOOST:
//call the functionality of the bombs powerup
//play FX
//App->audio->ControlAudio("speedUP", SFX, PLAY);
App->player[playerIndex]->powerUpActive = powerUpTypes::BOOST;
color = App->playerUnit[playerIndex]->actualUnitColor; //this not change the color of the unit
break;
case powerUpTypes::BRAKE:
//call the functionality of the bombs powerup
App->audio->ControlAudio("speedDN", SFX, PLAY);
color = App->playerUnit[playerIndex]->actualUnitColor; //this not change the color of the unit
App->player[playerIndex]->powerUpActive = powerUpTypes::BRAKE;
break;
case powerUpTypes::LASER:
//call the functionality of the laser powerup
App->player[playerIndex]->powerUpActive = powerUpTypes::LASER;
App->audio->ControlAudio("pickPowerUp", SFX, PLAY);
break;
case powerUpTypes::MISSILES:
//call the functionality of the missiles powerup
App->player[playerIndex]->powerUpActive = powerUpTypes::MISSILES;
App->audio->ControlAudio("pickPowerUp", SFX, PLAY);
break;
case powerUpTypes::NONE:
//for some functionality, if we need
break;
default:
break;
}
//always check the color of picked power up, for change the functionality and color of the unit
App->playerUnit[playerIndex]->swapColor(color); //swap color to the right picked up item
if (!App->playerUnit[playerIndex]->IsEnabled()) //if the player does not have the unit active, activate and swap color
{
App->audio->ControlAudio("Taking_Unit", SFX, PLAY);
App->playerUnit[playerIndex]->playerIndex = playerIndex;
App->playerUnit[playerIndex]->Enable();
}
//delete the instanciated powerup
delete active_powerUps[i];
active_powerUps[i] = nullptr;
//delete the collider
c1->to_delete = true;
break;
}
}
}
PowerUp::PowerUp()
{
position.SetToZero();
speed.SetToZero();
}
PowerUp::~PowerUp()
{
if (collider != nullptr)
collider->to_delete = true;
}
bool PowerUp::Update()
{
bool ret = true;
if (position.x < (abs(App->render->camera.x) / SCREEN_SIZE) - 30) //if powerUp passes without the attention of the player, destroy it
ret = false;
if(collider != nullptr) //updates collider position
collider->SetPos(position.x, position.y);
return ret;
}