BsComponent.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsGameObject.h"
  4. namespace BansheeEngine
  5. {
  6. /**
  7. * @brief Components represent primarily 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 Removes the component from parent SceneObject and deletes it. All
  29. * the references to this component will be marked as destroyed and you
  30. * will get an exception if you try to use them.
  31. *
  32. * @param [in] immediate If true the destruction will be performed immediately, otherwise
  33. * it will be delayed until the end of the current frame (preferred option).
  34. */
  35. void destroy(bool immediate = false);
  36. protected:
  37. friend class SceneObject;
  38. friend class SceneObjectRTTI;
  39. Component(const HSceneObject& parent);
  40. virtual ~Component();
  41. /**
  42. * @brief Called when the component is ready to be initialized.
  43. */
  44. virtual void onInitialized() {}
  45. /**
  46. * @brief Called just before the component is destroyed.
  47. */
  48. virtual void onDestroyed() {}
  49. /**
  50. * @brief Called just before the component is deactivated or destroyed.
  51. */
  52. virtual void onDisabled() {}
  53. /**
  54. * @brief Called when the component is activated or created.
  55. */
  56. virtual void onEnabled() {}
  57. /**
  58. * @brief Destroys this component.
  59. *
  60. * @param [in] handle Game object handle this this object.
  61. * @param [in] immediate If true, the object will be deallocated and become unusable
  62. * right away. Otherwise the deallocation will be delayed to the end of
  63. * frame (preferred method).
  64. *
  65. * @note Unlike "destroy", does not remove the component from its parent.
  66. */
  67. void destroyInternal(GameObjectHandleBase& handle, bool immediate = false) override;
  68. private:
  69. Component(const Component& other) { }
  70. protected:
  71. HSceneObject mParent;
  72. /************************************************************************/
  73. /* RTTI */
  74. /************************************************************************/
  75. public:
  76. friend class ComponentRTTI;
  77. static RTTITypeBase* getRTTIStatic();
  78. virtual RTTITypeBase* getRTTI() const;
  79. protected:
  80. Component() {} // Serialization only
  81. };
  82. }