From 97257c552c546c1924ccc9348f0ad7a95dea169b Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 5 Dec 2022 00:32:14 +0100 Subject: Extracted force balance bar into own file --- kurator/CMakeLists.txt | 1 + kurator/src/Battle.cpp | 14 ++------------ kurator/src/Battle.h | 2 ++ kurator/src/ForceBalance.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++ kurator/src/ForceBalance.h | 23 +++++++++++++++++++++++ 5 files changed, 71 insertions(+), 12 deletions(-) create mode 100644 kurator/src/ForceBalance.cpp create mode 100644 kurator/src/ForceBalance.h diff --git a/kurator/CMakeLists.txt b/kurator/CMakeLists.txt index 61b348c..fab8d3f 100644 --- a/kurator/CMakeLists.txt +++ b/kurator/CMakeLists.txt @@ -3,6 +3,7 @@ add_executable( ${PROJECT_NAME} src/Battle.cpp src/colors.cpp + src/ForceBalance.cpp src/main.cpp src/Session.cpp src/Skybox.cpp diff --git a/kurator/src/Battle.cpp b/kurator/src/Battle.cpp index 7bb4e7d..9c5afc6 100644 --- a/kurator/src/Battle.cpp +++ b/kurator/src/Battle.cpp @@ -63,6 +63,7 @@ Battle::update(const float dt) pop.speed.x -= damp.x; pop.speed.y -= damp.y; } + balance.update(registry); if (IsKeyPressed(KEY_SPACE)) session->set(std::make_shared(session)); } @@ -99,18 +100,7 @@ Battle::draw() const const int y = hheight + transform.position.y*scale - text.font_size/2 + offset.y; DrawText(text.text.c_str(), x, y, text.font_size, text.color); } - auto points = registry.view<sim::HitPoints, sim::Team>(); - double totals[2] {0.0, 0.0}; // FIXME and extract - for (const auto& [entity, points, team] : points.each()) { - if (team.id < 2) - totals[team.id] += points.health; - } - const int x1 = hwidth / 2.0; - const int w1 = hwidth * totals[1] / (totals[0] + totals[1]); - const int x2 = x1 + w1; - const int w2 = hwidth * totals[0] / (totals[0] + totals[1]); - DrawRectangle(x1, 10, w1, 10, GREEN); - DrawRectangle(x2, 10, w2, 10, RED); + balance.draw(); } diff --git a/kurator/src/Battle.h b/kurator/src/Battle.h index 85d4b18..b3a0d38 100644 --- a/kurator/src/Battle.h +++ b/kurator/src/Battle.h @@ -5,6 +5,7 @@ #include <kurator/sim/Battle.h> #include <kurator/sim/events.h> +#include "ForceBalance.h" #include "Scene.h" #include "Session.h" @@ -24,6 +25,7 @@ public: private: std::shared_ptr<Session> session; std::unique_ptr<sim::Battle> battle; + ForceBalance balance; }; diff --git a/kurator/src/ForceBalance.cpp b/kurator/src/ForceBalance.cpp new file mode 100644 index 0000000..4d56357 --- /dev/null +++ b/kurator/src/ForceBalance.cpp @@ -0,0 +1,43 @@ +#include "ForceBalance.h" + +#include <entt/entity/registry.hpp> +#include <raylib.h> + +#include <kurator/sim/components.h> + +#include "colors.h" + + +namespace kurator +{ + + +void +ForceBalance::update(const entt::registry& registry) +{ + total = 0.0; + teams.clear(); + auto points = registry.view<sim::HitPoints, sim::Team>(); + for (const auto& [entity, points, team] : points.each()) { + while (teams.size() <= static_cast<unsigned>(team.id)) + teams.emplace_back(0.0); + total += points.health; + teams[team.id] += points.health; + } +} + + +void +ForceBalance::draw() const +{ + const int width = GetScreenWidth() / 2; + int x = GetScreenWidth() / 4; + for (unsigned int i = 0; i < teams.size(); ++i) { + const int segment = width * teams[i] / total; + DrawRectangle(x, 10, segment, 10, team_color(i)); + x += segment; + } +} + + +} // namespace kurator diff --git a/kurator/src/ForceBalance.h b/kurator/src/ForceBalance.h new file mode 100644 index 0000000..317a4e8 --- /dev/null +++ b/kurator/src/ForceBalance.h @@ -0,0 +1,23 @@ +#pragma once + +#include <vector> + +#include <entt/entity/registry.hpp> + + +namespace kurator +{ + + +class ForceBalance +{ +public: + void update(const entt::registry& registry); + void draw() const; +private: + double total; + std::vector<double> teams; +}; + + +} // namespace kurator -- cgit v1.1