CmSceneObject.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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().rotate(-Vector3::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().rotate(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().rotate(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), "Specified type is not a valid Component.");
  179. GameObjectHandle<T> newComponent = GameObjectHandle<T>(
  180. new (cm_alloc<T, PoolAlloc>()) T(mThisHandle), &cm_delete<PoolAlloc, GameObject>);
  181. mComponents.push_back(newComponent);
  182. gSceneManager().notifyComponentAdded(newComponent);
  183. return newComponent;
  184. }
  185. // addComponent that accepts an arbitrary number of parameters > 0
  186. #define MAKE_ADD_COMPONENT(z, n, unused) \
  187. template<class Type BOOST_PP_ENUM_TRAILING_PARAMS(n, class T)> \
  188. GameObjectHandle<Type> addComponent(BOOST_PP_ENUM_BINARY_PARAMS(n, T, &&t) ) \
  189. { \
  190. BOOST_STATIC_ASSERT_MSG((boost::is_base_of<CamelotFramework::Component, Type>::value), \
  191. "Specified type is not a valid Component."); \
  192. \
  193. GameObjectHandle<Type> newComponent = GameObjectHandle<Type>( \
  194. new (cm_alloc<Type, PoolAlloc>()) Type(mThisHandle, std::forward<T0>(t0) BOOST_PP_REPEAT_FROM_TO(1, n, FORWARD_T, ~)), \
  195. &cm_delete<PoolAlloc, GameObject>); \
  196. \
  197. mComponents.push_back(newComponent); \
  198. \
  199. gSceneManager().notifyComponentAdded(newComponent); \
  200. \
  201. return newComponent; \
  202. }
  203. #define FORWARD_T(z, i, unused) \
  204. , std::forward<BOOST_PP_CAT(T, i)>(BOOST_PP_CAT(t, i))
  205. BOOST_PP_REPEAT_FROM_TO(1, 15, MAKE_ADD_COMPONENT, ~)
  206. #undef FORWARD_T
  207. #undef MAKE_ADD_COMPONENT
  208. /**
  209. * @brief Searches for a component with the specific type and returns the first one
  210. * it finds.
  211. *
  212. * @note Don't call this too often as it is relatively slow. It is more efficient
  213. * to call it once and store the result for further use.
  214. *
  215. * @tparam typename T Type of the component.
  216. *
  217. * @return Component if found, nullptr otherwise.
  218. */
  219. template <typename T>
  220. GameObjectHandle<T> getComponent()
  221. {
  222. BOOST_STATIC_ASSERT_MSG((boost::is_base_of<CamelotFramework::Component, T>::value),
  223. "Specified type is not a valid Component.");
  224. return static_object_cast<T>(getComponent(T::getRTTIStatic()->getRTTIId()));
  225. }
  226. /**
  227. * @brief Checks if the current object contains the specified component
  228. *
  229. * @note Don't call this too often as it is relatively slow.
  230. *
  231. * @tparam typename T Type of the component.
  232. *
  233. * @return True if component exists on the object.
  234. */
  235. template <typename T>
  236. bool hasComponent()
  237. {
  238. BOOST_STATIC_ASSERT_MSG((boost::is_base_of<CamelotFramework::Component, T>::value),
  239. "Specified type is not a valid Component.");
  240. return hasComponent(T::getRTTIStatic()->getRTTIId());
  241. }
  242. /**
  243. * @brief Searches for a component with the specified type id and returns the first one it
  244. * finds.
  245. *
  246. * @note Don't call this too often as it is relatively slow. It is more efficient to
  247. * call it once and store the result for further use.
  248. *
  249. * @param typeId Identifier for the type.
  250. *
  251. * @return Component if found, nullptr otherwise.
  252. */
  253. HComponent getComponent(UINT32 typeId) const;
  254. /**
  255. * @brief Removes the component from this SceneObject, and deallocates it.
  256. *
  257. * @param [in] component The component to destroy.
  258. */
  259. void destroyComponent(const HComponent& component);
  260. /**
  261. * @brief Removes the component from this SceneObject, and deallocates it.
  262. *
  263. * @param [in] component The component to destroy.
  264. */
  265. void destroyComponent(Component* component);
  266. /**
  267. * @brief Returns all components on this SceneObject.
  268. */
  269. Vector<HComponent>::type& getComponents() { return mComponents; }
  270. private:
  271. Vector<HComponent>::type mComponents;
  272. /************************************************************************/
  273. /* RTTI */
  274. /************************************************************************/
  275. public:
  276. friend class SceneObjectRTTI;
  277. static RTTITypeBase* getRTTIStatic();
  278. virtual RTTITypeBase* getRTTI() const;
  279. };
  280. }