-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGameObject.cpp
More file actions
454 lines (383 loc) · 12.3 KB
/
Copy pathGameObject.cpp
File metadata and controls
454 lines (383 loc) · 12.3 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
445
446
447
448
449
450
451
452
453
454
#include "stdafx.h"
#include "GameObject.h"
#include "GameWorld.h"
#include "AnimatingMeshObject.h"
#include "MeshAssetManager.h"
#include "GameMain.h"
#include "Physics.h"
#include "GameObjectManager.h"
void GameObject::ConfigureInstance(EntityUid instanceUid, GameObjectController* objectController, GameObjectDefinition* objectDef)
{
mInstanceUid = instanceUid;
cxx_assert((mDefinition == nullptr) && objectDef);
mDefinition = objectDef;
cxx_assert(mController == nullptr);
mController = objectController;
mCurrentState = mDefinition->mStartState;
// controller is optional for simpler objects
if (mController)
{
mController->ConfigureInstance(this);
}
}
void GameObject::SpawnInstance()
{
cxx_assert(!mLifecycleFlags.mWasSpawned);
cxx_assert(!mLifecycleFlags.mWasDespawned);
cxx_assert(!mLifecycleFlags.mWasDeleted);
if (mLifecycleFlags.mWasSpawned) return;
mOwnHandle = GetObjectManager().FindObject(mInstanceUid);
// configure locomotion
{
mLocomotion.ResetToDefaults();
mLocomotion.ClearGoals();
mLocomotion.SetArriveSpeed(mDefinition->mSpeed);
}
EnableMeshObject(true);
if (mController)
{
mController->SpawnInstance();
}
mLifecycleFlags.mWasSpawned = true;
}
void GameObject::DespawnInstance()
{
cxx_assert(mLifecycleFlags.mWasSpawned);
if (mLifecycleFlags.mWasDespawned) return;
if (mController)
{
mController->DespawnInstance();
}
mLocomotion.ClearGoals();
mLocomotion.ResetToDefaults();
EnablePhysics(false);
EnableMeshObject(false);
mOwnHandle = {};
mLifecycleFlags.mWasDespawned = true;
}
void GameObject::SetPosition(const glm::vec3& position)
{
mTransform.mPosition = position;
if (mMeshObject)
{
mMeshObject->SetPosition(mTransform.mPosition);
}
// sync physics
if (mPhysicsObject)
{
mPhysicsObject->SetTransform(mTransform);
}
}
void GameObject::SetPosition(const glm::vec2& position)
{
const glm::vec3 position3d { position.x, mTransform.mPosition.y, position.y };
SetPosition(position3d);
}
void GameObject::SnapPositionToFloor(bool withRespectToMeshBounds)
{
float floorHeight = GetGameWorld().GetGameMap().GetFloorHeightAt(mTransform.mPosition);
if (withRespectToMeshBounds)
{
float extraOffsetFromFloor = 0.0f;
// get the offset between the y position and the mesh's bottom aabbox
const cxx::aabbox& meshLocalBounds = GetMeshLocalBounds();
if (meshLocalBounds.is_valid())
{
extraOffsetFromFloor = (0.0f - meshLocalBounds.mMin.y);
}
floorHeight += extraOffsetFromFloor;
}
if (cxx::eps_equals(floorHeight, mTransform.mPosition.y))
return;
const glm::vec3 newPosition { mTransform.mPosition.x, floorHeight, mTransform.mPosition.z };
SetPosition(newPosition);
}
void GameObject::SetOrientation(cxx::angle_t orientation)
{
mTransform.mOrientation = orientation;
if (mMeshObject)
{
mMeshObject->ResetOrientation();
mMeshObject->RotateAroundAxis(WorldAxes::Y, mTransform.mOrientation);
}
// sync physics
if (mPhysicsObject)
{
mPhysicsObject->SetTransform(mTransform);
}
}
void GameObject::SetTransform(const EntityTransform& transform)
{
SetPosition(transform.mPosition);
SetOrientation(transform.mOrientation);
}
const cxx::aabbox& GameObject::GetMeshLocalBounds() const
{
static const cxx::aabbox nullBounds;
if (mMeshObject)
return mMeshObject->GetLocalBounds();
return nullBounds;
}
const cxx::aabbox& GameObject::GetMeshWorldBounds() const
{
static const cxx::aabbox nullBounds;
if (mMeshObject)
{
mMeshObject->ComputeTransformation();
return mMeshObject->GetWorldBounds();
}
return nullBounds;
}
MapPoint2D GameObject::GetTilePosition() const
{
const glm::vec3 position = GetPosition();
return MapUtils::ComputeTileFromPosition(position);
}
void GameObject::OnRecycle()
{
Entity::OnRecycle();
mController = nullptr;
cxx_assert(mPhysicsObject == nullptr);
mPhysicsObject = nullptr;
mCurrentState = eGameObjectState_None;
mParentRoom = {};
mComponents = {};
mCapabilities = {};
mDefinition = nullptr;
mMeshResourceId = eGameObjectMeshId_Main;
}
void GameObject::EnableMeshObject(bool isEnabled)
{
bool wasEnabled = (mMeshObject != nullptr);
if (wasEnabled == isEnabled) return;
if (wasEnabled)
{
// cleanup
mMeshObject->SetObjectActive(false);
mMeshObject.reset();
return;
}
// try init mesh object
if (!mDefinition->mResourceMesh.IsDefined())
return;
mMeshObject = GetScene().CreateAnimatingMesh();
cxx_assert(mMeshObject);
if (mMeshObject)
{
// sync mesh transformation with object
mMeshObject->ResetTransformation();
mMeshObject->SetPosition(mTransform.mPosition);
mMeshObject->RotateAroundAxis(WorldAxes::Y, mTransform.mOrientation);
ConfigureMeshResource(mDefinition->mResourceMesh);
mMeshObject->SetObjectActive(true);
}
}
bool GameObject::HasMeshResource(eGameObjectMeshId meshId) const
{
switch (meshId)
{
case eGameObjectMeshId_Main: return mDefinition->mResourceMesh.IsDefined();
case eGameObjectMeshId_Additional1: return mDefinition->mResourceAdditional1.IsDefined();
case eGameObjectMeshId_Additional2: return mDefinition->mResourceAdditional2.IsDefined();
case eGameObjectMeshId_Additional3: return mDefinition->mResourceAdditional3.IsDefined();
case eGameObjectMeshId_Additional4: return mDefinition->mResourceAdditional4.IsDefined();
}
cxx_assert(false);
return false;
}
bool GameObject::SetMeshResource(eGameObjectMeshId meshId)
{
if (mMeshResourceId == meshId) return true;
std::reference_wrapper<ArtResourceDefinition> artResource = mDefinition->mResourceMesh;
switch (meshId)
{
case eGameObjectMeshId_Additional1:
artResource = mDefinition->mResourceAdditional1;
break;
case eGameObjectMeshId_Additional2:
artResource = mDefinition->mResourceAdditional2;
break;
case eGameObjectMeshId_Additional3:
artResource = mDefinition->mResourceAdditional3;
break;
case eGameObjectMeshId_Additional4:
artResource = mDefinition->mResourceAdditional4;
break;
}
if (artResource.get().IsDefined())
{
mMeshResourceId = meshId;
EnableMeshObject(true);
ConfigureMeshResource(artResource.get());
return true;
}
return false;
}
void GameObject::ConfigureMeshResource(const ArtResourceDefinition& artResource)
{
if (mMeshObject == nullptr) return;
bool isSucess = false;
if ((artResource.mResourceType == eArtResource_Mesh) ||
(artResource.mResourceType == eArtResource_TerrainMesh))
{
MeshAsset* meshAsset = gMeshAssetManager.GetMesh(artResource.mResourceName);
mMeshObject->Configure(meshAsset);
isSucess = true;
}
if (artResource.mResourceType == eArtResource_AnimatingMesh)
{
MeshAsset* meshAsset = gMeshAssetManager.GetMesh(artResource.mResourceName);
AnimationParams animParams;
animParams.mFramesPerSecond = artResource.mAnimationDesc.mFps * 1.0f;
animParams.mFirstFrame = 0;
animParams.mLastFrame = (artResource.mAnimationDesc.mFrames > 0) ? (artResource.mAnimationDesc.mFrames - 1) : 0;
if (!artResource.mDoesntLoop)
{
switch (meshAsset->GetAnimFrameFactorFunc())
{
case MeshAsset::eAnimFrameFactorFunc::Clamp:
animParams.mLoopMode = eAnimationLoopMode_PingPong;
break;
case MeshAsset::eAnimFrameFactorFunc::Wrap:
animParams.mLoopMode = eAnimationLoopMode_Repeat;
break;
default:
cxx_assert(false);
animParams.mLoopMode = eAnimationLoopMode_Repeat;
break;
}
}
mMeshObject->Configure(meshAsset, animParams);
mMeshObject->ResetAnimationSpeedFactor();
// set random progress
if (artResource.mRandomStartFrame)
{
mMeshObject->SetAnimationProgress(Random::GenerateFloat01());
}
isSucess = true;
}
cxx_assert(isSucess);
}
void GameObject::SetCurrentState(eGameObjectState stateId)
{
mCurrentState = stateId;
}
bool GameObject::RescaleAnimationDuration(float animDuration)
{
cxx_assert(animDuration > 0.0f);
if (mMeshObject && mMeshObject->HasAnimation())
{
float origDuration = mMeshObject->GetAnimationDuration();
if (animDuration > 0.0f)
{
float speedFactor = origDuration / animDuration;
cxx_assert(speedFactor > 0.0f);
mMeshObject->SetAnimationSpeedFactor(speedFactor);
}
return true;
}
return false;
}
void GameObject::ResetAnimationDuration()
{
if (mMeshObject && mMeshObject->HasAnimation())
{
mMeshObject->ResetAnimationSpeedFactor();
}
}
void GameObject::ParentRoomChanged(EntityHandle roomHandle)
{
if (mParentRoom == roomHandle) return;
if (roomHandle && !roomHandle.IsRoom())
{
cxx_assert(false);
return;
}
mParentRoom = roomHandle;
if (mController)
{
mController->ParentRoomChanged();
}
}
void GameObject::EnablePhysics(bool isEnabled)
{
bool wasEnabled = (mPhysicsObject != nullptr);
if (wasEnabled == isEnabled) return;
if (wasEnabled)
{
// cleanup
GetGameWorld().GetPhysics().DetachUser(this);
mPhysicsObject = nullptr;
return;
}
// init physics
GetGameWorld().GetPhysics().AttachUser(this);
mPhysicsObject = GetGameWorld().GetPhysics().GetPhysicsObject(this);
cxx_assert(mPhysicsObject);
mPhysicsObject->ClearAngularVelocity();
mPhysicsObject->ClearLinearVelocity();
}
void GameObject::MarkDeleted()
{
mLifecycleFlags.mWasDeleted = true;
}
void GameObject::UpdateLogic(float stepDeltaTime)
{
if (mController)
{
mController->UpdateLogic(stepDeltaTime);
}
}
void GameObject::UpdatePhysics(float stepDeltaTime)
{
// process locomotion
if (mLocomotion.HasGoals())
{
Locomotion::VelocitiesResult velocities = mLocomotion.ProcessGoals(stepDeltaTime, GetTransform());
// notify self
if (mLocomotion.HasGoals())
{
Notify(EntityNotification::ForLocoApplyVelocities(velocities.mLinearVelocity, velocities.mAngularVelocity));
}
else
{
Notify(EntityNotification::ForLocoClearVelocities());
}
}
}
void GameObject::Notify(const EntityNotification& notificationData)
{
if (WasDeleted()) return;
if (notificationData.mID == EntityNotification::eID_LocoApplyVelocities)
{
if (mPhysicsObject)
{
mPhysicsObject->SetLinearVelocity(notificationData.mLocoVelocities.mLinear);
mPhysicsObject->SetAngularVelocity(notificationData.mLocoVelocities.mAngular);
}
return;
}
if (notificationData.mID == EntityNotification::eID_LocoClearVelocities)
{
if (mPhysicsObject)
{
mPhysicsObject->ClearLinearVelocity();
mPhysicsObject->ClearAngularVelocity();
}
return;
}
if (notificationData.mID == EntityNotification::eID_SyncWithPhysicsTransform)
{
if (mPhysicsObject)
{
mTransform = mPhysicsObject->GetTransform();
if (mMeshObject)
{
mMeshObject->SetPosition(mTransform.mPosition);
mMeshObject->ResetOrientation();
mMeshObject->RotateAroundAxis(WorldAxes::Y, mTransform.mOrientation);
}
}
return;
}
}