diff options
author | Aki <please@ignore.pl> | 2023-02-13 22:52:18 +0100 |
---|---|---|
committer | Aki <please@ignore.pl> | 2023-02-13 22:52:42 +0100 |
commit | 2a9f378c66b28cef1c5ee063cf4d7e4e2889076e (patch) | |
tree | d678fce067c527052462adf91909f23664ca0544 /kurator | |
parent | 03bb1614c25a56ef6225db09c4c59b7a5f8fa808 (diff) | |
download | kurator-2a9f378c66b28cef1c5ee063cf4d7e4e2889076e.zip kurator-2a9f378c66b28cef1c5ee063cf4d7e4e2889076e.tar.gz kurator-2a9f378c66b28cef1c5ee063cf4d7e4e2889076e.tar.bz2 |
Created sim::State object to hold overall state of simulation
This is seems like it creats even more chaotic binding between the
components but it is a very nice and testable intermediate step
before moving everything to standalone systems and shoving state into
the... well, State.
Diffstat (limited to 'kurator')
-rw-r--r-- | kurator/src/Battle.cpp | 47 | ||||
-rw-r--r-- | kurator/src/Battle.h | 4 |
2 files changed, 23 insertions, 28 deletions
diff --git a/kurator/src/Battle.cpp b/kurator/src/Battle.cpp index 683b0e6..3eadc28 100644 --- a/kurator/src/Battle.cpp +++ b/kurator/src/Battle.cpp @@ -48,15 +48,14 @@ Battle::Battle(std::shared_ptr<Session> _session, campaign::Scenario scenario, B battle {sim::prepare(scenario)}, report {std::move(_report)} { - battle->dispatcher().sink<sim::End>().connect<&Battle::on_end>(*this); - battle->dispatcher().sink<sim::Hit>().connect<&Battle::on_hit>(*this); - battle->dispatcher().sink<sim::Destroyed>().connect<&Battle::on_destroyed>(*this); - battle->dispatcher().sink<stats::ShipLeft>().connect<&Battle::on_ship_left>(*this); - camera.scale = std::min(GetScreenWidth()/30000.0, GetScreenHeight()/30000.0); - auto& registry = battle->registry(); - engine::Context ctx {registry, battle->dispatcher(), clock, camera}; + auto ctx = battle->context(); + ctx.dispatcher.sink<sim::End>().connect<&Battle::on_end>(*this); + ctx.dispatcher.sink<sim::Hit>().connect<&Battle::on_hit>(*this); + ctx.dispatcher.sink<sim::Destroyed>().connect<&Battle::on_destroyed>(*this); + ctx.dispatcher.sink<stats::ShipLeft>().connect<&Battle::on_ship_left>(*this); + ctx.camera.scale = std::min(GetScreenWidth()/30000.0, GetScreenHeight()/30000.0); attach_markers(ctx); - balance.update(registry); + balance.update(ctx.registry); } @@ -74,10 +73,11 @@ attach_markers(engine::Context& ctx) Battle::~Battle() { - battle->dispatcher().sink<sim::End>().disconnect(*this); - battle->dispatcher().sink<sim::Hit>().disconnect(*this); - battle->dispatcher().sink<sim::Destroyed>().disconnect(*this); - battle->dispatcher().sink<stats::ShipLeft>().disconnect(*this); + auto ctx = battle->context(); + ctx.dispatcher.sink<sim::End>().disconnect(*this); + ctx.dispatcher.sink<sim::Hit>().disconnect(*this); + ctx.dispatcher.sink<sim::Destroyed>().disconnect(*this); + ctx.dispatcher.sink<stats::ShipLeft>().disconnect(*this); } @@ -104,28 +104,27 @@ static void animate_lines(engine::Context& ctx); void Battle::update() { + auto ctx = battle->context(); if (IsKeyPressed(KEY_ESCAPE)) return session->set(std::make_shared<Pause>(session, session->current())); if (IsMouseButtonDown(MOUSE_BUTTON_RIGHT)) { const auto delta = GetMouseDelta(); - camera.offset.x -= delta.x / camera.scale; - camera.offset.y -= delta.y / camera.scale; + ctx.camera.offset.x -= delta.x / ctx.camera.scale; + ctx.camera.offset.y -= delta.y / ctx.camera.scale; } if (IsWindowResized()) - camera.scale = std::min(GetScreenWidth()/30000.0, GetScreenHeight()/30000.0); // won't work in frame - auto& registry = battle->registry(); - clock.update(); - engine::Context ctx {registry, battle->dispatcher(), clock, camera}; + ctx.camera.scale = std::min(GetScreenWidth()/30000.0, GetScreenHeight()/30000.0); + ctx.clock.update(); battle->update(ctx); progress_timers(ctx); move_ui_pops(ctx); blink_crosses(ctx); animate_lines(ctx); - balance.update(registry); + balance.update(ctx.registry); ImGui::SetNextWindowPos({GetScreenWidth()/2.0f, GetScreenHeight()-100.0f}, ImGuiCond_Once, {0.5f, 0.5f}); ImGui::SetNextWindowSize({240.0f, 0.0f}, ImGuiCond_Once); if (ImGui::Begin("Controls", nullptr, ImGuiWindowFlags_NoFocusOnAppearing)) - time_controls("TimeControls", clock.time_factor); + time_controls("TimeControls", ctx.clock.time_factor); ImGui::End(); } @@ -188,9 +187,9 @@ static void draw_pops(engine::ConstContext& ctx); void Battle::draw() const { + auto ctx = battle->const_context(); ClearBackground(BLACK); - Grid().draw(camera); - engine::ConstContext ctx {battle->registry(), battle->dispatcher(), clock, camera}; + Grid().draw(ctx.camera); draw_crosses(ctx); draw_lines(ctx); draw_markers(ctx); @@ -284,7 +283,7 @@ Battle::on_end(const sim::End&) void Battle::on_hit(const sim::Hit& hit) { - auto& registry = battle->registry(); + auto& registry = battle->context().registry; if (!registry.valid(hit.victim)) return; const auto& source = registry.get<sim::Transform>(hit.source); @@ -306,7 +305,7 @@ Battle::on_hit(const sim::Hit& hit) void Battle::on_destroyed(const sim::Destroyed& event) { - auto& registry = battle->registry(); + auto& registry = battle->context().registry; if (!registry.valid(event.victim)) return; const auto& victim = registry.get<sim::Transform>(event.victim); diff --git a/kurator/src/Battle.h b/kurator/src/Battle.h index 592366f..c02717d 100644 --- a/kurator/src/Battle.h +++ b/kurator/src/Battle.h @@ -4,8 +4,6 @@ #include <memory> #include <kurator/campaign/Scenario.h> -#include <kurator/engine/Camera.h> -#include <kurator/engine/Clock.h> #include <kurator/engine/Context.h> #include <kurator/sim/Battle.h> #include <kurator/sim/events.h> @@ -37,8 +35,6 @@ public: private: std::shared_ptr<Session> session; std::unique_ptr<sim::Battle> battle; - engine::Camera camera; - engine::Clock clock; ForceBalance balance; stats::EventLog log; Callback report; |