Mover.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #include <Urho3D/Graphics/AnimatedModel.h>
  4. #include <Urho3D/Graphics/AnimationState.h>
  5. #include <Urho3D/Scene/Scene.h>
  6. #include "Mover.h"
  7. #include <Urho3D/DebugNew.h>
  8. Mover::Mover(Context* context) :
  9. LogicComponent(context),
  10. moveSpeed_(0.0f),
  11. rotationSpeed_(0.0f)
  12. {
  13. // Only the scene update event is needed: unsubscribe from the rest for optimization
  14. SetUpdateEventMask(LogicComponentEvents::Update);
  15. }
  16. void Mover::SetParameters(float moveSpeed, float rotationSpeed, const BoundingBox& bounds)
  17. {
  18. moveSpeed_ = moveSpeed;
  19. rotationSpeed_ = rotationSpeed;
  20. bounds_ = bounds;
  21. }
  22. void Mover::Update(float timeStep)
  23. {
  24. node_->Translate(Vector3::FORWARD * moveSpeed_ * timeStep);
  25. // If in risk of going outside the plane, rotate the model right
  26. Vector3 pos = node_->GetPosition();
  27. if (pos.x_ < bounds_.min_.x_ || pos.x_ > bounds_.max_.x_ || pos.z_ < bounds_.min_.z_ || pos.z_ > bounds_.max_.z_)
  28. node_->Yaw(rotationSpeed_ * timeStep);
  29. // Get the model's first (only) animation state and advance its time. Note the convenience accessor to other components
  30. // in the same scene node
  31. auto* model = node_->GetComponent<AnimatedModel>(true);
  32. if (model->GetNumAnimationStates())
  33. {
  34. AnimationState* state = model->GetAnimationStates()[0];
  35. state->AddTime(timeStep);
  36. }
  37. }