diff --git a/CLAUDE.md b/CLAUDE.md index 9c24149..590c9f9 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -505,6 +505,284 @@ void dibuixar() { } ``` +## Title Screen Ship System (BETA 3.0) + +The title screen features two 3D ships floating on the starfield with perspective rendering, entry/exit animations, and subtle floating motion. + +### Architecture Overview + +**Files:** +- `source/game/title/ship_animator.hpp/cpp` - Ship animation state machine +- `source/core/rendering/shape_renderer.hpp/cpp` - 3D rotation + perspective projection +- `source/core/defaults.hpp` - Title::Ships namespace with all constants +- `source/game/escenes/escena_titol.hpp/cpp` - Integration with title scene + +**Design Philosophy:** +- **Static 3D rotation**: Ships have fixed pitch/yaw/roll angles (not recalculated per frame) +- **Simple Z-axis simulation**: Scale changes simulate depth, not full perspective recalculation +- **State machine**: ENTERING → FLOATING → EXITING states +- **Easing functions**: Smooth transitions with ease_out_quad (entry) and ease_in_quad (exit) +- **Sinusoidal floating**: Organic motion using X/Y oscillation with phase offset + +### 3D Rendering System + +#### Rotation3D Struct (shape_renderer.hpp) + +```cpp +struct Rotation3D { + float pitch; // X-axis rotation (nose up/down) + float yaw; // Y-axis rotation (turn left/right) + float roll; // Z-axis rotation (bank left/right) + + Rotation3D() : pitch(0.0f), yaw(0.0f), roll(0.0f) {} + Rotation3D(float p, float y, float r) : pitch(p), yaw(y), roll(r) {} + + bool has_rotation() const { + return pitch != 0.0f || yaw != 0.0f || roll != 0.0f; + } +}; +``` + +#### 3D Transformation Pipeline (shape_renderer.cpp) + +```cpp +static Punt apply_3d_rotation(float x, float y, const Rotation3D& rot) { + float z = 0.0f; // All 2D points start at Z=0 + + // 1. Pitch (X-axis): Rotate around horizontal axis + float cos_pitch = std::cos(rot.pitch); + float sin_pitch = std::sin(rot.pitch); + float y1 = y * cos_pitch - z * sin_pitch; + float z1 = y * sin_pitch + z * cos_pitch; + + // 2. Yaw (Y-axis): Rotate around vertical axis + float cos_yaw = std::cos(rot.yaw); + float sin_yaw = std::sin(rot.yaw); + float x2 = x * cos_yaw + z1 * sin_yaw; + float z2 = -x * sin_yaw + z1 * cos_yaw; + + // 3. Roll (Z-axis): Rotate around depth axis + float cos_roll = std::cos(rot.roll); + float sin_roll = std::sin(rot.roll); + float x3 = x2 * cos_roll - y1 * sin_roll; + float y3 = x2 * sin_roll + y1 * cos_roll; + + // 4. Perspective projection (Z-divide) + constexpr float perspective_factor = 500.0f; + float scale_factor = perspective_factor / (perspective_factor + z2); + + return {x3 * scale_factor, y3 * scale_factor}; +} +``` + +**Rendering order**: 3D rotation → perspective → 2D scale → 2D rotation → translation + +**Backward compatibility**: Optional `rotation_3d` parameter (default nullptr) - existing code unaffected + +### Ship Animation State Machine + +#### States (ship_animator.hpp) + +```cpp +enum class EstatNau { + ENTERING, // Entering from off-screen + FLOATING, // Floating at target position + EXITING // Flying towards vanishing point +}; + +struct NauTitol { + int jugador_id; // 1 or 2 + EstatNau estat; // Current state + float temps_estat; // Time in current state + + Punt posicio_inicial; // Start position + Punt posicio_objectiu; // Target position + Punt posicio_actual; // Current interpolated position + + float escala_inicial; // Start scale + float escala_objectiu; // Target scale + float escala_actual; // Current interpolated scale + + Rotation3D rotacio_3d; // STATIC 3D rotation (never changes) + float fase_oscilacio; // Oscillation phase accumulator + + std::shared_ptr forma; + bool visible; +}; +``` + +#### State Transitions + +**ENTERING** (2.0s): +- Ships appear from beyond screen edges (calculated radially from clock positions) +- Lerp position: off-screen → target (clock 8 / clock 4) +- Lerp scale: 1.0 → 0.6 (perspective effect) +- Easing: `ease_out_quad` (smooth deceleration) +- Transition: → FLOATING when complete + +**FLOATING** (indefinite): +- Sinusoidal oscillation on X/Y axes +- Different frequencies (0.5 Hz / 0.7 Hz) with phase offset (π/2) +- Creates organic circular/elliptical motion +- Scale constant at 0.6 +- Transition: → EXITING when START pressed + +**EXITING** (1.0s): +- Ships fly towards vanishing point (center: 320, 240) +- Lerp position: current → vanishing point +- Lerp scale: current → 0.0 (simulates Z → infinity) +- Easing: `ease_in_quad` (acceleration) +- Edge case: If START pressed during ENTERING, ships fly from mid-animation position +- Marks invisible when complete + +### Configuration (defaults.hpp) + +```cpp +namespace Title { +namespace Ships { + // Clock positions (polar coordinates from center 320, 240) + constexpr float CLOCK_8_ANGLE = 150.0f * Math::PI / 180.0f; // Bottom-left + constexpr float CLOCK_4_ANGLE = 30.0f * Math::PI / 180.0f; // Bottom-right + constexpr float CLOCK_RADIUS = 150.0f; + + // Target positions (pre-calculated) + constexpr float P1_TARGET_X = 190.0f; // Clock 8 + constexpr float P1_TARGET_Y = 315.0f; + constexpr float P2_TARGET_X = 450.0f; // Clock 4 + constexpr float P2_TARGET_Y = 315.0f; + + // 3D rotations (STATIC - tuned for subtle effect) + constexpr float P1_PITCH = 0.1f; // ~6° nose-up + constexpr float P1_YAW = -0.15f; // ~9° turn left + constexpr float P1_ROLL = -0.05f; // ~3° bank left + + constexpr float P2_PITCH = 0.1f; // ~6° nose-up + constexpr float P2_YAW = 0.15f; // ~9° turn right + constexpr float P2_ROLL = 0.05f; // ~3° bank right + + // Scales + constexpr float ENTRY_SCALE_START = 1.0f; + constexpr float FLOATING_SCALE = 0.6f; + + // Animation durations + constexpr float ENTRY_DURATION = 2.0f; + constexpr float EXIT_DURATION = 1.0f; + constexpr float ENTRY_OFFSET = 200.0f; // Distance beyond screen edge + + // Floating oscillation + constexpr float FLOAT_AMPLITUDE_X = 6.0f; + constexpr float FLOAT_AMPLITUDE_Y = 4.0f; + constexpr float FLOAT_FREQUENCY_X = 0.5f; + constexpr float FLOAT_FREQUENCY_Y = 0.7f; + constexpr float FLOAT_PHASE_OFFSET = 1.57f; // π/2 (90°) + + // Vanishing point + constexpr float VANISHING_POINT_X = 320.0f; + constexpr float VANISHING_POINT_Y = 240.0f; +} +} +``` + +### Integration with EscenaTitol + +#### Constructor + +```cpp +// Initialize ships after starfield +ship_animator_ = std::make_unique(sdl_.obte_renderer()); +ship_animator_->inicialitzar(); + +if (estat_actual_ == EstatTitol::MAIN) { + // Jump to MAIN: ships already in position (no entry animation) + ship_animator_->set_visible(true); +} else { + // Normal flow: ships enter during STARFIELD_FADE_IN + ship_animator_->set_visible(true); + ship_animator_->start_entry_animation(); +} +``` + +#### Update Loop + +```cpp +// Update ships in visible states +if (ship_animator_ && + (estat_actual_ == EstatTitol::STARFIELD_FADE_IN || + estat_actual_ == EstatTitol::STARFIELD || + estat_actual_ == EstatTitol::MAIN || + estat_actual_ == EstatTitol::PLAYER_JOIN_PHASE)) { + ship_animator_->actualitzar(delta_time); +} + +// Trigger exit when START pressed +if (checkStartGameButtonPressed()) { + estat_actual_ = EstatTitol::PLAYER_JOIN_PHASE; + ship_animator_->trigger_exit_animation(); // Edge case: handles mid-ENTERING + Audio::get()->fadeOutMusic(MUSIC_FADE); +} +``` + +#### Draw Loop + +```cpp +// Draw order: starfield → ships → logo → text +if (starfield_) starfield_->dibuixar(); + +if (ship_animator_ && + (estat_actual_ == EstatTitol::STARFIELD_FADE_IN || + estat_actual_ == EstatTitol::STARFIELD || + estat_actual_ == EstatTitol::MAIN || + estat_actual_ == EstatTitol::PLAYER_JOIN_PHASE)) { + ship_animator_->dibuixar(); +} + +// Logo and text drawn after ships (foreground) +``` + +### Timing & Visibility + +**Timeline:** +1. **STARFIELD_FADE_IN** (3.0s): Ships enter from off-screen +2. **STARFIELD** (4.0s): Ships floating +3. **MAIN** (indefinite): Ships floating + logo + text visible +4. **PLAYER_JOIN_PHASE** (2.5s): Ships exit (1.0s) + text blink +5. **BLACK_SCREEN** (2.0s): Ships already invisible (exit completed at 1.0s) + +**Automatic visibility management:** +- Ships marked `visible = false` when exit animation completes (actualitzar_exiting) +- No manual hiding needed - state machine handles it + +### Tuning Notes + +**If ships look distorted:** +- Reduce rotation angles (P1_PITCH, P1_YAW, P1_ROLL, P2_*) +- Current values (0.1, 0.15, 0.05) are tuned for subtle 3D effect +- Angles in radians: 0.1 rad ≈ 6°, 0.15 rad ≈ 9° + +**If ships are too large/small:** +- Adjust FLOATING_SCALE (currently 0.6) +- Adjust ENTRY_SCALE_START (currently 1.0) + +**If floating motion is too jerky/smooth:** +- Adjust FLOAT_AMPLITUDE_X/Y (currently 6.0/4.0 pixels) +- Adjust FLOAT_FREQUENCY_X/Y (currently 0.5/0.7 Hz) + +**If entry/exit animations are too fast/slow:** +- Adjust ENTRY_DURATION (currently 2.0s) +- Adjust EXIT_DURATION (currently 1.0s) + +### Implementation Phases (Completed) + +✅ **Phase 1**: 3D infrastructure (Rotation3D, render_shape extension) +✅ **Phase 2**: Foundation (ship_animator files, constants) +✅ **Phase 3**: Configuration & loading (shape loading, initialization) +✅ **Phase 4**: Floating animation (sinusoidal oscillation) +✅ **Phase 5**: Entry animation (off-screen → position with easing) +✅ **Phase 6**: Exit animation (position → vanishing point) +✅ **Phase 7**: EscenaTitol integration (constructor, update, draw) +✅ **Phase 8**: Polish & tuning (angles, scales, edge cases) +✅ **Phase 9**: Documentation (CLAUDE.md, code comments) + ## Migration Progress ### ✅ Phase 0: Project Setup diff --git a/data/shapes/ship2_p1.shp b/data/shapes/ship2_p1.shp new file mode 100644 index 0000000..7a1db33 --- /dev/null +++ b/data/shapes/ship2_p1.shp @@ -0,0 +1,9 @@ +# ship2_p1.shp - Nau interceptor jugador 1 ROTADA 60° cap al punt de fuga +# Rotació 60° + escala 2.5x per fer-la apuntar des de baix-esquerra cap al centre +# Original: ship2.shp + +name: ship2_p1 +scale: 1.0 +center: 0, 0 + +polyline: 22,-13 18,1 8,24 -10,23 -13,8 -25,-3 -17,-19 8,-16 22,-13 diff --git a/data/shapes/ship2_p2.shp b/data/shapes/ship2_p2.shp new file mode 100644 index 0000000..ee86969 --- /dev/null +++ b/data/shapes/ship2_p2.shp @@ -0,0 +1,26 @@ +# ship2_perspective.shp - Nau J2 (Interceptor) amb perspectiva pre-calculada +# Posición: "4 del reloj" (Abajo-Derecha) +# Dirección: Volant cap al fons (diagonal esquerra-amunt) + +name: ship2_perspective +scale: 1.0 +center: 0, 0 + +# TRANSFORMACIÓ: +# 1. Rotació -45° (apuntant al centre des de baix-dreta) +# 2. Perspectiva asimètrica: +# - Punta (p1): Reduïda i desplaçada cap a l'esquerra-amunt +# - Ala Dreta (p3): "Exterior", molt més gran i propera a l'espectador +# - Ala Esquerra (p7): "Interior", més curta i "amagada" per la perspectiva +# +# Nous Punts (Aprox): +# p1 (Punta): (-5, -5) -> Lluny (apunta al centre) +# p2 (Trans D): (1, -5) +# p3 (Ala D): (12, 4) -> Més a prop (gran) +# p4 (Base D): (7, 9) +# p5 (Base C): (2, 6) +# p6 (Base E): (-3, 7) +# p7 (Ala E): (-8, 1) -> Més lluny (xata) +# p8 (Trans E): (-6, -3) + +polyline: -5,-5 1,-5 12,4 7,9 2,6 -3,7 -8,1 -6,-3 -5,-5 \ No newline at end of file diff --git a/data/shapes/ship_p1.shp b/data/shapes/ship_p1.shp new file mode 100644 index 0000000..6fe6502 --- /dev/null +++ b/data/shapes/ship_p1.shp @@ -0,0 +1,21 @@ +# ship_perspective.shp - Nave con perspectiva pre-calculada +# Posición optimizada: "8 del reloj" (Abajo-Izquierda) +# Dirección: Volando hacia el fondo (centro pantalla) + +name: ship_perspective +scale: 1.0 +center: 0, 0 + +# TRANSFORMACIÓN APLICADA: +# 1. Rotación +45° (apuntando al centro desde abajo-izq) +# 2. Proyección de perspectiva: +# - Punta (p1): Reducida al 60% (simula lejanía) +# - Base (p2, p3): Aumentada al 110% (simula cercanía) +# +# Nuevos Puntos (aprox): +# p1 (Punta): (4, -4) -> Lejos, pequeña y apuntando arriba-dcha +# p2 (Ala Dcha): (3, 11) -> Cerca, lado interior +# p4 (Base Cnt): (-3, 5) -> Centro base +# p3 (Ala Izq): (-11, 2) -> Cerca, lado exterior (más grande) + +polyline: 4,-4 3,11 -3,5 -11,2 4,-4 \ No newline at end of file diff --git a/data/shapes/ship_p2.shp b/data/shapes/ship_p2.shp new file mode 100644 index 0000000..2567711 --- /dev/null +++ b/data/shapes/ship_p2.shp @@ -0,0 +1,9 @@ +# ship_p2.shp - Nau jugador 2 ROTADA -60° cap al punt de fuga +# Rotació -60° + escala 2.5x per fer-la apuntar des de baix-dreta cap al centre +# Original: ship.shp + +name: ship_p2 +scale: 1.0 +center: 0, 0 + +polyline: 26,15 -8,-29 -9,-5 8,29 26,15 diff --git a/source/core/defaults.hpp b/source/core/defaults.hpp index 0231b69..d74af67 100644 --- a/source/core/defaults.hpp +++ b/source/core/defaults.hpp @@ -358,6 +358,60 @@ constexpr int MOLINILLO_SCORE = 200; // Molinillo (agressiu, 50 px/s) } // namespace Enemies +// Title scene ship animations (naus 3D flotants a l'escena de títol) +namespace Title { +namespace Ships { +// Posicions clock (coordenades polars des del centre 320, 240) +// En coordenades de pantalla: 0° = dreta, 90° = baix, 180° = esquerra, 270° = dalt +constexpr float CLOCK_8_ANGLE = 150.0f * Math::PI / 180.0f; // 8 o'clock = bottom-left +constexpr float CLOCK_4_ANGLE = 30.0f * Math::PI / 180.0f; // 4 o'clock = bottom-right +constexpr float CLOCK_RADIUS = 150.0f; // Distància des del centre + +// P1 (8 o'clock, bottom-left) +// 150° → cos(150°)=-0.866, sin(150°)=0.5 → X = 320 - 130 = 190, Y = 240 + 75 = 315 +constexpr float P1_TARGET_X = 190.0f; +constexpr float P1_TARGET_Y = 315.0f; + +// P2 (4 o'clock, bottom-right) +// 30° → cos(30°)=0.866, sin(30°)=0.5 → X = 320 + 130 = 450, Y = 240 + 75 = 315 +constexpr float P2_TARGET_X = 450.0f; +constexpr float P2_TARGET_Y = 315.0f; + +// Escala base de les naus (ajusta aquí per fer-les més grans o petites) +constexpr float SHIP_BASE_SCALE = 2.5f; // Multiplicador global (1.0 = mida original) + +// Escales d'animació (perspectiva ja incorporada a les formes .shp) +constexpr float ENTRY_SCALE_START = 1.5f * SHIP_BASE_SCALE; // Més gran per veure millor +constexpr float FLOATING_SCALE = 1.0f * SHIP_BASE_SCALE; // Mida normal (més grans) + +// Animacions +constexpr float ENTRY_DURATION = 2.0f; // Entrada +constexpr float ENTRY_OFFSET = 340.0f; // Offset fora de pantalla (considera radi màxim 30px * escala 3.75 + marge) +constexpr float EXIT_DURATION = 1.0f; // Sortida (configurable) + +// Flotació (oscil·lació reduïda i diferenciada per nau) +constexpr float FLOAT_AMPLITUDE_X = 4.0f; // Era 6.0f +constexpr float FLOAT_AMPLITUDE_Y = 2.5f; // Era 4.0f + +// Freqüències base +constexpr float FLOAT_FREQUENCY_X_BASE = 0.5f; +constexpr float FLOAT_FREQUENCY_Y_BASE = 0.7f; +constexpr float FLOAT_PHASE_OFFSET = 1.57f; // π/2 (90°) + +// Delays d'entrada +constexpr float P1_ENTRY_DELAY = 0.0f; // P1 entra immediatament +constexpr float P2_ENTRY_DELAY = 0.5f; // P2 entra 0.5s després + +// Multiplicadors de freqüència per a cada nau (variació sutil ±12%) +constexpr float P1_FREQUENCY_MULTIPLIER = 0.88f; // 12% més lenta +constexpr float P2_FREQUENCY_MULTIPLIER = 1.12f; // 12% més ràpida + +// Punt de fuga +constexpr float VANISHING_POINT_X = Game::WIDTH / 2.0f; // 320.0f +constexpr float VANISHING_POINT_Y = Game::HEIGHT / 2.0f; // 240.0f +} // namespace Ships +} // namespace Title + // Floating score numbers (números flotants de puntuació) namespace FloatingScore { constexpr float LIFETIME = 2.0f; // Duració màxima (segons) diff --git a/source/core/rendering/shape_renderer.cpp b/source/core/rendering/shape_renderer.cpp index 6e86c58..87ae3ba 100644 --- a/source/core/rendering/shape_renderer.cpp +++ b/source/core/rendering/shape_renderer.cpp @@ -10,17 +10,55 @@ namespace Rendering { +// Helper: aplicar rotació 3D a un punt 2D (assumeix Z=0) +static Punt apply_3d_rotation(float x, float y, const Rotation3D& rot) { + float z = 0.0f; // Tots els punts 2D comencen a Z=0 + + // Pitch (rotació eix X): cabeceo arriba/baix + float cos_pitch = std::cos(rot.pitch); + float sin_pitch = std::sin(rot.pitch); + float y1 = y * cos_pitch - z * sin_pitch; + float z1 = y * sin_pitch + z * cos_pitch; + + // Yaw (rotació eix Y): guiñada esquerra/dreta + float cos_yaw = std::cos(rot.yaw); + float sin_yaw = std::sin(rot.yaw); + float x2 = x * cos_yaw + z1 * sin_yaw; + float z2 = -x * sin_yaw + z1 * cos_yaw; + + // Roll (rotació eix Z): alabeo lateral + float cos_roll = std::cos(rot.roll); + float sin_roll = std::sin(rot.roll); + float x3 = x2 * cos_roll - y1 * sin_roll; + float y3 = x2 * sin_roll + y1 * cos_roll; + + // Proyecció perspectiva (Z-divide simple) + // Naus volen cap al punt de fuga (320, 240) a "infinit" (Z → +∞) + // Z més gran = més lluny = més petit a pantalla + constexpr float perspective_factor = 500.0f; + float scale_factor = perspective_factor / (perspective_factor + z2); + + return {x3 * scale_factor, y3 * scale_factor}; +} + // Helper: transformar un punt amb rotació, escala i trasllació -static Punt transform_point(const Punt& point, const Punt& shape_centre, const Punt& posicio, float angle, float escala) { +static Punt transform_point(const Punt& point, const Punt& shape_centre, const Punt& posicio, float angle, float escala, const Rotation3D* rotation_3d) { // 1. Centrar el punt respecte al centre de la forma float centered_x = point.x - shape_centre.x; float centered_y = point.y - shape_centre.y; - // 2. Aplicar escala al punt centrat + // 2. Aplicar rotació 3D (si es proporciona) + if (rotation_3d && rotation_3d->has_rotation()) { + Punt rotated_3d = apply_3d_rotation(centered_x, centered_y, *rotation_3d); + centered_x = rotated_3d.x; + centered_y = rotated_3d.y; + } + + // 3. Aplicar escala al punt (després de rotació 3D) float scaled_x = centered_x * escala; float scaled_y = centered_y * escala; - // 3. Aplicar rotació + // 4. Aplicar rotació 2D (Z-axis, tradicional) // IMPORTANT: En el sistema original, angle=0 apunta AMUNT (no dreta) // Per això usem (angle - PI/2) per compensar // Però aquí angle ja ve en el sistema correcte del joc @@ -30,7 +68,7 @@ static Punt transform_point(const Punt& point, const Punt& shape_centre, const P float rotated_x = scaled_x * cos_a - scaled_y * sin_a; float rotated_y = scaled_x * sin_a + scaled_y * cos_a; - // 4. Aplicar trasllació a posició mundial + // 5. Aplicar trasllació a posició mundial return {rotated_x + posicio.x, rotated_y + posicio.y}; } @@ -41,7 +79,8 @@ void render_shape(SDL_Renderer* renderer, float escala, bool dibuixar, float progress, - float brightness) { + float brightness, + const Rotation3D* rotation_3d) { // Verificar que la forma és vàlida if (!shape || !shape->es_valida()) { return; @@ -60,16 +99,16 @@ void render_shape(SDL_Renderer* renderer, if (primitive.type == Graphics::PrimitiveType::POLYLINE) { // POLYLINE: connectar punts consecutius for (size_t i = 0; i < primitive.points.size() - 1; i++) { - Punt p1 = transform_point(primitive.points[i], shape_centre, posicio, angle, escala); - Punt p2 = transform_point(primitive.points[i + 1], shape_centre, posicio, angle, escala); + Punt p1 = transform_point(primitive.points[i], shape_centre, posicio, angle, escala, rotation_3d); + Punt p2 = transform_point(primitive.points[i + 1], shape_centre, posicio, angle, escala, rotation_3d); linea(renderer, static_cast(p1.x), static_cast(p1.y), static_cast(p2.x), static_cast(p2.y), dibuixar, brightness); } } else { // PrimitiveType::LINE // LINE: exactament 2 punts if (primitive.points.size() >= 2) { - Punt p1 = transform_point(primitive.points[0], shape_centre, posicio, angle, escala); - Punt p2 = transform_point(primitive.points[1], shape_centre, posicio, angle, escala); + Punt p1 = transform_point(primitive.points[0], shape_centre, posicio, angle, escala, rotation_3d); + Punt p2 = transform_point(primitive.points[1], shape_centre, posicio, angle, escala, rotation_3d); linea(renderer, static_cast(p1.x), static_cast(p1.y), static_cast(p2.x), static_cast(p2.y), dibuixar, brightness); } diff --git a/source/core/rendering/shape_renderer.hpp b/source/core/rendering/shape_renderer.hpp index 8bb6ffe..e404aea 100644 --- a/source/core/rendering/shape_renderer.hpp +++ b/source/core/rendering/shape_renderer.hpp @@ -12,6 +12,20 @@ namespace Rendering { +// Estructura per rotacions 3D (pitch, yaw, roll) +struct Rotation3D { + float pitch; // Rotació eix X (cabeceo arriba/baix) + float yaw; // Rotació eix Y (guiñada esquerra/dreta) + float roll; // Rotació eix Z (alabeo lateral) + + Rotation3D() : pitch(0.0f), yaw(0.0f), roll(0.0f) {} + Rotation3D(float p, float y, float r) : pitch(p), yaw(y), roll(r) {} + + bool has_rotation() const { + return pitch != 0.0f || yaw != 0.0f || roll != 0.0f; + } +}; + // Renderitzar forma amb transformacions // - renderer: SDL renderer // - shape: forma vectorial a dibuixar @@ -28,6 +42,7 @@ void render_shape(SDL_Renderer* renderer, float escala = 1.0f, bool dibuixar = true, float progress = 1.0f, - float brightness = 1.0f); + float brightness = 1.0f, + const Rotation3D* rotation_3d = nullptr); } // namespace Rendering diff --git a/source/game/escenes/escena_titol.cpp b/source/game/escenes/escena_titol.cpp index 162be04..5fd92b4 100644 --- a/source/game/escenes/escena_titol.cpp +++ b/source/game/escenes/escena_titol.cpp @@ -75,6 +75,19 @@ EscenaTitol::EscenaTitol(SDLManager& sdl, ContextEscenes& context) starfield_->set_brightness(0.0f); } + // Inicialitzar animador de naus 3D + ship_animator_ = std::make_unique(sdl_.obte_renderer()); + ship_animator_->inicialitzar(); + + if (estat_actual_ == EstatTitol::MAIN) { + // Jump to MAIN: empezar entrada inmediatamente + ship_animator_->set_visible(true); + ship_animator_->start_entry_animation(); + } else { + // Flux normal: NO empezar entrada todavía (esperaran a MAIN) + ship_animator_->set_visible(false); + } + // Inicialitzar lletres del títol "ORNI ATTACK!" inicialitzar_titol(); @@ -310,6 +323,15 @@ void EscenaTitol::actualitzar(float delta_time) { starfield_->actualitzar(delta_time); } + // Actualitzar naus (quan visibles) + if (ship_animator_ && + (estat_actual_ == EstatTitol::STARFIELD_FADE_IN || + estat_actual_ == EstatTitol::STARFIELD || + estat_actual_ == EstatTitol::MAIN || + estat_actual_ == EstatTitol::PLAYER_JOIN_PHASE)) { + ship_animator_->actualitzar(delta_time); + } + switch (estat_actual_) { case EstatTitol::STARFIELD_FADE_IN: { temps_acumulat_ += delta_time; @@ -337,6 +359,10 @@ void EscenaTitol::actualitzar(float delta_time) { temps_estat_main_ = 0.0f; // Reset timer al entrar a MAIN animacio_activa_ = false; // Comença estàtic factor_lerp_ = 0.0f; // Sense animació encara + + // Iniciar animació d'entrada de naus + ship_animator_->set_visible(true); + ship_animator_->start_entry_animation(); } break; @@ -372,17 +398,34 @@ void EscenaTitol::actualitzar(float delta_time) { actualitzar_animacio_logo(delta_time); // [NOU] Continuar comprovant si l'altre jugador vol unir-se durant la transició ("late join") - if (checkStartGameButtonPressed()) { - // Updates config_partida_ if pressed, logs are in the method - context_.set_config_partida(config_partida_); + { + bool p1_actiu_abans = config_partida_.jugador1_actiu; + bool p2_actiu_abans = config_partida_.jugador2_actiu; - // Reproducir so de LASER quan el segon jugador s'uneix - Audio::get()->playSound(Defaults::Sound::LASER, Audio::Group::GAME); + if (checkStartGameButtonPressed()) { + // Updates config_partida_ if pressed, logs are in the method + context_.set_config_partida(config_partida_); - // Reiniciar el timer per allargar el temps de transició - temps_acumulat_ = 0.0f; + // Trigger animació de sortida per la nau que acaba d'unir-se + if (ship_animator_) { + if (config_partida_.jugador1_actiu && !p1_actiu_abans) { + ship_animator_->trigger_exit_animation_for_player(1); + std::cout << "[EscenaTitol] P1 late join - ship exiting\n"; + } + if (config_partida_.jugador2_actiu && !p2_actiu_abans) { + ship_animator_->trigger_exit_animation_for_player(2); + std::cout << "[EscenaTitol] P2 late join - ship exiting\n"; + } + } - std::cout << "[EscenaTitol] Segon jugador s'ha unit - so i timer reiniciats\n"; + // Reproducir so de LASER quan el segon jugador s'uneix + Audio::get()->playSound(Defaults::Sound::LASER, Audio::Group::GAME); + + // Reiniciar el timer per allargar el temps de transició + temps_acumulat_ = 0.0f; + + std::cout << "[EscenaTitol] Segon jugador s'ha unit - so i timer reiniciats\n"; + } } if (temps_acumulat_ >= DURACIO_TRANSITION) { @@ -412,11 +455,19 @@ void EscenaTitol::actualitzar(float delta_time) { estat_actual_ = EstatTitol::MAIN; starfield_->set_brightness(BRIGHTNESS_STARFIELD); temps_estat_main_ = 0.0f; + + // Iniciar animació d'entrada de naus + ship_animator_->set_visible(true); + ship_animator_->start_entry_animation(); } } // Verificar boton START para iniciar partida desde MAIN if (estat_actual_ == EstatTitol::MAIN) { + // Guardar estat anterior per detectar qui ha premut START AQUEST frame + bool p1_actiu_abans = config_partida_.jugador1_actiu; + bool p2_actiu_abans = config_partida_.jugador2_actiu; + if (checkStartGameButtonPressed()) { // Configurar partida abans de canviar d'escena context_.set_config_partida(config_partida_); @@ -429,6 +480,19 @@ void EscenaTitol::actualitzar(float delta_time) { context_.canviar_escena(Escena::JOC); estat_actual_ = EstatTitol::PLAYER_JOIN_PHASE; temps_acumulat_ = 0.0f; + + // Trigger animació de sortida NOMÉS per les naus que han premut START + if (ship_animator_) { + if (config_partida_.jugador1_actiu && !p1_actiu_abans) { + ship_animator_->trigger_exit_animation_for_player(1); + std::cout << "[EscenaTitol] P1 ship exiting\n"; + } + if (config_partida_.jugador2_actiu && !p2_actiu_abans) { + ship_animator_->trigger_exit_animation_for_player(2); + std::cout << "[EscenaTitol] P2 ship exiting\n"; + } + } + Audio::get()->fadeOutMusic(MUSIC_FADE); Audio::get()->playSound(Defaults::Sound::LASER, Audio::Group::GAME); } @@ -471,6 +535,15 @@ void EscenaTitol::dibuixar() { starfield_->dibuixar(); } + // Dibuixar naus (després starfield, abans logo) + if (ship_animator_ && + (estat_actual_ == EstatTitol::STARFIELD_FADE_IN || + estat_actual_ == EstatTitol::STARFIELD || + estat_actual_ == EstatTitol::MAIN || + estat_actual_ == EstatTitol::PLAYER_JOIN_PHASE)) { + ship_animator_->dibuixar(); + } + // En els estats STARFIELD_FADE_IN i STARFIELD, només mostrar starfield (sense text) if (estat_actual_ == EstatTitol::STARFIELD_FADE_IN || estat_actual_ == EstatTitol::STARFIELD) { return; diff --git a/source/game/escenes/escena_titol.hpp b/source/game/escenes/escena_titol.hpp index fa3fbab..f55b146 100644 --- a/source/game/escenes/escena_titol.hpp +++ b/source/game/escenes/escena_titol.hpp @@ -19,6 +19,7 @@ #include "core/system/context_escenes.hpp" #include "core/system/game_config.hpp" #include "core/types.hpp" +#include "game/title/ship_animator.hpp" // Botones para INICIAR PARTIDA desde MAIN (solo START) static constexpr std::array START_GAME_BUTTONS = { @@ -53,8 +54,9 @@ class EscenaTitol { SDLManager& sdl_; GestorEscenes::ContextEscenes& context_; GameConfig::ConfigPartida config_partida_; // Configuració de jugadors actius - Graphics::VectorText text_; // Sistema de text vectorial + Graphics::VectorText text_; // Sistema de text vectorial std::unique_ptr starfield_; // Camp d'estrelles de fons + std::unique_ptr ship_animator_; // Naus 3D flotants EstatTitol estat_actual_; // Estat actual de la màquina float temps_acumulat_; // Temps acumulat per l'estat INIT diff --git a/source/game/title/ship_animator.cpp b/source/game/title/ship_animator.cpp new file mode 100644 index 0000000..3272cf3 --- /dev/null +++ b/source/game/title/ship_animator.cpp @@ -0,0 +1,305 @@ +// ship_animator.cpp - Implementació del sistema d'animació de naus +// © 2025 Port a C++20 amb SDL3 + +#include "ship_animator.hpp" + +#include + +#include "core/defaults.hpp" +#include "core/graphics/shape_loader.hpp" +#include "core/math/easing.hpp" + +namespace Title { + +ShipAnimator::ShipAnimator(SDL_Renderer* renderer) + : renderer_(renderer) { +} + +void ShipAnimator::inicialitzar() { + // Carregar formes de naus amb perspectiva pre-calculada + auto forma_p1 = Graphics::ShapeLoader::load("ship_p1.shp"); // Perspectiva esquerra + auto forma_p2 = Graphics::ShapeLoader::load("ship2_p2.shp"); // Perspectiva dreta + + // Configurar nau P1 + naus_[0].jugador_id = 1; + naus_[0].forma = forma_p1; + configurar_nau_p1(naus_[0]); + + // Configurar nau P2 + naus_[1].jugador_id = 2; + naus_[1].forma = forma_p2; + configurar_nau_p2(naus_[1]); +} + +void ShipAnimator::actualitzar(float delta_time) { + // Dispatcher segons estat de cada nau + for (auto& nau : naus_) { + if (!nau.visible) continue; + + switch (nau.estat) { + case EstatNau::ENTERING: + actualitzar_entering(nau, delta_time); + break; + case EstatNau::FLOATING: + actualitzar_floating(nau, delta_time); + break; + case EstatNau::EXITING: + actualitzar_exiting(nau, delta_time); + break; + } + } +} + +void ShipAnimator::dibuixar() const { + for (const auto& nau : naus_) { + if (!nau.visible) continue; + + // Renderitzar nau (perspectiva ja incorporada a la forma) + Rendering::render_shape( + renderer_, + nau.forma, + nau.posicio_actual, + 0.0f, // angle (rotació 2D no utilitzada) + nau.escala_actual, + true, // dibuixar + 1.0f, // progress (sempre visible) + 1.0f // brightness (brillantor màxima) + ); + } +} + +void ShipAnimator::start_entry_animation() { + using namespace Defaults::Title::Ships; + + // Configurar nau P1 per a l'animació d'entrada + naus_[0].estat = EstatNau::ENTERING; + naus_[0].temps_estat = 0.0f; + naus_[0].posicio_inicial = calcular_posicio_fora_pantalla(CLOCK_8_ANGLE); + naus_[0].posicio_actual = naus_[0].posicio_inicial; + naus_[0].escala_actual = naus_[0].escala_inicial; + + // Configurar nau P2 per a l'animació d'entrada + naus_[1].estat = EstatNau::ENTERING; + naus_[1].temps_estat = 0.0f; + naus_[1].posicio_inicial = calcular_posicio_fora_pantalla(CLOCK_4_ANGLE); + naus_[1].posicio_actual = naus_[1].posicio_inicial; + naus_[1].escala_actual = naus_[1].escala_inicial; +} + +void ShipAnimator::trigger_exit_animation() { + // Configurar ambdues naus per a l'animació de sortida + for (auto& nau : naus_) { + // Canviar estat a EXITING + nau.estat = EstatNau::EXITING; + nau.temps_estat = 0.0f; + + // Preservar posició actual (pot estar a mig camí si START es prem durant ENTERING) + nau.posicio_inicial = nau.posicio_actual; + + // La escala objectiu es preserva per a calcular la interpolació + // (escala_actual pot ser diferent si està en ENTERING) + } +} + +void ShipAnimator::trigger_exit_animation_for_player(int jugador_id) { + // Trobar la nau del jugador especificat + for (auto& nau : naus_) { + if (nau.jugador_id == jugador_id) { + // Canviar estat a EXITING només per aquesta nau + nau.estat = EstatNau::EXITING; + nau.temps_estat = 0.0f; + + // Preservar posició actual (pot estar a mig camí si START es prem durant ENTERING) + nau.posicio_inicial = nau.posicio_actual; + + // La escala objectiu es preserva per a calcular la interpolació + // (escala_actual pot ser diferent si està en ENTERING) + break; // Només una nau per jugador + } + } +} + +void ShipAnimator::set_visible(bool visible) { + for (auto& nau : naus_) { + nau.visible = visible; + } +} + +bool ShipAnimator::is_animation_complete() const { + // Comprovar si totes les naus són invisibles (han completat l'animació de sortida) + for (const auto& nau : naus_) { + if (nau.visible) { + return false; // Encara hi ha alguna nau visible + } + } + return true; // Totes les naus són invisibles +} + +// Mètodes d'animació (stubs) +void ShipAnimator::actualitzar_entering(NauTitol& nau, float delta_time) { + using namespace Defaults::Title::Ships; + + nau.temps_estat += delta_time; + + // Esperar al delay abans de començar l'animació + if (nau.temps_estat < nau.entry_delay) { + // Encara en delay: la nau es queda fora de pantalla (posició inicial) + nau.posicio_actual = nau.posicio_inicial; + nau.escala_actual = nau.escala_inicial; + return; + } + + // Càlcul del progrés (restant el delay) + float elapsed = nau.temps_estat - nau.entry_delay; + float progress = std::min(1.0f, elapsed / ENTRY_DURATION); + + // Aplicar easing (ease_out_quad per arribada suau) + float eased_progress = Easing::ease_out_quad(progress); + + // Lerp posició (inicial → objectiu) + nau.posicio_actual.x = Easing::lerp(nau.posicio_inicial.x, nau.posicio_objectiu.x, eased_progress); + nau.posicio_actual.y = Easing::lerp(nau.posicio_inicial.y, nau.posicio_objectiu.y, eased_progress); + + // Lerp escala (gran → normal) + nau.escala_actual = Easing::lerp(nau.escala_inicial, nau.escala_objectiu, eased_progress); + + // Transicionar a FLOATING quan completi + if (elapsed >= ENTRY_DURATION) { + nau.estat = EstatNau::FLOATING; + nau.temps_estat = 0.0f; + nau.fase_oscilacio = 0.0f; // Reiniciar fase d'oscil·lació + } +} + +void ShipAnimator::actualitzar_floating(NauTitol& nau, float delta_time) { + using namespace Defaults::Title::Ships; + + // Actualitzar temps i fase d'oscil·lació + nau.temps_estat += delta_time; + nau.fase_oscilacio += delta_time; + + // Oscil·lació sinusoïdal X/Y (paràmetres específics per nau) + float offset_x = nau.amplitude_x * std::sin(2.0f * Defaults::Math::PI * nau.frequency_x * nau.fase_oscilacio); + float offset_y = nau.amplitude_y * std::sin(2.0f * Defaults::Math::PI * nau.frequency_y * nau.fase_oscilacio + FLOAT_PHASE_OFFSET); + + // Aplicar oscil·lació a la posició objectiu + nau.posicio_actual.x = nau.posicio_objectiu.x + offset_x; + nau.posicio_actual.y = nau.posicio_objectiu.y + offset_y; + + // Escala constant (sense "breathing" per ara) + nau.escala_actual = nau.escala_objectiu; +} + +void ShipAnimator::actualitzar_exiting(NauTitol& nau, float delta_time) { + using namespace Defaults::Title::Ships; + + nau.temps_estat += delta_time; + + // Calcular progrés (0.0 → 1.0) + float progress = std::min(1.0f, nau.temps_estat / EXIT_DURATION); + + // Aplicar easing (ease_in_quad per acceleració cap al punt de fuga) + float eased_progress = Easing::ease_in_quad(progress); + + // Punt de fuga (centre del starfield) + constexpr Punt punt_fuga{VANISHING_POINT_X, VANISHING_POINT_Y}; + + // Lerp posició cap al punt de fuga (preservar posició inicial actual) + // Nota: posicio_inicial conté la posició on estava quan es va activar EXITING + nau.posicio_actual.x = Easing::lerp(nau.posicio_inicial.x, punt_fuga.x, eased_progress); + nau.posicio_actual.y = Easing::lerp(nau.posicio_inicial.y, punt_fuga.y, eased_progress); + + // Escala redueix a 0 (simula Z → infinit) + nau.escala_actual = nau.escala_objectiu * (1.0f - eased_progress); + + // Marcar invisible quan l'animació completi + if (progress >= 1.0f) { + nau.visible = false; + } +} + +// Configuració +void ShipAnimator::configurar_nau_p1(NauTitol& nau) { + using namespace Defaults::Title::Ships; + + // Estat inicial: FLOATING (per test estàtic) + nau.estat = EstatNau::FLOATING; + nau.temps_estat = 0.0f; + + // Posicions (clock 8, bottom-left) + nau.posicio_objectiu = {P1_TARGET_X, P1_TARGET_Y}; + + // Calcular posició inicial (fora de pantalla) + nau.posicio_inicial = calcular_posicio_fora_pantalla(CLOCK_8_ANGLE); + nau.posicio_actual = nau.posicio_inicial; // Començar fora de pantalla + + // Escales + nau.escala_objectiu = FLOATING_SCALE; + nau.escala_actual = FLOATING_SCALE; + nau.escala_inicial = ENTRY_SCALE_START; + + // Flotació + nau.fase_oscilacio = 0.0f; + + // Paràmetres d'entrada + nau.entry_delay = P1_ENTRY_DELAY; + + // Paràmetres d'oscil·lació específics P1 + nau.amplitude_x = FLOAT_AMPLITUDE_X; + nau.amplitude_y = FLOAT_AMPLITUDE_Y; + nau.frequency_x = FLOAT_FREQUENCY_X_BASE * P1_FREQUENCY_MULTIPLIER; + nau.frequency_y = FLOAT_FREQUENCY_Y_BASE * P1_FREQUENCY_MULTIPLIER; + + // Visibilitat + nau.visible = true; +} + +void ShipAnimator::configurar_nau_p2(NauTitol& nau) { + using namespace Defaults::Title::Ships; + + // Estat inicial: FLOATING (per test estàtic) + nau.estat = EstatNau::FLOATING; + nau.temps_estat = 0.0f; + + // Posicions (clock 4, bottom-right) + nau.posicio_objectiu = {P2_TARGET_X, P2_TARGET_Y}; + + // Calcular posició inicial (fora de pantalla) + nau.posicio_inicial = calcular_posicio_fora_pantalla(CLOCK_4_ANGLE); + nau.posicio_actual = nau.posicio_inicial; // Començar fora de pantalla + + // Escales + nau.escala_objectiu = FLOATING_SCALE; + nau.escala_actual = FLOATING_SCALE; + nau.escala_inicial = ENTRY_SCALE_START; + + // Flotació + nau.fase_oscilacio = 0.0f; + + // Paràmetres d'entrada + nau.entry_delay = P2_ENTRY_DELAY; + + // Paràmetres d'oscil·lació específics P2 + nau.amplitude_x = FLOAT_AMPLITUDE_X; + nau.amplitude_y = FLOAT_AMPLITUDE_Y; + nau.frequency_x = FLOAT_FREQUENCY_X_BASE * P2_FREQUENCY_MULTIPLIER; + nau.frequency_y = FLOAT_FREQUENCY_Y_BASE * P2_FREQUENCY_MULTIPLIER; + + // Visibilitat + nau.visible = true; +} + +Punt ShipAnimator::calcular_posicio_fora_pantalla(float angle_rellotge) const { + using namespace Defaults::Title::Ships; + + // Convertir angle del rellotge a radians (per exemple: 240° per clock 8) + // Calcular posició en direcció radial des del centre, però més lluny (+ ENTRY_OFFSET) + float extended_radius = CLOCK_RADIUS + ENTRY_OFFSET; + + float x = Defaults::Game::WIDTH / 2.0f + extended_radius * std::cos(angle_rellotge); + float y = Defaults::Game::HEIGHT / 2.0f + extended_radius * std::sin(angle_rellotge); + + return {x, y}; +} + +} // namespace Title diff --git a/source/game/title/ship_animator.hpp b/source/game/title/ship_animator.hpp new file mode 100644 index 0000000..c0652c2 --- /dev/null +++ b/source/game/title/ship_animator.hpp @@ -0,0 +1,96 @@ +// ship_animator.hpp - Sistema d'animació de naus per a l'escena de títol +// © 2025 Port a C++20 amb SDL3 + +#pragma once + +#include + +#include +#include + +#include "core/graphics/shape.hpp" +#include "core/rendering/shape_renderer.hpp" +#include "core/types.hpp" + +namespace Title { + +// Estats de l'animació de la nau +enum class EstatNau { + ENTERING, // Entrant des de fora de pantalla + FLOATING, // Flotant en posició estàtica + EXITING // Volant cap al punt de fuga +}; + +// Dades d'una nau individual al títol +struct NauTitol { + // Identificació + int jugador_id; // 1 o 2 + + // Estat + EstatNau estat; + float temps_estat; // Temps acumulat en l'estat actual + + // Posicions + Punt posicio_inicial; // Posició d'inici (fora de pantalla per ENTERING) + Punt posicio_objectiu; // Posició objectiu (rellotge 8 o 4) + Punt posicio_actual; // Posició interpolada actual + + // Escales (simulació eix Z) + float escala_inicial; // Escala d'inici (més gran = més a prop) + float escala_objectiu; // Escala objectiu (mida flotació) + float escala_actual; // Escala interpolada actual + + // Flotació + float fase_oscilacio; // Acumulador de fase per moviment sinusoïdal + + // Paràmetres d'entrada + float entry_delay; // Delay abans d'entrar (0.0 per P1, 0.5 per P2) + + // Paràmetres d'oscil·lació per nau + float amplitude_x; + float amplitude_y; + float frequency_x; + float frequency_y; + + // Forma + std::shared_ptr forma; + + // Visibilitat + bool visible; +}; + +// Gestor d'animació de naus per a l'escena de títol +class ShipAnimator { +public: + explicit ShipAnimator(SDL_Renderer* renderer); + + // Cicle de vida + void inicialitzar(); + void actualitzar(float delta_time); + void dibuixar() const; + + // Control d'estat (cridat per EscenaTitol) + void start_entry_animation(); + void trigger_exit_animation(); // Anima totes les naus + void trigger_exit_animation_for_player(int jugador_id); // Anima només una nau (P1=1, P2=2) + + // Control de visibilitat + void set_visible(bool visible); + bool is_animation_complete() const; + +private: + SDL_Renderer* renderer_; + std::array naus_; // Naus P1 i P2 + + // Mètodes d'animació + void actualitzar_entering(NauTitol& nau, float delta_time); + void actualitzar_floating(NauTitol& nau, float delta_time); + void actualitzar_exiting(NauTitol& nau, float delta_time); + + // Configuració + void configurar_nau_p1(NauTitol& nau); + void configurar_nau_p2(NauTitol& nau); + Punt calcular_posicio_fora_pantalla(float angle_rellotge) const; +}; + +} // namespace Title