diff options
Diffstat (limited to 'sim/src/FloatingMovement.cpp')
-rw-r--r-- | sim/src/FloatingMovement.cpp | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/sim/src/FloatingMovement.cpp b/sim/src/FloatingMovement.cpp new file mode 100644 index 0000000..e1794d5 --- /dev/null +++ b/sim/src/FloatingMovement.cpp @@ -0,0 +1,39 @@ +#include <kurator/sim/FloatingMovement.h> + +#include <entt/entity/registry.hpp> +#include <entt/signal/dispatcher.hpp> + +#include <kurator/sim/components.h> + + +namespace kurator +{ +namespace sim +{ + + +void +FloatingMovement::update(entt::registry& registry, entt::dispatcher&, const float dt) +{ + auto view = registry.view<Transform, FloatingMovement, AIState>(); + for (auto&& [entity, transform, movement, ai] : view.each()) { + const auto offset = ai.destination - transform.position; + const auto at_destination = offset.magnitude() > movement.destination_boundary; + const auto acceleration = + at_destination ? + offset.normalized().scale(movement.acceleration * dt) : + offset.normalized().scale(-1 * movement.deceleration * dt); + movement.speed.x += acceleration.x; + movement.speed.y += acceleration.y; + if (movement.speed.magnitude() > movement.max_speed) + movement.speed = movement.speed.normalized().scale(movement.max_speed); + const auto speed = movement.speed.scale(dt); + transform.position.x += speed.x; + transform.position.y += speed.y; + transform.angle = speed.angle(); + } +} + + +} // namespace sim +} // namespace kurator |