CmSceneObject.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 "CmGameObjectManager.h"
  9. #include "CmGameObject.h"
  10. #include "boost/static_assert.hpp"
  11. namespace CamelotFramework
  12. {
  13. class CM_EXPORT SceneObject : public GameObject
  14. {
  15. friend class SceneManager;
  16. public:
  17. ~SceneObject();
  18. static HSceneObject create(const String& name);
  19. void destroy();
  20. private:
  21. HSceneObject mThisHandle;
  22. SceneObject(const String& name);
  23. static HSceneObject createInternal(const String& name);
  24. void destroyInternal();
  25. /************************************************************************/
  26. /* Transform */
  27. /************************************************************************/
  28. public:
  29. void setPosition(const Vector3& position);
  30. const Vector3& getPosition() const { return mPosition; }
  31. const Vector3& getWorldPosition() const;
  32. void setRotation(const Quaternion& rotation);
  33. const Quaternion& getRotation() const { return mRotation; }
  34. const Quaternion& getWorldRotation() const;
  35. void setScale(const Vector3& scale);
  36. const Vector3& getScale() const { return mScale; }
  37. const Vector3& getWorldScale() const;
  38. void lookAt(const Vector3& location, const Vector3& up = Vector3::UNIT_Y);
  39. const Matrix4& getWorldTfrm() const;
  40. const Matrix4& getLocalTfrm() const;
  41. /** Moves the object's position by the vector offset provided along world axes.
  42. */
  43. void move(const Vector3& vec);
  44. /** Moves the object's position by the vector offset provided along it's own axes (relative to orientation).
  45. */
  46. void moveRelative(const Vector3& vec);
  47. /**
  48. * @brief Gets the Z (forward) axis of the object, in world space.
  49. *
  50. * @return Forward axis of the object.
  51. */
  52. Vector3 getForward() const { return getWorldRotation().rotate(-Vector3::UNIT_Z); }
  53. /**
  54. * @brief Gets the Y (up) axis of the object, in world space.
  55. *
  56. * @return Up axis of the object.
  57. */
  58. Vector3 getUp() const { return getWorldRotation().rotate(Vector3::UNIT_Y); }
  59. /**
  60. * @brief Gets the X (right) axis of the object, in world space.
  61. *
  62. * @return Right axis of the object.
  63. */
  64. Vector3 getRight() const { return getWorldRotation().rotate(Vector3::UNIT_X); }
  65. /**
  66. * @brief Rotates the game object so it's forward axis faces the provided
  67. * direction.
  68. *
  69. * @note Local forward axis is considered to be negative Z.
  70. *
  71. * @param forwardDir The forward direction to face, in world space.
  72. */
  73. void setForward(const Vector3& forwardDir);
  74. /** Rotate the object around an arbitrary axis.
  75. */
  76. void rotate(const Vector3& axis, const Radian& angle);
  77. /** Rotate the object around an arbitrary axis using a Quaternion.
  78. */
  79. void rotate(const Quaternion& q);
  80. /**
  81. * @brief Rotates around local Z axis.
  82. *
  83. * @param angle Angle to rotate by.
  84. */
  85. void roll(const Radian& angle);
  86. /**
  87. * @brief Rotates around Y axis.
  88. *
  89. * @param angle Angle to rotate by.
  90. */
  91. void yaw(const Radian& angle);
  92. /**
  93. * @brief Rotates around X axis
  94. *
  95. * @param angle Angle to rotate by.
  96. */
  97. void pitch(const Radian& angle);
  98. private:
  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. /**
  155. * @brief Makes a deep copy of this object.
  156. */
  157. HSceneObject clone();
  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. std::shared_ptr<T> gameObject(new (cm_alloc<T, PoolAlloc>()) T(mThisHandle), &cm_delete<PoolAlloc, T>, StdAlloc<PoolAlloc>());
  184. GameObjectHandle<T> newComponent = GameObjectHandle<T>(GameObjectManager::instance().registerObject(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. std::shared_ptr<Type> gameObject(new (cm_alloc<Type, PoolAlloc>()) Type(mThisHandle, \
  198. std::forward<T0>(t0) BOOST_PP_REPEAT_FROM_TO(1, n, FORWARD_T, ~)), \
  199. &cm_delete<PoolAlloc, Type>, StdAlloc<PoolAlloc>()); \
  200. \
  201. GameObjectHandle<Type> newComponent = \
  202. GameObjectHandle<Type>(GameObjectManager::instance().registerObject(gameObject)); \
  203. \
  204. mComponents.push_back(newComponent); \
  205. \
  206. gSceneManager().notifyComponentAdded(newComponent); \
  207. \
  208. return newComponent; \
  209. }
  210. #define FORWARD_T(z, i, unused) \
  211. , std::forward<BOOST_PP_CAT(T, i)>(BOOST_PP_CAT(t, i))
  212. BOOST_PP_REPEAT_FROM_TO(1, 15, MAKE_ADD_COMPONENT, ~)
  213. #undef FORWARD_T
  214. #undef MAKE_ADD_COMPONENT
  215. /**
  216. * @brief Searches for a component with the specific type and returns the first one
  217. * it finds.
  218. *
  219. * @note Don't call this too often as it is relatively slow. It is more efficient
  220. * to call it once and store the result for further use.
  221. *
  222. * @tparam typename T Type of the component.
  223. *
  224. * @return Component if found, nullptr otherwise.
  225. */
  226. template <typename T>
  227. GameObjectHandle<T> getComponent()
  228. {
  229. BOOST_STATIC_ASSERT_MSG((boost::is_base_of<CamelotFramework::Component, T>::value),
  230. "Specified type is not a valid Component.");
  231. return static_object_cast<T>(getComponent(T::getRTTIStatic()->getRTTIId()));
  232. }
  233. /**
  234. * @brief Checks if the current object contains the specified component
  235. *
  236. * @note Don't call this too often as it is relatively slow.
  237. *
  238. * @tparam typename T Type of the component.
  239. *
  240. * @return True if component exists on the object.
  241. */
  242. template <typename T>
  243. bool hasComponent()
  244. {
  245. BOOST_STATIC_ASSERT_MSG((boost::is_base_of<CamelotFramework::Component, T>::value),
  246. "Specified type is not a valid Component.");
  247. return hasComponent(T::getRTTIStatic()->getRTTIId());
  248. }
  249. /**
  250. * @brief Searches for a component with the specified type id and returns the first one it
  251. * finds.
  252. *
  253. * @note Don't call this too often as it is relatively slow. It is more efficient to
  254. * call it once and store the result for further use.
  255. *
  256. * @param typeId Identifier for the type.
  257. *
  258. * @return Component if found, nullptr otherwise.
  259. */
  260. HComponent getComponent(UINT32 typeId) const;
  261. /**
  262. * @brief Removes the component from this SceneObject, and deallocates it.
  263. *
  264. * @param [in] component The component to destroy.
  265. */
  266. void destroyComponent(const HComponent& component);
  267. /**
  268. * @brief Removes the component from this SceneObject, and deallocates it.
  269. *
  270. * @param [in] component The component to destroy.
  271. */
  272. void destroyComponent(Component* component);
  273. /**
  274. * @brief Returns all components on this SceneObject.
  275. */
  276. const Vector<HComponent>::type& getComponents() const { return mComponents; }
  277. private:
  278. template <typename T>
  279. static std::shared_ptr<T> createEmptyComponent()
  280. {
  281. BOOST_STATIC_ASSERT_MSG((boost::is_base_of<CamelotFramework::Component, T>::value), "Specified type is not a valid Component.");
  282. std::shared_ptr<T> gameObject(new (cm_alloc<T, PoolAlloc>()) T(), &cm_delete<PoolAlloc, T>, StdAlloc<PoolAlloc>());
  283. GameObjectHandle<T>(GameObjectManager::instance().registerObject(gameObject));
  284. return gameObject;
  285. }
  286. void addComponentInternal(const std::shared_ptr<Component> component);
  287. Vector<HComponent>::type mComponents;
  288. /************************************************************************/
  289. /* RTTI */
  290. /************************************************************************/
  291. public:
  292. friend class GameObjectRTTI;
  293. friend class SceneObjectRTTI;
  294. static RTTITypeBase* getRTTIStatic();
  295. virtual RTTITypeBase* getRTTI() const;
  296. };
  297. }