From 2a9f378c66b28cef1c5ee063cf4d7e4e2889076e Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 13 Feb 2023 22:52:18 +0100 Subject: 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. --- kurator/src/Battle.cpp | 47 ++++++++++++++++++++-------------------- kurator/src/Battle.h | 4 ---- sim/CMakeLists.txt | 2 ++ sim/include/kurator/sim.h | 18 +++++++++++++++ sim/include/kurator/sim/Battle.h | 7 ++---- sim/include/kurator/sim/State.h | 29 +++++++++++++++++++++++++ sim/src/BaseBattle.cpp | 44 ++++++++++--------------------------- sim/src/BaseBattle.h | 11 ++++------ sim/src/State.cpp | 25 +++++++++++++++++++++ sim/src/sim.cpp | 36 ++++++++++++++++++++++++++++++ 10 files changed, 151 insertions(+), 72 deletions(-) create mode 100644 sim/include/kurator/sim.h create mode 100644 sim/include/kurator/sim/State.h create mode 100644 sim/src/State.cpp create mode 100644 sim/src/sim.cpp 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, campaign::Scenario scenario, B battle {sim::prepare(scenario)}, report {std::move(_report)} { - battle->dispatcher().sink().connect<&Battle::on_end>(*this); - battle->dispatcher().sink().connect<&Battle::on_hit>(*this); - battle->dispatcher().sink().connect<&Battle::on_destroyed>(*this); - battle->dispatcher().sink().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().connect<&Battle::on_end>(*this); + ctx.dispatcher.sink().connect<&Battle::on_hit>(*this); + ctx.dispatcher.sink().connect<&Battle::on_destroyed>(*this); + ctx.dispatcher.sink().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().disconnect(*this); - battle->dispatcher().sink().disconnect(*this); - battle->dispatcher().sink().disconnect(*this); - battle->dispatcher().sink().disconnect(*this); + auto ctx = battle->context(); + ctx.dispatcher.sink().disconnect(*this); + ctx.dispatcher.sink().disconnect(*this); + ctx.dispatcher.sink().disconnect(*this); + ctx.dispatcher.sink().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(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(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(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 #include -#include -#include #include #include #include @@ -37,8 +35,6 @@ public: private: std::shared_ptr session; std::unique_ptr battle; - engine::Camera camera; - engine::Clock clock; ForceBalance balance; stats::EventLog log; Callback report; diff --git a/sim/CMakeLists.txt b/sim/CMakeLists.txt index 27f312b..55dcc60 100644 --- a/sim/CMakeLists.txt +++ b/sim/CMakeLists.txt @@ -7,6 +7,8 @@ add_library( src/FloatingMovement.cpp src/HitPoints.cpp src/RandomSpawner.cpp + src/sim.cpp + src/State.cpp src/systems.cpp src/TeamManager.cpp src/TurretControl.cpp diff --git a/sim/include/kurator/sim.h b/sim/include/kurator/sim.h new file mode 100644 index 0000000..3c4364c --- /dev/null +++ b/sim/include/kurator/sim.h @@ -0,0 +1,18 @@ +#pragma once + +#include + +#include "sim/State.h" + + +namespace kurator +{ +namespace sim +{ + + +void load_scenario(State& ctx, const campaign::Scenario& scenario); + + +} // namespace sim +} // namespace kurator diff --git a/sim/include/kurator/sim/Battle.h b/sim/include/kurator/sim/Battle.h index 6874b39..1692c0c 100644 --- a/sim/include/kurator/sim/Battle.h +++ b/sim/include/kurator/sim/Battle.h @@ -2,9 +2,6 @@ #include -#include -#include - #include #include @@ -19,8 +16,8 @@ class Battle { public: virtual ~Battle() = default; - virtual entt::registry& registry() = 0; - virtual entt::dispatcher& dispatcher() = 0; + virtual engine::Context context() = 0; + virtual engine::ConstContext const_context() const = 0; virtual void update(engine::Context& ctx) = 0; }; diff --git a/sim/include/kurator/sim/State.h b/sim/include/kurator/sim/State.h new file mode 100644 index 0000000..8449ad2 --- /dev/null +++ b/sim/include/kurator/sim/State.h @@ -0,0 +1,29 @@ +#pragma once + +#include +#include + +#include +#include +#include + + +namespace kurator +{ +namespace sim +{ + + +struct State +{ + entt::registry registry; + entt::dispatcher dispatcher; + engine::Clock clock; + engine::Camera camera; // does not fit in here really + operator engine::Context(); + operator engine::ConstContext() const; +}; + + +} // namespace sim +} // namespace kurator diff --git a/sim/src/BaseBattle.cpp b/sim/src/BaseBattle.cpp index 888379e..916a9f8 100644 --- a/sim/src/BaseBattle.cpp +++ b/sim/src/BaseBattle.cpp @@ -7,6 +7,7 @@ #include #include +#include #include #include #include @@ -23,45 +24,24 @@ namespace sim { -static void setup_scenario(entt::registry& registry, const campaign::Scenario& scenario); - - -BaseBattle::BaseBattle(const campaign::Scenario& scenario) : - _registry {} -{ - setup_scenario(_registry, scenario); - manager.extend(_registry); -} - - -void -setup_scenario(entt::registry& registry, const campaign::Scenario& scenario) +BaseBattle::BaseBattle(const campaign::Scenario& scenario) { - RandomSpawner spawner {scenario.last_team(), scenario.radius, 0.1}; - Builder build {registry, spawner}; - for (const auto& ship : scenario.ships) { - const auto entity = build(ship.loadout.type, ship.team); - registry.emplace(entity, ship.identifier); - auto& state = registry.get(entity); - for (const auto& turret_type : ship.loadout.turrets) { - build(turret_type, entity); - state.keep_at_range = std::min(state.keep_at_range, turret_type.optimal_range); - } - } + load_scenario(state, scenario); + manager.extend(state.registry); } -entt::registry& -BaseBattle::registry() +engine::Context +BaseBattle::context() { - return _registry; + return state; } -entt::dispatcher& -BaseBattle::dispatcher() +engine::ConstContext +BaseBattle::const_context() const { - return _dispatcher; + return state; } @@ -80,9 +60,9 @@ BaseBattle::update(engine::Context& ctx) void BaseBattle::pick_random_targets() { - auto view = _registry.view(); + auto view = state.registry.view(); for (auto&& [entity, team, ai] : view.each()) { - if (!_registry.valid(ai.target)) + if (!state.registry.valid(ai.target)) ai.target = manager.random(team.id); } } diff --git a/sim/src/BaseBattle.h b/sim/src/BaseBattle.h index 77e1d10..6f59457 100644 --- a/sim/src/BaseBattle.h +++ b/sim/src/BaseBattle.h @@ -1,11 +1,9 @@ #pragma once -#include -#include - #include #include #include +#include #include "TeamManager.h" @@ -20,12 +18,11 @@ class BaseBattle : public Battle { public: BaseBattle(const campaign::Scenario& scenario); - entt::registry& registry() override; - entt::dispatcher& dispatcher() override; + engine::Context context() override; + engine::ConstContext const_context() const override; void update(engine::Context& ctx) override; private: - entt::registry _registry; - entt::dispatcher _dispatcher; + State state; TeamManager manager; void pick_random_targets(); }; diff --git a/sim/src/State.cpp b/sim/src/State.cpp new file mode 100644 index 0000000..3c8a709 --- /dev/null +++ b/sim/src/State.cpp @@ -0,0 +1,25 @@ +#include + +#include + + +namespace kurator +{ +namespace sim +{ + + +State::operator engine::Context() +{ + return {registry, dispatcher, clock, camera}; +} + + +State::operator engine::ConstContext() const +{ + return {registry, dispatcher, clock, camera}; +} + + +} // namespace sim +} // namespace kurator diff --git a/sim/src/sim.cpp b/sim/src/sim.cpp new file mode 100644 index 0000000..f915d20 --- /dev/null +++ b/sim/src/sim.cpp @@ -0,0 +1,36 @@ +#include + +#include +#include +#include +#include + +#include "Builder.h" +#include "RandomSpawner.h" + + +namespace kurator +{ +namespace sim +{ + + +void +load_scenario(State& ctx, const campaign::Scenario& scenario) +{ + RandomSpawner spawner {scenario.last_team(), scenario.radius, 0.1}; + Builder build {ctx.registry, spawner}; + for (const auto& ship : scenario.ships) { + const auto entity = build(ship.loadout.type, ship.team); + ctx.registry.emplace(entity, ship.identifier); + auto& state = ctx.registry.get(entity); + for (const auto& turret_type : ship.loadout.turrets) { + build(turret_type, entity); + state.keep_at_range = std::min(state.keep_at_range, turret_type.optimal_range); + } + } +} + + +} // namespace sim +} // namespace kurator -- cgit v1.1