From 98982513f97f449fd200132d0f19130ab31e2c36 Mon Sep 17 00:00:00 2001 From: Aki Date: Sat, 19 Nov 2022 13:26:42 +0100 Subject: Implemented funky floating movement It's far from complete but it's hilarious and deserves to be a commit --- battles/include/kurator/battles/components.h | 6 +++++- battles/src/BaseBattle.cpp | 22 ++++++++++++++-------- battles/src/Builder.cpp | 2 +- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/battles/include/kurator/battles/components.h b/battles/include/kurator/battles/components.h index 8f5813b..d4363c4 100644 --- a/battles/include/kurator/battles/components.h +++ b/battles/include/kurator/battles/components.h @@ -34,7 +34,11 @@ struct AIState struct FloatingMovement { - double speed; // linear and instant angular for now + double max_speed; + double acceleration; + double deceleration; + double destination_boundary; + Point speed = {0.0, 0.0}; }; diff --git a/battles/src/BaseBattle.cpp b/battles/src/BaseBattle.cpp index 98af764..e102d79 100644 --- a/battles/src/BaseBattle.cpp +++ b/battles/src/BaseBattle.cpp @@ -87,14 +87,20 @@ void BaseBattle::floating_movement(const float dt) { auto view = _registry.view(); - for (auto&& [entity, self, movement, ai] : view.each()) { - const double speed = movement.speed * dt; - const Point offset = ai.destination - self.position; - if (offset.magnitude() > speed) { - const Point move = offset.normalized().scale(speed); - self.position.x += move.x; - self.position.y += move.y; - } + 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; } } diff --git a/battles/src/Builder.cpp b/battles/src/Builder.cpp index 228e893..4ead634 100644 --- a/battles/src/Builder.cpp +++ b/battles/src/Builder.cpp @@ -30,7 +30,7 @@ Builder::operator()(const universe::ShipType& ship_type, const int team) const registry.emplace(entity, ship_type); registry.emplace(entity, team); registry.emplace(entity, spawner.get(team)); - registry.emplace(entity, 0.4); + registry.emplace(entity, 0.5, 2.0, 3.0, 0.1); registry.emplace(entity, Point{0.0, 0.0}); registry.emplace(entity, ship_type.base_health_points); return entity; -- cgit v1.1