-
Notifications
You must be signed in to change notification settings - Fork 0
/
GraphObject.h
206 lines (169 loc) · 3.81 KB
/
GraphObject.h
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
#ifndef GRAPHOBJ_H_
#define GRAPHOBJ_H_
#include "SpriteManager.h"
#include "GameConstants.h"
#include <set>
#include <cmath>
const int ANIMATION_POSITIONS_PER_TICK = 1;
class GraphObject
{
public:
static const int left = 180;
static const int right = 0;
static const int up = 90;
static const int down = 270;
GraphObject(int imageID, int startX, int startY, int dir = right, int depth = 0, double size = 1.0)
: m_imageID(imageID), m_visible(true), m_x(startX), m_y(startY),
m_destX(startX), m_destY(startY), m_brightness(1.0),
m_animationNumber(0), m_direction(dir), m_depth(depth), m_size(size)
{
if (m_size <= 0)
m_size = 1;
getGraphObjects(m_depth).insert(this);
setVisible(true);
}
virtual ~GraphObject()
{
getGraphObjects(m_depth).erase(this);
}
int getX() const
{
// If already moved but not yet animated, use new location anyway.
return m_destX;
}
int getY() const
{
// If already moved but not yet animated, use new location anyway.
return m_destY;
}
virtual void moveTo(int x, int y)
{
m_destX = x;
m_destY = y;
increaseAnimationNumber();
}
int getDirection() const
{
return m_direction;
}
void setDirection(int d)
{
if (d < 0)
d = 360 - (-d % 360);
m_direction = d % 360;
}
void getPositionInThisDirection(int angle, int distance, int& newX, int& newY) const
{
// computes new position only if angle is left, right, up, or down
newX = getX();
newY = getY();
switch (angle)
{
case left: newX -= distance; break;
case right: newX += distance; break;
case up: newY += distance; break;
case down: newY -= distance; break;
}
// General version for arbitrary angles:
// static const double PI = 4 * atan(1.0);
// newX = (getX() + distance * cos(angle*1.0 / 360 * 2 * PI));
// newY = (getY() + distance * sin(angle*1.0 / 360 * 2 * PI));
}
void moveAtAngle(int angle, int distance)
{
int newX;
int newY;
getPositionInThisDirection(angle, distance, newX, newY);
moveTo(newX, newY);
}
// The following should be used by only the framework, not the student
void moveForward(int distance)
{
moveAtAngle(getDirection(), distance);
}
bool isVisible() const
{
return m_visible;
}
void setVisible(bool shouldIDisplay)
{
m_visible = shouldIDisplay;
}
void setSize(double size)
{
m_size = size;
}
double getSize() const
{
return m_size;
}
double getBrightness() const
{
return m_brightness;
}
void setBrightness(double brightness)
{
m_brightness = brightness;
}
int getAnimationNumber() const
{
return m_animationNumber;
}
void getAnimationLocation(double& x, double& y) const
{ // double for SpriteManager
x = m_x;
y = m_y;
}
void animate()
{
m_x = m_destX;
m_y = m_destY;
// moveALittle(m_x, m_destX);
// moveALittle(m_y, m_destY);
}
static std::set<GraphObject*>& getGraphObjects(int layer)
{
static std::set<GraphObject*> graphObjects[NUM_DEPTHS];
if (layer < NUM_DEPTHS)
return graphObjects[layer];
else
return graphObjects[0]; // empty
}
void increaseAnimationNumber()
{
m_animationNumber++;
}
private:
friend class GameController;
int getID() const
{
return m_imageID;
}
private:
// Prevent copying or assigning GraphObjects
GraphObject(const GraphObject&);
GraphObject& operator=(const GraphObject&);
static const int NUM_DEPTHS = 4;
int m_imageID;
bool m_visible;
int m_x;
int m_y;
int m_destX;
int m_destY;
double m_brightness;
int m_animationNumber;
int m_direction;
int m_depth;
double m_size;
//void moveALittle(double& from, double& to)
//{
// static const double DISTANCE = 1.0/ANIMATION_POSITIONS_PER_TICK;
// if (to - from >= DISTANCE)
// from += DISTANCE;
// else if (from - to >= DISTANCE)
// from -= DISTANCE;
// else
// from = to;
//}
};
#endif // GRAPHOBJ_H_