CmSceneObject.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. #pragma once
  2. #include "CmPrerequisites.h"
  3. #include "CmMatrix4.h"
  4. #include "CmVector3.h"
  5. #include "CmQuaternion.h"
  6. #include "CmRTTIType.h"
  7. #include "CmSceneManager.h"
  8. #include "CmGameObject.h"
  9. #include "boost/static_assert.hpp"
  10. namespace CamelotFramework
  11. {
  12. class CM_EXPORT SceneObject : public GameObject
  13. {
  14. friend class SceneManager;
  15. public:
  16. static HSceneObject create(const String& name);
  17. void destroy();
  18. private:
  19. HSceneObject mThisHandle;
  20. SceneObject(const String& name);
  21. ~SceneObject();
  22. static HSceneObject createInternal(const String& name);
  23. void destroyInternal();
  24. /************************************************************************/
  25. /* Transform */
  26. /************************************************************************/
  27. public:
  28. void setPosition(const Vector3& position);
  29. const Vector3& getPosition() const { return mPosition; }
  30. const Vector3& getWorldPosition() const;
  31. void setRotation(const Quaternion& rotation);
  32. const Quaternion& getRotation() const { return mRotation; }
  33. const Quaternion& getWorldRotation() const;
  34. void setScale(const Vector3& scale);
  35. const Vector3& getScale() const { return mScale; }
  36. const Vector3& getWorldScale() const;
  37. void lookAt(const Vector3& location, const Vector3& up = Vector3::UNIT_Y);
  38. const Matrix4& getWorldTfrm() const;
  39. const Matrix4& getLocalTfrm() const;
  40. /** Moves the object's position by the vector offset provided along world axes.
  41. */
  42. void move(const Vector3& vec);
  43. /** Moves the object's position by the vector offset provided along it's own axes (relative to orientation).
  44. */
  45. void moveRelative(const Vector3& vec);
  46. /**
  47. * @brief Gets the Z (forward) axis of the object, in world space.
  48. *
  49. * @return Forward axis of the object.
  50. */
  51. Vector3 getForward() const { return getWorldRotation() * Vector3::NEGATIVE_UNIT_Z; }
  52. /**
  53. * @brief Gets the Y (up) axis of the object, in world space.
  54. *
  55. * @return Up axis of the object.
  56. */
  57. Vector3 getUp() const { return getWorldRotation() * Vector3::UNIT_Y; }
  58. /**
  59. * @brief Gets the X (right) axis of the object, in world space.
  60. *
  61. * @return Right axis of the object.
  62. */
  63. Vector3 getRight() const { return getWorldRotation() * Vector3::UNIT_X; }
  64. /**
  65. * @brief Rotates the game object so it's forward axis faces the provided
  66. * direction.
  67. *
  68. * @note Local forward axis is considered to be negative Z.
  69. *
  70. * @param forwardDir The forward direction to face, in world space.
  71. */
  72. void setForward(const Vector3& forwardDir);
  73. /** Rotate the object around an arbitrary axis.
  74. */
  75. void rotate(const Vector3& axis, const Radian& angle);
  76. /** Rotate the object around an arbitrary axis using a Quaternion.
  77. */
  78. void rotate(const Quaternion& q);
  79. /**
  80. * @brief Rotates around local Z axis.
  81. *
  82. * @param angle Angle to rotate by.
  83. */
  84. void roll(const Radian& angle);
  85. /**
  86. * @brief Rotates around Y axis.
  87. *
  88. * @param angle Angle to rotate by.
  89. */
  90. void yaw(const Radian& angle);
  91. /**
  92. * @brief Rotates around X axis
  93. *
  94. * @param angle Angle to rotate by.
  95. */
  96. void pitch(const Radian& angle);
  97. private:
  98. String mName;
  99. Vector3 mPosition;
  100. Quaternion mRotation;
  101. Vector3 mScale;
  102. mutable Vector3 mWorldPosition;
  103. mutable Quaternion mWorldRotation;
  104. mutable Vector3 mWorldScale;
  105. mutable Matrix4 mCachedLocalTfrm;
  106. mutable bool mIsCachedLocalTfrmUpToDate;
  107. mutable Matrix4 mCachedWorldTfrm;
  108. mutable bool mIsCachedWorldTfrmUpToDate;
  109. Matrix4 mCustomWorldTfrm; // TODO
  110. bool mIsCustomTfrmModeActive; // TODO
  111. void markTfrmDirty() const;
  112. void updateLocalTfrm() const;
  113. void updateWorldTfrm() const;
  114. /************************************************************************/
  115. /* Hierarchy */
  116. /************************************************************************/
  117. public:
  118. /**
  119. * @brief Changes the parent of this object. Also removes the object from the current parent,
  120. * and assigns it to the new parent.
  121. *
  122. * @param [in] parent New parent.
  123. */
  124. void setParent(const HSceneObject& parent);
  125. /**
  126. * @brief Gets the parent of this object.
  127. *
  128. * @return Parent object, or nullptr if this SceneObject is at root level.
  129. */
  130. HSceneObject getParent() const { return mParent; }
  131. /**
  132. * @brief Gets a child of this item.
  133. *
  134. * @param idx The zero based index of the child.
  135. *
  136. * @return SceneObject of the child.
  137. *
  138. * @throws ERR_INVALIDPARAMS If the index is out of range.
  139. */
  140. HSceneObject getChild(unsigned int idx) const;
  141. /**
  142. * @brief Find the index of the specified child. Don't persist this value as
  143. * it may change whenever you add/remove children.
  144. *
  145. * @param child The child to look for.
  146. *
  147. * @return The zero-based index of the found child, or -1 if no match was found.
  148. */
  149. int indexOfChild(const HSceneObject& child) const;
  150. /**
  151. * @brief Gets the number of all child GameObjects.
  152. */
  153. UINT32 getNumChildren() const { return (UINT32)mChildren.size(); }
  154. private:
  155. HSceneObject mParent;
  156. Vector<HSceneObject>::type mChildren;
  157. /**
  158. * @brief Adds a child to the child array. This method doesn't check for null or duplicate values.
  159. *
  160. * @param [in] object New child.
  161. */
  162. void addChild(const HSceneObject& object);
  163. /**
  164. * @brief Removes the child from the object.
  165. *
  166. * @param [in] object Child to remove.
  167. *
  168. * @throws INTERNAL_ERROR If the provided child isn't a child of the current object.
  169. */
  170. void removeChild(const HSceneObject& object);
  171. /************************************************************************/
  172. /* Component */
  173. /************************************************************************/
  174. public:
  175. template <typename T>
  176. GameObjectHandle<T> addComponent()
  177. {
  178. BOOST_STATIC_ASSERT_MSG((boost::is_base_of<CamelotFramework::Component, T>::value),
  179. "Specified type is not a valid Component.");
  180. GameObjectHandle<T> newComponent = GameObjectHandle<T>(
  181. new (cm_alloc<T, PoolAlloc>()) T(mThisHandle), &cm_delete<PoolAlloc, GameObject>);
  182. mComponents.push_back(newComponent);
  183. gSceneManager().notifyComponentAdded(newComponent);
  184. return newComponent;
  185. }
  186. /**
  187. * @brief Searches for a component with the specific type and returns the first one
  188. * it finds.
  189. *
  190. * @note Don't call this too often as it is relatively slow. It is more efficient
  191. * to call it once and store the result for further use.
  192. *
  193. * @tparam typename T Type of the component.
  194. *
  195. * @return Component if found, nullptr otherwise.
  196. */
  197. template <typename T>
  198. GameObjectHandle<T> getComponent()
  199. {
  200. BOOST_STATIC_ASSERT_MSG((boost::is_base_of<CamelotFramework::Component, T>::value),
  201. "Specified type is not a valid Component.");
  202. return static_object_cast<T>(getComponent(T::getRTTIStatic()->getRTTIId()));
  203. }
  204. /**
  205. * @brief Checks if the current object contains the specified component
  206. *
  207. * @note Don't call this too often as it is relatively slow.
  208. *
  209. * @tparam typename T Type of the component.
  210. *
  211. * @return True if component exists on the object.
  212. */
  213. template <typename T>
  214. bool hasComponent()
  215. {
  216. BOOST_STATIC_ASSERT_MSG((boost::is_base_of<CamelotFramework::Component, T>::value),
  217. "Specified type is not a valid Component.");
  218. return hasComponent(T::getRTTIStatic()->getRTTIId());
  219. }
  220. /**
  221. * @brief Searches for a component with the specified type id and returns the first one it
  222. * finds.
  223. *
  224. * @note Don't call this too often as it is relatively slow. It is more efficient to
  225. * call it once and store the result for further use.
  226. *
  227. * @param typeId Identifier for the type.
  228. *
  229. * @return Component if found, nullptr otherwise.
  230. */
  231. HComponent getComponent(UINT32 typeId) const;
  232. /**
  233. * @brief Removes the component from this SceneObject, and deallocates it.
  234. *
  235. * @param [in] component The component to destroy.
  236. */
  237. void destroyComponent(const HComponent& component);
  238. /**
  239. * @brief Removes the component from this SceneObject, and deallocates it.
  240. *
  241. * @param [in] component The component to destroy.
  242. */
  243. void destroyComponent(Component* component);
  244. /**
  245. * @brief Returns all components on this SceneObject.
  246. */
  247. Vector<HComponent>::type& getComponents() { return mComponents; }
  248. private:
  249. Vector<HComponent>::type mComponents;
  250. /************************************************************************/
  251. /* RTTI */
  252. /************************************************************************/
  253. public:
  254. friend class SceneObjectRTTI;
  255. static RTTITypeBase* getRTTIStatic();
  256. virtual RTTITypeBase* getRTTI() const;
  257. };
  258. }