CmSceneObject.h 10 KB

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