From d90ab6b90f2507970c2da52b166d26a7c9b48d66 Mon Sep 17 00:00:00 2001 From: Aki Date: Sat, 31 Dec 2022 14:27:11 +0100 Subject: Extracted FloatingMovement component to own header and impl --- sim/CMakeLists.txt | 1 + sim/include/kurator/sim/FloatingMovement.h | 27 +++++++++++++++++++++ sim/include/kurator/sim/components.h | 10 -------- sim/src/BaseBattle.cpp | 26 ++------------------ sim/src/BaseBattle.h | 1 - sim/src/Builder.cpp | 4 +-- sim/src/FloatingMovement.cpp | 39 ++++++++++++++++++++++++++++++ 7 files changed, 71 insertions(+), 37 deletions(-) create mode 100644 sim/include/kurator/sim/FloatingMovement.h create mode 100644 sim/src/FloatingMovement.cpp (limited to 'sim') diff --git a/sim/CMakeLists.txt b/sim/CMakeLists.txt index f3d5494..ff2621c 100644 --- a/sim/CMakeLists.txt +++ b/sim/CMakeLists.txt @@ -4,6 +4,7 @@ add_library( src/BaseBattle.cpp src/Battle.cpp src/Builder.cpp + src/FloatingMovement.cpp src/Point.cpp src/RandomSpawner.cpp src/TeamManager.cpp diff --git a/sim/include/kurator/sim/FloatingMovement.h b/sim/include/kurator/sim/FloatingMovement.h new file mode 100644 index 0000000..352377d --- /dev/null +++ b/sim/include/kurator/sim/FloatingMovement.h @@ -0,0 +1,27 @@ +#pragma once + +#include +#include + +#include "Point.h" + + +namespace kurator +{ +namespace sim +{ + + +struct FloatingMovement +{ + double max_speed; + double acceleration; + double deceleration; + double destination_boundary = 100.0; + Point speed = {0.0, 0.0}; + static void update(entt::registry& registry, entt::dispatcher& dispatcher, float dt); +}; + + +} // namespace sim +} // namespace kurator diff --git a/sim/include/kurator/sim/components.h b/sim/include/kurator/sim/components.h index 6fbab55..47ff5b2 100644 --- a/sim/include/kurator/sim/components.h +++ b/sim/include/kurator/sim/components.h @@ -33,16 +33,6 @@ struct AIState }; -struct FloatingMovement -{ - double max_speed; - double acceleration; - double deceleration; - double destination_boundary; - Point speed = {0.0, 0.0}; -}; - - struct HitPoints { double health; diff --git a/sim/src/BaseBattle.cpp b/sim/src/BaseBattle.cpp index 4b65139..99c1b99 100644 --- a/sim/src/BaseBattle.cpp +++ b/sim/src/BaseBattle.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -56,7 +57,7 @@ BaseBattle::update(const float dt) time += dt; pick_random_targets(); keep_at_range(); - floating_movement(dt); + FloatingMovement::update(_registry, _dispatcher, dt); turrets(dt); kill_off_dead(); manager.clear(_registry); // registry supports on destructions events @@ -90,29 +91,6 @@ BaseBattle::keep_at_range() void -BaseBattle::floating_movement(const float dt) -{ - auto view = _registry.view(); - 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(); - } -} - - -void BaseBattle::turrets(const float dt) { auto view = _registry.view(); diff --git a/sim/src/BaseBattle.h b/sim/src/BaseBattle.h index d2f7d19..3c19b9c 100644 --- a/sim/src/BaseBattle.h +++ b/sim/src/BaseBattle.h @@ -31,7 +31,6 @@ private: TeamManager manager; void pick_random_targets(); void keep_at_range(); - void floating_movement(float dt); void turrets(float dt); void kill_off_dead(); }; diff --git a/sim/src/Builder.cpp b/sim/src/Builder.cpp index a5319fd..e0198a1 100644 --- a/sim/src/Builder.cpp +++ b/sim/src/Builder.cpp @@ -3,6 +3,7 @@ #include #include +#include #include #include #include @@ -34,8 +35,7 @@ Builder::operator()(const universe::ShipType& ship_type, const int team) const entity, ship_type.max_speed, ship_type.max_speed * 2.0, - ship_type.max_speed * 3.0, - 100.0); + ship_type.max_speed * 3.0); registry.emplace(entity, 6000.0, Point{0.0, 0.0}); registry.emplace(entity, ship_type.base_health_points); return entity; 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 + +#include +#include + +#include + + +namespace kurator +{ +namespace sim +{ + + +void +FloatingMovement::update(entt::registry& registry, entt::dispatcher&, const float dt) +{ + auto view = registry.view(); + 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 -- cgit v1.1