CmGameObject.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #pragma once
  2. #include "CmPrerequisites.h"
  3. #include "CmMatrix4.h"
  4. #include "CmVector3.h"
  5. #include "CmQuaternion.h"
  6. #include "boost/static_assert.hpp"
  7. namespace CamelotEngine
  8. {
  9. class CM_EXPORT GameObject
  10. {
  11. friend class SceneManager;
  12. public:
  13. static GameObjectPtr create(const String& name);
  14. ~GameObject();
  15. void destroy();
  16. bool isDestroyed() { return mIsDestroyed; }
  17. private:
  18. GameObject(const String& name);
  19. static GameObjectPtr createInternal(const String& name);
  20. std::weak_ptr<GameObject> mThis;
  21. bool mIsDestroyed;
  22. /************************************************************************/
  23. /* Transform */
  24. /************************************************************************/
  25. public:
  26. void setPosition(const Vector3& position);
  27. const Vector3& getPosition() const { return mPosition; }
  28. const Vector3& getWorldPosition() const;
  29. void setRotation(const Quaternion& rotation);
  30. const Quaternion& getRotation() const { return mRotation; }
  31. const Quaternion& getWorldRotation() const;
  32. void setScale(const Vector3& scale);
  33. const Vector3& getScale() const { return mScale; }
  34. const Vector3& getWorldScale() const;
  35. void lookAt(const Vector3& location, const Vector3& up = Vector3::UNIT_Y);
  36. const Matrix4& getWorldTfrm() const;
  37. const Matrix4& getLocalTfrm() const;
  38. private:
  39. String mName;
  40. Vector3 mPosition;
  41. Quaternion mRotation;
  42. Vector3 mScale;
  43. mutable Vector3 mWorldPosition;
  44. mutable Quaternion mWorldRotation;
  45. mutable Vector3 mWorldScale;
  46. mutable Matrix4 mCachedLocalTfrm;
  47. mutable bool mIsCachedLocalTfrmUpToDate;
  48. mutable Matrix4 mCachedWorldTfrm;
  49. mutable bool mIsCachedWorldTfrmUpToDate;
  50. Matrix4 mCustomWorldTfrm; // TODO
  51. bool mIsCustomTfrmModeActive; // TODO
  52. void markTfrmDirty() const;
  53. void updateLocalTfrm() const;
  54. void updateWorldTfrm() const;
  55. /************************************************************************/
  56. /* Hierarchy */
  57. /************************************************************************/
  58. public:
  59. /**
  60. * @brief Changes the parent of this object. Also removes the object from the current parent,
  61. * and assigns it to the new parent.
  62. *
  63. * @param [in] parent New parent.
  64. */
  65. void setParent(GameObjectPtr parent);
  66. /**
  67. * @brief Gets the parent of this object.
  68. *
  69. * @return Parent object, or nullptr if this GameObject is at root level.
  70. */
  71. GameObjectPtr getParent() const { return mParent.lock(); }
  72. /**
  73. * @brief Gets a child of this item.
  74. *
  75. * @param idx The zero based index of the child.
  76. *
  77. * @return GameObject of the child.
  78. *
  79. * @throws ERR_INVALIDPARAMS If the index is out of range.
  80. */
  81. GameObjectPtr getChild(unsigned int idx) const;
  82. /**
  83. * @brief Find the index of the specified child. Don't persist this value as
  84. * it may change whenever you add/remove children.
  85. *
  86. * @param child The child to look for.
  87. *
  88. * @return The zero-based index of the found child, or -1 if no match was found.
  89. */
  90. int indexOfChild(const GameObjectPtr child) const;
  91. /**
  92. * @brief Gets the number of all child GameObjects.
  93. */
  94. int getNumChildren() const { return mChildren.size(); }
  95. private:
  96. std::weak_ptr<GameObject> mParent;
  97. vector<GameObjectPtr>::type mChildren;
  98. /**
  99. * @brief Adds a child to the child array. This method doesn't check for null or duplicate values.
  100. *
  101. * @param [in] object New child.
  102. */
  103. void addChild(GameObjectPtr object);
  104. /**
  105. * @brief Removes the child from the object.
  106. *
  107. * @param [in] object Child to remove.
  108. *
  109. * @throws INTERNAL_ERROR If the provided child isn't a child of the current object.
  110. */
  111. void removeChild(GameObjectPtr object);
  112. /************************************************************************/
  113. /* Component */
  114. /************************************************************************/
  115. public:
  116. template <typename T>
  117. std::shared_ptr<T> addComponent()
  118. {
  119. BOOST_STATIC_ASSERT_MSG((boost::is_base_of<CamelotEngine::Component, T>::value),
  120. "Specified type is not a valid Component.");
  121. std::shared_ptr<T> newComponent = std::shared_ptr<T>(new T(mThis.lock()));
  122. mComponents.push_back(newComponent);
  123. gSceneManager().notifyComponentAdded(newComponent);
  124. return newComponent;
  125. }
  126. /**
  127. * @brief Removes the component from this GameObject, and deallocates it.
  128. *
  129. * @param [in] component The component to destroy.
  130. */
  131. void destroyComponent(ComponentPtr component);
  132. private:
  133. vector<ComponentPtr>::type mComponents;
  134. };
  135. }