CSComponent.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //
  2. // Copyright (c) 2008-2014 the Urho3D project.
  3. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include <Atomic/Scene/Component.h>
  25. namespace Atomic
  26. {
  27. enum CSComponentMethod
  28. {
  29. CSComponentMethod_Start,
  30. CSComponentMethod_DelayedStart,
  31. CSComponentMethod_Update,
  32. CSComponentMethod_PostUpdate,
  33. CSComponentMethod_FixedUpdate,
  34. CSComponentMethod_PostFixedUpdate
  35. };
  36. /// 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.
  37. class ATOMIC_API CSComponent : public Component
  38. {
  39. friend class CSComponentFactory;
  40. enum EventFlags
  41. {
  42. USE_UPDATE = 0x1,
  43. USE_POSTUPDATE = 0x2,
  44. USE_FIXEDUPDATE = 0x4,
  45. USE_FIXEDPOSTUPDATE = 0x8
  46. };
  47. public:
  48. OBJECT(CSComponent);
  49. /// Construct.
  50. CSComponent(Context* context);
  51. /// Destruct.
  52. virtual ~CSComponent();
  53. /// Register object factory.
  54. static void RegisterObject(Context* context);
  55. bool Load(Deserializer& source, bool setInstanceDefault);
  56. bool LoadXML(const XMLElement& source, bool setInstanceDefault);
  57. void ApplyAttributes();
  58. /// Handle enabled/disabled state change. Changes update event subscription.
  59. virtual void OnSetEnabled();
  60. /// 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.
  61. void SetUpdateEventMask(unsigned char mask);
  62. /// Return what update events are subscribed to.
  63. unsigned char GetUpdateEventMask() const { return updateEventMask_; }
  64. /// Return whether the DelayedStart() function has been called.
  65. bool IsDelayedStartCalled() const { return delayedStartCalled_; }
  66. void SetDestroyed() { destroyed_ = true; }
  67. void SetManagedID(unsigned id) { managedID_ = id; }
  68. protected:
  69. /// Handle scene node being assigned at creation.
  70. virtual void OnNodeSet(Node* node);
  71. /// Handle scene being assigned.
  72. virtual void OnSceneSet(Scene* scene);
  73. private:
  74. /// Subscribe/unsubscribe to update events based on current enabled state and update event mask.
  75. void UpdateEventSubscription();
  76. /// Handle scene update event.
  77. void HandleSceneUpdate(StringHash eventType, VariantMap& eventData);
  78. /// Handle scene post-update event.
  79. void HandleScenePostUpdate(StringHash eventType, VariantMap& eventData);
  80. #ifdef ATOMIC_PHYSICS
  81. /// Handle physics pre-step event.
  82. void HandlePhysicsPreStep(StringHash eventType, VariantMap& eventData);
  83. /// Handle physics post-step event.
  84. void HandlePhysicsPostStep(StringHash eventType, VariantMap& eventData);
  85. #endif
  86. void CallScriptMethod(CSComponentMethod method, float value = 0.0f);
  87. /// Called when the component is added to a scene node. Other components may not yet exist.
  88. virtual void Start();
  89. /// 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.
  90. virtual void DelayedStart();
  91. /// 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.
  92. virtual void Stop() {}
  93. /// Called on scene update, variable timestep.
  94. virtual void Update(float timeStep);
  95. /// Called on scene post-update, variable timestep.
  96. virtual void PostUpdate(float timeStep);
  97. /// Called on physics update, fixed timestep.
  98. virtual void FixedUpdate(float timeStep);
  99. /// Called on physics post-update, fixed timestep.
  100. virtual void FixedPostUpdate(float timeStep);
  101. /// Requested event subscription mask.
  102. unsigned char updateEventMask_;
  103. /// Current event subscription mask.
  104. unsigned char currentEventMask_;
  105. bool instanceInitialized_;
  106. bool started_;
  107. bool destroyed_;
  108. bool scriptClassInstance_;
  109. /// Flag for delayed start.
  110. bool delayedStartCalled_;
  111. bool loading_;
  112. unsigned managedID_;
  113. };
  114. }