From d599de1ffc38f8c7f17b454fdf6eb3aaf320c89e Mon Sep 17 00:00:00 2001 From: Aki Date: Tue, 13 Dec 2022 23:29:03 +0100 Subject: Added naive events for ship destruction --- CMakeLists.txt | 1 + kurator/CMakeLists.txt | 1 + kurator/src/Battle.cpp | 21 +++++++++++++++++++-- kurator/src/Battle.h | 4 +++- sim/CMakeLists.txt | 1 + sim/src/BaseBattle.cpp | 12 ++++++++++-- sim/src/BaseBattle.h | 1 + stats/CMakeLists.txt | 12 ++++++++++++ stats/include/kurator/stats/events.h | 22 ++++++++++++++++++++++ 9 files changed, 70 insertions(+), 5 deletions(-) create mode 100644 stats/CMakeLists.txt create mode 100644 stats/include/kurator/stats/events.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 80264a4..8791616 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,4 +8,5 @@ find_package(raylib 4 REQUIRED) add_subdirectory(campaign) add_subdirectory(kurator) add_subdirectory(sim) +add_subdirectory(stats) add_subdirectory(universe) diff --git a/kurator/CMakeLists.txt b/kurator/CMakeLists.txt index 4fe1c75..2a46ef9 100644 --- a/kurator/CMakeLists.txt +++ b/kurator/CMakeLists.txt @@ -16,5 +16,6 @@ target_link_libraries( PRIVATE raylib PRIVATE campaign PRIVATE sim + PRIVATE stats PRIVATE universe ) diff --git a/kurator/src/Battle.cpp b/kurator/src/Battle.cpp index 5470de1..a77b57e 100644 --- a/kurator/src/Battle.cpp +++ b/kurator/src/Battle.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -14,6 +15,7 @@ #include #include #include +#include #include #include "colors.h" @@ -30,7 +32,8 @@ Battle::Battle(std::shared_ptr _session) : session {std::move(_session)}, battle {sim::prepare(campaign::scenarios::example())} { - battle->dispatcher().sink().connect<&Battle::receive>(*this); + battle->dispatcher().sink().connect<&Battle::on_hit>(*this); + battle->dispatcher().sink().connect<&Battle::on_ship_left>(*this); auto& registry = battle->registry(); auto ships = registry.view(); for (const auto& [entity, team, type, identifier] : ships.each()) { @@ -43,6 +46,7 @@ Battle::Battle(std::shared_ptr _session) : Battle::~Battle() { battle->dispatcher().sink().disconnect(*this); + battle->dispatcher().sink().disconnect(*this); } @@ -113,7 +117,7 @@ Battle::draw() const void -Battle::receive(const sim::Hit& hit) +Battle::on_hit(const sim::Hit& hit) { auto& registry = battle->registry(); if (!registry.valid(hit.victim)) @@ -133,4 +137,17 @@ Battle::receive(const sim::Hit& hit) } +void +Battle::on_ship_left(const stats::ShipLeft& event) +{ + std::cout + << event.time + << "\tShip " << (event.destroyed ? "destroyed" : "left") << ": " + << event.ship.id + << " team " + << event.team + << std::endl; +} + + } // namespace kurator diff --git a/kurator/src/Battle.h b/kurator/src/Battle.h index b3a0d38..9080219 100644 --- a/kurator/src/Battle.h +++ b/kurator/src/Battle.h @@ -4,6 +4,7 @@ #include #include +#include #include "ForceBalance.h" #include "Scene.h" @@ -21,7 +22,8 @@ public: virtual ~Battle(); void update(float dt) override; void draw() const override; - void receive(const sim::Hit& hit); + void on_hit(const sim::Hit& hit); + void on_ship_left(const stats::ShipLeft& event); private: std::shared_ptr session; std::unique_ptr battle; diff --git a/sim/CMakeLists.txt b/sim/CMakeLists.txt index d193630..f3d5494 100644 --- a/sim/CMakeLists.txt +++ b/sim/CMakeLists.txt @@ -16,5 +16,6 @@ target_link_libraries( ${PROJECT_NAME} PUBLIC EnTT::EnTT PUBLIC campaign + PRIVATE stats PUBLIC universe ) diff --git a/sim/src/BaseBattle.cpp b/sim/src/BaseBattle.cpp index 87b2096..449cddb 100644 --- a/sim/src/BaseBattle.cpp +++ b/sim/src/BaseBattle.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include "Builder.h" @@ -19,6 +20,7 @@ namespace sim BaseBattle::BaseBattle(const campaign::Scenario& scenario) : + time {0.0}, _registry {}, spawner {scenario.total_teams(), scenario.radius, 0.1} { @@ -51,6 +53,7 @@ BaseBattle::dispatcher() void BaseBattle::update(const float dt) { + time += dt; pick_random_targets(); keep_at_range(); floating_movement(dt); @@ -144,8 +147,13 @@ BaseBattle::kill_off_dead() { auto view = _registry.view(); for (auto&& [entity, points] : view.each()) { - if (points.health <= 0.0) - _registry.destroy(entity); + if (points.health > 0.0) + continue; + if (_registry.all_of(entity)) { + const auto& [identifier, team] = _registry.get(entity); + _dispatcher.trigger(stats::ShipLeft{time, identifier, team.id, true}); + } + _registry.destroy(entity); } } diff --git a/sim/src/BaseBattle.h b/sim/src/BaseBattle.h index b0980fe..d2f7d19 100644 --- a/sim/src/BaseBattle.h +++ b/sim/src/BaseBattle.h @@ -24,6 +24,7 @@ public: entt::dispatcher& dispatcher() override; void update(float dt) override; private: + double time; entt::registry _registry; entt::dispatcher _dispatcher; RandomSpawner spawner; diff --git a/stats/CMakeLists.txt b/stats/CMakeLists.txt new file mode 100644 index 0000000..02b68a8 --- /dev/null +++ b/stats/CMakeLists.txt @@ -0,0 +1,12 @@ +project(stats) +add_library( + ${PROJECT_NAME} INTERFACE +) +target_include_directories( + ${PROJECT_NAME} + INTERFACE include +) +target_link_libraries( + ${PROJECT_NAME} + INTERFACE campaign +) diff --git a/stats/include/kurator/stats/events.h b/stats/include/kurator/stats/events.h new file mode 100644 index 0000000..a9a9612 --- /dev/null +++ b/stats/include/kurator/stats/events.h @@ -0,0 +1,22 @@ +#pragma once + +#include + + +namespace kurator +{ +namespace stats +{ + + +struct ShipLeft +{ + double time; + campaign::UniqueIdentifier ship; + int team; + bool destroyed; +}; + + +} // namespace stats +} // namespace kurator -- cgit v1.1