summaryrefslogtreecommitdiff
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
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.
-rw-r--r--battles/include/kurator/battles/components.h7
-rw-r--r--battles/src/BaseBattle.cpp26
-rw-r--r--battles/src/TeamManager.cpp2
-rw-r--r--battles/src/scenarios.cpp35
4 files changed, 52 insertions, 18 deletions
diff --git a/battles/include/kurator/battles/components.h b/battles/include/kurator/battles/components.h
index 35a9251..8f5813b 100644
--- a/battles/include/kurator/battles/components.h
+++ b/battles/include/kurator/battles/components.h
@@ -44,5 +44,12 @@ struct HitPoints
};
+struct TurretControl
+{
+ double reload;
+ entt::entity owner;
+};
+
+
} // namespace battles
} // namespace kurator
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
}
diff --git a/battles/src/TeamManager.cpp b/battles/src/TeamManager.cpp
index e4afeb3..9dc90e3 100644
--- a/battles/src/TeamManager.cpp
+++ b/battles/src/TeamManager.cpp
@@ -41,6 +41,8 @@ entt::entity
TeamManager::random(int team)
{
auto& members = teams.at(team);
+ if (members.size() == 0)
+ return entt::null;
std::uniform_int_distribution<Team::size_type> uniform{0, members.size() - 1};
return members.at(uniform(generator));
}
diff --git a/battles/src/scenarios.cpp b/battles/src/scenarios.cpp
index cf348ca..3b35f44 100644
--- a/battles/src/scenarios.cpp
+++ b/battles/src/scenarios.cpp
@@ -17,26 +17,27 @@ example()
{
const universe::ShipType halo {"halo", 6.0};
const universe::ShipType cube {"cube", 12.0};
- const universe::ShipType bell {"bell", 20.0};
+ const universe::ShipType bell {"bell", 30.0};
+ const universe::TurretType cannon {"cannon", 1.0, 1.0, 0.5};
return {
"example",
{
- {0, halo, {}},
- {0, halo, {}},
- {0, cube, {}},
- {0, cube, {}},
- {0, cube, {}},
- {0, bell, {}},
- {0, bell, {}},
- {0, bell, {}},
- {1, halo, {}},
- {1, halo, {}},
- {1, bell, {}},
- {1, cube, {}},
- {1, cube, {}},
- {1, cube, {}},
- {1, bell, {}},
- {1, bell, {}},
+ {0, halo, {cannon}},
+ {0, halo, {cannon}},
+ {0, cube, {cannon, cannon}},
+ {0, cube, {cannon, cannon}},
+ {0, cube, {cannon, cannon}},
+ {0, bell, {cannon}},
+ {0, bell, {cannon}},
+ {0, bell, {cannon}},
+ {1, halo, {cannon}},
+ {1, halo, {cannon}},
+ {1, bell, {cannon}},
+ {1, cube, {cannon, cannon}},
+ {1, cube, {cannon, cannon}},
+ {1, cube, {cannon, cannon}},
+ {1, bell, {cannon}},
+ {1, bell, {cannon}},
},
};
}