BsSceneObject.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. //__________________________ Banshee Project - A modern game development toolkit _________________________________//
  2. //_____________________________________ www.banshee-project.com __________________________________________________//
  3. //________________________ Copyright (c) 2014 Marko Pintera. All rights reserved. ________________________________//
  4. #pragma once
  5. #include "BsCorePrerequisites.h"
  6. #include "BsMatrix4.h"
  7. #include "BsVector3.h"
  8. #include "BsQuaternion.h"
  9. #include "BsRTTIType.h"
  10. #include "BsCoreSceneManager.h"
  11. #include "BsGameObjectManager.h"
  12. #include "BsGameObject.h"
  13. namespace BansheeEngine
  14. {
  15. /**
  16. * @brief SceneObject represents an object in the scene graph. It has a world position,
  17. * place in the hierarchy and optionally a number of attached components.
  18. */
  19. class BS_CORE_EXPORT SceneObject : public GameObject
  20. {
  21. friend class CoreSceneManager;
  22. public:
  23. ~SceneObject();
  24. /**
  25. * @brief Creates a new SceneObject with the specified name. Object will be placed in the top
  26. * of the scene hierarchy.
  27. */
  28. static HSceneObject create(const String& name);
  29. /**
  30. * @brief Destroys this object and any of its held components.
  31. */
  32. void destroy();
  33. private:
  34. SceneObject(const String& name);
  35. static HSceneObject createInternal(const String& name);
  36. void destroyInternal();
  37. private:
  38. HSceneObject mThisHandle;
  39. /************************************************************************/
  40. /* Transform */
  41. /************************************************************************/
  42. public:
  43. /**
  44. * @brief Sets the local position of the object.
  45. */
  46. void setPosition(const Vector3& position);
  47. /**
  48. * @brief Gets the local position of the object.
  49. */
  50. const Vector3& getPosition() const { return mPosition; }
  51. /**
  52. * @brief Sets the world position of the object.
  53. */
  54. void setWorldPosition(const Vector3& position);
  55. /**
  56. * @brief Gets the world position of the object.
  57. *
  58. * @note Performance warning: This might involve updating the transforms if the transform is dirty.
  59. */
  60. const Vector3& getWorldPosition() const;
  61. /**
  62. * @brief Sets the local rotation of the object.
  63. */
  64. void setRotation(const Quaternion& rotation);
  65. /**
  66. * @brief Gets the local rotation of the object.
  67. */
  68. const Quaternion& getRotation() const { return mRotation; }
  69. /**
  70. * @brief Sets the world rotation of the object.
  71. */
  72. void setWorldRotation(const Quaternion& rotation);
  73. /**
  74. * @brief Gets world rotation of the object.
  75. *
  76. * @note Performance warning: This might involve updating the transforms if the transform is dirty.
  77. */
  78. const Quaternion& getWorldRotation() const;
  79. /**
  80. * @brief Sets the local scale of the object.
  81. */
  82. void setScale(const Vector3& scale);
  83. /**
  84. * @brief Gets the local scale of the object.
  85. */
  86. const Vector3& getScale() const { return mScale; }
  87. /**
  88. * @brief Gets world scale of the object.
  89. *
  90. * @note Performance warning: This might involve updating the transforms if the transform is dirty.
  91. */
  92. const Vector3& getWorldScale() const;
  93. /**
  94. * @brief Orients the object so it is looking at the provided "location" (local space)
  95. * where "up" is used for determining the location of the objects Y axis.
  96. *
  97. */
  98. void lookAt(const Vector3& location, const Vector3& up = Vector3::UNIT_Y);
  99. /**
  100. * @brief Gets the objects world transform matrix.
  101. *
  102. * @note Performance warning: This might involve updating the transforms if the transform is dirty.
  103. */
  104. const Matrix4& getWorldTfrm() const;
  105. /**
  106. * @brief Gets the objects local transform matrix.
  107. */
  108. const Matrix4& getLocalTfrm() const;
  109. /**
  110. * @brief Moves the object's position by the vector offset provided along world axes.
  111. */
  112. void move(const Vector3& vec);
  113. /**
  114. * @brief Moves the object's position by the vector offset provided along it's own axes (relative to orientation).
  115. */
  116. void moveRelative(const Vector3& vec);
  117. /**
  118. * @brief Gets the Z (forward) axis of the object, in world space.
  119. *
  120. * @return Forward axis of the object.
  121. */
  122. Vector3 getForward() const { return getWorldRotation().rotate(-Vector3::UNIT_Z); }
  123. /**
  124. * @brief Gets the Y (up) axis of the object, in world space.
  125. *
  126. * @return Up axis of the object.
  127. */
  128. Vector3 getUp() const { return getWorldRotation().rotate(Vector3::UNIT_Y); }
  129. /**
  130. * @brief Gets the X (right) axis of the object, in world space.
  131. *
  132. * @return Right axis of the object.
  133. */
  134. Vector3 getRight() const { return getWorldRotation().rotate(Vector3::UNIT_X); }
  135. /**
  136. * @brief Rotates the game object so it's forward axis faces the provided
  137. * direction.
  138. *
  139. * @note Local forward axis is considered to be negative Z.
  140. *
  141. * @param forwardDir The forward direction to face, in world space.
  142. */
  143. void setForward(const Vector3& forwardDir);
  144. /**
  145. * @brief Rotate the object around an arbitrary axis.
  146. */
  147. void rotate(const Vector3& axis, const Radian& angle);
  148. /**
  149. * @brief Rotate the object around an arbitrary axis using a Quaternion.
  150. */
  151. void rotate(const Quaternion& q);
  152. /**
  153. * @brief Rotates around local Z axis.
  154. *
  155. * @param angle Angle to rotate by.
  156. */
  157. void roll(const Radian& angle);
  158. /**
  159. * @brief Rotates around Y axis.
  160. *
  161. * @param angle Angle to rotate by.
  162. */
  163. void yaw(const Radian& angle);
  164. /**
  165. * @brief Rotates around X axis
  166. *
  167. * @param angle Angle to rotate by.
  168. */
  169. void pitch(const Radian& angle);
  170. /**
  171. * @brief Forces any dirty transform matrices on this object to be updated.
  172. *
  173. * @note Normally this is done internally when retrieving a transform, but sometimes
  174. * it is useful to update transforms manually.
  175. */
  176. void updateTransformsIfDirty();
  177. /**
  178. * @brief Checks is the core dirty flag set. This is used by external systems
  179. * to know when internal data has changed and core thread potentially needs to be notified.
  180. */
  181. bool _isCoreDirty() const { return mIsCoreDirtyFlags != 0; }
  182. /**
  183. * @brief Marks the core dirty flag as clean.
  184. */
  185. void _markCoreClean() { mIsCoreDirtyFlags = 0; }
  186. private:
  187. Vector3 mPosition;
  188. Quaternion mRotation;
  189. Vector3 mScale;
  190. mutable Vector3 mWorldPosition;
  191. mutable Quaternion mWorldRotation;
  192. mutable Vector3 mWorldScale;
  193. mutable Matrix4 mCachedLocalTfrm;
  194. mutable bool mIsCachedLocalTfrmUpToDate;
  195. mutable Matrix4 mCachedWorldTfrm;
  196. mutable bool mIsCachedWorldTfrmUpToDate;
  197. mutable UINT32 mIsCoreDirtyFlags;
  198. /**
  199. * @brief Marks the transform as dirty so that we know to update
  200. * it when the transform is requested.
  201. */
  202. void markTfrmDirty() const;
  203. /**
  204. * @brief Updates the local transform. Normally just reconstructs the
  205. * transform matrix from the position/rotation/scale.
  206. */
  207. void updateLocalTfrm() const;
  208. /**
  209. * @brief Updates the world transform. Reconstructs the local transform
  210. * matrix and multiplies it with any parent transforms.
  211. *
  212. * @note If parent transforms are dirty they will be updated.
  213. */
  214. void updateWorldTfrm() const;
  215. /************************************************************************/
  216. /* Hierarchy */
  217. /************************************************************************/
  218. public:
  219. /**
  220. * @brief Changes the parent of this object. Also removes the object from the current parent,
  221. * and assigns it to the new parent.
  222. *
  223. * @param [in] parent New parent.
  224. */
  225. void setParent(const HSceneObject& parent);
  226. /**
  227. * @brief Gets the parent of this object.
  228. *
  229. * @return Parent object, or nullptr if this SceneObject is at root level.
  230. */
  231. HSceneObject getParent() const { return mParent; }
  232. /**
  233. * @brief Gets a child of this item.
  234. *
  235. * @param idx The zero based index of the child.
  236. *
  237. * @return SceneObject of the child.
  238. */
  239. HSceneObject getChild(UINT32 idx) const;
  240. /**
  241. * @brief Find the index of the specified child. Don't persist this value as
  242. * it may change whenever you add/remove children.
  243. *
  244. * @param child The child to look for.
  245. *
  246. * @return The zero-based index of the found child, or -1 if no match was found.
  247. */
  248. int indexOfChild(const HSceneObject& child) const;
  249. /**
  250. * @brief Gets the number of all child GameObjects.
  251. */
  252. UINT32 getNumChildren() const { return (UINT32)mChildren.size(); }
  253. /**
  254. * @brief Makes a deep copy of this object.
  255. */
  256. HSceneObject clone();
  257. private:
  258. HSceneObject mParent;
  259. Vector<HSceneObject> mChildren;
  260. /**
  261. * @brief Adds a child to the child array. This method doesn't check for null or duplicate values.
  262. *
  263. * @param [in] object New child.
  264. */
  265. void addChild(const HSceneObject& object);
  266. /**
  267. * @brief Removes the child from the object.
  268. *
  269. * @param [in] object Child to remove.
  270. *
  271. * @throws INTERNAL_ERROR If the provided child isn't a child of the current object.
  272. */
  273. void removeChild(const HSceneObject& object);
  274. /************************************************************************/
  275. /* Component */
  276. /************************************************************************/
  277. public:
  278. /**
  279. * @brief Constructs a new component of the specified type and adds it to
  280. * the internal component list.
  281. */
  282. template<class T, class... Args>
  283. GameObjectHandle<T> addComponent(Args &&... args)
  284. {
  285. static_assert((std::is_base_of<BansheeEngine::Component, T>::value),
  286. "Specified type is not a valid Component.");
  287. std::shared_ptr<T> gameObject(new (bs_alloc<T, PoolAlloc>()) T(mThisHandle,
  288. std::forward<Args>(args)...),
  289. &bs_delete<PoolAlloc, T>, StdAlloc<PoolAlloc>());
  290. GameObjectHandle<T> newComponent =
  291. GameObjectHandle<T>(GameObjectManager::instance().registerObject(gameObject));
  292. mComponents.push_back(newComponent);
  293. gSceneManager().notifyComponentAdded(newComponent);
  294. return newComponent;
  295. }
  296. /**
  297. * @brief Searches for a component with the specific type and returns the first one
  298. * it finds.
  299. *
  300. * @note Don't call this too often as it is relatively slow. It is more efficient
  301. * to call it once and store the result for further use.
  302. *
  303. * @tparam typename T Type of the component.
  304. *
  305. * @return Component if found, nullptr otherwise.
  306. */
  307. template <typename T>
  308. GameObjectHandle<T> getComponent()
  309. {
  310. static_assert((std::is_base_of<BansheeEngine::Component, T>::value),
  311. "Specified type is not a valid Component.");
  312. return static_object_cast<T>(getComponent(T::getRTTIStatic()->getRTTIId()));
  313. }
  314. /**
  315. * @brief Checks if the current object contains the specified component
  316. *
  317. * @note Don't call this too often as it is relatively slow.
  318. *
  319. * @tparam typename T Type of the component.
  320. *
  321. * @return True if component exists on the object.
  322. */
  323. template <typename T>
  324. bool hasComponent()
  325. {
  326. static_assert((std::is_base_of<BansheeEngine::Component, T>::value),
  327. "Specified type is not a valid Component.");
  328. return hasComponent(T::getRTTIStatic()->getRTTIId());
  329. }
  330. /**
  331. * @brief Searches for a component with the specified type id and returns the first one it
  332. * finds.
  333. *
  334. * @note Don't call this too often as it is relatively slow. It is more efficient to
  335. * call it once and store the result for further use.
  336. *
  337. * @param typeId Identifier for the type.
  338. *
  339. * @return Component if found, nullptr otherwise.
  340. */
  341. HComponent getComponent(UINT32 typeId) const;
  342. /**
  343. * @brief Removes the component from this object, and deallocates it.
  344. *
  345. * @param [in] component The component to destroy.
  346. */
  347. void destroyComponent(const HComponent& component);
  348. /**
  349. * @brief Removes the component from this object, and deallocates it.
  350. *
  351. * @param [in] component The component to destroy.
  352. */
  353. void destroyComponent(Component* component);
  354. /**
  355. * @brief Returns all components on this object.
  356. */
  357. const Vector<HComponent>& getComponents() const { return mComponents; }
  358. private:
  359. /**
  360. * @brief Creates an empty component with the default constructor.
  361. */
  362. template <typename T>
  363. static std::shared_ptr<T> createEmptyComponent()
  364. {
  365. static_assert((std::is_base_of<BansheeEngine::Component, T>::value), "Specified type is not a valid Component.");
  366. std::shared_ptr<T> gameObject(new (bs_alloc<T, PoolAlloc>()) T(), &bs_delete<PoolAlloc, T>, StdAlloc<PoolAlloc>());
  367. GameObjectHandle<T>(GameObjectManager::instance().registerObject(gameObject));
  368. return gameObject;
  369. }
  370. /**
  371. * @brief Adds the component to the internal component array.
  372. */
  373. void addComponentInternal(const std::shared_ptr<Component> component);
  374. Vector<HComponent> mComponents;
  375. /************************************************************************/
  376. /* RTTI */
  377. /************************************************************************/
  378. public:
  379. friend class GameObjectRTTI;
  380. friend class SceneObjectRTTI;
  381. static RTTITypeBase* getRTTIStatic();
  382. virtual RTTITypeBase* getRTTI() const;
  383. };
  384. }