LogicComponent.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. /// \file
  4. #pragma once
  5. #include "../Container/FlagSet.h"
  6. #include "../Scene/Component.h"
  7. namespace Urho3D
  8. {
  9. enum class LogicComponentEvents
  10. {
  11. /// Not use any events
  12. None = 0,
  13. /// Use the scene update event
  14. Update = 1 << 0,
  15. /// Use the scene post-update event
  16. PostUpdate = 1 << 1,
  17. /// Use the physics update event
  18. FixedUpdate = 1 << 2,
  19. /// Use the physics post-update event
  20. FixedPostUpdate = 1 << 3,
  21. /// Use all events
  22. All = Update | PostUpdate | FixedUpdate | FixedPostUpdate
  23. };
  24. URHO3D_FLAGS(LogicComponentEvents);
  25. /// Helper base class for user-defined game logic components that hooks up to update events and forwards them to virtual functions similar to ScriptInstance class.
  26. class URHO3D_API LogicComponent : public Component
  27. {
  28. URHO3D_OBJECT(LogicComponent, Component);
  29. public:
  30. /// Construct.
  31. explicit LogicComponent(Context* context);
  32. /// Destruct.
  33. ~LogicComponent() override;
  34. /// Handle enabled/disabled state change. Changes update event subscription.
  35. void OnSetEnabled() override;
  36. /// Called when the component is added to a scene node. Other components may not yet exist.
  37. virtual void Start() { }
  38. /// Called before the first update. At this point all other components of the node should exist. Will also be called if update events are not wanted; in that case the event is immediately unsubscribed afterward.
  39. virtual void DelayedStart() { }
  40. /// Called when the component is detached from a scene node, usually on destruction. Note that you will no longer have access to the node and scene at that point.
  41. virtual void Stop() { }
  42. /// Called on scene update, variable timestep.
  43. virtual void Update(float timeStep);
  44. /// Called on scene post-update, variable timestep.
  45. virtual void PostUpdate(float timeStep);
  46. /// Called on physics update, fixed timestep.
  47. virtual void FixedUpdate(float timeStep);
  48. /// Called on physics post-update, fixed timestep.
  49. virtual void FixedPostUpdate(float timeStep);
  50. /// Set what update events should be subscribed to. Use this for optimization: by default all are in use. Note that this is not an attribute and is not saved or network-serialized, therefore it should always be called eg. in the subclass constructor.
  51. void SetUpdateEventMask(LogicComponentEvents mask);
  52. /// Return what update events are subscribed to.
  53. LogicComponentEvents GetUpdateEventMask() const { return updateEventMask_; }
  54. /// Return whether the DelayedStart() function has been called.
  55. bool IsDelayedStartCalled() const { return delayedStartCalled_; }
  56. protected:
  57. /// Handle scene node being assigned at creation.
  58. void OnNodeSet(Node* node) override;
  59. /// Handle scene being assigned.
  60. void OnSceneSet(Scene* scene) override;
  61. private:
  62. /// Subscribe/unsubscribe to update events based on current enabled state and update event mask.
  63. void UpdateEventSubscription();
  64. /// Handle scene update event.
  65. void HandleSceneUpdate(StringHash eventType, VariantMap& eventData);
  66. /// Handle scene post-update event.
  67. void HandleScenePostUpdate(StringHash eventType, VariantMap& eventData);
  68. #if defined(URHO3D_PHYSICS) || defined(URHO3D_PHYSICS2D)
  69. /// Handle physics pre-step event.
  70. void HandlePhysicsPreStep(StringHash eventType, VariantMap& eventData);
  71. /// Handle physics post-step event.
  72. void HandlePhysicsPostStep(StringHash eventType, VariantMap& eventData);
  73. #endif
  74. /// Requested event subscription mask.
  75. LogicComponentEvents updateEventMask_;
  76. /// Current event subscription mask.
  77. LogicComponentEvents currentEventMask_;
  78. /// Flag for delayed start.
  79. bool delayedStartCalled_;
  80. };
  81. }