r-type  0.0.0
R-Type main
Loading...
Searching...
No Matches
HUD.cpp
Go to the documentation of this file.
1#include <ranges>
2
3#include "ECS/Component.hpp"
5#include "Utils/Common.hpp"
6
7void gme::HUDSystem::createScoreHUD(ecs::Registry &registry, float x, float y)
8{
10 .with<ecs::Transform>("score_bg_transform", x, y, 0.0f)
11 .with<ecs::Scale>("score_bg_scale", 3.0f, 2.0f)
13 .with<ecs::Layer>("score_bg_layer", 1)
14 .build();
15
17 for (int i = 0; i < 6; ++i)
18 {
19 auto digitEntity = registry.createEntity()
20 .with<ecs::Transform>("score_digit_transform", x + 140.0f + i * 20.0f, y + 4.0f, 0.0f)
21 .with<ecs::Scale>("score_digit_scale", 1.0f, 1.0f)
22 .with<ecs::Texture>("score_digit_texture", utl::Path::Texture::TEXTURE_SCORE_DIGIT_0)
23 .with<ecs::Layer>("score_digit_layer", 2)
24 .build();
25 m_scoreDigitEntities.push_back(digitEntity);
26 }
27}
28
29void gme::HUDSystem::updateScore(ecs::Registry &registry, int newScore) const
30{
31 int digits[6];
32 for (int i = 5; i >= 0; --i)
33 {
34 digits[i] = newScore % 10;
35 newScore /= 10;
36 }
37
38 static const char *digitPaths[10] = {
44
45 for (int i = 0; i < 6 && i < m_scoreDigitEntities.size(); ++i)
46 {
47 if (auto *digitTexture = registry.getComponent<ecs::Texture>(m_scoreDigitEntities[i]))
48 {
49 digitTexture->path = digitPaths[digits[i]];
50 }
51
52 auto *digitTransform = registry.getComponent<ecs::Transform>(m_scoreDigitEntities[i]);
53 if (auto *digitScale = registry.getComponent<ecs::Scale>(m_scoreDigitEntities[i]);
54 (digitTransform != nullptr) && (digitScale != nullptr))
55 {
56 digitTransform->x = 10.0f + 130.0f + i * 20.0f;
57 digitTransform->y = 10.0f + 4.0f;
58 digitScale->x = 1.5f;
59 digitScale->y = 1.5f;
60 }
61 }
62}
63
64void gme::HUDSystem::update(ecs::Registry &registry, float /* dt */)
65{
66 int currentScore = 0;
67 for (const auto &score : registry.getAll<ecs::Score>() | std::views::values)
68 {
69 currentScore = score.value;
70 break;
71 }
72
73 updateScore(registry, currentScore);
74}
This file contains the component definitions.
HUD System for managing score display and UI elements.
EntityBuilder & with(Args &&...args)
Definition Registry.hpp:40
Class for managing entities and their components.
Definition Registry.hpp:25
EntityBuilder createEntity()
Definition Registry.hpp:53
T * getComponent(Entity e)
Definition Registry.hpp:71
void update(ecs::Registry &registry, float) override
Update the HUD system (called each frame)
Definition HUD.cpp:64
void updateScore(ecs::Registry &registry, int newScore) const
Update the displayed score value.
Definition HUD.cpp:29
std::vector< ecs::Entity > m_scoreDigitEntities
Entities for individual score digits.
Definition HUD.hpp:105
ecs::Entity m_scoreBgEntity
Entity for score background sprite.
Definition HUD.hpp:104
void createScoreHUD(ecs::Registry &registry, float x, float y)
Create the score display HUD elements.
Definition HUD.cpp:7
This file contains common definitions and constants.
constexpr auto TEXTURE_SCORE_DIGIT_3
Definition Common.hpp:131
constexpr auto TEXTURE_SCORE_DIGIT_5
Definition Common.hpp:133
constexpr auto TEXTURE_SCORE_DIGIT_2
Definition Common.hpp:130
constexpr auto TEXTURE_SCORE_DIGIT_9
Definition Common.hpp:137
constexpr auto TEXTURE_SCORE_DIGIT_6
Definition Common.hpp:134
constexpr auto TEXTURE_SCORE_DIGIT_1
Definition Common.hpp:129
constexpr auto TEXTURE_SCORE_DIGIT_4
Definition Common.hpp:132
constexpr auto TEXTURE_SCORE_COUNTER_BG
Definition Common.hpp:127
constexpr auto TEXTURE_SCORE_DIGIT_0
Definition Common.hpp:128
constexpr auto TEXTURE_SCORE_DIGIT_8
Definition Common.hpp:136
constexpr auto TEXTURE_SCORE_DIGIT_7
Definition Common.hpp:135