feat(entities): migrar bullet a data/entities/bullet/bullet.yaml

This commit is contained in:
2026-05-25 11:42:43 +02:00
parent 5fb6c68df4
commit bea844d51e
12 changed files with 306 additions and 107 deletions
+22 -32
View File
@@ -14,38 +14,39 @@
#include "core/rendering/shape_renderer.hpp"
#include "core/types.hpp"
#include "game/constants.hpp"
#include "game/entities/bullet_config.hpp"
#include "game/entities/bullet_registry.hpp"
Bullet::Bullet(Rendering::Renderer* renderer)
: Entity(renderer) {
// Brightness específico para balas
: Entity(renderer),
config_(&BulletRegistry::get()) {
brightness_ = Defaults::Brightness::BALA;
// Configuración del cuerpo físico.
// Las balas son cinemáticas: no colisionan con otros bodies ni paredes.
// El gameplay (GameScene) gestiona los hits con check_collision y la
// salida del PLAYAREA. Por eso radius=0 en el world (no participa en
// resolveBodyCollisions ni resolveBoundsCollisions).
body_.setMass(0.5F); // Ligera (no afecta a nadie, pero por consistencia)
body_.radius = 0.0F; // Sin colisión física (cinemática pura)
body_.restitution = 0.0F; // Irrelevante (no rebota)
body_.linear_damping = 0.0F; // Sin fricción (movimiento rectilíneo uniforme)
body_.angular_damping = 0.0F;
// Cinemàtiques pures: no col·lisionen al PhysicsWorld (body_.radius = 0).
// El gameplay (GameScene) gestiona els hits via checkCollisionSwept i la
// sortida del PLAYAREA.
body_.setMass(config_->physics.mass);
body_.radius = 0.0F;
body_.restitution = config_->physics.restitution;
body_.linear_damping = config_->physics.linear_damping;
body_.angular_damping = config_->physics.angular_damping;
// Cargar shape compartida desde archivo
shape_ = Graphics::ShapeLoader::load("bullet.shp");
shape_ = Graphics::ShapeLoader::load(config_->shape.path);
if (!shape_ || !shape_->isValid()) {
std::cerr << "[Bullet] Error: no s'ha pogut load bullet.shp" << '\n';
std::cerr << "[Bullet] Error: no s'ha pogut carregar " << config_->shape.path << '\n';
}
// Radi de col·lisió derivat del cercle circumscrit de la shape.
const float BOUNDING = (shape_ != nullptr) ? shape_->getBoundingRadius() : 0.0F;
collision_radius_ = BOUNDING * config_->shape.scale * config_->shape.collision_factor;
}
void Bullet::init() {
// Inicialment inactiva
is_active_ = false;
center_ = {.x = 0.0F, .y = 0.0F};
prev_position_ = {.x = 0.0F, .y = 0.0F};
angle_ = 0.0F;
// Reset del cuerpo físico
body_.position = Vec2{};
body_.velocity = Vec2{};
body_.angle = 0.0F;
@@ -54,19 +55,15 @@ void Bullet::init() {
}
void Bullet::fire(const Vec2& position, float angle, uint8_t owner_id, float bullet_speed) {
// Activar bullet
is_active_ = true;
// Almacenar propietario (0=P1, 1=P2)
owner_id_ = owner_id;
// Posición y orientación iniciales = ship
center_ = position;
prev_position_ = position; // Al spawn no hi ha moviment encara: swept degenera a punt-cercle
prev_position_ = position; // spawn: swept degenera a punt-cercle
angle_ = angle;
// Sincronizar el body físico: posición + velocidad cartesiana
// angle - PI/2 porque angle=0 apunta hacia arriba (eje Y negativo SDL)
// Sincronizar el body físic: posició + velocitat cartesiana.
// angle - PI/2 perquè angle=0 apunta cap amunt (eje Y negatiu SDL).
body_.position = position;
body_.angle = angle;
const float DIR_X = std::cos(angle - (Constants::PI / 2.0F));
@@ -75,7 +72,6 @@ void Bullet::fire(const Vec2& position, float angle, uint8_t owner_id, float bul
body_.angular_velocity = 0.0F;
body_.clearAccumulators();
// Reproducir sonido de disparo láser
Audio::get()->playSound(Defaults::Sound::LASER, Audio::Group::GAME);
}
@@ -87,24 +83,18 @@ void Bullet::update(float /*delta_time*/) {
}
void Bullet::postUpdate(float /*delta_time*/) {
// Captura la posició al final del frame anterior abans de sobreescriure center_;
// així el sistema de col·lisions pot fer swept (segment-vs-cercle) entre prev_position_
// i la nova center_, evitant tunneling a velocitats altes.
prev_position_ = center_;
center_ = body_.position;
// angle_ no cambia (las balas no rotan visualmente).
}
void Bullet::desactivar() {
is_active_ = false;
// Detener el cuerpo físico para que no acumule deriva mientras inactiva.
body_.velocity = Vec2{};
body_.angular_velocity = 0.0F;
}
void Bullet::draw() const {
if (is_active_ && shape_) {
// Les bales roten segons l'angle de trayectòria (estático tras disparo)
Rendering::renderShape(renderer_, shape_, center_, angle_, 1.0F, 1.0F, brightness_, Defaults::Palette::BULLET);
Rendering::renderShape(renderer_, shape_, center_, angle_, 1.0F, 1.0F, brightness_, config_->colors.normal);
}
}
+12 -10
View File
@@ -6,10 +6,12 @@
#include <cstdint>
#include "core/defaults.hpp"
#include "core/entities/entity.hpp"
#include "core/types.hpp"
// Forward declaration — la definició completa s'inclou només al .cpp.
struct BulletConfig;
class Bullet : public Entities::Entity {
public:
Bullet()
@@ -25,25 +27,25 @@ class Bullet : public Entities::Entity {
// Override: Interfaz de Entity
[[nodiscard]] auto isActive() const -> bool override { return is_active_; }
// Override: Interfaz de colisión (gameplay-level: PLAYAREA bounds-check)
[[nodiscard]] auto getCollisionRadius() const -> float override {
return Defaults::Entities::BULLET_RADIUS;
}
// Override: Interfaz de colisión (radi derivat al ctor des del shape).
[[nodiscard]] auto getCollisionRadius() const -> float override { return collision_radius_; }
[[nodiscard]] auto isCollidable() const -> bool override {
return is_active_;
}
// Configuració associada (vàlida des del ctor — la bala l'agafa del BulletRegistry).
[[nodiscard]] auto getConfig() const -> const BulletConfig& { return *config_; }
// Getters (API pública sin cambios)
[[nodiscard]] auto getOwnerId() const -> uint8_t { return owner_id_; }
// Posició al final del frame anterior, per a CCD segment-vs-cercle.
[[nodiscard]] auto getPrevPosition() const -> const Vec2& { return prev_position_; }
void desactivar();
private:
// Miembros específicos de Bullet (heredados: renderer_, shape_, center_, angle_, brightness_, body_).
// Inicializados en la declaración para que tanto el ctor por defecto como el que toma renderer
// dejen el objeto en estado coherente (proyectil inactivo, sin owner).
const BulletConfig* config_{nullptr}; // apunta al BulletRegistry; vàlid post-ctor
float collision_radius_{0.0F}; // derivat: shape.bounding_radius × scale × collision_factor
bool is_active_{false};
uint8_t owner_id_{0}; // 0=P1, 1=P2
Vec2 prev_position_{}; // Posició al final del frame anterior (per a swept collision)
Vec2 prev_position_{}; // posició al final del frame anterior (swept CCD)
};
+80
View File
@@ -0,0 +1,80 @@
// bullet_config.cpp - Implementació del parser de BulletConfig
// © 2026 JailDesigner
#include "game/entities/bullet_config.hpp"
#include <cstdint>
#include <exception>
#include <iostream>
#include <string>
namespace {
auto parseColor(const fkyaml::node& node, SDL_Color& out) -> bool {
if (!node.is_sequence() || node.size() != 3) {
return false;
}
const auto R = node[0].get_value<uint32_t>();
const auto G = node[1].get_value<uint32_t>();
const auto B = node[2].get_value<uint32_t>();
out = SDL_Color{
.r = static_cast<uint8_t>(R),
.g = static_cast<uint8_t>(G),
.b = static_cast<uint8_t>(B),
.a = 255};
return true;
}
auto parseShape(const fkyaml::node& node, BulletConfig::ShapeCfg& out) -> bool {
if (!node.contains("shape") || !node["shape"].contains("path")) {
std::cerr << "[BulletConfig] Error: falta 'shape.path'\n";
return false;
}
const auto& s = node["shape"];
out.path = s["path"].get_value<std::string>();
out.scale = s.contains("scale") ? s["scale"].get_value<float>() : 1.0F;
out.collision_factor = s.contains("collision_factor")
? s["collision_factor"].get_value<float>()
: 1.0F;
return true;
}
auto parsePhysics(const fkyaml::node& node, BulletConfig::PhysicsCfg& out) -> bool {
if (!node.contains("physics")) {
std::cerr << "[BulletConfig] Error: falta 'physics'\n";
return false;
}
const auto& p = node["physics"];
out.mass = p["mass"].get_value<float>();
out.restitution = p["restitution"].get_value<float>();
out.linear_damping = p["linear_damping"].get_value<float>();
out.angular_damping = p["angular_damping"].get_value<float>();
out.impact_momentum_factor = p["impact_momentum_factor"].get_value<float>();
return true;
}
auto parseColors(const fkyaml::node& node, BulletConfig::ColorsCfg& out) -> bool {
if (!node.contains("colors") || !parseColor(node["colors"]["normal"], out.normal)) {
std::cerr << "[BulletConfig] Error: 'colors.normal' no és [r,g,b]\n";
return false;
}
return true;
}
} // namespace
auto BulletConfig::fromYaml(const fkyaml::node& node) -> std::optional<BulletConfig> {
try {
BulletConfig cfg;
cfg.name = node.contains("name") ? node["name"].get_value<std::string>() : "bullet";
if (!parseShape(node, cfg.shape)) { return std::nullopt; }
if (!parsePhysics(node, cfg.physics)) { return std::nullopt; }
if (!parseColors(node, cfg.colors)) { return std::nullopt; }
return cfg;
} catch (const std::exception& e) {
std::cerr << "[BulletConfig] Excepció parsejant: " << e.what() << '\n';
return std::nullopt;
}
}
+41
View File
@@ -0,0 +1,41 @@
// bullet_config.hpp - Configuració de la bala carregada des de YAML
// © 2026 JailDesigner
//
// Paral·lel a Player/EnemyConfig. Una sola instància a tot el joc (per ara);
// es comparteix entre totes les bales actives via BulletRegistry.
#pragma once
#include <SDL3/SDL.h>
#include <optional>
#include <string>
#include "external/fkyaml_node.hpp"
struct BulletConfig {
struct ShapeCfg {
std::string path;
float scale;
float collision_factor;
};
struct PhysicsCfg {
float mass;
float restitution;
float linear_damping;
float angular_damping;
float impact_momentum_factor; // factor de transferència bullet→enemic
};
struct ColorsCfg {
SDL_Color normal;
};
std::string name;
ShapeCfg shape;
PhysicsCfg physics;
ColorsCfg colors;
static auto fromYaml(const fkyaml::node& node) -> std::optional<BulletConfig>;
};
+37
View File
@@ -0,0 +1,37 @@
// bullet_registry.cpp - Implementació del registre de bala
// © 2026 JailDesigner
#include "game/entities/bullet_registry.hpp"
#include <cstdlib>
#include <iostream>
#include "core/entities/entity_loader.hpp"
BulletConfig BulletRegistry::config;
bool BulletRegistry::loaded = false;
auto BulletRegistry::load() -> bool {
auto yaml = Entities::EntityLoader::load("bullet");
if (!yaml) {
std::cerr << "[BulletRegistry] Error: no s'ha pogut carregar bullet.yaml\n";
return false;
}
auto cfg = BulletConfig::fromYaml(*yaml);
if (!cfg) {
std::cerr << "[BulletRegistry] Error: format invàlid a bullet.yaml\n";
return false;
}
config = *cfg;
loaded = true;
std::cout << "[BulletRegistry] Configuració de bala carregada.\n";
return true;
}
auto BulletRegistry::get() -> const BulletConfig& {
if (!loaded) {
std::cerr << "[BulletRegistry] FATAL: get() abans de load()\n";
std::exit(EXIT_FAILURE);
}
return config;
}
+26
View File
@@ -0,0 +1,26 @@
// bullet_registry.hpp - Registre estàtic de la configuració de la bala
// © 2026 JailDesigner
//
// Una única instància per a tota la sessió. Es manté el patró registry
// (paral·lel a EnemyRegistry) tot i ser una sola entitat: si el dia de demà
// hi ha més tipus de bala (laser/plasma/etc.) només cal estendre-ho.
#pragma once
#include "game/entities/bullet_config.hpp"
class BulletRegistry {
public:
BulletRegistry() = delete;
// Carrega data/entities/bullet/bullet.yaml. Retorna false si falla.
static auto load() -> bool;
// Accés a la configuració. Avorta amb log fatal si load() no s'ha cridat
// o ha fallat.
static auto get() -> const BulletConfig&;
private:
static BulletConfig config;
static bool loaded;
};