From dd997209468bde60f57089960f46067a375890ca Mon Sep 17 00:00:00 2001 From: Aki Date: Sun, 29 Jan 2023 02:06:52 +0100 Subject: Damage popups will now merge when possible --- kurator/CMakeLists.txt | 1 + kurator/src/Battle.cpp | 11 ++++------- kurator/src/PopupEmitter.cpp | 38 ++++++++++++++++++++++++++++++++++++++ kurator/src/PopupEmitter.h | 20 ++++++++++++++++++++ 4 files changed, 63 insertions(+), 7 deletions(-) create mode 100644 kurator/src/PopupEmitter.cpp create mode 100644 kurator/src/PopupEmitter.h (limited to 'kurator') diff --git a/kurator/CMakeLists.txt b/kurator/CMakeLists.txt index e2e4821..5b49bfb 100644 --- a/kurator/CMakeLists.txt +++ b/kurator/CMakeLists.txt @@ -8,6 +8,7 @@ add_executable( src/ForceBalance.cpp src/main.cpp src/Pause.cpp + src/PopupEmitter.cpp src/ScenarioEditor.cpp src/SceneBuilder.cpp src/SceneFrame.cpp diff --git a/kurator/src/Battle.cpp b/kurator/src/Battle.cpp index d7232ff..8073585 100644 --- a/kurator/src/Battle.cpp +++ b/kurator/src/Battle.cpp @@ -22,6 +22,7 @@ #include "colors.h" #include "components.h" #include "Pause.h" +#include "PopupEmitter.h" #include "Session.h" #include "Summary.h" @@ -51,6 +52,7 @@ Battle::Battle(std::shared_ptr _session, campaign::Scenario scenario, B for (const auto& [entity, team, type, identifier] : ships.each()) { std::string label = TextFormat("%s (%d)", type.name.c_str(), identifier.id); registry.emplace(entity, 5.0, team_color(team.id), std::move(label)); + registry.emplace(entity); } } @@ -195,15 +197,10 @@ Battle::on_hit(const sim::Hit& hit) auto& registry = battle->registry(); if (!registry.valid(hit.victim)) return; - const std::string text = TextFormat("%.1f", hit.damage); const auto& source = registry.get(hit.source); const auto& victim = registry.get(hit.victim); - const auto popup = registry.create(); - registry.emplace(popup, 1.2); - registry.emplace(popup, text, MeasureText(text.c_str(), 10), 10, RED); - registry.emplace(popup, victim.position, 0.0); - registry.emplace(popup, 0, 0); - registry.emplace(popup, sim::Point{0.0, -20}, 0.9998); + auto& popup = registry.get(hit.victim); + popup.emit(registry, victim, hit.damage); const auto line = registry.create(); registry.emplace(line, 0.2, true); registry.emplace( diff --git a/kurator/src/PopupEmitter.cpp b/kurator/src/PopupEmitter.cpp new file mode 100644 index 0000000..c382ef1 --- /dev/null +++ b/kurator/src/PopupEmitter.cpp @@ -0,0 +1,38 @@ +#include "PopupEmitter.h" + +#include + +#include +#include + +#include +#include + +#include "components.h" + + +namespace kurator +{ + + +void +PopupEmitter::emit(entt::registry& registry, const sim::Transform& transform, const double damage) +{ + constexpr int size = 10; + if (!registry.valid(popup)) { + total = 0.0; + popup = registry.create(); + registry.emplace(popup, 1.2); + registry.emplace(popup, transform.position, 0.0); + registry.emplace(popup, std::string{}, 0, size, RED); + registry.emplace(popup, 0, 0); + registry.emplace(popup, sim::Point{0, -20}, 0.9998); + } + total += damage; + auto& text = registry.get(popup); + text.text = TextFormat("%.1f", total); + text.width = MeasureText(text.text.c_str(), size); +} + + +} // namespace kurator diff --git a/kurator/src/PopupEmitter.h b/kurator/src/PopupEmitter.h new file mode 100644 index 0000000..0d90427 --- /dev/null +++ b/kurator/src/PopupEmitter.h @@ -0,0 +1,20 @@ +#pragma once + +#include + +#include + + +namespace kurator +{ + + +struct PopupEmitter +{ + entt::entity popup = entt::null; + double total = 0.0; + void emit(entt::registry& registry, const sim::Transform& transform, double damage); +}; + + +} // namespace kurator -- cgit v1.1