afegit sistema de punts
This commit is contained in:
99
source/game/effects/gestor_puntuacio_flotant.cpp
Normal file
99
source/game/effects/gestor_puntuacio_flotant.cpp
Normal file
@@ -0,0 +1,99 @@
|
||||
// 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
|
||||
54
source/game/effects/gestor_puntuacio_flotant.hpp
Normal file
54
source/game/effects/gestor_puntuacio_flotant.hpp
Normal file
@@ -0,0 +1,54 @@
|
||||
// gestor_puntuacio_flotant.hpp - Gestor de números de puntuació flotants
|
||||
// © 2025 Port a C++20 amb SDL3
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <array>
|
||||
|
||||
#include "core/defaults.hpp"
|
||||
#include "core/graphics/vector_text.hpp"
|
||||
#include "core/types.hpp"
|
||||
#include "puntuacio_flotant.hpp"
|
||||
|
||||
namespace Effects {
|
||||
|
||||
// Gestor de números de puntuació flotants
|
||||
// Manté un pool de PuntuacioFlotant i gestiona el seu cicle de vida
|
||||
class GestorPuntuacioFlotant {
|
||||
public:
|
||||
explicit GestorPuntuacioFlotant(SDL_Renderer* renderer);
|
||||
|
||||
// Crear número flotant
|
||||
// - punts: valor numèric (100, 150, 200)
|
||||
// - posicio: on apareix (normalment centre d'enemic destruït)
|
||||
void crear(int punts, const Punt& posicio);
|
||||
|
||||
// Actualitzar tots els números actius
|
||||
void actualitzar(float delta_time);
|
||||
|
||||
// Dibuixar tots els números actius
|
||||
void dibuixar();
|
||||
|
||||
// Reiniciar tots (neteja)
|
||||
void reiniciar();
|
||||
|
||||
// Obtenir número actius (debug)
|
||||
int get_num_actius() const;
|
||||
|
||||
private:
|
||||
SDL_Renderer* renderer_;
|
||||
Graphics::VectorText text_; // Sistema de text vectorial
|
||||
|
||||
// Pool de números flotants (màxim concurrent)
|
||||
// Màxim 15 enemics simultanis = màxim 15 números
|
||||
static constexpr int MAX_PUNTUACIONS =
|
||||
Defaults::FloatingScore::MAX_CONCURRENT;
|
||||
std::array<PuntuacioFlotant, MAX_PUNTUACIONS> pool_;
|
||||
|
||||
// Trobar primer slot inactiu
|
||||
PuntuacioFlotant* trobar_slot_lliure();
|
||||
};
|
||||
|
||||
} // namespace Effects
|
||||
33
source/game/effects/puntuacio_flotant.hpp
Normal file
33
source/game/effects/puntuacio_flotant.hpp
Normal file
@@ -0,0 +1,33 @@
|
||||
// puntuacio_flotant.hpp - Número de puntuació que apareix i desapareix
|
||||
// © 2025 Port a C++20 amb SDL3
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "core/types.hpp"
|
||||
|
||||
namespace Effects {
|
||||
|
||||
// PuntuacioFlotant: text animat que mostra punts guanyats
|
||||
// S'activa quan es destrueix un enemic i s'esvaeix després d'un temps
|
||||
struct PuntuacioFlotant {
|
||||
// Text a mostrar (e.g., "100", "150", "200")
|
||||
std::string text;
|
||||
|
||||
// Posició actual (coordenades mundials)
|
||||
Punt posicio;
|
||||
|
||||
// Animació de moviment
|
||||
Punt velocitat; // px/s (normalment cap amunt: {0.0f, -30.0f})
|
||||
|
||||
// Animació de fade
|
||||
float temps_vida; // Temps transcorregut (segons)
|
||||
float temps_max; // Temps de vida màxim (segons)
|
||||
float brightness; // Brillantor calculada (0.0-1.0)
|
||||
|
||||
// Estat
|
||||
bool actiu;
|
||||
};
|
||||
|
||||
} // namespace Effects
|
||||
Reference in New Issue
Block a user