Benchmark02_WomanMover.cpp 1.5 KB

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