Files
letnik3koncni-prap/src/game/agame/hudtext.cpp
2026-05-20 07:32:57 +02:00

79 lines
2.8 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <game/agame/hudtext.hpp>
#include <game/gamemanager.hpp>
#include <window/window.hpp>
#include <sstream>
namespace Game::AGame {
void HUDText::start() {
mZIndex = 1000;
Object::UIText::start();
setText("Level 1 | Score 0 | Trash 0 | Polluters 0");
}
void HUDText::update(float deltaTime) {
(void)deltaTime;
// Use logical world dimensions (1280×720) not actual screen size
constexpr int windowW = 1280;
constexpr int windowH = 720;
float camX = 0.f;
float camY = 0.f;
Object::Camera::getInstance().getPosition(camX, camY);
auto anchorTopRight = [&]() {
if (!mTex) {
return;
}
const float marginX = 24.f;
const float marginY = 24.f;
const float textWidth = mTex->getWidth() * mTransform.adjustedScaleX();
mTransform.x = camX + windowW / 2.f - marginX - textWidth;
mTransform.y = camY - windowH / 2.f + marginY;
};
if (GameManager::getSharedData<bool>("gameLost")) {
const std::string leaderboardText = GameManager::getSharedData<std::string>("leaderboardText");
std::string endText = "Umrl si!";
if (!leaderboardText.empty()) {
endText += "\n\n" + leaderboardText;
}
if (getText() != endText) {
Window::Window::postToMainThread([this, endText]() { setText(endText); });
}
anchorTopRight();
return;
}
if (GameManager::getSharedData<bool>("gameWon")) {
const std::string leaderboardText = GameManager::getSharedData<std::string>("leaderboardText");
std::string endText = "Zmagal si!";
if (!leaderboardText.empty()) {
endText += "\n\n" + leaderboardText;
}
if (getText() != endText) {
Window::Window::postToMainThread([this, endText]() { setText(endText); });
}
anchorTopRight();
return;
}
const std::string playerName = GameManager::getSharedData<std::string>("playerName");
std::stringstream stream;
stream << "Igralec: " << (playerName.empty() ? std::string("Anonimni") : playerName)
<< " | Level " << GameManager::getSharedData<int>("gameStage")
<< " | Točke " << GameManager::getSharedData<int>("gameScore")
<< " | Smeti " << GameManager::getSharedData<int>("trashActiveCount")
<< " | Sovražniki " << GameManager::getSharedData<int>("enemyActiveCount");
const std::string newHudText = stream.str();
if (getText() != newHudText) {
Window::Window::postToMainThread([this, newHudText]() { setText(newHudText); });
}
anchorTopRight();
}
}