LogicComponent.cpp 6.3 KB

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