diff options
-rw-r--r-- | kurator/src/Battle.cpp | 12 | ||||
-rw-r--r-- | kurator/src/Battle.h | 5 | ||||
-rw-r--r-- | 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> _session) : } -Battle::Battle(std::shared_ptr<Session> _session, campaign::Scenario scenario) : +Battle::Battle(std::shared_ptr<Session> _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<sim::End>().connect<&Battle::on_end>(*this); battle->dispatcher().sink<sim::Hit>().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<Summary>(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<Summary>(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 <functional> #include <memory> #include <kurator/campaign/Scenario.h> @@ -20,8 +21,9 @@ namespace kurator class Battle : public Scene { public: + using Callback = std::function<void(const EventLog&)>; explicit Battle(std::shared_ptr<Session> _session); - Battle(std::shared_ptr<Session> _session, campaign::Scenario scenario); + Battle(std::shared_ptr<Session> _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 <algorithm> #include <memory> #include <utility> +#include <vector> #include <raylib.h> #include <imgui.h> #include <kurator/campaign/Scenario.h> #include <kurator/campaign/scenarios.h> +#include <kurator/campaign/ShipConfig.h> +#include <kurator/stats/events.h> #include "Battle.h" +#include "EventLog.h" #include "Session.h" @@ -17,6 +22,16 @@ namespace kurator { +class Reporter +{ +public: + Reporter(std::vector<campaign::ShipConfig>& _ships) : ships {_ships} {} + void operator()(const stats::ShipLeft& event); +private: + std::vector<campaign::ShipConfig>& ships; +}; + + Campaign::Campaign(std::shared_ptr<Session> _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<Battle>(session, std::move(scenario))); + auto destroy = [&](const EventLog& log) { log.for_each(Reporter{ships}); }; + session->push(std::make_shared<Battle>(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 |