-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObject.cpp
More file actions
133 lines (108 loc) · 2.95 KB
/
Object.cpp
File metadata and controls
133 lines (108 loc) · 2.95 KB
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
#include "Object.h"
#include "WorldManager.h"
#include "DisplayManager.h"
#include "LogManager.h"
namespace df {
// Static counter for unique IDs
static int object_count = 0;
Object::Object()
: m_id(++object_count)
, m_type("Object")
, m_position(0, 0)
, m_altitude(MAX_ALTITUDE / 2)
, m_speed(0.0f)
, m_direction(0, 0)
, m_solidness(HARD)
, m_shape("*")
{
WM.insertObject(this);
LM.writeLog("Object::Object() - created object id %d", m_id);
}
Object::~Object() {
LM.writeLog("Object::~Object() - destroying object id %d", m_id);
WM.removeObject(this);
}
void Object::setId(int new_id) {
m_id = new_id;
}
int Object::getId() const {
return m_id;
}
void Object::setType(std::string new_type) {
m_type = new_type;
}
std::string Object::getType() const {
return m_type;
}
void Object::setPosition(Vector new_pos) {
m_position = new_pos;
}
Vector Object::getPosition() const {
return m_position;
}
// BUG FIX: Original returned 1 on error instead of -1
int Object::setAltitude(int new_altitude) {
if (new_altitude >= 0 && new_altitude <= MAX_ALTITUDE) {
m_altitude = new_altitude;
return 0;
}
return -1;
}
int Object::getAltitude() const {
return m_altitude;
}
void Object::setSpeed(float speed) {
m_speed = speed;
}
float Object::getSpeed() const {
return m_speed;
}
void Object::setDirection(Vector new_direction) {
m_direction = new_direction;
}
Vector Object::getDirection() const {
return m_direction;
}
void Object::setVelocity(Vector new_velocity) {
m_speed = new_velocity.getMagnitude();
Vector direction = new_velocity;
direction.normalize();
m_direction = direction;
}
Vector Object::getVelocity() const {
Vector velocity = m_direction;
velocity.scale(m_speed);
return velocity;
}
// BUG FIX: Original predictPosition only added speed (scalar) to both x and y
// independently, ignoring direction entirely. Fixed to add velocity vector.
Vector Object::predictPosition() {
Vector velocity = getVelocity();
Vector new_pos(m_position.getX() + velocity.getX(),
m_position.getY() + velocity.getY());
return new_pos;
}
bool Object::isSolid() const {
return (m_solidness == HARD || m_solidness == SOFT);
}
// BUG FIX: Original always set m_solidness, then checked if it WASN'T equal
// to new_solid (which was always false since it just assigned it), so it
// always returned 0 even on invalid input. Now properly validates first.
int Object::setSolidness(Solidness new_solid) {
if (new_solid != HARD && new_solid != SOFT && new_solid != SPECTRAL) {
return -1;
}
m_solidness = new_solid;
return 0;
}
Solidness Object::getSolidness() const {
return m_solidness;
}
int Object::eventHandler(const Event */*p_e*/) {
return 0; // Base class does not handle events
}
int Object::draw() {
DM.drawString(m_position, m_shape, LEFT_JUSTIFIED, GREEN);
return 0;
}
} // end namespace df