-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHero.cpp
173 lines (140 loc) · 3.93 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
//
// Hero.cpp
//
// Engine includes.
#include "DisplayManager.h"
#include "EventMouse.h"
#include "EventStep.h"
#include "LogManager.h"
#include "WorldManager.h"
// Game includes.
#include "Bullet.h"
#include "EventNuke.h"
#include "Explosion.h"
#include "GameOver.h"
#include "Hero.h"
Hero::Hero() {
#ifdef DF_REGISTER_INTEREST
// Player controls hero, so register with keyboard and mouse.
registerInterest(df::KEYBOARD_EVENT);
registerInterest(df::MSE_EVENT);
// Need to update fire rate control each step.
registerInterest(df::STEP_EVENT);
#endif
// Set object type.
setType("Hero");
// Set starting location.
df::Vector pos(7.0f, DM.getVertical()/2.0f);
setPosition(pos);
// Create reticle for firing bullets.
p_reticle = new Reticle();
p_reticle->draw();
// Set firing variables.
fire_slowdown = 15;
fire_countdown = fire_slowdown;
nuke_count = 1;
}
Hero::~Hero() {
// Create GameOver object.
GameOver *p_go = new GameOver;
// Make big explosion.
for (int i=-8; i<=8; i+=5) {
for (int j=-5; j<=5; j+=3) {
df::Vector temp_pos = this->getPosition();
temp_pos.setX(this->getPosition().getX() + i);
temp_pos.setY(this->getPosition().getY() + j);
Explosion *p_explosion = new Explosion;
p_explosion -> setPosition(temp_pos);
}
}
}
// Handle event.
// Return 0 if ignored, else 1.
int Hero::eventHandler(const df::Event *p_e) {
if (p_e->getType() == df::KEYBOARD_EVENT) {
const df::EventKeyboard *p_keyboard_event = dynamic_cast <const df::EventKeyboard *> (p_e);
kbd(p_keyboard_event);
return 1;
}
if (p_e->getType() == df::MSE_EVENT) {
const df::EventMouse *p_mouse_event = dynamic_cast <const df::EventMouse *> (p_e);
mouse(p_mouse_event);
return 1;
}
if (p_e->getType() == df::STEP_EVENT) {
step();
return 1;
}
// If get here, have ignored this event.
return 0;
}
// Take appropriate action according to mouse action.
void Hero::mouse(const df::EventMouse *p_mouse_event) {
// Pressed button?
if ((p_mouse_event->getMouseAction() == df::CLICKED) &&
(p_mouse_event->getMouseButton() == df::Mouse::LEFT))
fire(p_mouse_event->getMousePosition());
}
// Take appropriate action according to key pressed.
void Hero::kbd(const df::EventKeyboard *p_keyboard_event) {
switch(p_keyboard_event->getKey()) {
case df::Keyboard::W: // up
if (p_keyboard_event->getKeyboardAction() == df::KEY_PRESSED)
move(-1);
break;
case df::Keyboard::S: // down
if (p_keyboard_event->getKeyboardAction() == df::KEY_PRESSED)
move(+1);
break;
case df::Keyboard::SPACE: // nuke!
if (p_keyboard_event->getKeyboardAction() == df::KEY_PRESSED)
nuke();
break;
case df::Keyboard::Q: // quit
if (p_keyboard_event->getKeyboardAction() == df::KEY_PRESSED)
WM.markForDelete(this);
break;
};
return;
}
// Move up or down.
void Hero::move(int dy) {
df::Vector new_pos(getPosition().getX(), getPosition().getY() + dy);
// If stays on screen, allow move.
if ((new_pos.getY() >= 0) &&
(new_pos.getY() < DM.getVertical()))
WM.moveObject(this, new_pos);
}
// Fire bullet towards target.
void Hero::fire(df::Vector target) {
// See if time to fire.
if (fire_countdown > 0)
return;
fire_countdown = fire_slowdown;
// Fire Bullet towards target.
Bullet *p = new Bullet(getPosition());
p->setVelocity(df::Vector(p->getVelocity().getX(),
(target.getY() - getPosition().getY()) /
(target.getX() - getPosition().getX())));
}
// Decrease fire restriction.
void Hero::step() {
fire_countdown--;
if (fire_countdown < 0)
fire_countdown = 0;
}
// Send nuke event to all objects.
void Hero::nuke() {
// Check if nukes left.
if (!nuke_count)
return;
nuke_count--;
// Create "nuke" event and send to interested.
EventNuke nuke;
WM.onEvent(&nuke);
}
// Custom draw.
int Hero::draw() {
DM.drawCh(getPosition(), HERO_CHAR, df::BLUE);
return 0;
}