-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hero.cpp
210 lines (191 loc) · 5.21 KB
/
Hero.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
// game engine includes
#include "GraphicsManager.h"
#include "WorldManager.h"
#include "ResourceManager.h"
#include "LogManager.h"
#include "GameManager.h"
#include "EventStep.h"
#include "EventView.h"
// IHOP includes
#include "Hero.h"
#include "EventHeroPosition.h"
#include "MapManager.h"
#include "Shield.h"
#include "EventPower.h"
#include "Weapon.h"
#include "StartScreen.h"
#include "Lives.h"
// default constructor
Hero::Hero(df::Position pos){
// Dragonfly managers needed for this method.
df::LogManager &log_manager = df::LogManager::getInstance();
df::ResourceManager &resource_manager = df::ResourceManager::getInstance();
df::WorldManager &world_manager = df::WorldManager::getInstance();
// Setup "hero" sprite.
df::Sprite *p_temp_sprite = resource_manager.getSprite("hero");
if (!p_temp_sprite) {
log_manager.writeLog("Hero::Hero(): Warning! Sprite '%s' not found",
"hero");
}
else {
setSprite(p_temp_sprite);
setSpriteSlowdown(4);
}
// set attributes
setType(HERO_TYPE);
setPosition(pos);
initialPosition = getPosition();
// set slowdown
move_slowdown = 2;
move_countdown = move_slowdown;
setAltitude(4);
setSolidness(df::HARD);
// set starting direction
wep_direction = EAST;
// send out initial hero position event
EventHeroPosition *e = new EventHeroPosition();
e->setHeroPosition(getPosition());
df::WorldManager::getInstance().onEvent(e);
}
// event handler
int Hero::eventHandler(df::Event *p_e){
// if its a keyboard event then process in keyboardInput() function
if (p_e->getType() == df::DF_KEYBOARD_EVENT){
keyboardInput(static_cast <const df::EventKeyboard *> (p_e));
}
if (p_e->getType() == DF_COLLISION_EVENT){
eventCollision(static_cast <const df::EventCollision *> (p_e));
}
if (p_e->getType() == DF_STEP_EVENT) {
if (power_countdown == 0) {
power = NONE;
}
else {
power_countdown--;
}
}
return 0;
}
// process keyboardInput to move hero or quit game
int Hero::keyboardInput(const df::EventKeyboard *p_e){
// if escape is pressed then quit game
if (p_e->getKeyboardAction() == df::KEY_PRESSED && p_e->getKey() == df::Input::ESCAPE){
}
// if wasd keys are pressed then move in the appropriate direction
if (p_e->getKeyboardAction() == df::KEY_DOWN){
switch (p_e->getKey()){
case df::Input::A:
move(-1, 0);
wep_direction = WEST;
return 1;
break;
case df::Input::D:
move(1, 0);
wep_direction = EAST;
return 1;
break;
case df::Input::W:
move(0, -1);
wep_direction = NORTH;
return 1;
break;
case df::Input::S:
move(0, 1);
wep_direction = SOUTH;
return 1;
break;
case df::Input::SPACE:
new Weapon(getPosition(), wep_direction);
return 1;
break;
}
}
return 0;
}
int Hero::eventCollision(const df::EventCollision *p_e) {
// if collider is hero then start falling down screen
if (p_e->getObject1()->getType().compare("Power") == 0){
Power *newPower = (Power*) p_e->getObject1();
power = newPower->getPowerUp();
power_countdown = DEFAULT_POWER_COUNT;
activatePower();
df::WorldManager &wm = df::WorldManager::getInstance();
wm.markForDelete(newPower);
return 1;
}
if (p_e->getObject2()->getType().compare("Power") == 0) {
Power *newPower = (Power*)p_e->getObject2();
power = newPower->getPowerUp();
power_countdown = DEFAULT_POWER_COUNT;
activatePower();
df::WorldManager &wm = df::WorldManager::getInstance();
wm.markForDelete(newPower);
return 1;
}
if (p_e->getObject1()->getType().compare("Enemy") == 0 || p_e->getObject2()->getType().compare("Enemy") == 0) {
if (power != SHIELD) {
df::ResourceManager::getInstance().getSound("screaming")->play();
if (lives > 1){
lives--;
resetPosition();
df::EventView *e = new df::EventView();
e->setTag(LIVES_STRING);
e->setDelta(true);
e->setValue(-1);
df::WorldManager::getInstance().onEvent(e);
}
else{
StartScreen::removeAllObjectsExceptThis();
}
}
else {
power_countdown = 10;
}
}
return 0;
}
void Hero::activatePower() {
switch (power) {
case SHIELD:
new Shield(getPosition());
break;
case SPEED:
break;
default:
EventPower *ep = new EventPower(power);
}
}
// move hero with the given deltas
void Hero::move(int dx, int dy) {
// See if time to move.
if (move_countdown > 0){
move_countdown--;
if (power == SPEED && !(move_countdown > 0)) {
move_countdown--;
}
if (power == SPEED && !(move_countdown > 0)) {
move_countdown--;
}
return;
}
// reset countdown
move_countdown = move_slowdown;
// If stays on window, allow move.
df::Position new_pos(getPosition().getX() + dx, getPosition().getY() + dy);
df::WorldManager &world_manager = df::WorldManager::getInstance();
MapManager &mm = MapManager::getInstance();
// If stays on window, allow move.
if ((new_pos.getY() > 0) &&
(new_pos.getY() < world_manager.getBoundary().getVertical()) &&
mm.onMap(df::Position(new_pos.getX(), new_pos.getY()+1))){
world_manager.moveObject(this, new_pos);
// send out new hero position event to enemies
EventHeroPosition *e = new EventHeroPosition();
e->setHeroPosition(df::Position(getPosition().getX(), getPosition().getY() + 1));
df::WorldManager::getInstance().onEvent(e);
}
}
// reset hero to initial position
void Hero::resetPosition(){
setPosition(initialPosition);
}