-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObject.cpp
199 lines (174 loc) · 4.77 KB
/
Object.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
#include "Object.h"
#include "GameManager.h"
#include "WorldManager.h"
#include "LogManager.h"
#include "ResourceManager.h"
#include "EventOut.h"
#include "EventKeyboard.h"
namespace df {
//Object constructor
//add to WorldManager
Object::Object() {
static int max_id;
setId(max_id);
max_id++;
m_position = Vector(0, 0);
setType("Object");
//Altitude
setAltitude(MAX_ALTITUDE/2);
//Kinematics
m_direction = Vector(0, 0);
float m_speed=0.25;
//Box
m_box = Box(Vector(0,0), 1, 1);
LM.writeLog(0, "Object created! ID %d", getId());//Turned off for testing
//Add self to game world (WorldManager)
WM.insertObject(this);
}
//Object destructor
//Removes object from the game world (WorldManager)
Object::~Object() {
LM.writeLog(0, "Object deleted! ID %d", getId());//Turned off for testing
WM.removeObject(this);
}
//Sets the object's ID
void Object::setId(int new_id) {
m_id=new_id;
}
//Gets the object's ID
int Object::getId() const {
return m_id;
}
//Sets the object's type
void Object::setType(std::string new_type) {
m_type = new_type;
}
//Gets the object's type
std::string Object::getType() const {
return m_type;
}
//Sets the object's position
void Object::setPosition(Vector new_pos) {
m_position = new_pos;
}
//Gets the object's position
Vector Object::getPosition() const {
return m_position;
}
//Handles events
//Returns 0 when ignored
int Object::eventHandler(const Event* p_e) {
if (p_e->getType() == df::KEYBOARD_EVENT) {//testObject moves to the cursor when clicked
EventKeyboard* m = (EventKeyboard*)p_e;
LM.writeLog(3, "Key %d", m->getKey());
if (m->getKey() == Keyboard::Key::Q && m->getKeyboardAction() == EventKeyboardAction::KEY_DOWN) {
GM.setGameOver(true);
}
return 1;
}
if (p_e->getType() == df::OUT_EVENT) {
LM.writeLog(3, "EventOut");
return 1;
}
return 0;
}
//Set altitude of Object with checks for range [0, MAX_ALTITUDE]
//Return 0 if ok, else -1
int Object::setAltitude(int new_altitude) {
if (new_altitude <= MAX_ALTITUDE && new_altitude >= 0) {
m_altitude = new_altitude;
return 0;
}
LM.writeLog("Object's altitude is not possible");
return -1;
}
//Return altitude of Object
int Object::getAltitude() const {
return m_altitude;
}
//Set speed of Object
void Object::setSpeed(float speed) {
m_speed = speed;
}
//Get speed of Object
float Object::getSpeed() const {
return m_speed;
}
//Set the direction of Object
void Object::setDirection(Vector new_direction) {
m_direction = new_direction;
}
//Get the direction of Object
Vector Object::getDirection() const {
return m_direction;
}
//Set direction and speed of Object
void Object::setVelocity(Vector new_velocity) {
m_speed = new_velocity.getMagnitude();
new_velocity.normalize();
m_direction = new_velocity;
}
//Get the velocity of Object based on direction and speed
Vector Object::getVelocity() const {
Vector v = m_direction;
v.scale(m_speed);
return v;
}
//Predict Object position based on speed and direction
//Return predicted position
Vector Object::predictPosition() {
Vector new_pos = m_position + getVelocity();
return new_pos;
}
//True if Hard or Soft, else false
bool Object::isSolid() const {
return (getSolidness() == df::HARD)|| (getSolidness() == df::SOFT);
}
//Set object solidness, with checks for consistency
//Return 0 if ok, else -1
int Object::setSolidness(Solidness new_solid) {
if ((new_solid == df::HARD) || (new_solid == df::SOFT) || (new_solid == df::SPECTRAL)) {
m_solidness = new_solid;
return 0;
}
return -1;
}
//Return object solidness
Solidness Object::getSolidness() const {
return m_solidness;
}
//Animation
//Set sprite for object to animate
//Return 0 if ok, else -1
int Object::setSprite(std::string sprite_label) {
Sprite* p_sprite = RM.getSprite(sprite_label);
if (p_sprite == NULL) {
return -1;
}
m_animation.setSprite(p_sprite);
setBox(m_animation.getBox());
LM.writeLog(0, "Object::setSprite %f %f", m_animation.getBox().getCorner().getX(),m_animation.getBox().getCorner().getY());
return 0;
}
//Set Animation for this Object
//Set bounding box to size of associated Sprite
void Object::setAnimation(Animation new_animation) {
m_animation = new_animation;
}
//Get animation
Animation Object::getAnimation() const {
return m_animation;
}
int Object::draw() {
Vector pos = getPosition();
return m_animation.draw(pos);
}
// Set Object's bounding box.
void Object::setBox(Box new_box) {
m_box = new_box;
}
// Get Object's bounding box.
Box Object::getBox() const {
return m_box;
}
}