101 lines
2.8 KiB
C++
101 lines
2.8 KiB
C++
// gestor_puntuacio_flotant.cpp - Implementació del gestor de números flotants
|
|
// © 2025 Port a C++20 amb SDL3
|
|
|
|
#include "gestor_puntuacio_flotant.hpp"
|
|
|
|
#include <string>
|
|
|
|
namespace Effects {
|
|
|
|
GestorPuntuacioFlotant::GestorPuntuacioFlotant(SDL_Renderer* renderer)
|
|
: renderer_(renderer),
|
|
text_(renderer) {
|
|
// Inicialitzar tots els slots com inactius
|
|
for (auto& pf : pool_) {
|
|
pf.actiu = false;
|
|
}
|
|
}
|
|
|
|
void GestorPuntuacioFlotant::crear(int punts, const Punt& posicio) {
|
|
// 1. Trobar slot lliure
|
|
PuntuacioFlotant* pf = trobar_slot_lliure();
|
|
if (!pf)
|
|
return; // Pool ple (improbable)
|
|
|
|
// 2. Inicialitzar puntuació flotant
|
|
pf->text = std::to_string(punts);
|
|
pf->posicio = posicio;
|
|
pf->velocitat = {Defaults::FloatingScore::VELOCITY_X,
|
|
Defaults::FloatingScore::VELOCITY_Y};
|
|
pf->temps_vida = 0.0f;
|
|
pf->temps_max = Defaults::FloatingScore::LIFETIME;
|
|
pf->brightness = 1.0f;
|
|
pf->actiu = true;
|
|
}
|
|
|
|
void GestorPuntuacioFlotant::actualitzar(float delta_time) {
|
|
for (auto& pf : pool_) {
|
|
if (!pf.actiu)
|
|
continue;
|
|
|
|
// 1. Actualitzar posició (deriva cap amunt)
|
|
pf.posicio.x += pf.velocitat.x * delta_time;
|
|
pf.posicio.y += pf.velocitat.y * delta_time;
|
|
|
|
// 2. Actualitzar temps de vida
|
|
pf.temps_vida += delta_time;
|
|
|
|
// 3. Calcular brightness (fade lineal)
|
|
float progress = pf.temps_vida / pf.temps_max; // 0.0 → 1.0
|
|
pf.brightness = 1.0f - progress; // 1.0 → 0.0
|
|
|
|
// 4. Desactivar quan acaba el temps
|
|
if (pf.temps_vida >= pf.temps_max) {
|
|
pf.actiu = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
void GestorPuntuacioFlotant::dibuixar() {
|
|
for (const auto& pf : pool_) {
|
|
if (!pf.actiu)
|
|
continue;
|
|
|
|
// 1. Calcular dimensions del text per centrar-lo
|
|
constexpr float escala = Defaults::FloatingScore::SCALE;
|
|
constexpr float spacing = Defaults::FloatingScore::SPACING;
|
|
float text_width = text_.get_text_width(pf.text, escala, spacing);
|
|
|
|
// 2. Centrar text sobre la posició
|
|
Punt render_pos = {pf.posicio.x - text_width / 2.0f, pf.posicio.y};
|
|
|
|
// 3. Renderitzar amb brightness (fade)
|
|
text_.render(pf.text, render_pos, escala, spacing, pf.brightness);
|
|
}
|
|
}
|
|
|
|
void GestorPuntuacioFlotant::reiniciar() {
|
|
for (auto& pf : pool_) {
|
|
pf.actiu = false;
|
|
}
|
|
}
|
|
|
|
int GestorPuntuacioFlotant::get_num_actius() const {
|
|
int count = 0;
|
|
for (const auto& pf : pool_) {
|
|
if (pf.actiu)
|
|
count++;
|
|
}
|
|
return count;
|
|
}
|
|
|
|
PuntuacioFlotant* GestorPuntuacioFlotant::trobar_slot_lliure() {
|
|
for (auto& pf : pool_) {
|
|
if (!pf.actiu)
|
|
return &pf;
|
|
}
|
|
return nullptr; // Pool ple
|
|
}
|
|
|
|
} // namespace Effects
|