-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTankTrap.cpp
101 lines (84 loc) · 2.34 KB
/
TankTrap.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
/*
* TankTrap.cpp
*/
//Dragonfly Headers
#include "LogManager.h"
#include "ResourceManager.h"
#include "WorldManager.h"
#include "EventOut.h"
#include "EventStep.h"
//Game Headers
#include "TankTrap.h"
#include "SmallExplosion.h"
TankTrap::TankTrap(df::Position p) {
df::ResourceManager &resource_manager = df::ResourceManager::getInstance();
df::LogManager &log_manager = df::LogManager::getInstance();
// Link to "tanktrap" sprite
df::Sprite *p_temp_sprite;
p_temp_sprite = resource_manager.getSprite("tanktrap");
if (!p_temp_sprite) {
log_manager.writeLog("TankTrap::TankTrap(): Warning! Sprite '%s' not found", "tanktrap");
}
else {
setSprite(p_temp_sprite);
setSpriteSlowdown(0);
}
setType("TankTrap");
setSolidness(df::Solidness::SOFT);
//Set starting position
setPosition(p);
health = 5;
hit_slowdown = 5;
hit_countdown = hit_slowdown;
was_hit = false;
has_flashed = false;
}
// Handle event.
// Return 0 if ignored, else 1.
int TankTrap::eventHandler(const df::Event *p_e) {
if (p_e->getType() == df::STEP_EVENT) {
step();
return 1;
}
if (p_e->getType() == df::COLLISION_EVENT) {
const df::EventCollision *p_collision_event = dynamic_cast <df::EventCollision const *> (p_e);
hit(p_collision_event);
return 1;
}
if (p_e->getType() == df::OUT_EVENT) {
df::WorldManager::getInstance().markForDelete(this);
return 1;
}
return 0;
}
void TankTrap::step() {
if (was_hit) {
hit_countdown--;
if (hit_countdown < 0) {
hit_countdown = 0;
was_hit = false;
}
}
}
void TankTrap::hit(const df::EventCollision *p_collision_event) {
if ((p_collision_event->getObject1()->getType() == "PlayerGunShot") ||
(p_collision_event->getObject2()->getType() == "PlayerGunShot")) {
if (!was_hit) {
was_hit = true;
has_flashed = false;
hit_countdown = hit_slowdown;
}
}
// If PlayerCannonShot or Player, create explosion and delete rock
if ((p_collision_event->getObject1()->getType() == "Tank") ||
(p_collision_event->getObject2()->getType() == "Tank") ||
(p_collision_event->getObject1()->getType() == "PlayerCannonShot") ||
(p_collision_event->getObject2()->getType() == "PlayerCannonShot") ||
health <= 0) {
// Create an explosion.
SmallExplosion *p_explosion = new SmallExplosion(getPosition());
// Play "explode" sound
//Delete this object
df::WorldManager::getInstance().markForDelete(this);
}
}