summaryrefslogtreecommitdiff
path: root/sim/src/FloatingMovement.cpp
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2022-12-31 14:27:11 +0100
committerAki <please@ignore.pl>2022-12-31 15:03:09 +0100
commitd90ab6b90f2507970c2da52b166d26a7c9b48d66 (patch)
tree42c23c2665dde54a893d8eccbfb0fe1df67f30d3 /sim/src/FloatingMovement.cpp
parent8cf68dfc5725e4a92a87390dcba2b9d7e5ef4e84 (diff)
downloadkurator-d90ab6b90f2507970c2da52b166d26a7c9b48d66.zip
kurator-d90ab6b90f2507970c2da52b166d26a7c9b48d66.tar.gz
kurator-d90ab6b90f2507970c2da52b166d26a7c9b48d66.tar.bz2
Extracted FloatingMovement component to own header and impl
Diffstat (limited to 'sim/src/FloatingMovement.cpp')
-rw-r--r--sim/src/FloatingMovement.cpp39
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