-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathButton.cpp
More file actions
79 lines (69 loc) · 1.78 KB
/
Button.cpp
File metadata and controls
79 lines (69 loc) · 1.78 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
#include "Button.h"
#include "ResourceManager.h"
#include "LogManager.h"
#include "WorldManager.h"
Button::Button(){
wall_count = 0;
df::ResourceManager &resource_manager = df::ResourceManager::getInstance();
df::LogManager &log_manager = df::LogManager::getInstance();
// Setup "button" sprite.
df::Sprite *p_temp_sprite = resource_manager.getSprite("button");
if (!p_temp_sprite) {
log_manager.writeLog("Button::Button(): Warning! Sprite '%s' not found",
"button");
}
else {
setSprite(p_temp_sprite);
setSpriteSlowdown(0);
}
setSolidness(df::SOFT);
}
// event handler
int Button::eventHandler(df::Event *p_e) {
if (p_e->getType() == DF_COLLISION_EVENT){
eventCollision(static_cast <df::EventCollision *> (p_e));
return 1;
}
return 0;
}
// handle collision event
int Button::eventCollision(const df::EventCollision *p_e){
if ((p_e->getObject1()->getType().compare("hero") == 0 || p_e->getObject2()->getType().compare("hero") == 0) && !activated) {
deactivateWalls();
setSpriteIndex(1);
df::ResourceManager::getInstance().getSound("buttonclick")->play();
activated = true;
}
return 0;
}
int Button::addWall(Wall *new_wall){
if (wall_count < MAX_WALL_COUNT) {
p_wall[wall_count] = new_wall;
wall_count++;
return 0;
}
return -1;
}
int Button::removeWall(Wall *rem_wall){
for (int i = 0; i < wall_count - 1; i++) {
if (p_wall[i] == rem_wall) {
df::WorldManager &wm = df::WorldManager::getInstance();
wm.markForDelete(p_wall[i]);
for (int j = i; j < MAX_WALL_COUNT - 2; j++) {
p_wall[j] = p_wall[j + 1];
return 0;
}
}
}
return -1;
}
void Button::activateWalls(){
for (int i = 0; i < wall_count; i++) {
p_wall[i]->setActive(true);
}
}
void Button::deactivateWalls(){
for (int i = 0; i < wall_count; i++) {
p_wall[i]->setActive(false);
}
}