LogicComponent.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. //
  2. // Copyright (c) 2008-2017 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 "../Precompiled.h"
  23. #include "../IO/Log.h"
  24. #if defined(ATOMIC_PHYSICS) || defined(ATOMIC_ATOMIC2D)
  25. #include "../Physics/PhysicsEvents.h"
  26. #endif
  27. #include "../Scene/LogicComponent.h"
  28. #include "../Scene/Scene.h"
  29. #include "../Scene/SceneEvents.h"
  30. namespace Atomic
  31. {
  32. LogicComponent::LogicComponent(Context* context) :
  33. Component(context),
  34. updateEventMask_(USE_UPDATE | USE_POSTUPDATE | USE_FIXEDUPDATE | USE_FIXEDPOSTUPDATE),
  35. currentEventMask_(0),
  36. delayedStartCalled_(false)
  37. {
  38. }
  39. LogicComponent::~LogicComponent()
  40. {
  41. }
  42. void LogicComponent::OnSetEnabled()
  43. {
  44. UpdateEventSubscription();
  45. }
  46. void LogicComponent::Update(float timeStep)
  47. {
  48. }
  49. void LogicComponent::PostUpdate(float timeStep)
  50. {
  51. }
  52. void LogicComponent::FixedUpdate(float timeStep)
  53. {
  54. }
  55. void LogicComponent::FixedPostUpdate(float timeStep)
  56. {
  57. }
  58. void LogicComponent::SetUpdateEventMask(unsigned char mask)
  59. {
  60. if (updateEventMask_ != mask)
  61. {
  62. updateEventMask_ = mask;
  63. UpdateEventSubscription();
  64. }
  65. }
  66. void LogicComponent::OnNodeSet(Node* node)
  67. {
  68. if (node)
  69. {
  70. // Execute the user-defined start function
  71. Start();
  72. }
  73. else
  74. {
  75. // We are being detached from a node: execute user-defined stop function and prepare for destruction
  76. Stop();
  77. }
  78. }
  79. void LogicComponent::OnSceneSet(Scene* scene)
  80. {
  81. if (scene)
  82. UpdateEventSubscription();
  83. else
  84. {
  85. UnsubscribeFromEvent(E_SCENEUPDATE);
  86. UnsubscribeFromEvent(E_SCENEPOSTUPDATE);
  87. #if defined(ATOMIC_PHYSICS) || defined(ATOMIC_ATOMIC2D)
  88. UnsubscribeFromEvent(E_PHYSICSPRESTEP);
  89. UnsubscribeFromEvent(E_PHYSICSPOSTSTEP);
  90. #endif
  91. currentEventMask_ = 0;
  92. }
  93. }
  94. void LogicComponent::UpdateEventSubscription()
  95. {
  96. Scene* scene = GetScene();
  97. if (!scene)
  98. return;
  99. bool enabled = IsEnabledEffective();
  100. bool needUpdate = enabled && ((updateEventMask_ & USE_UPDATE) || !delayedStartCalled_);
  101. if (needUpdate && !(currentEventMask_ & USE_UPDATE))
  102. {
  103. SubscribeToEvent(scene, E_SCENEUPDATE, ATOMIC_HANDLER(LogicComponent, HandleSceneUpdate));
  104. currentEventMask_ |= USE_UPDATE;
  105. }
  106. else if (!needUpdate && (currentEventMask_ & USE_UPDATE))
  107. {
  108. UnsubscribeFromEvent(scene, E_SCENEUPDATE);
  109. currentEventMask_ &= ~USE_UPDATE;
  110. }
  111. bool needPostUpdate = enabled && (updateEventMask_ & USE_POSTUPDATE);
  112. if (needPostUpdate && !(currentEventMask_ & USE_POSTUPDATE))
  113. {
  114. SubscribeToEvent(scene, E_SCENEPOSTUPDATE, ATOMIC_HANDLER(LogicComponent, HandleScenePostUpdate));
  115. currentEventMask_ |= USE_POSTUPDATE;
  116. }
  117. else if (!needPostUpdate && (currentEventMask_ & USE_POSTUPDATE))
  118. {
  119. UnsubscribeFromEvent(scene, E_SCENEPOSTUPDATE);
  120. currentEventMask_ &= ~USE_POSTUPDATE;
  121. }
  122. #if defined(ATOMIC_PHYSICS) || defined(ATOMIC_ATOMIC2D)
  123. Component* world = GetFixedUpdateSource();
  124. if (!world)
  125. return;
  126. bool needFixedUpdate = enabled && (updateEventMask_ & USE_FIXEDUPDATE);
  127. if (needFixedUpdate && !(currentEventMask_ & USE_FIXEDUPDATE))
  128. {
  129. SubscribeToEvent(world, E_PHYSICSPRESTEP, ATOMIC_HANDLER(LogicComponent, HandlePhysicsPreStep));
  130. currentEventMask_ |= USE_FIXEDUPDATE;
  131. }
  132. else if (!needFixedUpdate && (currentEventMask_ & USE_FIXEDUPDATE))
  133. {
  134. UnsubscribeFromEvent(world, E_PHYSICSPRESTEP);
  135. currentEventMask_ &= ~USE_FIXEDUPDATE;
  136. }
  137. bool needFixedPostUpdate = enabled && (updateEventMask_ & USE_FIXEDPOSTUPDATE);
  138. if (needFixedPostUpdate && !(currentEventMask_ & USE_FIXEDPOSTUPDATE))
  139. {
  140. SubscribeToEvent(world, E_PHYSICSPOSTSTEP, ATOMIC_HANDLER(LogicComponent, HandlePhysicsPostStep));
  141. currentEventMask_ |= USE_FIXEDPOSTUPDATE;
  142. }
  143. else if (!needFixedPostUpdate && (currentEventMask_ & USE_FIXEDPOSTUPDATE))
  144. {
  145. UnsubscribeFromEvent(world, E_PHYSICSPOSTSTEP);
  146. currentEventMask_ &= ~USE_FIXEDPOSTUPDATE;
  147. }
  148. #endif
  149. }
  150. void LogicComponent::HandleSceneUpdate(StringHash eventType, VariantMap& eventData)
  151. {
  152. using namespace SceneUpdate;
  153. // Execute user-defined delayed start function before first update
  154. if (!delayedStartCalled_)
  155. {
  156. DelayedStart();
  157. delayedStartCalled_ = true;
  158. // If did not need actual update events, unsubscribe now
  159. if (!(updateEventMask_ & USE_UPDATE))
  160. {
  161. UnsubscribeFromEvent(GetScene(), E_SCENEUPDATE);
  162. currentEventMask_ &= ~USE_UPDATE;
  163. return;
  164. }
  165. }
  166. // Then execute user-defined update function
  167. Update(eventData[P_TIMESTEP].GetFloat());
  168. }
  169. void LogicComponent::HandleScenePostUpdate(StringHash eventType, VariantMap& eventData)
  170. {
  171. using namespace ScenePostUpdate;
  172. // Execute user-defined post-update function
  173. PostUpdate(eventData[P_TIMESTEP].GetFloat());
  174. }
  175. #if defined(ATOMIC_PHYSICS) || defined(ATOMIC_ATOMIC2D)
  176. void LogicComponent::HandlePhysicsPreStep(StringHash eventType, VariantMap& eventData)
  177. {
  178. using namespace PhysicsPreStep;
  179. // Execute user-defined delayed start function before first fixed update if not called yet
  180. if (!delayedStartCalled_)
  181. {
  182. DelayedStart();
  183. delayedStartCalled_ = true;
  184. }
  185. // Execute user-defined fixed update function
  186. FixedUpdate(eventData[P_TIMESTEP].GetFloat());
  187. }
  188. void LogicComponent::HandlePhysicsPostStep(StringHash eventType, VariantMap& eventData)
  189. {
  190. using namespace PhysicsPostStep;
  191. // Execute user-defined fixed post-update function
  192. FixedPostUpdate(eventData[P_TIMESTEP].GetFloat());
  193. }
  194. #endif
  195. }