BsComponent.h 3.4 KB

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