BsSceneObject.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsSceneObject.h"
  4. #include "BsComponent.h"
  5. #include "BsSceneManager.h"
  6. #include "BsException.h"
  7. #include "BsDebug.h"
  8. #include "BsSceneObjectRTTI.h"
  9. #include "BsMemorySerializer.h"
  10. #include "BsGameObjectManager.h"
  11. #include "BsPrefabUtility.h"
  12. #include "BsMatrix3.h"
  13. #include "BsCoreApplication.h"
  14. namespace bs
  15. {
  16. SceneObject::SceneObject(const String& name, UINT32 flags)
  17. : GameObject(), mPrefabHash(0), mFlags(flags), mPosition(Vector3::ZERO), mRotation(Quaternion::IDENTITY)
  18. , mScale(Vector3::ONE), mWorldPosition(Vector3::ZERO), mWorldRotation(Quaternion::IDENTITY)
  19. , mWorldScale(Vector3::ONE), mCachedLocalTfrm(Matrix4::IDENTITY), mCachedWorldTfrm(Matrix4::IDENTITY)
  20. , mDirtyFlags(0xFFFFFFFF), mDirtyHash(0), mActiveSelf(true), mActiveHierarchy(true)
  21. {
  22. setName(name);
  23. }
  24. SceneObject::~SceneObject()
  25. {
  26. if(!mThisHandle.isDestroyed())
  27. {
  28. LOGWRN("Object is being deleted without being destroyed first? " + mName);
  29. destroyInternal(mThisHandle, true);
  30. }
  31. }
  32. HSceneObject SceneObject::create(const String& name, UINT32 flags)
  33. {
  34. HSceneObject newObject = createInternal(name, flags);
  35. if (newObject->isInstantiated())
  36. gSceneManager().registerNewSO(newObject);
  37. return newObject;
  38. }
  39. HSceneObject SceneObject::createInternal(const String& name, UINT32 flags)
  40. {
  41. SPtr<SceneObject> sceneObjectPtr = SPtr<SceneObject>(new (bs_alloc<SceneObject>()) SceneObject(name, flags),
  42. &bs_delete<SceneObject>, StdAlloc<SceneObject>());
  43. HSceneObject sceneObject = GameObjectManager::instance().registerObject(sceneObjectPtr);
  44. sceneObject->mThisHandle = sceneObject;
  45. return sceneObject;
  46. }
  47. HSceneObject SceneObject::createInternal(const SPtr<SceneObject>& soPtr, UINT64 originalId)
  48. {
  49. HSceneObject sceneObject = GameObjectManager::instance().registerObject(soPtr, originalId);
  50. sceneObject->mThisHandle = sceneObject;
  51. return sceneObject;
  52. }
  53. void SceneObject::destroy(bool immediate)
  54. {
  55. // Parent is our owner, so when his reference to us is removed, delete might be called.
  56. // So make sure this is the last thing we do.
  57. if(mParent != nullptr)
  58. {
  59. if(!mParent.isDestroyed())
  60. mParent->removeChild(mThisHandle);
  61. mParent = nullptr;
  62. }
  63. destroyInternal(mThisHandle, immediate);
  64. }
  65. void SceneObject::destroyInternal(GameObjectHandleBase& handle, bool immediate)
  66. {
  67. if (immediate)
  68. {
  69. for (auto iter = mChildren.begin(); iter != mChildren.end(); ++iter)
  70. (*iter)->destroyInternal(*iter, true);
  71. mChildren.clear();
  72. // It's important to remove the elements from the array as soon as they're destroyed, as OnDestroy callbacks
  73. // for components might query the SO's components, and we want to only return live ones
  74. while (!mComponents.empty())
  75. {
  76. HComponent component = mComponents.back();
  77. component->_setIsDestroyed();
  78. if (isInstantiated())
  79. gSceneManager()._notifyComponentDestroyed(component);
  80. component->destroyInternal(component, true);
  81. mComponents.erase(mComponents.end() - 1);
  82. }
  83. GameObjectManager::instance().unregisterObject(handle);
  84. }
  85. else
  86. GameObjectManager::instance().queueForDestroy(handle);
  87. }
  88. void SceneObject::_setInstanceData(GameObjectInstanceDataPtr& other)
  89. {
  90. GameObject::_setInstanceData(other);
  91. // Instance data changed, so make sure to refresh the handles to reflect that
  92. SPtr<SceneObject> thisPtr = mThisHandle.getInternalPtr();
  93. mThisHandle._setHandleData(thisPtr);
  94. }
  95. String SceneObject::getPrefabLink(bool onlyDirect) const
  96. {
  97. const SceneObject* curObj = this;
  98. while (curObj != nullptr)
  99. {
  100. if (!curObj->mPrefabLinkUUID.empty())
  101. return curObj->mPrefabLinkUUID;
  102. if (curObj->mParent != nullptr && !onlyDirect)
  103. curObj = curObj->mParent.get();
  104. else
  105. curObj = nullptr;
  106. }
  107. return "";
  108. }
  109. HSceneObject SceneObject::getPrefabParent() const
  110. {
  111. HSceneObject curObj = mThisHandle;
  112. while (curObj != nullptr)
  113. {
  114. if (!curObj->mPrefabLinkUUID.empty())
  115. return curObj;
  116. if (curObj->mParent != nullptr)
  117. curObj = curObj->mParent;
  118. else
  119. curObj = nullptr;
  120. }
  121. return curObj;
  122. }
  123. void SceneObject::breakPrefabLink()
  124. {
  125. SceneObject* rootObj = this;
  126. while (rootObj != nullptr)
  127. {
  128. if (!rootObj->mPrefabLinkUUID.empty())
  129. break;
  130. if (rootObj->mParent != nullptr)
  131. rootObj = rootObj->mParent.get();
  132. else
  133. rootObj = nullptr;
  134. }
  135. if (rootObj != nullptr)
  136. {
  137. rootObj->mPrefabLinkUUID = "";
  138. rootObj->mPrefabDiff = nullptr;
  139. PrefabUtility::clearPrefabIds(rootObj->getHandle(), true, false);
  140. }
  141. }
  142. bool SceneObject::hasFlag(UINT32 flag) const
  143. {
  144. return (mFlags & flag) != 0;
  145. }
  146. void SceneObject::_setFlags(UINT32 flags)
  147. {
  148. mFlags |= flags;
  149. for (auto& child : mChildren)
  150. child->_setFlags(flags);
  151. }
  152. void SceneObject::_unsetFlags(UINT32 flags)
  153. {
  154. mFlags &= ~flags;
  155. for (auto& child : mChildren)
  156. child->_unsetFlags(flags);
  157. }
  158. void SceneObject::_instantiate(bool prefabOnly)
  159. {
  160. std::function<void(SceneObject*)> instantiateRecursive = [&](SceneObject* obj)
  161. {
  162. obj->mFlags &= ~SOF_DontInstantiate;
  163. if (obj->mParent == nullptr)
  164. gSceneManager().registerNewSO(obj->mThisHandle);
  165. for (auto& component : obj->mComponents)
  166. component->_instantiate();
  167. for (auto& child : obj->mChildren)
  168. {
  169. if(!prefabOnly || child->mPrefabLinkUUID.empty())
  170. instantiateRecursive(child.get());
  171. }
  172. };
  173. std::function<void(SceneObject*)> triggerEventsRecursive = [&](SceneObject* obj)
  174. {
  175. for (auto& component : obj->mComponents)
  176. gSceneManager()._notifyComponentCreated(component, obj->getActive());
  177. for (auto& child : obj->mChildren)
  178. {
  179. if (!prefabOnly || child->mPrefabLinkUUID.empty())
  180. triggerEventsRecursive(child.get());
  181. }
  182. };
  183. instantiateRecursive(this);
  184. triggerEventsRecursive(this);
  185. }
  186. /************************************************************************/
  187. /* Transform */
  188. /************************************************************************/
  189. void SceneObject::setPosition(const Vector3& position)
  190. {
  191. mPosition = position;
  192. notifyTransformChanged(TCF_Transform);
  193. }
  194. void SceneObject::setRotation(const Quaternion& rotation)
  195. {
  196. mRotation = rotation;
  197. notifyTransformChanged(TCF_Transform);
  198. }
  199. void SceneObject::setScale(const Vector3& scale)
  200. {
  201. mScale = scale;
  202. notifyTransformChanged(TCF_Transform);
  203. }
  204. void SceneObject::setWorldPosition(const Vector3& position)
  205. {
  206. if (mParent != nullptr)
  207. {
  208. Vector3 invScale = mParent->getWorldScale();
  209. if (invScale.x != 0) invScale.x = 1.0f / invScale.x;
  210. if (invScale.y != 0) invScale.y = 1.0f / invScale.y;
  211. if (invScale.z != 0) invScale.z = 1.0f / invScale.z;
  212. Quaternion invRotation = mParent->getWorldRotation().inverse();
  213. mPosition = invRotation.rotate(position - mParent->getWorldPosition()) * invScale;
  214. }
  215. else
  216. mPosition = position;
  217. notifyTransformChanged(TCF_Transform);
  218. }
  219. void SceneObject::setWorldRotation(const Quaternion& rotation)
  220. {
  221. if (mParent != nullptr)
  222. {
  223. Quaternion invRotation = mParent->getWorldRotation().inverse();
  224. mRotation = invRotation * rotation;
  225. }
  226. else
  227. mRotation = rotation;
  228. notifyTransformChanged(TCF_Transform);
  229. }
  230. void SceneObject::setWorldScale(const Vector3& scale)
  231. {
  232. if (mParent != nullptr)
  233. {
  234. Matrix3 rotScale;
  235. mParent->getWorldTfrm().extract3x3Matrix(rotScale);
  236. rotScale.inverse();
  237. Matrix3 scaleMat = Matrix3(Quaternion::IDENTITY, scale);
  238. scaleMat = rotScale * scaleMat;
  239. Quaternion rotation;
  240. Vector3 localScale;
  241. scaleMat.decomposition(rotation, localScale);
  242. mScale = localScale;
  243. }
  244. else
  245. mScale = scale;
  246. notifyTransformChanged(TCF_Transform);
  247. }
  248. const Vector3& SceneObject::getWorldPosition() const
  249. {
  250. if (!isCachedWorldTfrmUpToDate())
  251. updateWorldTfrm();
  252. return mWorldPosition;
  253. }
  254. const Quaternion& SceneObject::getWorldRotation() const
  255. {
  256. if (!isCachedWorldTfrmUpToDate())
  257. updateWorldTfrm();
  258. return mWorldRotation;
  259. }
  260. const Vector3& SceneObject::getWorldScale() const
  261. {
  262. if (!isCachedWorldTfrmUpToDate())
  263. updateWorldTfrm();
  264. return mWorldScale;
  265. }
  266. void SceneObject::lookAt(const Vector3& location, const Vector3& up)
  267. {
  268. Vector3 forward = location - getWorldPosition();
  269. Quaternion rotation = getWorldRotation();
  270. rotation.lookRotation(forward, up);
  271. setWorldRotation(rotation);
  272. }
  273. const Matrix4& SceneObject::getWorldTfrm() const
  274. {
  275. if (!isCachedWorldTfrmUpToDate())
  276. updateWorldTfrm();
  277. return mCachedWorldTfrm;
  278. }
  279. Matrix4 SceneObject::getInvWorldTfrm() const
  280. {
  281. if (!isCachedWorldTfrmUpToDate())
  282. updateWorldTfrm();
  283. Matrix4 worldToLocal = Matrix4::inverseTRS(mWorldPosition, mWorldRotation, mWorldScale);
  284. return worldToLocal;
  285. }
  286. const Matrix4& SceneObject::getLocalTfrm() const
  287. {
  288. if (!isCachedLocalTfrmUpToDate())
  289. updateLocalTfrm();
  290. return mCachedLocalTfrm;
  291. }
  292. void SceneObject::move(const Vector3& vec)
  293. {
  294. setPosition(mPosition + vec);
  295. }
  296. void SceneObject::moveRelative(const Vector3& vec)
  297. {
  298. // Transform the axes of the relative vector by camera's local axes
  299. Vector3 trans = mRotation.rotate(vec);
  300. setPosition(mPosition + trans);
  301. }
  302. void SceneObject::rotate(const Vector3& axis, const Radian& angle)
  303. {
  304. Quaternion q;
  305. q.fromAxisAngle(axis, angle);
  306. rotate(q);
  307. }
  308. void SceneObject::rotate(const Quaternion& q)
  309. {
  310. // Note the order of the mult, i.e. q comes after
  311. // Normalize the quat to avoid cumulative problems with precision
  312. Quaternion qnorm = q;
  313. qnorm.normalize();
  314. setRotation(qnorm * mRotation);
  315. }
  316. void SceneObject::roll(const Radian& angle)
  317. {
  318. // Rotate around local Z axis
  319. Vector3 zAxis = mRotation.rotate(Vector3::UNIT_Z);
  320. rotate(zAxis, angle);
  321. }
  322. void SceneObject::yaw(const Radian& angle)
  323. {
  324. Vector3 yAxis = mRotation.rotate(Vector3::UNIT_Y);
  325. rotate(yAxis, angle);
  326. }
  327. void SceneObject::pitch(const Radian& angle)
  328. {
  329. // Rotate around local X axis
  330. Vector3 xAxis = mRotation.rotate(Vector3::UNIT_X);
  331. rotate(xAxis, angle);
  332. }
  333. void SceneObject::setForward(const Vector3& forwardDir)
  334. {
  335. Quaternion currentRotation = getWorldRotation();
  336. currentRotation.lookRotation(forwardDir);
  337. setWorldRotation(currentRotation);
  338. }
  339. void SceneObject::updateTransformsIfDirty()
  340. {
  341. if (!isCachedLocalTfrmUpToDate())
  342. updateLocalTfrm();
  343. if (!isCachedWorldTfrmUpToDate())
  344. updateWorldTfrm();
  345. }
  346. void SceneObject::notifyTransformChanged(TransformChangedFlags flags) const
  347. {
  348. mDirtyFlags |= DirtyFlags::LocalTfrmDirty | DirtyFlags::WorldTfrmDirty;
  349. mDirtyHash++;
  350. for(auto& entry : mComponents)
  351. {
  352. if (entry->supportsNotify(flags))
  353. {
  354. bool alwaysRun = entry->hasFlag(ComponentFlag::AlwaysRun);
  355. if(alwaysRun || gSceneManager().isRunning())
  356. entry->onTransformChanged(flags);
  357. }
  358. }
  359. for (auto& entry : mChildren)
  360. entry->notifyTransformChanged(flags);
  361. }
  362. void SceneObject::updateWorldTfrm() const
  363. {
  364. if(mParent != nullptr)
  365. {
  366. // Update orientation
  367. const Quaternion& parentOrientation = mParent->getWorldRotation();
  368. mWorldRotation = parentOrientation * mRotation;
  369. // Update scale
  370. const Vector3& parentScale = mParent->getWorldScale();
  371. // Scale own position by parent scale, just combine
  372. // as equivalent axes, no shearing
  373. mWorldScale = parentScale * mScale;
  374. // Change position vector based on parent's orientation & scale
  375. mWorldPosition = parentOrientation.rotate(parentScale * mPosition);
  376. // Add altered position vector to parents
  377. mWorldPosition += mParent->getWorldPosition();
  378. mCachedWorldTfrm.setTRS(mWorldPosition, mWorldRotation, mWorldScale);
  379. }
  380. else
  381. {
  382. mWorldRotation = mRotation;
  383. mWorldPosition = mPosition;
  384. mWorldScale = mScale;
  385. mCachedWorldTfrm = getLocalTfrm();
  386. }
  387. mDirtyFlags &= ~DirtyFlags::WorldTfrmDirty;
  388. }
  389. void SceneObject::updateLocalTfrm() const
  390. {
  391. mCachedLocalTfrm.setTRS(mPosition, mRotation, mScale);
  392. mDirtyFlags &= ~DirtyFlags::LocalTfrmDirty;
  393. }
  394. /************************************************************************/
  395. /* Hierarchy */
  396. /************************************************************************/
  397. void SceneObject::setParent(const HSceneObject& parent, bool keepWorldTransform)
  398. {
  399. if (parent.isDestroyed())
  400. return;
  401. #if BS_EDITOR_BUILD
  402. String originalPrefab = getPrefabLink();
  403. #endif
  404. _setParent(parent, keepWorldTransform);
  405. #if BS_EDITOR_BUILD
  406. if (gCoreApplication().isEditor())
  407. {
  408. String newPrefab = getPrefabLink();
  409. if (originalPrefab != newPrefab)
  410. PrefabUtility::clearPrefabIds(mThisHandle);
  411. }
  412. #endif
  413. }
  414. void SceneObject::_setParent(const HSceneObject& parent, bool keepWorldTransform)
  415. {
  416. if (mThisHandle == parent)
  417. return;
  418. if (mParent == nullptr || mParent != parent)
  419. {
  420. Vector3 worldPos;
  421. Quaternion worldRot;
  422. Vector3 worldScale;
  423. if (keepWorldTransform)
  424. {
  425. // Make sure the object keeps its world coordinates
  426. worldPos = getWorldPosition();
  427. worldRot = getWorldRotation();
  428. worldScale = getWorldScale();
  429. }
  430. if (mParent != nullptr)
  431. mParent->removeChild(mThisHandle);
  432. if (parent != nullptr)
  433. parent->addChild(mThisHandle);
  434. mParent = parent;
  435. if (keepWorldTransform)
  436. {
  437. setWorldPosition(worldPos);
  438. setWorldRotation(worldRot);
  439. setWorldScale(worldScale);
  440. }
  441. notifyTransformChanged((TransformChangedFlags)(TCF_Parent | TCF_Transform));
  442. }
  443. }
  444. HSceneObject SceneObject::getChild(UINT32 idx) const
  445. {
  446. if(idx >= mChildren.size())
  447. {
  448. BS_EXCEPT(InternalErrorException, "Child index out of range.");
  449. }
  450. return mChildren[idx];
  451. }
  452. int SceneObject::indexOfChild(const HSceneObject& child) const
  453. {
  454. for(int i = 0; i < (int)mChildren.size(); i++)
  455. {
  456. if(mChildren[i] == child)
  457. return i;
  458. }
  459. return -1;
  460. }
  461. void SceneObject::addChild(const HSceneObject& object)
  462. {
  463. mChildren.push_back(object);
  464. object->_setFlags(mFlags);
  465. }
  466. void SceneObject::removeChild(const HSceneObject& object)
  467. {
  468. auto result = find(mChildren.begin(), mChildren.end(), object);
  469. if(result != mChildren.end())
  470. mChildren.erase(result);
  471. else
  472. {
  473. BS_EXCEPT(InternalErrorException,
  474. "Trying to remove a child but it's not a child of the transform.");
  475. }
  476. }
  477. HSceneObject SceneObject::findChild(const String& name, bool recursive)
  478. {
  479. for (auto& child : mChildren)
  480. {
  481. if (child->getName() == name)
  482. return child;
  483. }
  484. if (recursive)
  485. {
  486. for (auto& child : mChildren)
  487. {
  488. HSceneObject foundObject = child->findChild(name, true);
  489. if (foundObject != nullptr)
  490. return foundObject;
  491. }
  492. }
  493. return HSceneObject();
  494. }
  495. Vector<HSceneObject> SceneObject::findChildren(const String& name, bool recursive)
  496. {
  497. std::function<void(const HSceneObject&, Vector<HSceneObject>&)> findChildrenInternal =
  498. [&](const HSceneObject& so, Vector<HSceneObject>& output)
  499. {
  500. for (auto& child : so->mChildren)
  501. {
  502. if (child->getName() == name)
  503. output.push_back(child);
  504. }
  505. if (recursive)
  506. {
  507. for (auto& child : so->mChildren)
  508. findChildrenInternal(child, output);
  509. }
  510. };
  511. Vector<HSceneObject> output;
  512. findChildrenInternal(mThisHandle, output);
  513. return output;
  514. }
  515. void SceneObject::setActive(bool active)
  516. {
  517. mActiveSelf = active;
  518. setActiveHierarchy(active);
  519. }
  520. void SceneObject::setActiveHierarchy(bool active, bool triggerEvents)
  521. {
  522. bool activeHierarchy = active && mActiveSelf;
  523. if (mActiveHierarchy != activeHierarchy)
  524. {
  525. mActiveHierarchy = activeHierarchy;
  526. if (triggerEvents)
  527. {
  528. if (activeHierarchy)
  529. {
  530. for (auto& component : mComponents)
  531. gSceneManager()._notifyComponentActivated(component, triggerEvents);
  532. }
  533. else
  534. {
  535. for (auto& component : mComponents)
  536. gSceneManager()._notifyComponentDeactivated(component, triggerEvents);
  537. }
  538. }
  539. }
  540. for (auto child : mChildren)
  541. {
  542. child->setActiveHierarchy(mActiveHierarchy, triggerEvents);
  543. }
  544. }
  545. bool SceneObject::getActive(bool self)
  546. {
  547. if (self)
  548. return mActiveSelf;
  549. else
  550. return mActiveHierarchy;
  551. }
  552. HSceneObject SceneObject::clone(bool instantiate)
  553. {
  554. bool isInstantiated = !hasFlag(SOF_DontInstantiate);
  555. if (!instantiate)
  556. _setFlags(SOF_DontInstantiate);
  557. else
  558. _unsetFlags(SOF_DontInstantiate);
  559. UINT32 bufferSize = 0;
  560. MemorySerializer serializer;
  561. UINT8* buffer = serializer.encode(this, bufferSize, (void*(*)(UINT32))&bs_alloc);
  562. GameObjectManager::instance().setDeserializationMode(GODM_UseNewIds | GODM_RestoreExternal);
  563. SPtr<SceneObject> cloneObj = std::static_pointer_cast<SceneObject>(serializer.decode(buffer, bufferSize));
  564. bs_free(buffer);
  565. if(isInstantiated)
  566. _unsetFlags(SOF_DontInstantiate);
  567. else
  568. _setFlags(SOF_DontInstantiate);
  569. return cloneObj->mThisHandle;
  570. }
  571. HComponent SceneObject::getComponent(RTTITypeBase* type) const
  572. {
  573. for(auto& entry : mComponents)
  574. {
  575. if(entry->getRTTI()->isDerivedFrom(type))
  576. return entry;
  577. }
  578. return HComponent();
  579. }
  580. void SceneObject::destroyComponent(const HComponent component, bool immediate)
  581. {
  582. if(component == nullptr)
  583. {
  584. LOGDBG("Trying to remove a null component");
  585. return;
  586. }
  587. auto iter = std::find(mComponents.begin(), mComponents.end(), component);
  588. if(iter != mComponents.end())
  589. {
  590. (*iter)->_setIsDestroyed();
  591. if (isInstantiated())
  592. gSceneManager()._notifyComponentDestroyed(*iter);
  593. (*iter)->destroyInternal(*iter, immediate);
  594. mComponents.erase(iter);
  595. }
  596. else
  597. LOGDBG("Trying to remove a component that doesn't exist on this SceneObject.");
  598. }
  599. void SceneObject::destroyComponent(Component* component, bool immediate)
  600. {
  601. auto iterFind = std::find_if(mComponents.begin(), mComponents.end(),
  602. [component](const HComponent& x)
  603. {
  604. if(x.isDestroyed())
  605. return false;
  606. return x._getHandleData()->mPtr->object.get() == component; }
  607. );
  608. if(iterFind != mComponents.end())
  609. {
  610. destroyComponent(*iterFind, immediate);
  611. }
  612. }
  613. HComponent SceneObject::addComponent(UINT32 typeId)
  614. {
  615. SPtr<IReflectable> newObj = rtti_create(typeId);
  616. if(!rtti_is_subclass<Component>(newObj.get()))
  617. {
  618. LOGERR("Specified type is not a valid Component.");
  619. return HComponent();
  620. }
  621. SPtr<Component> componentPtr = std::static_pointer_cast<Component>(newObj);
  622. HComponent newComponent = GameObjectManager::instance().registerObject(componentPtr);
  623. newComponent->mParent = mThisHandle;
  624. addAndInitializeComponent(newComponent);
  625. return newComponent;
  626. }
  627. void SceneObject::addComponentInternal(const SPtr<Component> component)
  628. {
  629. GameObjectHandle<Component> newComponent = GameObjectManager::instance().getObject(component->getInstanceId());
  630. newComponent->mParent = mThisHandle;
  631. newComponent->mThisHandle = newComponent;
  632. mComponents.push_back(newComponent);
  633. }
  634. void SceneObject::addAndInitializeComponent(const HComponent& component)
  635. {
  636. component->mThisHandle = component;
  637. mComponents.push_back(component);
  638. if (isInstantiated())
  639. {
  640. component->_instantiate();
  641. gSceneManager()._notifyComponentCreated(component, getActive());
  642. }
  643. }
  644. void SceneObject::addAndInitializeComponent(const SPtr<Component> component)
  645. {
  646. GameObjectHandle<Component> newComponent = GameObjectManager::instance().getObject(component->getInstanceId());
  647. newComponent->mParent = mThisHandle;
  648. addAndInitializeComponent(newComponent);
  649. }
  650. RTTITypeBase* SceneObject::getRTTIStatic()
  651. {
  652. return SceneObjectRTTI::instance();
  653. }
  654. RTTITypeBase* SceneObject::getRTTI() const
  655. {
  656. return SceneObject::getRTTIStatic();
  657. }
  658. }