CmGameObject.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #include "CmGameObject.h"
  2. #include "CmComponent.h"
  3. #include "CmSceneManager.h"
  4. #include "CmException.h"
  5. #include "CmDebug.h"
  6. namespace CamelotEngine
  7. {
  8. GameObject::GameObject(const String& name)
  9. :mName(name), mPosition(Vector3::ZERO), mRotation(Quaternion::IDENTITY), mScale(Vector3::ZERO),
  10. mCachedLocalTfrm(Matrix4::IDENTITY), mIsCachedLocalTfrmUpToDate(false),
  11. mCachedWorldTfrm(Matrix4::IDENTITY), mIsCachedWorldTfrmUpToDate(false),
  12. mCustomWorldTfrm(Matrix4::IDENTITY), mIsCustomTfrmModeActive(false),
  13. mIsDestroyed(false)
  14. { }
  15. GameObject::~GameObject()
  16. {
  17. if(!mIsDestroyed)
  18. destroy();
  19. }
  20. GameObjectPtr GameObject::create(const String& name)
  21. {
  22. GameObjectPtr newObject = createInternal(name);
  23. gSceneManager().registerNewGO(newObject);
  24. return newObject;
  25. }
  26. GameObjectPtr GameObject::createInternal(const String& name)
  27. {
  28. GameObjectPtr newObject = GameObjectPtr(new GameObject(name));
  29. newObject->mThis = newObject;
  30. return newObject;
  31. }
  32. void GameObject::destroy()
  33. {
  34. mIsDestroyed = true;
  35. for(auto iter = mChildren.begin(); iter != mChildren.end(); ++iter)
  36. (*iter)->destroy();
  37. mChildren.clear();
  38. for(auto iter = mComponents.begin(); iter != mComponents.end(); ++iter)
  39. (*iter)->destroy();
  40. mComponents.clear();
  41. // Parent is our owner, so when his reference to us is removed, delete might be called.
  42. // So make sure this is the last thing we do.
  43. if(!mParent.expired())
  44. {
  45. GameObjectPtr parentPtr = mParent.lock();
  46. if(!parentPtr->isDestroyed())
  47. parentPtr->removeChild(mThis.lock());
  48. }
  49. }
  50. /************************************************************************/
  51. /* Transform */
  52. /************************************************************************/
  53. void GameObject::setPosition(const Vector3& position)
  54. {
  55. mPosition = position;
  56. markTfrmDirty();
  57. }
  58. void GameObject::setRotation(const Quaternion& rotation)
  59. {
  60. mRotation = rotation;
  61. markTfrmDirty();
  62. }
  63. void GameObject::setScale(const Vector3& scale)
  64. {
  65. mScale = scale;
  66. markTfrmDirty();
  67. }
  68. const Matrix4& GameObject::getWorldTfrm()
  69. {
  70. if(!mIsCachedWorldTfrmUpToDate)
  71. updateWorldTfrm();
  72. return mCachedWorldTfrm;
  73. }
  74. const Matrix4& GameObject::getLocalTfrm()
  75. {
  76. if(!mIsCachedLocalTfrmUpToDate)
  77. updateLocalTfrm();
  78. return mCachedLocalTfrm;
  79. }
  80. void GameObject::markTfrmDirty()
  81. {
  82. mIsCachedLocalTfrmUpToDate = false;
  83. if(mIsCachedWorldTfrmUpToDate) // If it's already marked as dirty, no point is recursing the hierarchy again
  84. {
  85. mIsCachedWorldTfrmUpToDate = false;
  86. for(auto iter = mChildren.begin(); iter != mChildren.end(); ++iter)
  87. {
  88. (*iter)->markTfrmDirty();
  89. }
  90. }
  91. }
  92. void GameObject::updateWorldTfrm()
  93. {
  94. if(!mParent.expired())
  95. mCachedWorldTfrm = getLocalTfrm() * mParent.lock()->getWorldTfrm();
  96. else
  97. mCachedWorldTfrm = getLocalTfrm();
  98. mIsCachedWorldTfrmUpToDate = true;
  99. }
  100. void GameObject::updateLocalTfrm()
  101. {
  102. mCachedLocalTfrm.makeTransform(mPosition, mScale, mRotation);
  103. mIsCachedLocalTfrmUpToDate = true;
  104. }
  105. /************************************************************************/
  106. /* Hierarchy */
  107. /************************************************************************/
  108. void GameObject::setParent(GameObjectPtr parent)
  109. {
  110. if(parent == nullptr || parent->isDestroyed())
  111. {
  112. CM_EXCEPT(InternalErrorException,
  113. "Parent is not allowed to be NULL or destroyed.");
  114. }
  115. if(mParent.expired() || mParent.lock() != parent)
  116. {
  117. if(!mParent.expired())
  118. mParent.lock()->removeChild(mThis.lock());
  119. if(parent != nullptr)
  120. parent->addChild(mThis.lock());
  121. mParent = parent;
  122. markTfrmDirty();
  123. }
  124. }
  125. GameObjectPtr GameObject::getChild(unsigned int idx) const
  126. {
  127. if(idx < 0 || idx >= mChildren.size())
  128. {
  129. CM_EXCEPT(InternalErrorException,
  130. "Child index out of range.");
  131. }
  132. return mChildren[idx];
  133. }
  134. int GameObject::indexOfChild(const GameObjectPtr child) const
  135. {
  136. for(int i = 0; i < (int)mChildren.size(); i++)
  137. {
  138. if(mChildren[i] == child)
  139. return i;
  140. }
  141. return -1;
  142. }
  143. void GameObject::addChild(GameObjectPtr object)
  144. {
  145. mChildren.push_back(object);
  146. }
  147. void GameObject::removeChild(GameObjectPtr object)
  148. {
  149. auto result = find(mChildren.begin(), mChildren.end(), object);
  150. if(result != mChildren.end())
  151. mChildren.erase(result);
  152. else
  153. {
  154. CM_EXCEPT(InternalErrorException,
  155. "Trying to remove a child but it's not a child of the transform.");
  156. }
  157. }
  158. void GameObject::destroyComponent(ComponentPtr component)
  159. {
  160. if(component == nullptr)
  161. {
  162. LOGDBG("Trying to remove a null component");
  163. return;
  164. }
  165. auto iter = std::find(mComponents.begin(), mComponents.end(), component);
  166. if(iter != mComponents.end())
  167. {
  168. (*iter)->destroy();
  169. mComponents.erase(iter);
  170. }
  171. else
  172. LOGDBG("Trying to remove a component that doesn't exist on this GameObject.");
  173. }
  174. }