-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameObject.h
219 lines (163 loc) · 5.14 KB
/
GameObject.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
207
208
209
210
211
212
213
214
215
216
217
218
#pragma once
#include <Windows.h>
#include <d3dx9.h>
#include <vector>
#include "SpriteManager.h"
#include "AnimationSet.h"
#include "PhysicsConsts.h"
class CCell;
typedef CCell* LPCell;
#define ID_TEX_BBOX "-100" // special texture to draw object bounding box
class CGameObject;
typedef CGameObject* LPGAMEOBJECT;
struct CCollisionEvent;
typedef CCollisionEvent* LPCOLLISIONEVENT;
struct CCollisionEvent
{
LPGAMEOBJECT obj;
float t, nx, ny;
float dx, dy; // *RELATIVE* movement distance between this object and obj
CCollisionEvent(float t, float nx, float ny, float dx = 0, float dy = 0, LPGAMEOBJECT obj = NULL)
{
this->t = t;
this->nx = nx;
this->ny = ny;
this->dx = dx;
this->dy = dy;
this->obj = obj;
}
static bool compare(const LPCOLLISIONEVENT& a, LPCOLLISIONEVENT& b)
{
return a->t < b->t;
}
};
struct SPrevBoundingBox {
float left, top, right, bottom = 0;
};
struct SRenderAnimation {
std::string AnimationID;
bool isFlipY = false;
};
enum class EExtraEffect {
NONE,
BEING_DAMAGED,
BONUS,
};
enum class EPriorityFlag {
MAP_OBJECT, // QUESTIONBOX, GOLDEN BRICK,
DYNAMIC_OBJECT, // Enemy
DYNAMIC_OBJECT_BEHIND_MAP, // Venus
MAIN_OBJECT, // Mario, Mario Bullets
HIGH_PRIORITY_OBJECT,
TEMP_OBJECT,
ENEMY_BULLET
};
struct SExtraEffect {
EExtraEffect type = EExtraEffect::NONE;
int timeBegin = 0;
int timeEffect = 0;
Vector2 initPosition = Vector2(0, 0);
};
class CGameObject
{
public:
SExtraEffect effect;
bool allowOthersGoThrough = false;
// Position
float x;
float y;
// Velocity
float vx;
float vy;
// Acceleration
float ax;
float ay;
// Distance
float dx; // dx = vx*dt
float dy; // dy = vy*dt
int powerX = 0;
// Direction of Object ( Left , Right )
int nx = 1;
int ny = 1;
LPCell cell;
DWORD dt;
std::vector<EPriorityFlag> priorityFlag;
Vector2 basePosition = Vector2(0, 0);
LPANIMATION_SET animation_set;
bool VerifyCollidedLeftRight(LPGAMEOBJECT);
bool isDisable = false;
bool isTemp = false;
SRenderAnimation renderAnimation;
SPrevBoundingBox prevBoundingBox;
public:
// Con/Destructor
CGameObject();
CGameObject(Vector2);
~CGameObject();
// Position
void GetPosition(float& x, float& y) { x = this->x; y = this->y; }
Vector2 GetPosition() { return Vector2(this->x, this->y); }
virtual Vector2 GetBasePosition() { return basePosition; }
virtual void ChangeBasePosition(Vector2);
void SetPosition(float x, float y) { this->x = x; this->y = y; }
void SetPosition(Vector2 pos) { this->x = pos.x; this->y = pos.y; }
// Speed
void GetSpeed(float& vx, float& vy) { vx = this->vx; vy = this->vy; }
void SetSpeed(float vx, float vy) { this->vx = vx, this->vy = vy; }
bool AddPriority(EPriorityFlag);
bool RemovePriority(EPriorityFlag);
int GetNX() { return nx; }
std::vector<EPriorityFlag> GetPriorityFlag() { return priorityFlag; }
virtual std::string GetRenderAnimationId(EExtraEffect);
virtual void SetEffect(EExtraEffect = EExtraEffect::NONE, DWORD = 0);
virtual void SwitchEffect(EExtraEffect = EExtraEffect::NONE);
virtual void RenderExtraEffect(Vector2);
void ResetRenderAnimation();
virtual void ChangeRenderAnimation(std::string newRenderAnimationID);
virtual void ChangeRenderAnimation(SRenderAnimation newRenderAnimation);
// BoundingBox
virtual void GetBoundingBox(float& left, float& top, float& right, float& bottom) = 0;
void RenderBoundingBox(Vector2 finalPos);
void RenderMotionableAnimation(int);
// AnimationSet
void SetAnimationSet(LPANIMATION_SET ani_set) { animation_set = ani_set; }
// Collision
bool HasOverLap(Vector2, Vector2, Vector2, Vector2);
LPCOLLISIONEVENT SweptAABBEx(LPGAMEOBJECT coO);
void CalcPotentialCollisions(vector<LPGAMEOBJECT>* coObjects, vector<LPCOLLISIONEVENT>& coEvents);
void FilterCollision(
vector<LPCOLLISIONEVENT>& coEvents,
vector<LPCOLLISIONEVENT>& coEventsResult,
float& min_tx,
float& min_ty,
float& nx,
float& ny,
float& rdx,
float& rdy);
// Physics
void ApplyGravity();
void ApplyNoGravity();
void ApplyFriction();
virtual void Update(DWORD dt, vector<LPGAMEOBJECT>* coObjects = nullptr);
virtual void Render(Vector2 finalPos) {};
virtual void OnHadCollided(LPGAMEOBJECT obj) { Collided(obj); };
virtual void CollidedLeftRight(LPGAMEOBJECT);
virtual void CollidedLeft(LPGAMEOBJECT);
virtual void CollidedTop(LPGAMEOBJECT);
virtual void CollidedRight(LPGAMEOBJECT);
virtual void CollidedBottom(LPGAMEOBJECT);
virtual void Collided(LPGAMEOBJECT) {};
virtual void NoCollided() {};
virtual void BeingCollided(LPGAMEOBJECT) {};
virtual void BeingCollidedLeftRight(LPGAMEOBJECT obj) { BeingCollided(obj); }
virtual void BeingCollidedLeft(LPGAMEOBJECT obj) { BeingCollidedLeftRight(obj); }
virtual void BeingCollidedTopBottom(LPGAMEOBJECT obj) { BeingCollided(obj); }
virtual void BeingCollidedTop(LPGAMEOBJECT obj) { BeingCollidedTopBottom(obj); }
virtual void BeingCollidedRight(LPGAMEOBJECT obj) { BeingCollidedLeftRight(obj); }
virtual void BeingCollidedBottom(LPGAMEOBJECT obj) { BeingCollidedTopBottom(obj); }
void UpdateWithCollision(vector<LPGAMEOBJECT>* coObjects);
void UpdateNoCollision();
// Cell
LPCell GetCell() { return this->cell; }
void SetCell(LPCell cell) { this->cell = cell; }
};