-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathModulePowerUp.cpp
More file actions
134 lines (107 loc) · 3.14 KB
/
ModulePowerUp.cpp
File metadata and controls
134 lines (107 loc) · 3.14 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
//=================================
// included dependencies
#include "Application.h"
#include "ModulePowerUp.h"
#include "ModuleTextures.h"
#include "ModuleRender.h"
#include "ModuleAudio.h"
#include "ModuleParticles.h"
#include "ModulePlayer.h"
#include "ModuleSceneSpace.h"
#include "BasicPowerUp.h"
#include "RibbonPowerUp.h"
#include "MissilePowerUp.h"
//=================================
// the actual code
ModulePowerUp::ModulePowerUp(Application *app, bool start_enabled) : Module(app, start_enabled)
{ }
ModulePowerUp::~ModulePowerUp()
{ }
// Load assets
bool ModulePowerUp::start()
{
LOG("Loading PowerUp textures");
fx_power_up = app->audio->loadFx("Sounds/PowerUP.wav");
basic_graphics = app->textures->load("Sprites/BasicPowerUp.png");
ribbon_graphics = app->textures->load("Sprites/RibbonPowerUp.png");
missile_graphics = app->textures->load("Sprites/MissilePowerUp.png");
LOG("Loading PowerUps...");
// Adding powerups
addPowerUp(BASIC_POWERUP, 3000, 110, COLLIDER_POWER_UP);
addPowerUp(RIBBON_POWERUP, 650, 110, COLLIDER_POWER_UP);
addPowerUp(MISSILE_POWERUP, 1400, 110, COLLIDER_POWER_UP);
return true;
}
bool ModulePowerUp::cleanUp()
{
app->textures->unload(basic_graphics);
app->textures->unload(ribbon_graphics);
app->textures->unload(missile_graphics);
doubleNode<PowerUp*> *item = active.getLast();
while (item != NULL)
{
delete item->data;
item = item->previous;
}
active.clear();
return true;
}
// Update: draw PowerUps
update_status ModulePowerUp::update()
{
doubleNode<PowerUp*>* tmp = active.getFirst();
doubleNode<PowerUp*>* tmp_next = active.getFirst();
while (tmp != NULL)
{
PowerUp *p = tmp->data;
tmp_next = tmp->next;
if (p->update() == false)
{
delete p;
active.del(tmp);
}
else if (SDL_GetTicks() >= p->born)
{
app->renderer->blit(p->graphics, p->position.x, p->position.y, &(p->anim.getCurrentFrame()));
}
tmp = tmp_next;
}
return UPDATE_CONTINUE;
}
void ModulePowerUp::onCollision(Collider *col1, Collider *col2)
{
doubleNode<PowerUp*> *item = active.getFirst();
while (item != NULL && item->data->collider != col1)
item = item->next;
if (item != NULL)
{
app->audio->playFx(fx_power_up);
switch (item->data->type)
{
case(BASIC_POWERUP) : app->player->weapon_type = BASIC_PLAYER_SHOT; break;
case(RIBBON_POWERUP) : app->player->weapon_type = RIBBON_PLAYER_SHOT; break;
case(MISSILE_POWERUP) : app->player->weapon_type = MISSILE_PLAYER_SHOT; break;
}
delete item->data;
active.del(item);
}
}
void ModulePowerUp::addPowerUp(POWERUP_TYPES type, int x, int y, COLLIDER_TYPE collider_type, Uint32 delay)
{
PowerUp *p = NULL;
switch (type)
{
case(BASIC_POWERUP) : p = new BasicPowerUp(app, basic_graphics); break;
case(RIBBON_POWERUP) : p = new RibbonPowerUp(app, ribbon_graphics); break;
case(MISSILE_POWERUP) : p = new MissilePowerUp(app, missile_graphics); break;
}
p->born = SDL_GetTicks() + delay;
p->position.x = x * SCALE_FACTOR;
p->position.y = y * SCALE_FACTOR;
if (collider_type != COLLIDER_NONE)
{
p->collider = app->collision->addCollider({ p->position.x, p->position.y, 0, 0 }, collider_type, true, this);
}
if (p != NULL)
active.add(p);
}