| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- //
- // Copyright (c) 2008-2015 the Urho3D project.
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- #include "../Precompiled.h"
- #include "../IO/Log.h"
- #ifdef URHO3D_PHYSICS
- #include "../Physics/PhysicsEvents.h"
- #include "../Physics/PhysicsWorld.h"
- #endif
- #include "../Scene/LogicComponent.h"
- #include "../Scene/Scene.h"
- #include "../Scene/SceneEvents.h"
- namespace Urho3D
- {
- LogicComponent::LogicComponent(Context* context) :
- Component(context),
- updateEventMask_(USE_UPDATE | USE_POSTUPDATE | USE_FIXEDUPDATE | USE_FIXEDPOSTUPDATE),
- currentEventMask_(0),
- delayedStartCalled_(false)
- {
- }
- LogicComponent::~LogicComponent()
- {
- }
- void LogicComponent::OnSetEnabled()
- {
- UpdateEventSubscription();
- }
- void LogicComponent::Update(float timeStep)
- {
- }
- void LogicComponent::PostUpdate(float timeStep)
- {
- }
- void LogicComponent::FixedUpdate(float timeStep)
- {
- }
- void LogicComponent::FixedPostUpdate(float timeStep)
- {
- }
- void LogicComponent::SetUpdateEventMask(unsigned char mask)
- {
- if (updateEventMask_ != mask)
- {
- updateEventMask_ = mask;
- UpdateEventSubscription();
- }
- }
- void LogicComponent::OnNodeSet(Node* node)
- {
- if (node)
- {
- // Execute the user-defined start function
- Start();
- }
- else
- {
- // We are being detached from a node: execute user-defined stop function and prepare for destruction
- Stop();
- }
- }
- void LogicComponent::OnSceneSet(Scene* scene)
- {
- if (scene)
- UpdateEventSubscription();
- else
- {
- UnsubscribeFromEvent(E_SCENEUPDATE);
- UnsubscribeFromEvent(E_SCENEPOSTUPDATE);
- #ifdef URHO3D_PHYSICS
- UnsubscribeFromEvent(E_PHYSICSPRESTEP);
- UnsubscribeFromEvent(E_PHYSICSPOSTSTEP);
- #endif
- currentEventMask_ = 0;
- }
- }
- void LogicComponent::UpdateEventSubscription()
- {
- Scene* scene = GetScene();
- if (!scene)
- return;
- bool enabled = IsEnabledEffective();
- bool needUpdate = enabled && ((updateEventMask_ & USE_UPDATE) || !delayedStartCalled_);
- if (needUpdate && !(currentEventMask_ & USE_UPDATE))
- {
- SubscribeToEvent(scene, E_SCENEUPDATE, URHO3D_HANDLER(LogicComponent, HandleSceneUpdate));
- currentEventMask_ |= USE_UPDATE;
- }
- else if (!needUpdate && (currentEventMask_ & USE_UPDATE))
- {
- UnsubscribeFromEvent(scene, E_SCENEUPDATE);
- currentEventMask_ &= ~USE_UPDATE;
- }
- bool needPostUpdate = enabled && (updateEventMask_ & USE_POSTUPDATE);
- if (needPostUpdate && !(currentEventMask_ & USE_POSTUPDATE))
- {
- SubscribeToEvent(scene, E_SCENEPOSTUPDATE, URHO3D_HANDLER(LogicComponent, HandleScenePostUpdate));
- currentEventMask_ |= USE_POSTUPDATE;
- }
- else if (!needUpdate && (currentEventMask_ & USE_POSTUPDATE))
- {
- UnsubscribeFromEvent(scene, E_SCENEPOSTUPDATE);
- currentEventMask_ &= ~USE_POSTUPDATE;
- }
- #ifdef URHO3D_PHYSICS
- PhysicsWorld* world = scene->GetComponent<PhysicsWorld>();
- if (!world)
- return;
- bool needFixedUpdate = enabled && (updateEventMask_ & USE_FIXEDUPDATE);
- if (needFixedUpdate && !(currentEventMask_ & USE_FIXEDUPDATE))
- {
- SubscribeToEvent(world, E_PHYSICSPRESTEP, URHO3D_HANDLER(LogicComponent, HandlePhysicsPreStep));
- currentEventMask_ |= USE_FIXEDUPDATE;
- }
- else if (!needFixedUpdate && (currentEventMask_ & USE_FIXEDUPDATE))
- {
- UnsubscribeFromEvent(world, E_PHYSICSPRESTEP);
- currentEventMask_ &= ~USE_FIXEDUPDATE;
- }
- bool needFixedPostUpdate = enabled && (updateEventMask_ & USE_FIXEDPOSTUPDATE);
- if (needFixedPostUpdate && !(currentEventMask_ & USE_FIXEDPOSTUPDATE))
- {
- SubscribeToEvent(world, E_PHYSICSPOSTSTEP, URHO3D_HANDLER(LogicComponent, HandlePhysicsPostStep));
- currentEventMask_ |= USE_FIXEDPOSTUPDATE;
- }
- else if (!needFixedPostUpdate && (currentEventMask_ & USE_FIXEDPOSTUPDATE))
- {
- UnsubscribeFromEvent(world, E_PHYSICSPOSTSTEP);
- currentEventMask_ &= ~USE_FIXEDPOSTUPDATE;
- }
- #endif
- }
- void LogicComponent::HandleSceneUpdate(StringHash eventType, VariantMap& eventData)
- {
- using namespace SceneUpdate;
- // Execute user-defined delayed start function before first update
- if (!delayedStartCalled_)
- {
- DelayedStart();
- delayedStartCalled_ = true;
- // If did not need actual update events, unsubscribe now
- if (!(updateEventMask_ & USE_UPDATE))
- {
- UnsubscribeFromEvent(GetScene(), E_SCENEUPDATE);
- currentEventMask_ &= ~USE_UPDATE;
- return;
- }
- }
- // Then execute user-defined update function
- Update(eventData[P_TIMESTEP].GetFloat());
- }
- void LogicComponent::HandleScenePostUpdate(StringHash eventType, VariantMap& eventData)
- {
- using namespace ScenePostUpdate;
- // Execute user-defined post-update function
- PostUpdate(eventData[P_TIMESTEP].GetFloat());
- }
- #ifdef URHO3D_PHYSICS
- void LogicComponent::HandlePhysicsPreStep(StringHash eventType, VariantMap& eventData)
- {
- using namespace PhysicsPreStep;
- // Execute user-defined fixed update function
- FixedUpdate(eventData[P_TIMESTEP].GetFloat());
- }
- void LogicComponent::HandlePhysicsPostStep(StringHash eventType, VariantMap& eventData)
- {
- using namespace PhysicsPostStep;
- // Execute user-defined fixed post-update function
- FixedPostUpdate(eventData[P_TIMESTEP].GetFloat());
- }
- #endif
- }
|