summaryrefslogtreecommitdiff
path: root/battles/src/BaseBattle.cpp
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2022-11-14 00:32:08 +0100
committerAki <please@ignore.pl>2022-11-14 00:32:08 +0100
commit4f3de714cd04ee5d99167f415843573d6bbd9f62 (patch)
tree73d9b37221c8b647bf6c625aaddc0f8cb8ae475c /battles/src/BaseBattle.cpp
parent39ae4f2fd4c382f25ab98c6e98816e3e3a6f4e22 (diff)
downloadkurator-4f3de714cd04ee5d99167f415843573d6bbd9f62.zip
kurator-4f3de714cd04ee5d99167f415843573d6bbd9f62.tar.gz
kurator-4f3de714cd04ee5d99167f415843573d6bbd9f62.tar.bz2
Implemented naive turrets and targeting mechanics
Coupling between components, implementation of systems and behaviour of some state-related methods should be refactored, but the overall look is nice.
Diffstat (limited to 'battles/src/BaseBattle.cpp')
-rw-r--r--battles/src/BaseBattle.cpp26
1 files changed, 25 insertions, 1 deletions
diff --git a/battles/src/BaseBattle.cpp b/battles/src/BaseBattle.cpp
index 4d80671..815c683 100644
--- a/battles/src/BaseBattle.cpp
+++ b/battles/src/BaseBattle.cpp
@@ -31,6 +31,12 @@ BaseBattle::BaseBattle(const Scenario& scenario) :
_registry.emplace<FloatingMovement>(entity, 0.4);
_registry.emplace<AIState>(entity, Point{0.0, 0.0});
_registry.emplace<HitPoints>(entity, ship.type.base_health_points);
+ for (const auto& turret_def : ship.turrets) {
+ const auto turret = _registry.create();
+ _registry.emplace<universe::TurretType>(turret, turret_def);
+ _registry.emplace<TurretControl>(turret, 0.0, entity);
+ _registry.emplace<Transform>(turret, Point{0.0, 0.0}, 0.0, entity);
+ }
manager.add(ship.team, entity); // registry supports on construction events
}
}
@@ -50,6 +56,8 @@ BaseBattle::update(const float dt)
for (auto&& [entity, team, transform, state, movement] : view.each()) {
if (!_registry.valid(state.target))
state.target = manager.random((team.id + 1) % 2);
+ if (!_registry.valid(state.target))
+ continue;
const auto target = _registry.get<Transform>(state.target);
const double speed = movement.speed * dt;
const Point diff = target.position - transform.position;
@@ -63,10 +71,26 @@ BaseBattle::update(const float dt)
}
auto view2 = _registry.view<HitPoints>();
for (auto&& [entity, points] : view2.each()) {
- points.health -= dt;
if (points.health <= 0.0)
_registry.destroy(entity);
}
+ auto view3 = _registry.view<TurretControl, universe::TurretType>();
+ for (auto&& [entity, control, def] : view3.each()) { // split into systems!
+ if (!_registry.valid(control.owner)) {
+ _registry.destroy(entity);
+ continue;
+ }
+ const auto& state = _registry.get<AIState>(control.owner); // no checks
+ if (!_registry.valid(state.target))
+ continue;
+ if (control.reload > 0.0)
+ control.reload -= dt;
+ if (control.reload <= 0.0) {
+ auto& target_points = _registry.get<HitPoints>(state.target);
+ target_points.health -= def.base_damage;
+ control.reload = def.rate_of_fire;
+ }
+ }
manager.clear(_registry); // registry supports on destructions events
}