From bc05dbcb04dbce5e88921d318fc3094e3bcddf2f Mon Sep 17 00:00:00 2001 From: Aki Date: Thu, 29 Dec 2022 00:44:45 +0100 Subject: Carry over ship deaths to campaign state --- kurator/src/Battle.cpp | 12 +++++++++--- kurator/src/Battle.h | 5 ++++- kurator/src/Campaign.cpp | 28 +++++++++++++++++++++++++++- 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/kurator/src/Battle.cpp b/kurator/src/Battle.cpp index 1f30f0a..f9f402e 100644 --- a/kurator/src/Battle.cpp +++ b/kurator/src/Battle.cpp @@ -35,10 +35,11 @@ Battle::Battle(std::shared_ptr _session) : } -Battle::Battle(std::shared_ptr _session, campaign::Scenario scenario) : +Battle::Battle(std::shared_ptr _session, campaign::Scenario scenario, Battle::Callback _report) : session {std::move(_session)}, battle {sim::prepare(scenario)}, - time_factor {1.0} + time_factor {1.0}, + report {std::move(_report)} { battle->dispatcher().sink().connect<&Battle::on_end>(*this); battle->dispatcher().sink().connect<&Battle::on_hit>(*this); @@ -95,8 +96,11 @@ Battle::update(const float dt) ImGui::PopItemWidth(); } ImGui::End(); - if (IsKeyPressed(KEY_SPACE)) + if (IsKeyPressed(KEY_SPACE)) { + if (report) + report(log); session->set(std::make_shared(session, log)); + } } @@ -148,6 +152,8 @@ Battle::draw() const void Battle::on_end(const sim::End&) { + if (report) + report(log); session->set(std::make_shared(session, log)); } diff --git a/kurator/src/Battle.h b/kurator/src/Battle.h index 81dd4b7..fd772de 100644 --- a/kurator/src/Battle.h +++ b/kurator/src/Battle.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include @@ -20,8 +21,9 @@ namespace kurator class Battle : public Scene { public: + using Callback = std::function; explicit Battle(std::shared_ptr _session); - Battle(std::shared_ptr _session, campaign::Scenario scenario); + Battle(std::shared_ptr _session, campaign::Scenario scenario, Callback _report={}); virtual ~Battle(); void update(float dt) override; void draw() const override; @@ -34,6 +36,7 @@ private: ForceBalance balance; EventLog log; float time_factor; + Callback report; }; diff --git a/kurator/src/Campaign.cpp b/kurator/src/Campaign.cpp index a3f788e..9d88518 100644 --- a/kurator/src/Campaign.cpp +++ b/kurator/src/Campaign.cpp @@ -1,15 +1,20 @@ #include "Campaign.h" +#include #include #include +#include #include #include #include #include +#include +#include #include "Battle.h" +#include "EventLog.h" #include "Session.h" @@ -17,6 +22,16 @@ namespace kurator { +class Reporter +{ +public: + Reporter(std::vector& _ships) : ships {_ships} {} + void operator()(const stats::ShipLeft& event); +private: + std::vector& ships; +}; + + Campaign::Campaign(std::shared_ptr _session) : session {std::move(_session)}, ships {}, @@ -53,7 +68,9 @@ Campaign::update(const float) scenario.ships.push_back(std::move(ship)); } } - session->push(std::make_shared(session, std::move(scenario))); + auto destroy = [&](const EventLog& log) { log.for_each(Reporter{ships}); }; + session->push(std::make_shared(session, std::move(scenario), destroy)); + level++; } if (ImGui::Button("Back")) session->pop(); @@ -69,4 +86,13 @@ Campaign::draw() const } +void +Reporter::operator()(const stats::ShipLeft& event) +{ + using campaign::ShipConfig; + auto was_destroyed = [&event](const ShipConfig& ship){ return ship.identifier.id == event.ship.id; }; + ships.erase(std::remove_if(ships.begin(), ships.end(), was_destroyed), ships.end()); +} + + } // namespace kurator -- cgit v1.1