CmSceneObject.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. namespace BansheeEngine
  11. {
  12. class CM_EXPORT SceneObject : public GameObject
  13. {
  14. friend class SceneManagerBase;
  15. public:
  16. ~SceneObject();
  17. static HSceneObject create(const String& name);
  18. void destroy();
  19. private:
  20. HSceneObject mThisHandle;
  21. SceneObject(const String& name);
  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. Vector3 mPosition;
  99. Quaternion mRotation;
  100. Vector3 mScale;
  101. mutable Vector3 mWorldPosition;
  102. mutable Quaternion mWorldRotation;
  103. mutable Vector3 mWorldScale;
  104. mutable Matrix4 mCachedLocalTfrm;
  105. mutable bool mIsCachedLocalTfrmUpToDate;
  106. mutable Matrix4 mCachedWorldTfrm;
  107. mutable bool mIsCachedWorldTfrmUpToDate;
  108. Matrix4 mCustomWorldTfrm; // TODO
  109. bool mIsCustomTfrmModeActive; // TODO
  110. void markTfrmDirty() const;
  111. void updateLocalTfrm() const;
  112. void updateWorldTfrm() const;
  113. /************************************************************************/
  114. /* Hierarchy */
  115. /************************************************************************/
  116. public:
  117. /**
  118. * @brief Changes the parent of this object. Also removes the object from the current parent,
  119. * and assigns it to the new parent.
  120. *
  121. * @param [in] parent New parent.
  122. */
  123. void setParent(const HSceneObject& parent);
  124. /**
  125. * @brief Gets the parent of this object.
  126. *
  127. * @return Parent object, or nullptr if this SceneObject is at root level.
  128. */
  129. HSceneObject getParent() const { return mParent; }
  130. /**
  131. * @brief Gets a child of this item.
  132. *
  133. * @param idx The zero based index of the child.
  134. *
  135. * @return SceneObject of the child.
  136. *
  137. * @throws ERR_INVALIDPARAMS If the index is out of range.
  138. */
  139. HSceneObject getChild(unsigned int idx) const;
  140. /**
  141. * @brief Find the index of the specified child. Don't persist this value as
  142. * it may change whenever you add/remove children.
  143. *
  144. * @param child The child to look for.
  145. *
  146. * @return The zero-based index of the found child, or -1 if no match was found.
  147. */
  148. int indexOfChild(const HSceneObject& child) const;
  149. /**
  150. * @brief Gets the number of all child GameObjects.
  151. */
  152. UINT32 getNumChildren() const { return (UINT32)mChildren.size(); }
  153. /**
  154. * @brief Makes a deep copy of this object.
  155. */
  156. HSceneObject clone();
  157. private:
  158. HSceneObject mParent;
  159. Vector<HSceneObject> mChildren;
  160. /**
  161. * @brief Adds a child to the child array. This method doesn't check for null or duplicate values.
  162. *
  163. * @param [in] object New child.
  164. */
  165. void addChild(const HSceneObject& object);
  166. /**
  167. * @brief Removes the child from the object.
  168. *
  169. * @param [in] object Child to remove.
  170. *
  171. * @throws INTERNAL_ERROR If the provided child isn't a child of the current object.
  172. */
  173. void removeChild(const HSceneObject& object);
  174. /************************************************************************/
  175. /* Component */
  176. /************************************************************************/
  177. public:
  178. template<class T, class... Args>
  179. GameObjectHandle<T> addComponent(Args &&... args)
  180. {
  181. static_assert((std::is_base_of<BansheeEngine::Component, T>::value),
  182. "Specified type is not a valid Component.");
  183. std::shared_ptr<T> gameObject(new (cm_alloc<T, PoolAlloc>()) T(mThisHandle,
  184. std::forward<Args>(args)...),
  185. &cm_delete<PoolAlloc, T>, StdAlloc<PoolAlloc>());
  186. GameObjectHandle<T> newComponent =
  187. GameObjectHandle<T>(GameObjectManager::instance().registerObject(gameObject));
  188. mComponents.push_back(newComponent);
  189. gSceneManager().notifyComponentAdded(newComponent);
  190. return newComponent;
  191. }
  192. /**
  193. * @brief Searches for a component with the specific type and returns the first one
  194. * it finds.
  195. *
  196. * @note Don't call this too often as it is relatively slow. It is more efficient
  197. * to call it once and store the result for further use.
  198. *
  199. * @tparam typename T Type of the component.
  200. *
  201. * @return Component if found, nullptr otherwise.
  202. */
  203. template <typename T>
  204. GameObjectHandle<T> getComponent()
  205. {
  206. static_assert((std::is_base_of<BansheeEngine::Component, T>::value),
  207. "Specified type is not a valid Component.");
  208. return static_object_cast<T>(getComponent(T::getRTTIStatic()->getRTTIId()));
  209. }
  210. /**
  211. * @brief Checks if the current object contains the specified component
  212. *
  213. * @note Don't call this too often as it is relatively slow.
  214. *
  215. * @tparam typename T Type of the component.
  216. *
  217. * @return True if component exists on the object.
  218. */
  219. template <typename T>
  220. bool hasComponent()
  221. {
  222. static_assert((std::is_base_of<BansheeEngine::Component, T>::value),
  223. "Specified type is not a valid Component.");
  224. return hasComponent(T::getRTTIStatic()->getRTTIId());
  225. }
  226. /**
  227. * @brief Searches for a component with the specified type id and returns the first one it
  228. * finds.
  229. *
  230. * @note Don't call this too often as it is relatively slow. It is more efficient to
  231. * call it once and store the result for further use.
  232. *
  233. * @param typeId Identifier for the type.
  234. *
  235. * @return Component if found, nullptr otherwise.
  236. */
  237. HComponent getComponent(UINT32 typeId) const;
  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(const HComponent& component);
  244. /**
  245. * @brief Removes the component from this SceneObject, and deallocates it.
  246. *
  247. * @param [in] component The component to destroy.
  248. */
  249. void destroyComponent(Component* component);
  250. /**
  251. * @brief Returns all components on this SceneObject.
  252. */
  253. const Vector<HComponent>& getComponents() const { return mComponents; }
  254. private:
  255. template <typename T>
  256. static std::shared_ptr<T> createEmptyComponent()
  257. {
  258. static_assert((std::is_base_of<BansheeEngine::Component, T>::value), "Specified type is not a valid Component.");
  259. std::shared_ptr<T> gameObject(new (cm_alloc<T, PoolAlloc>()) T(), &cm_delete<PoolAlloc, T>, StdAlloc<PoolAlloc>());
  260. GameObjectHandle<T>(GameObjectManager::instance().registerObject(gameObject));
  261. return gameObject;
  262. }
  263. void addComponentInternal(const std::shared_ptr<Component> component);
  264. Vector<HComponent> mComponents;
  265. /************************************************************************/
  266. /* RTTI */
  267. /************************************************************************/
  268. public:
  269. friend class GameObjectRTTI;
  270. friend class SceneObjectRTTI;
  271. static RTTITypeBase* getRTTIStatic();
  272. virtual RTTITypeBase* getRTTI() const;
  273. };
  274. }