diff options
author | Aki <please@ignore.pl> | 2022-12-24 01:56:28 +0100 |
---|---|---|
committer | Aki <please@ignore.pl> | 2022-12-24 01:56:28 +0100 |
commit | e732f10d32f96837f5787ab946bdbe6ba6bf6f2a (patch) | |
tree | 672ca21f5b4a419a9f290dfb212728e9cf1e57ca | |
parent | 84e0e1e45f908f93d0a6a60632e0ff177addfb77 (diff) | |
download | kurator-e732f10d32f96837f5787ab946bdbe6ba6bf6f2a.zip kurator-e732f10d32f96837f5787ab946bdbe6ba6bf6f2a.tar.gz kurator-e732f10d32f96837f5787ab946bdbe6ba6bf6f2a.tar.bz2 |
Added end event to mark end of a battle
-rw-r--r-- | kurator/src/Battle.cpp | 9 | ||||
-rw-r--r-- | kurator/src/Battle.h | 1 | ||||
-rw-r--r-- | sim/include/kurator/sim/events.h | 5 | ||||
-rw-r--r-- | sim/src/BaseBattle.cpp | 1 | ||||
-rw-r--r-- | sim/src/TeamManager.cpp | 12 | ||||
-rw-r--r-- | sim/src/TeamManager.h | 2 |
6 files changed, 30 insertions, 0 deletions
diff --git a/kurator/src/Battle.cpp b/kurator/src/Battle.cpp index d5390ae..920c444 100644 --- a/kurator/src/Battle.cpp +++ b/kurator/src/Battle.cpp @@ -34,6 +34,7 @@ Battle::Battle(std::shared_ptr<Session> _session) : battle {sim::prepare(campaign::scenarios::example())}, time_factor {1.0} { + battle->dispatcher().sink<sim::End>().connect<&Battle::on_end>(*this); battle->dispatcher().sink<sim::Hit>().connect<&Battle::on_hit>(*this); battle->dispatcher().sink<stats::ShipLeft>().connect<&Battle::on_ship_left>(*this); auto& registry = battle->registry(); @@ -47,6 +48,7 @@ Battle::Battle(std::shared_ptr<Session> _session) : Battle::~Battle() { + battle->dispatcher().sink<sim::End>().disconnect(*this); battle->dispatcher().sink<sim::Hit>().disconnect(*this); battle->dispatcher().sink<stats::ShipLeft>().disconnect(*this); } @@ -138,6 +140,13 @@ Battle::draw() const void +Battle::on_end(const sim::End&) +{ + session->set(std::make_shared<Log>(session, log)); +} + + +void Battle::on_hit(const sim::Hit& hit) { auto& registry = battle->registry(); diff --git a/kurator/src/Battle.h b/kurator/src/Battle.h index 1ebfa35..918ec1c 100644 --- a/kurator/src/Battle.h +++ b/kurator/src/Battle.h @@ -23,6 +23,7 @@ public: virtual ~Battle(); void update(float dt) override; void draw() const override; + void on_end(const sim::End& end); void on_hit(const sim::Hit& hit); void on_ship_left(const stats::ShipLeft& event); private: diff --git a/sim/include/kurator/sim/events.h b/sim/include/kurator/sim/events.h index cacf443..71ce098 100644 --- a/sim/include/kurator/sim/events.h +++ b/sim/include/kurator/sim/events.h @@ -17,5 +17,10 @@ struct Hit }; +struct End +{ +}; + + } // namespace sim } // namespace kurator diff --git a/sim/src/BaseBattle.cpp b/sim/src/BaseBattle.cpp index 449cddb..4b65139 100644 --- a/sim/src/BaseBattle.cpp +++ b/sim/src/BaseBattle.cpp @@ -60,6 +60,7 @@ BaseBattle::update(const float dt) turrets(dt); kill_off_dead(); manager.clear(_registry); // registry supports on destructions events + manager.update(_dispatcher); } diff --git a/sim/src/TeamManager.cpp b/sim/src/TeamManager.cpp index b792297..d26ed67 100644 --- a/sim/src/TeamManager.cpp +++ b/sim/src/TeamManager.cpp @@ -6,6 +6,9 @@ #include <utility> #include <entt/entity/registry.hpp> +#include <entt/signal/dispatcher.hpp> + +#include <kurator/sim/events.h> namespace kurator @@ -64,5 +67,14 @@ TeamManager::clear(entt::registry& registry) } +void +TeamManager::update(entt::dispatcher& dispatcher) +{ + const auto left = std::count_if(teams.begin(), teams.end(), [](const Team& team){ return team.size() > 0; }); + if (left < 2) + dispatcher.trigger(End{}); +} + + } // namespace sim } // namespace kurator diff --git a/sim/src/TeamManager.h b/sim/src/TeamManager.h index 050af94..502bed2 100644 --- a/sim/src/TeamManager.h +++ b/sim/src/TeamManager.h @@ -4,6 +4,7 @@ #include <vector> #include <entt/entity/registry.hpp> +#include <entt/signal/dispatcher.hpp> namespace kurator @@ -21,6 +22,7 @@ public: Team get(int team) const; entt::entity random(int exclude); void clear(entt::registry& registry); + void update(entt::dispatcher& dispatcher); private: std::vector<Team> teams; std::mt19937 generator; |