LogicComponent.cpp 5.9 KB

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