-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModuleWeapons.cpp
233 lines (198 loc) · 5.14 KB
/
ModuleWeapons.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
#include "ModuleWeapons.h"
#include "Application.h"
#include "ModuleRenderer.h"
#include "ModuleTextureManager.h"
#include "ModuleAudio.h"
#include "Weapon.h"
#include "Bomb.h"
#include "S.Shell.h"
#include "Falcon.h"
#include "Ceiling.h"
#include "GF_Hook.h"
#include "SB_Bomb.h"
#include "SB_BombShrapnel.h"
#include "PowerupOrange.h"
#include "PowerupBlue.h"
#include "Level1Gun.h"
#include "ModuleParticles.h"
#include "ModulePlayer.h"
#include<cassert>
#define SPAWN_MARGIN 50
ModuleWeapons::ModuleWeapons(bool startEnabled) : Module(startEnabled)
{
name = "Weapons";
for (uint i = 0; i < MAX_WEAPONS; ++i)
weapons[i] = nullptr;
}
ModuleWeapons::~ModuleWeapons()
{
}
bool ModuleWeapons::Start()
{
texture = App->textures->Load("Assets/sprites/weapons/weapons.png");
++activeTextures; ++totalTextures;
weaponDestroyedFx = App->audio->LoadFx("Assets/music/explosionWeapon.wav"); // Change later
++activeFx; ++totalFx;
return true;
}
update_status ModuleWeapons::PreUpdate()
{
// Remove all weapons scheduled for deletion
for (uint i = 0; i < MAX_WEAPONS; ++i)
{
if (weapons[i] != nullptr && weapons[i]->pendingToDelete)
{
delete weapons[i];
weapons[i] = nullptr;
}
}
return update_status::UPDATE_CONTINUE;
}
update_status ModuleWeapons::Update()
{
for (uint i = 0; i < MAX_WEAPONS; ++i)
{
if (weapons[i] != nullptr)
weapons[i]->Update();
}
DespawnWeapon();
return update_status::UPDATE_CONTINUE;
}
update_status ModuleWeapons::PostUpdate()
{
for (uint i = 0; i < MAX_WEAPONS; ++i)
{
if (weapons[i] != nullptr)
weapons[i]->Draw();
}
return update_status::UPDATE_CONTINUE;
}
// Called before quitting
bool ModuleWeapons::CleanUp()
{
LOG("Freeing all weapons");
for (uint i = 0; i < MAX_WEAPONS; ++i)
{
if (weapons[i] != nullptr)
{
delete weapons[i];
weapons[i] = nullptr;
}
}
activeTextures = activeFx = 0;
if (!App->textures->Unload(texture)) {
LOG("Start Screen -> Error unloading the texture.");
return false;
}
--totalTextures;
App->audio->UnloadFx(weaponDestroyedFx);
--totalFx;
return true;
}
void ModuleWeapons::SpawnWeapon(WEAPON_TYPE weaponType, int x, int y)
{
for (uint i = 0; i < MAX_WEAPONS; ++i)
{
if (weapons[i] == nullptr)
{
switch (weaponType)
{
case WEAPON_TYPE::BOMB:
weapons[i] = new Bomb(App->player->GetPlayerPosition().x, App->player->GetPlayerPosition().y);
break;
case WEAPON_TYPE::SHELL:
weapons[i] = new Shell(App->player->GetPlayerPosition().x, App->player->GetPlayerPosition().y);
break;
case WEAPON_TYPE::FALCON:
weapons[i] = new Falcon(App->player->GetPlayerPosition().x, App->player->GetPlayerPosition().y);
break;
case WEAPON_TYPE::CEILING:
weapons[i] = new Ceiling(x, y);
break;
case WEAPON_TYPE::POWERUP_ORANGE:
weapons[i] = new PowerupOrange(x, y);
break;
case WEAPON_TYPE::POWERUP_BLUE:
weapons[i] = new PowerupBlue(x, y);
break;
case WEAPON_TYPE::LEVEL1GUN:
weapons[i] = new Level1Gun(x, y);
break;
}
weapons[i]->texture = this->texture;
weapons[i]->destroyedFx = weaponDestroyedFx;
break;
}
}
}
void ModuleWeapons::SpawnWeapon(WEAPON_TYPE weaponType,int x, int y, unsigned int number) {
assert(number <= 7);
for (uint i = 0; i < MAX_WEAPONS; ++i)
{
if (weapons[i] == nullptr)
{
switch (weaponType)
{
case WEAPON_TYPE::GF_HOOK:
weapons[i] = new GF_Hook(x, y,number);
break;
case WEAPON_TYPE::SB_BOMB:
weapons[i] = new SB_Bomb(x, y,number);
break;
case WEAPON_TYPE::SB_SHRAPNEL:
weapons[i] = new SB_BombShrapnel(x, y, number);
break;
}
weapons[i]->texture = this->texture;
weapons[i]->destroyedFx = weaponDestroyedFx;
break;
}
}
}
void ModuleWeapons::DespawnWeapon()
{
// Iterate existing weapons
for (uint i = 0; i < MAX_WEAPONS; ++i)
{
if (weapons[i] != nullptr)
{
// Delete the weapon when it has reached the end of the screen
if (weapons[i]->position.x * SCREEN_SIZE < (App->render->camera.x) - SPAWN_MARGIN)
{
LOG("DeSpawning weapon at %d", weapons[i]->position.x * SCREEN_SIZE);
weapons[i]->SetToDelete();
}
else if (weapons[i]->position.x * SCREEN_SIZE > ((App->render->camera.x) + SPAWN_MARGIN + (SCREEN_WIDTH * SCREEN_SIZE)))
{
LOG("DeSpawning weapon at %d", weapons[i]->position.x * SCREEN_SIZE);
weapons[i]->SetToDelete();
}
else if (weapons[i]->position.y * SCREEN_SIZE < (App->render->camera.y) - SPAWN_MARGIN)
{
LOG("DeSpawning weapon at %d", weapons[i]->position.x * SCREEN_SIZE);
weapons[i]->SetToDelete();
}
else if (weapons[i]->position.y * SCREEN_SIZE > ((App->render->camera.y) + SPAWN_MARGIN + (SCREEN_HEIGHT * SCREEN_SIZE)))
{
LOG("DeSpawning weapon at %d", weapons[i]->position.x * SCREEN_SIZE);
weapons[i]->SetToDelete();
}
}
}
}
void ModuleWeapons::OnCollision(Collider* c1, Collider* c2)
{
for (uint i = 0; i < MAX_WEAPONS; ++i)
{
if (weapons[i] != nullptr && weapons[i]->GetCollider() == c1)
{
weapons[i]->OnCollision(c2); // Notify the weapon of a collision
if (c1->type == Collider::Type::WEAPON_SHELL && c2->type == Collider::Type::ENEMY) {}
else {
delete weapons[i];
weapons[i] = nullptr;
break;
}
}
}
}