LogicComponent.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //
  2. // Copyright (c) 2008-2014 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Component.h"
  23. namespace Urho3D
  24. {
  25. /// Bitmask for using the scene update event.
  26. static const unsigned char USE_UPDATE = 0x1;
  27. /// Bitmask for using the scene post-update event.
  28. static const unsigned char USE_POSTUPDATE = 0x2;
  29. /// Bitmask for using the physics update event.
  30. static const unsigned char USE_FIXEDUPDATE = 0x4;
  31. /// Bitmask for using the physics post-update event.
  32. static const unsigned char USE_FIXEDPOSTUPDATE = 0x8;
  33. /// 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.
  34. class URHO3D_API LogicComponent : public Component
  35. {
  36. OBJECT(LogicComponent);
  37. /// Construct.
  38. LogicComponent(Context* context);
  39. /// Destruct.
  40. virtual ~LogicComponent();
  41. /// Handle enabled/disabled state change. Changes update event subscription.
  42. virtual void OnSetEnabled();
  43. /// Called when the component is added to a scene node. Other components may not yet exist.
  44. virtual void Start() {}
  45. /// 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.
  46. virtual void DelayedStart() {}
  47. /// Called when the component is detached from a scene node, usually on destruction.
  48. virtual void Stop() {}
  49. /// Called on scene update, variable timestep.
  50. virtual void Update(float timeStep) {}
  51. /// Called on scene post-update, variable timestep.
  52. virtual void PostUpdate(float timeStep) {}
  53. /// Called on physics update, fixed timestep.
  54. virtual void FixedUpdate(float timeStep) {}
  55. /// Called on physics post-update, fixed timestep.
  56. virtual void FixedPostUpdate(float timeStep) {}
  57. /// 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.
  58. void SetUpdateEventMask(unsigned char mask);
  59. /// Return what update events are subscribed to.
  60. unsigned char GetUpdateEventMask() const { return updateEventMask_; }
  61. /// Return whether the DelayedStart() function has been called.
  62. bool IsDelayedStartCalled() const { return delayedStartCalled_; }
  63. protected:
  64. /// Handle scene node being assigned at creation.
  65. virtual void OnNodeSet(Node* node);
  66. private:
  67. /// Subscribe/unsubscribe to update events based on current enabled state and update event mask.
  68. void UpdateEventSubscription();
  69. /// Handle scene update event.
  70. void HandleSceneUpdate(StringHash eventType, VariantMap& eventData);
  71. /// Handle scene post-update event.
  72. void HandleScenePostUpdate(StringHash eventType, VariantMap& eventData);
  73. /// Handle physics pre-step event.
  74. void HandlePhysicsPreStep(StringHash eventType, VariantMap& eventData);
  75. /// Handle physics post-step event.
  76. void HandlePhysicsPostStep(StringHash eventType, VariantMap& eventData);
  77. /// Requested event subscription mask.
  78. unsigned char updateEventMask_;
  79. /// Current event subscription mask.
  80. unsigned char currentEventMask_;
  81. /// Flag for delayed start.
  82. bool delayedStartCalled_;
  83. };
  84. }