summaryrefslogtreecommitdiff
path: root/battles
diff options
context:
space:
mode:
Diffstat (limited to 'battles')
-rw-r--r--battles/include/kurator/battles/components.h6
-rw-r--r--battles/src/BaseBattle.cpp22
-rw-r--r--battles/src/Builder.cpp2
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<Transform, FloatingMovement, AIState>();
- 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<universe::ShipType>(entity, ship_type);
registry.emplace<Team>(entity, team);
registry.emplace<Transform>(entity, spawner.get(team));
- registry.emplace<FloatingMovement>(entity, 0.4);
+ registry.emplace<FloatingMovement>(entity, 0.5, 2.0, 3.0, 0.1);
registry.emplace<AIState>(entity, Point{0.0, 0.0});
registry.emplace<HitPoints>(entity, ship_type.base_health_points);
return entity;