-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphObject.h
146 lines (119 loc) · 3.2 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
#ifndef GRAPHOBJ_H_
#define GRAPHOBJ_H_
#include "SpriteManager.h"
#include "GameConstants.h"
#include <set>
#include <cmath>
const int ANIMATION_POSITIONS_PER_TICK = 1;
using Direction = int;
class GraphObject
{
public:
static const int right = 0;
static const int left = 180;
static const int up = 90;
static const int down = 270;
GraphObject(int imageID, double startX, double startY, Direction dir = 0, int depth = 0, double size = 1.0)
: m_imageID(imageID), m_x(startX), m_y(startY), m_destX(startX), m_destY(startY),
m_animationNumber(0), m_direction(dir), m_depth(depth), m_size(size)
{
if (m_size <= 0)
m_size = 1;
getGraphObjects(m_depth).insert(this);
}
virtual ~GraphObject()
{
getGraphObjects(m_depth).erase(this);
}
double getX() const
{
// If already moved but not yet animated, use new location anyway.
return m_destX;
}
double getY() const
{
// If already moved but not yet animated, use new location anyway.
return m_destY;
}
virtual void moveTo(double x, double y)
{
m_destX = x;
m_destY = y;
increaseAnimationNumber();
}
Direction getDirection() const
{
return m_direction;
}
void setDirection(Direction d)
{
while (d < 0)
d += 360;
m_direction = d % 360;
}
void setSize(double size)
{
m_size = size;
}
double getSize() const
{
return m_size;
}
// The following should be used by only the framework, not the student
void increaseAnimationNumber()
{
m_animationNumber++;
}
template<typename Func>
static void drawAllObjects(Func plotFunc)
{
for (int depth = NUM_DEPTHS - 1; depth >= 0; depth--)
{
for (GraphObject* go : getGraphObjects(depth))
{
go->animate();
plotFunc(go->m_imageID, go->m_animationNumber, go->m_x, go->m_y, go->m_direction, go->m_size);
}
}
}
// Prevent copying or assigning GraphObjects
GraphObject(const GraphObject&) = delete;
GraphObject& operator=(const GraphObject&) = delete;
private:
static const int NUM_DEPTHS = 4;
int m_imageID;
double m_x;
double m_y;
double m_destX;
double m_destY;
int m_animationNumber;
Direction m_direction;
int m_depth;
double m_size;
void animate()
{
m_x = m_destX;
m_y = m_destY;
//moveALittle(m_x, m_destX);
//moveALittle(m_y, m_destY);
}
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;
}
static std::set<GraphObject*>& getGraphObjects(int depth)
{
static std::set<GraphObject*> graphObjects[NUM_DEPTHS];
if (depth < NUM_DEPTHS)
return graphObjects[depth];
else
return graphObjects[0]; // empty;
}
};
#endif // GRAPHOBJ_H_