LogicComponent.cpp 5.3 KB

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