BsSceneObject.cpp 19 KB

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