bf83f161b0
Tres tareas de pulido para cerrar la Fase 1 por completo: #pragma once uniforme: - sdl_manager.hpp y game_scene.hpp pasan de #ifndef/#define guards a #pragma once. Los archivos externos (stb_vorbis.h, fkyaml_node.hpp) se mantienen intactos (codigo de terceros). Variables locales y parametros restantes (catalan -> ingles): - fitxer -> file, moviment -> movement, inici -> start - comptador -> counter, escalada -> scaled - missatges -> messages, llista -> list - alçada -> height, amplada -> width, llargada -> length - origen -> origin, distancia -> distance, valor -> value, desti -> target - neteja -> clear, presenta -> present (SDLManager) - total_enemics -> total_enemies, configurar -> configure, iniciar -> start Comentarios catalan -> castellano: - Cabeceras de fichero actualizadas con nombres nuevos (escena_joc.hpp -> game_scene.hpp, etc.) - Palabras tecnicas: trasllacio->traslacion, col-lisio->colision, inicialitzacio->inicializacion, posicio->posicion, rotacio->rotacion, velocitat->velocidad, acceleracio->aceleracion, explosio->explosion, renderitzat->renderizado, calcul->calculo, transicio->transicion, comprovacio->comprobacion, substitucio->sustitucion, utilitzacio->utilizacion, opcio->opcion, configuracio->configuracion, funcio->funcion, distancia, animacio->animacion - Determinantes y conectores: aquest->este, aquesta->esta, amb->con, sense->sin, pero->pero, mai->nunca, nomes->solo, tambe->tambien, sempre->siempre, ja->ya, mateix->mismo, vegada->vez, dintre->dentro, fora->fuera, dreta->derecha, esquerra->izquierda, sortir->salir, sortida->salida, petit->pequeno, gran->grande, nou->nuevo, vell->viejo, molt->mucho, els->los, les->las, totes les->todas las, d'->de, com->como, quan->cuando, mentre->mientras, despres->despues, abans->antes, durant->durante, fins->hasta, encara->aun, llavors->entonces, aixi->asi, perque->porque - Sustantivos: classe->clase, metode->metodo, parametre->parametro, versio->version, entitat->entidad, joc->juego, nivell->nivel, enemic->enemigo, naus->naves, bales->balas, fitxer->archivo, pentagon->pentagono, pun- tuacio->puntuacion, flotant->flotante, titol->titulo, objectiu->objetivo, mostra->muestra, tipus->tipo Strings literales preservados en valenciano segun decision del usuario: el texto del HUD del juego (puntuaciones, mensajes en pantalla, archivo de config) se mantiene en valenciano original. 70 fitxers tocats, +1117 / -1123. Compila i enllaca. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
214 lines
6.7 KiB
C++
214 lines
6.7 KiB
C++
// ship.cpp - Implementació de la nave del player
|
|
// © 1999 Visente i Sergi (versión Pascal)
|
|
// © 2025 Port a C++20 con SDL3
|
|
|
|
#include "game/entities/ship.hpp"
|
|
|
|
#include <SDL3/SDL.h>
|
|
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <cstdint>
|
|
#include <iostream>
|
|
|
|
#include "core/defaults.hpp"
|
|
#include "core/entities/entity.hpp"
|
|
#include "core/graphics/shape_loader.hpp"
|
|
#include "core/input/input.hpp"
|
|
#include "core/input/input_types.hpp"
|
|
#include "core/rendering/shape_renderer.hpp"
|
|
#include "core/types.hpp"
|
|
#include "game/constants.hpp"
|
|
|
|
Ship::Ship(SDL_Renderer* renderer, const char* shape_file)
|
|
: Entity(renderer),
|
|
velocity_(0.0F),
|
|
is_hit_(false),
|
|
invulnerable_timer_(0.0F) {
|
|
// [NUEVO] Brightness específic per naves
|
|
brightness_ = Defaults::Brightness::NAU;
|
|
|
|
// [NUEVO] Carregar shape compartida desde file
|
|
shape_ = Graphics::ShapeLoader::load(shape_file);
|
|
|
|
if (!shape_ || !shape_->isValid()) {
|
|
std::cerr << "[Ship] Error: no s'ha pogut load " << shape_file << '\n';
|
|
}
|
|
}
|
|
|
|
void Ship::init(const Vec2* spawn_point, bool activar_invulnerabilitat) {
|
|
// Inicialización de la ship (triangle)
|
|
// Basat en el codi Pascal original: lines 380-384
|
|
// Copiat de joc_asteroides.cpp línies 30-44
|
|
|
|
// [NUEVO] Ya no necesario configure points polars - la geometria es carrega del
|
|
// file Solo inicialitzem l'state de la instància
|
|
|
|
// Use custom spawn point if provided, otherwise use center
|
|
if (spawn_point != nullptr) {
|
|
center_.x = spawn_point->x;
|
|
center_.y = spawn_point->y;
|
|
} else {
|
|
// Default: center of play area
|
|
float centre_x;
|
|
float centre_y;
|
|
Constants::obtenir_centre_zona(centre_x, centre_y);
|
|
center_.x = static_cast<int>(centre_x);
|
|
center_.y = static_cast<int>(centre_y);
|
|
}
|
|
|
|
// Estat inicial
|
|
angle_ = 0.0F;
|
|
velocity_ = 0.0F;
|
|
|
|
// Activar invulnerabilidad solo si es respawn
|
|
if (activar_invulnerabilitat) {
|
|
invulnerable_timer_ = Defaults::Ship::INVULNERABILITY_DURATION;
|
|
} else {
|
|
invulnerable_timer_ = 0.0F;
|
|
}
|
|
|
|
is_hit_ = false;
|
|
}
|
|
|
|
void Ship::processInput(float delta_time, uint8_t player_id) {
|
|
// Processar input continu (como teclapuls() del Pascal original)
|
|
// Basat en joc_asteroides.cpp línies 66-85
|
|
// Solo processa input si la ship está viva
|
|
if (is_hit_) {
|
|
return;
|
|
}
|
|
|
|
auto* input = Input::get();
|
|
|
|
// Processar input segons el player
|
|
if (player_id == 0) {
|
|
// Jugador 1
|
|
if (input->checkActionPlayer1(InputAction::RIGHT, Input::ALLOW_REPEAT)) {
|
|
angle_ += Defaults::Physics::ROTATION_SPEED * delta_time;
|
|
}
|
|
|
|
if (input->checkActionPlayer1(InputAction::LEFT, Input::ALLOW_REPEAT)) {
|
|
angle_ -= Defaults::Physics::ROTATION_SPEED * delta_time;
|
|
}
|
|
|
|
if (input->checkActionPlayer1(InputAction::THRUST, Input::ALLOW_REPEAT)) {
|
|
if (velocity_ < Defaults::Physics::MAX_VELOCITY) {
|
|
velocity_ += Defaults::Physics::ACCELERATION * delta_time;
|
|
velocity_ = std::min(velocity_, Defaults::Physics::MAX_VELOCITY);
|
|
}
|
|
}
|
|
} else {
|
|
// Jugador 2
|
|
if (input->checkActionPlayer2(InputAction::RIGHT, Input::ALLOW_REPEAT)) {
|
|
angle_ += Defaults::Physics::ROTATION_SPEED * delta_time;
|
|
}
|
|
|
|
if (input->checkActionPlayer2(InputAction::LEFT, Input::ALLOW_REPEAT)) {
|
|
angle_ -= Defaults::Physics::ROTATION_SPEED * delta_time;
|
|
}
|
|
|
|
if (input->checkActionPlayer2(InputAction::THRUST, Input::ALLOW_REPEAT)) {
|
|
if (velocity_ < Defaults::Physics::MAX_VELOCITY) {
|
|
velocity_ += Defaults::Physics::ACCELERATION * delta_time;
|
|
velocity_ = std::min(velocity_, Defaults::Physics::MAX_VELOCITY);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void Ship::update(float delta_time) {
|
|
// Solo update si la ship está viva
|
|
if (is_hit_) {
|
|
return;
|
|
}
|
|
|
|
// Decrementar timer de invulnerabilidad
|
|
if (invulnerable_timer_ > 0.0F) {
|
|
invulnerable_timer_ -= delta_time;
|
|
invulnerable_timer_ = std::max(invulnerable_timer_, 0.0F);
|
|
}
|
|
|
|
// Aplicar física (movement + fricció)
|
|
applyPhysics(delta_time);
|
|
}
|
|
|
|
void Ship::draw() const {
|
|
// Solo draw si la ship está viva
|
|
if (is_hit_) {
|
|
return;
|
|
}
|
|
|
|
// Si invulnerable, parpadear (toggle on/off)
|
|
if (isInvulnerable()) {
|
|
// Calcular ciclo de parpadeo
|
|
float blink_cycle = Defaults::Ship::BLINK_VISIBLE_TIME +
|
|
Defaults::Ship::BLINK_INVISIBLE_TIME;
|
|
float time_in_cycle = std::fmod(invulnerable_timer_, blink_cycle);
|
|
|
|
// Si estamos en fase invisible, no dibujar
|
|
if (time_in_cycle < Defaults::Ship::BLINK_INVISIBLE_TIME) {
|
|
return; // No dibujar durante fase invisible
|
|
}
|
|
}
|
|
|
|
if (!shape_) {
|
|
return;
|
|
}
|
|
|
|
// Escalar velocity per l'efecte visual (200 px/s → ~6 px de efecte)
|
|
// El codi Pascal original sumava velocity (0-6) al radi per donar
|
|
// sensació de "empenta". Ara velocity está en px/s (0-200).
|
|
// Basat en joc_asteroides.cpp línies 127-134
|
|
//
|
|
// [NUEVO] Convertir suma de velocitat_visual a scale multiplicativa
|
|
// Radio base del ship = 12 px
|
|
// velocitat_visual = 0-6 → r = 12-18 → scale = 1.0-1.5
|
|
float velocitat_visual = velocity_ / 33.33F;
|
|
float scale = 1.0F + (velocitat_visual / 12.0F);
|
|
|
|
Rendering::render_shape(renderer_, shape_, center_, angle_, scale, 1.0F, brightness_);
|
|
}
|
|
|
|
void Ship::applyPhysics(float delta_time) {
|
|
// Aplicar física de movement
|
|
// Basat en joc_asteroides.cpp línies 87-113
|
|
|
|
// Calcular nueva posición basada en velocity i angle
|
|
// S'usa (angle - PI/2) perquè angle=0 apunta sin amunt, no hacia la derecha
|
|
// velocity_ está en px/s, así que multipliquem per delta_time
|
|
float dy =
|
|
((velocity_ * delta_time) * std::sin(angle_ - (Constants::PI / 2.0F))) +
|
|
center_.y;
|
|
float dx =
|
|
((velocity_ * delta_time) * std::cos(angle_ - (Constants::PI / 2.0F))) +
|
|
center_.x;
|
|
|
|
// Boundary checking con radi de la ship
|
|
// CORRECCIÓ: Usar límits segurs i inequalitats inclusives
|
|
float min_x;
|
|
float max_x;
|
|
float min_y;
|
|
float max_y;
|
|
Constants::obtenir_limits_zona_segurs(Defaults::Entities::SHIP_RADIUS,
|
|
min_x,
|
|
max_x,
|
|
min_y,
|
|
max_y);
|
|
|
|
// Inequalitats inclusives (>= i <=)
|
|
if (dy >= min_y && dy <= max_y) {
|
|
center_.y = dy;
|
|
}
|
|
|
|
if (dx >= min_x && dx <= max_x) {
|
|
center_.x = dx;
|
|
}
|
|
|
|
// Fricció - desacceleració gradual (time-based)
|
|
if (velocity_ > 0.1F) {
|
|
velocity_ -= Defaults::Physics::FRICTION * delta_time;
|
|
velocity_ = std::max(velocity_, 0.0F);
|
|
}
|
|
}
|