-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameEntity.cpp
More file actions
160 lines (110 loc) · 4.56 KB
/
GameEntity.cpp
File metadata and controls
160 lines (110 loc) · 4.56 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
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
//-------------------------------------------------------------------------------------------------------------------//
// GameEntity.cpp //
// The base class for all game entities, providing position, rotation, and scaling functionality to all entities. //
// //
// Sets up a system to parent GameEntity objects to one another //
// making the child's position, rotation, and scale relative to the parent's instead of the world origin //
// //
// By: Ather Omar //
//-------------------------------------------------------------------------------------------------------------------//
#include "GameEntity.h"
//-------------------------------------------------------------
// QuickSDL
//-------------------------------------------------------------
namespace QuickSDL {
GameEntity::GameEntity(Vector2 pos) {
mPos = pos;
mRotation = 0.0f;
mActive = true;
mParent = NULL;
mScale = VEC2_ONE;
}
GameEntity::~GameEntity() {
mParent = NULL;
}
void GameEntity::Pos(Vector2 pos) {
mPos = pos;
}
Vector2 GameEntity::Pos(SPACE space) {
if(space == local || mParent == NULL)
return mPos;
Vector2 parentScale = mParent->Scale(world);
//The object's local position is rotated by the parent's rotation
Vector2 rotPos = RotateVector(Vector2(mPos.x * parentScale.x, mPos.y * parentScale.y), mParent->Rotation(local));
//The final position also depends on the parent's scale (if the parent is scaled up, the object should be further away from the parent)
return mParent->Pos(world) + rotPos;
}
void GameEntity::Rotation(float r) {
mRotation = r;
//Wraps the angle between 0 and 360 degrees, addition and subtraction is sed to avoid snapping
//Updated to deal with degrees higher than 360 and -360
if(mRotation > 360.0f) {
int mul = mRotation / 360;
mRotation -= 360.0f * mul;
} else if(mRotation < 0.0f) {
int mul = (mRotation / 360) - 1;
mRotation -= 360.0f * mul;
}
}
float GameEntity::Rotation(SPACE space) {
if(space == local || mParent == NULL)
return mRotation;
return mParent->Rotation(world) + mRotation;
}
void GameEntity::Scale(Vector2 scale) {
mScale = scale;
}
Vector2 GameEntity::Scale(SPACE space) {
if(space == local || mParent == NULL)
return mScale;
Vector2 scale = mParent->Scale(world);
scale.x *= mScale.x;
scale.y *= mScale.y;
return scale;
}
void GameEntity::Active(bool active) {
mActive = active;
}
bool GameEntity::Active() {
return mActive;
}
void GameEntity::Parent(GameEntity* parent) {
//If the parent is removed, The Position/Rotation/Scale are the GameEntity's world values, to keep the object looking the same after the removal of the parent;
if(parent == NULL) {
mPos = Pos(world);
mScale = Scale(world);
mRotation = Rotation(world);
} else {
//If the object already has a parent, remove the current parent to get it ready to be the child for the new parent
if(mParent != NULL)
Parent(NULL);
Vector2 parentScale = parent->Scale(world);
//Setting the local position to be relative to the new parent (while maintaining the same world position as before)
mPos = RotateVector(Pos(world) - parent->Pos(world), -parent->Rotation(world));
mPos.x /= parentScale.x;
mPos.y /= parentScale.y;
//Setting the local rotation to be relative to the new parent (while maintaining the same world rotation as before)
mRotation = mRotation - parent->Rotation(world);
//Setting the new scale to be relative to the new parent (while maintaining the same world scale as before)
mScale = Vector2(mScale.x / parentScale.x, mScale.y / parentScale.y);
}
mParent = parent;
}
GameEntity* GameEntity::Parent() {
return mParent;
}
void GameEntity::Translate(Vector2 vec, SPACE space) {
if (space == world) {
mPos += vec;
} else {
mPos += RotateVector(vec, Rotation());
}
}
void GameEntity::Rotate(float amount) {
mRotation += amount;
}
void GameEntity::Update() {
}
void GameEntity::Render() {
}
}