CmSceneObject.h 10 KB

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