-
Notifications
You must be signed in to change notification settings - Fork 0
/
LargeBuilding.cpp
120 lines (102 loc) · 2.91 KB
/
LargeBuilding.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
/*
* LargeBuilding.cpp
*/
//Dragonfly Headers
#include "EventOut.h"
#include "EventStep.h"
#include "EventView.h"
#include "GraphicsManager.h"
#include "LogManager.h"
#include "ResourceManager.h"
#include "WorldManager.h"
//Game Headers
#include "LargeBuilding.h"
#include "Score.h"
#include "SmallExplosion.h"
LargeBuilding::LargeBuilding(df::Position p) {
df::ResourceManager &resource_manager = df::ResourceManager::getInstance();
df::LogManager &log_manager = df::LogManager::getInstance();
// Link to "player" sprite
df::Sprite *p_temp_sprite;
p_temp_sprite = resource_manager.getSprite("largebuilding");
if (!p_temp_sprite) {
log_manager.writeLog("LargeBuilding::LargeBuilding(): Warning! Sprite '%s' not found", "largebuilding");
}
else {
setSprite(p_temp_sprite);
setSpriteSlowdown(0);
}
setType("LargeBuilding");
setSolidness(df::Solidness::SOFT);
//Set starting position
setPosition(p);
health = 30;
hit_slowdown = 5;
hit_countdown = hit_slowdown;
was_hit = false;
has_flashed = false;
}
// Handle event.
// Return 0 if ignored, else 1.
int LargeBuilding::eventHandler(const df::Event *p_e) {
if (p_e->getType() == df::OUT_EVENT) {
df::WorldManager::getInstance().markForDelete(this);
return 1;
}
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;
}
return 0;
}
void LargeBuilding::step() {
if (was_hit) {
hit_countdown--;
if (hit_countdown < 0) {
hit_countdown = 0;
was_hit = false;
}
}
}
void LargeBuilding::hit(const df::EventCollision *p_collision_event) {
if ((p_collision_event->getObject1()->getType() == "PlayerGunShot") ||
(p_collision_event->getObject2()->getType() == "PlayerGunShot")) {
if (!was_hit) {
health--;
was_hit = true;
has_flashed = false;
hit_countdown = hit_slowdown;
}
}
// If PlayerCannonShot or Player, create explosion and delete building
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
//Send Points for deletion
df::EventView ev(SCORE_STRING, LARGEBUILDING_POINTS, true);
df::WorldManager::getInstance().onEvent(&ev);
//Delete this object
df::WorldManager::getInstance().markForDelete(this);
}
}
//draw frames to match where the soldier is
void LargeBuilding::draw(void) {
int dir_frame = 0;
if (was_hit && !has_flashed) {
dir_frame = 1;
has_flashed = true;
}
else
dir_frame = 0;
df::GraphicsManager::getInstance().drawFrame(getPosition(), getSprite()->getFrame(dir_frame), true);
}