Files
orni-attack/source/game/scenes/game_scene.hpp
T

174 lines
7.7 KiB
C++

// game_scene.hpp - Lógica principal del juego
// © 2026 JailDesigner
#pragma once
#include <array>
#include <cstdint>
#include <memory>
#include <string>
#include "core/graphics/border.hpp"
#include "core/graphics/playfield.hpp"
#include "core/graphics/starfield_parallax.hpp"
#include "core/graphics/vector_text.hpp"
#include "core/physics/physics_world.hpp"
#include "core/rendering/sdl_manager.hpp"
#include "core/system/game_config.hpp"
#include "core/system/scene.hpp"
#include "core/system/scene_context.hpp"
#include "core/types.hpp"
#include "game/constants.hpp"
#include "game/effects/debris_manager.hpp"
#include "game/effects/firework_manager.hpp"
#include "game/effects/floating_score_manager.hpp"
#include "game/effects/trail_manager.hpp"
#include "game/entities/bullet.hpp"
#include "game/entities/enemy.hpp"
#include "game/entities/ship.hpp"
#include "game/stage_system/stage_config.hpp"
#include "game/stage_system/stage_manager.hpp"
#include "game/systems/collision_system.hpp"
#include "game/systems/demo_pilot.hpp"
#include "game/systems/init_hud_animator.hpp"
// Game over state machine
enum class GameOverState : uint8_t {
NONE, // Normal gameplay
CONTINUE, // Continue countdown screen (9→0)
GAME_OVER // Final game over (returning to title)
};
// Clase principal del juego (escena)
class GameScene final : public Scene {
public:
explicit GameScene(SDLManager& sdl, SceneManager::SceneContext& context);
~GameScene() override; // Restaura l'estat dels SFX si la demo els ha silenciat
// Scene interface
void handleEvent(const SDL_Event& event) override;
void update(float delta_time) override;
void draw() override;
[[nodiscard]] auto isFinished() const -> bool override;
private:
SDLManager& sdl_;
SceneManager::SceneContext& context_;
GameConfig::MatchConfig match_config_; // Configuración de jugadors active
// Mundo físico (Fase 5) — integración cinemática + colisiones
Physics::PhysicsWorld physics_world_;
// Efectes visuals
Effects::DebrisManager debris_manager_;
Effects::FireworkManager firework_manager_;
Effects::FloatingScoreManager floating_score_manager_;
Effects::TrailManager trail_manager_;
// Estat del juego
std::array<Ship, 2> ships_; // [0]=P1, [1]=P2
std::array<Enemy, Constants::MAX_ORNIS> enemies_;
// 6 balas: P1=[0,1,2], P2=[3,4,5]. El cast a size_t evita la
// widening conversion implícita que detecta clang-tidy.
std::array<Bullet, static_cast<std::size_t>(Defaults::Entities::MAX_BULLETS_TOTAL)> bullets_;
std::array<float, 2> hit_timer_per_player_; // Death timers per player (seconds)
// Lives and game over system
std::array<int, 2> lives_per_player_; // [0]=P1, [1]=P2
GameOverState game_over_state_; // Game over state machine (NONE, CONTINUE, GAME_OVER)
int continue_counter_; // Continue countdown (9→0)
float continue_tick_timer_; // Timer for countdown tick (1.0s)
int continues_used_; // Continues used this game (0-3 max)
float game_over_timer_; // Final GAME OVER timer before title screen
Vec2 death_position_; // Death position (for respawn)
std::array<int, 2> score_per_player_; // [0]=P1, [1]=P2
// Text vectorial
Graphics::VectorText text_;
// Capa més profunda del fons: estrelles 2D amb parallax (estàtiques de moment).
Graphics::StarfieldParallax starfield_parallax_;
// Fons del playfield (graella + futures capes)
Graphics::Playfield playfield_;
// Border del playfield (4 línies amb desplaçaments i flash per impactes)
Graphics::Border border_;
// [NEW] Stage system
std::unique_ptr<StageSystem::StageSystemConfig> stage_config_;
std::unique_ptr<StageSystem::StageManager> stage_manager_;
// Control de sons de animación INIT_HUD
bool init_hud_rect_sound_played_{false}; // Flag para evitar repetir sonido del rectángulo
// Attract mode (mode DEMO): un pilot IA per nau activa (1 o 2 jugadors).
std::array<Systems::Demo::DemoPilot, 2> demo_pilots_;
std::array<Systems::Demo::Control, 2> demo_ctrls_{}; // Control per nau al frame actual
float demo_timer_{0.0F}; // Temps restant de la demo (→ LOGO)
bool sound_was_enabled_{true}; // Estat dels SFX abans de la demo (per restaurar-lo)
// Funciones privades
// bullet_velocity: velocitat de la bala que ha causat la mort (Vec2{} si no
// ve d'una bala). Es passa al debris perquè els fragments volin en direcció
// de la bala (independent de la inèrcia del cos del ship).
void tocado(uint8_t player_id, const Vec2& bullet_velocity = {.x = 0.0F, .y = 0.0F});
void drawScoreboard(); // Dibuixar marcador de puntuación
void fireBullet(uint8_t player_id); // Shoot bullet from player
[[nodiscard]] auto getSpawnPoint(uint8_t player_id) const -> Vec2; // Get spawn position for player
// [NEW] Continue & Join system
void joinPlayer(uint8_t player_id); // Join inactive player mid-game
void drawContinue(); // Draw continue screen
// [NEW] Stage system helpers
void drawStageMessage(const std::string& message);
// Helpers de renderitzat (extracció de draw() per reduir complexitat).
// Cadascun gestiona un bloc concret; draw() només despatxa segons l'estat.
void drawEnemies() const;
void drawBullets() const;
void drawActiveShipsAlive() const;
void drawContinueState();
void drawGameOverState();
void drawInitHudState();
void drawLevelStartState();
void drawPlayingState();
void drawLevelCompletedState();
// [NEW] Helper del marcador: construeix els 5 segments (score_p1, vides_p1,
// level, score_p2, vides_p2) per a render colorit per segment.
[[nodiscard]] auto buildScoreboardSegments() const -> Systems::InitHud::ScoreboardSegments;
// Sub-pasos de update() (descompuestos en Fase 9d para reducir
// complejidad cognitiva; cada uno es responsable de una sección).
void stepPhysics(float delta_time);
void stepShootingInput();
void stepMidGameJoin();
// Mueve las naves activas: en mode DEMO cada nau usa su pilot IA (demo_ctrls_),
// el resto usa processInput (Input). Compartido por los 3 estados jugables.
void updateShipsControl(float delta_time);
// Mode DEMO: gestiona salida (input→título, timeout/muerte→logo) y calcula
// el control + disparo del pilot. Devuelve true si la escena transiciona.
[[nodiscard]] auto stepDemo(float delta_time) -> bool;
void endDemo();
// Devuelven true si el frame debe salir tras esta sección.
[[nodiscard]] auto stepContinueScreen(float delta_time) -> bool;
[[nodiscard]] auto stepGameOver(float delta_time) -> bool;
// Avanza el death timer / respawn / transición a CONTINUE. Si algún
// jugador está en secuencia de muerte, también actualiza efectos
// (enemigos, balas, debris) que siguen vivos en el escenario.
void stepDeathSequence(float delta_time);
void stepStageStateMachine(float delta_time);
void runStageInitHud(float delta_time);
void runStageLevelStart(float delta_time);
void runStagePlaying(float delta_time);
void runStageLevelCompleted(float delta_time);
// Construeix el Collision::Context del frame actual (referències a tots els
// pools/managers + on_player_hit). Reutilitzat tant per al tick d'IA com
// per a runCollisionDetections.
[[nodiscard]] auto buildCollisionContext() -> Systems::Collision::Context;
// Helper: ejecuta colisiones de gameplay con el Context preparado.
void runCollisionDetections();
};