2
0

BsComponent.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsGameObject.h"
  4. namespace BansheeEngine
  5. {
  6. /**
  7. * @brief Components represent primary logic elements in the scene.
  8. * They are attached to scene objects.
  9. */
  10. class BS_CORE_EXPORT Component : public GameObject
  11. {
  12. public:
  13. /**
  14. * @brief Returns the SceneObject this Component is assigned to.
  15. */
  16. HSceneObject sceneObject() const { return mParent; }
  17. /**
  18. * @copydoc sceneObject
  19. */
  20. HSceneObject SO() const { return sceneObject(); }
  21. /**
  22. * @brief Called once per frame on all components.
  23. *
  24. * @note Internal method.
  25. */
  26. virtual void update() { }
  27. /**
  28. * @brief Checks if this and the provided component represent the same type.
  29. *
  30. * @note RTTI type cannot be checked directly since components can be further specialized internally
  31. * for scripting purposes.
  32. */
  33. virtual bool typeEquals(const Component& other);
  34. /**
  35. * @brief Removes the component from parent SceneObject and deletes it. All
  36. * the references to this component will be marked as destroyed and you
  37. * will get an exception if you try to use them.
  38. *
  39. * @param [in] immediate If true the destruction will be performed immediately, otherwise
  40. * it will be delayed until the end of the current frame (preferred option).
  41. */
  42. void destroy(bool immediate = false);
  43. protected:
  44. friend class SceneObject;
  45. friend class SceneObjectRTTI;
  46. Component(const HSceneObject& parent);
  47. virtual ~Component();
  48. /**
  49. * @brief Construct any resources the component needs before use. Called when the parent
  50. * scene object is instantiated. A non-instantiated component shouldn't be used for
  51. * any other purpose than serialization.
  52. */
  53. virtual void instantiate() {}
  54. /**
  55. * @brief Called when the component is ready to be initialized.
  56. */
  57. virtual void onInitialized() {}
  58. /**
  59. * @brief Called just before the component is destroyed.
  60. */
  61. virtual void onDestroyed() {}
  62. /**
  63. * @brief Called just before the component is deactivated or destroyed.
  64. */
  65. virtual void onDisabled() {}
  66. /**
  67. * @brief Called when the component is activated or created.
  68. */
  69. virtual void onEnabled() {}
  70. /**
  71. * @brief Destroys this component.
  72. *
  73. * @param [in] handle Game object handle this this object.
  74. * @param [in] immediate If true, the object will be deallocated and become unusable
  75. * right away. Otherwise the deallocation will be delayed to the end of
  76. * frame (preferred method).
  77. *
  78. * @note Unlike "destroy", does not remove the component from its parent.
  79. */
  80. void destroyInternal(GameObjectHandleBase& handle, bool immediate = false) override;
  81. private:
  82. Component(const Component& other) { }
  83. protected:
  84. HSceneObject mParent;
  85. /************************************************************************/
  86. /* RTTI */
  87. /************************************************************************/
  88. public:
  89. friend class ComponentRTTI;
  90. static RTTITypeBase* getRTTIStatic();
  91. virtual RTTITypeBase* getRTTI() const;
  92. protected:
  93. Component() {} // Serialization only
  94. };
  95. }