Repository navigation
Expand file tree
/
Copy pathGameObject.cpp
More file actions
444 lines (386 loc) · 12.1 KB
/
Copy pathGameObject.cpp
File metadata and controls
444 lines (386 loc) · 12.1 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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#include "Scene2D/GameObject.h"
#include "Animation/AnimationManager.h"
#include "Logger/Logger.h"
#include "Scene2D/GameObjectManager.h"
#include "Animation/LinearAnimation.h"
#include "Animation/BezierSplineAnimation.h"
#include "Collision/CollisionObject2.h"
#include "Animation/KeyframeAnimation.h"
#include "Scene2D/GameObjectAnimations.h"
#include "Render/2D/Systems/RenderSystem2D.h"
#include "Render/RenderHelper.h"
namespace DAVA
{
GameObject* GameObject::Create(const FilePath& _pathToSprite, int32 frame)
{
GameObject* object = new GameObject();
Sprite* sprite = Sprite::Create(_pathToSprite);
object->SetSprite(sprite);
object->SetFrame(frame);
SafeRelease(sprite);
return object;
}
GameObject* GameObject::Create(Sprite* sprite, int32 frame)
{
GameObject* object = new GameObject();
object->SetSprite(sprite);
object->SetFrame(frame);
return object;
}
GameObject::GameObject()
{
//Logger::FrameworkDebug("[GameObject] ctor");
//GetEngineContext()->animationManager->AddObject(this);
visible = true;
dead = false;
priorityChanged = false;
addedObject = false;
sprite = 0;
manager = 0;
color = Color(1.0f, 1.0f, 1.0f, 1.0f);
blending = BLENDING_ALPHABLEND;
collision = 0;
groupId = 0;
priority = 0;
parent = 0;
userData = 0;
isDebugDraw = false;
nextManager = 0;
}
GameObject::~GameObject()
{
SafeRelease(collision);
SafeRelease(sprite);
//Logger::FrameworkDebug("[GameObject] destructor");
//GetEngineContext()->animationManager->RemoveObject(this);
}
Animation* GameObject::WaitAnimation(float32 time, int32 track)
{
Animation* animation = new Animation(this, time, Interpolation::LINEAR);
animation->Start(track);
return animation;
}
Animation* GameObject::MoveAnimation(float32 x, float32 y, float32 time, Interpolation::FuncType interpolationFunc, int32 track)
{
LinearAnimation<Vector2>* animation = new LinearAnimation<Vector2>(this, &localDrawState.position, Vector2(x, y), time, interpolationFunc);
animation->Start(track);
return animation;
}
Animation* GameObject::MoveBySplineAnimation(BezierSpline2* spline, float32 time, Interpolation::FuncType interpolationFunc, int32 track)
{
BezierSplineAnimation2* animation = new BezierSplineAnimation2(this, &localDrawState.position, spline, time, interpolationFunc);
animation->Start(track);
return animation;
}
Animation* GameObject::ScaleAnimation(const Vector2& finalScale, float32 time, Interpolation::FuncType interpolationFunc, int32 track)
{
LinearAnimation<Vector2>* animation = new LinearAnimation<Vector2>(this, &localDrawState.scale, finalScale, time, interpolationFunc);
animation->Start(track);
return animation;
}
Animation* GameObject::ColorAnimation(const Color& finalColor, float32 time, Interpolation::FuncType interpolationFunc, int32 track)
{
LinearAnimation<Color>* animation = new LinearAnimation<Color>(this, &color, finalColor, time, interpolationFunc);
animation->Start(track);
return animation;
}
void GameObject::VisibleAnimationCallback(BaseObject* caller, void* param, void* callerData)
{
bool* params = static_cast<bool*>(param);
SetVisible(params[0]);
delete[] params;
}
Animation* GameObject::VisibleAnimation(bool visible, int32 track /* = 0*/)
{
//TODO: change to bool animation - Dizz
Animation* animation = new Animation(this, 0.01f, Interpolation::LINEAR);
bool* params = new bool[1];
params[0] = visible;
animation->AddEvent(Animation::EVENT_ANIMATION_START, Message(this, &GameObject::VisibleAnimationCallback, static_cast<void*>(params)));
animation->Start(track);
return animation;
}
Animation* GameObject::RotateAnimation(float32 newAngle, float32 time, Interpolation::FuncType interpolationFunc, int32 track)
{
LinearAnimation<float32>* animation = new LinearAnimation<float32>(this, &localDrawState.angle, newAngle, time, interpolationFunc);
animation->Start(track);
return animation;
}
Animation* GameObject::FrameInterpolateAnimation(int32 startFrame, int32 endFrame, float32 time, int32 track)
{
KeyframeData* data = new KeyframeData();
data->AddKeyframe(startFrame, 0.0f);
data->AddKeyframe(endFrame, time);
KeyframeAnimation* animation = new KeyframeAnimation(this, &localDrawState.frame, data, time);
SafeRelease(data);
animation->Start(track);
return animation;
}
void GameObject::RemoveFromManagerAnimation(BaseObject* animation, void* userData, void* callerData)
{
if (manager)
manager->RemoveObject(this);
}
Animation* GameObject::RemoveFromManagerAnimation(int32 track)
{
Animation* animation = new Animation(this, 0.001f, Interpolation::LINEAR);
animation->AddEvent(Animation::EVENT_ANIMATION_START, Message(this, &GameObject::RemoveFromManagerAnimation));
animation->Start(track);
return animation;
}
/*
void GameObject::ApplyAnimation(SceneObjectAnimation * _animation)
{
animations.push_back(_animation);
visible = true; // make visible all objects if we started animation
}
void GameObject::ApplyAnimationWithDelay(SceneObjectAnimation * _animation, float32 _delayTime)
{
// animation = _animation;
// if (animation)
// {
// // set public properties of object
// animation->position = position;
// animation->angle = angle;
//
// // start animation
// animation->StartAnimationWithDelay(_animationTime, _delayTime);
// }
}
void GameObject::Update(float32 timeElapsed)
{
if ((!currentAnimationState) && (animations.size() > 0))
{
AnimationState * state = SceneManager::Instance()->NewAnimationState();
state->startPosition = position;
state->startAngle = angle;
state->startFrame = frame;
state->position = &position;
state->angle = ∠
state->frame = &frame;
state->animation = animations.front();
state->animationTimeLength = state->animation->animationTimeLength;
state->interpolationFunc = state->animation->func;
animations.pop_front();
currentAnimationState = state;
// start animation
SceneManager::Instance()->StartAnimationImmediatelly(state);
}
if (currentAnimationState)
if (!currentAnimationState->isAnimating)
{
currentAnimationState->canRemove = true;
currentAnimationState = 0;
}
if ((currentAnimationState == 0) && (animations.size() == 0) && (makeInvisibleAfterAllAnimations))
visible = false;
} */
void GameObject::SetFrame(int32 _frame)
{
localDrawState.frame = _frame;
globalDrawState.frame = _frame;
}
void GameObject::SetPriority(int32 _priority)
{
priority = _priority;
if (manager)
{
manager->MarkToChangeObjectPriority(this);
}
}
void GameObject::SetManager(GameObjectManager* _manager)
{
manager = _manager;
}
void GameObject::FastForward(float32 skipTime, float32 updateTime)
{
DVASSERT(skipTime > 0.0f && updateTime > 0.0f, "FastForward skipTime and updateTime must be greater than 0");
float32 remainingSkipTime = skipTime;
while (remainingSkipTime > 0.0f)
{
Update(updateTime);
remainingSkipTime -= updateTime;
}
}
void GameObject::Update(float32 timeElapsed)
{
if (collision)
collision->Update(globalDrawState);
if (!children.empty())
{
List<GameObject*>::iterator it_end = children.end();
for (List<GameObject*>::iterator t = children.begin(); t != it_end; ++t)
{
GameObject* o = *t;
o->Update(timeElapsed);
}
}
}
void GameObject::RecalcHierarchy(const SpriteDrawState& parentDrawState)
{
globalDrawState.BuildStateFromParentAndLocal(parentDrawState, localDrawState);
if (!children.empty())
{
List<GameObject*>::iterator it_end = children.end();
for (List<GameObject*>::iterator t = children.begin(); t != it_end; ++t)
{
GameObject* o = *t;
o->RecalcHierarchy(globalDrawState);
}
}
}
void GameObject::CollisionPhase()
{
}
void GameObject::Draw()
{
if (!visible)
return;
if (sprite)
{
RenderSystem2D::Instance()->Draw(sprite, &globalDrawState, color);
// RenderSystem2D::Instance()->SetColor(1.0f, 0.0f, 0.0f, 1.0f);
// RenderManager::Instance()->FillRect(Rect(globalDrawState.position.x - 1, globalDrawState.position.y - 1, 3, 3));
// RenderSystem2D::Instance()->SetColor(1.0f, 1.0f, 1.0f, 1.0f);
}
else
{
// RenderSystem2D::Instance()->SetColor(1.0f, 0.0f, 0.0f, 1.0f);
// RenderManager::Instance()->FillRect(Rect(globalDrawState.position.x, globalDrawState.position.y, 10, 10));
// RenderSystem2D::Instance()->SetColor(1.0f, 1.0f, 1.0f, 1.0f);
}
if (isDebugDraw && collision)
{
collision->DebugDraw();
}
// if (align == ALIGN_LEFTTOP)
// {
// //[sprite drawFrameF:frame atX:position.x atY:position.y];
// }else if (align == ALIGN_CENTER)
// {
// //[sprite drawFrameF:frame atCenterX:position.x atCenterY:position.y];
// }
}
void GameObject::AttachObject(GameObject* gameObject)
{
children.push_back(gameObject);
gameObject->parent = this;
}
void GameObject::DetachObject(GameObject* gameObject)
{
for (List<GameObject*>::iterator t = children.begin(); t != children.end(); ++t)
{
if (*t == gameObject)
{
gameObject->localDrawState = gameObject->globalDrawState;
gameObject->parent = 0;
children.erase(t);
break;
}
}
}
void GameObject::AddObject(GameObject* gameObject)
{
children.push_back(gameObject);
gameObject->parent = this;
//gameObject->needToBeAddedToManager = true;
//manager->changesStack.push_back(gameObject);
if (manager)
gameObject->ChangeManager(manager);
}
void GameObject::RemoveObject(GameObject* gameObject)
{
//std::remove(animations.begin(), animations.end(), animation);
for (List<GameObject*>::iterator t = children.begin(); t != children.end(); ++t)
{
if (*t == gameObject)
{
gameObject->parent = 0;
gameObject->ChangeManager(0);
children.erase(t);
break;
}
}
}
GameObject* GameObject::GetParent() const
{
return parent;
}
void GameObject::ChangeManager(GameObjectManager* newManager)
{
Retain();
if (manager)
{
manager->RemoveObject(this);
}
if (newManager)
{
newManager->AddObject(this);
for (List<GameObject*>::iterator t = children.begin(); t != children.end(); ++t)
{
GameObject* child = *t;
child->ChangeManager(newManager);
}
}
Release();
}
void GameObject::SetPivotPoint(int32 alignFlags)
{ //WTF? Pivot point it's a pivot point. Align it's align. Why function that sets align calls SetPivotPoint!!!!????
if (!sprite)
return;
if (alignFlags & ALIGN_LEFT)
{
localDrawState.pivotPoint.x = 0.0f;
}
else if (alignFlags & ALIGN_HCENTER)
{
localDrawState.pivotPoint.x = sprite->GetWidth() / 2.0f;
}
else if (alignFlags & ALIGN_RIGHT)
{
localDrawState.pivotPoint.x = sprite->GetWidth();
}
if (alignFlags & ALIGN_TOP)
{
localDrawState.pivotPoint.y = 0.0f;
}
else if (alignFlags & ALIGN_VCENTER)
{
localDrawState.pivotPoint.y = sprite->GetHeight() / 2.0f;
}
else if (alignFlags & ALIGN_BOTTOM)
{
localDrawState.pivotPoint.y = sprite->GetHeight();
}
}
bool GameObject::IsCollideWith(GameObject* gameObject)
{
CollisionObject2* collision2 = gameObject->GetCollision();
if (collision && collision2)
{
collision->Update(globalDrawState);
collision2->Update(gameObject->globalDrawState);
return collision->IsCollideWith(collision2);
}
return false;
}
bool GameObject::IsCollideWith(CollisionObject2* collision2)
{
if (collision && collision2)
{
collision->Update(globalDrawState);
//collision2->Update(gameObject->globalDrawState);
return collision->IsCollideWith(collision2);
}
return false;
}
void GameObject::SetCollisionObject(CollisionObject2* obj)
{
SafeRelease(collision);
collision = SafeRetain(obj);
}
List<GameObject*>& GameObject::GetChildren()
{
return children;
}
};