diff options
author | Aki <please@ignore.pl> | 2022-12-31 14:27:11 +0100 |
---|---|---|
committer | Aki <please@ignore.pl> | 2022-12-31 15:03:09 +0100 |
commit | d90ab6b90f2507970c2da52b166d26a7c9b48d66 (patch) | |
tree | 42c23c2665dde54a893d8eccbfb0fe1df67f30d3 /sim/src | |
parent | 8cf68dfc5725e4a92a87390dcba2b9d7e5ef4e84 (diff) | |
download | kurator-d90ab6b90f2507970c2da52b166d26a7c9b48d66.zip kurator-d90ab6b90f2507970c2da52b166d26a7c9b48d66.tar.gz kurator-d90ab6b90f2507970c2da52b166d26a7c9b48d66.tar.bz2 |
Extracted FloatingMovement component to own header and impl
Diffstat (limited to 'sim/src')
-rw-r--r-- | sim/src/BaseBattle.cpp | 26 | ||||
-rw-r--r-- | sim/src/BaseBattle.h | 1 | ||||
-rw-r--r-- | sim/src/Builder.cpp | 4 | ||||
-rw-r--r-- | sim/src/FloatingMovement.cpp | 39 |
4 files changed, 43 insertions, 27 deletions
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 <kurator/campaign/UniqueIdentifier.h> #include <kurator/sim/components.h> #include <kurator/sim/events.h> +#include <kurator/sim/FloatingMovement.h> #include <kurator/stats/events.h> #include <kurator/universe.h> @@ -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<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(); - } -} - - -void BaseBattle::turrets(const float dt) { auto view = _registry.view<TurretControl, universe::TurretType>(); 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 <entt/entity/registry.hpp> #include <kurator/sim/components.h> +#include <kurator/sim/FloatingMovement.h> #include <kurator/sim/Point.h> #include <kurator/universe/ShipType.h> #include <kurator/universe/TurretType.h> @@ -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<AIState>(entity, 6000.0, Point{0.0, 0.0}); registry.emplace<HitPoints>(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 <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 |