LOGO explota
This commit is contained in:
@@ -3,84 +3,88 @@
|
||||
// © 2025 Port a C++20 amb SDL3
|
||||
|
||||
#include "game/entities/bala.hpp"
|
||||
#include "core/graphics/shape_loader.hpp"
|
||||
#include "core/rendering/shape_renderer.hpp"
|
||||
#include "game/constants.hpp"
|
||||
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
|
||||
Bala::Bala(SDL_Renderer *renderer)
|
||||
: renderer_(renderer), centre_({0.0f, 0.0f}), angle_(0.0f),
|
||||
velocitat_(0.0f), esta_(false) {
|
||||
#include "core/graphics/shape_loader.hpp"
|
||||
#include "core/rendering/shape_renderer.hpp"
|
||||
#include "game/constants.hpp"
|
||||
|
||||
// [NUEVO] Carregar forma compartida des de fitxer
|
||||
forma_ = Graphics::ShapeLoader::load("bullet.shp");
|
||||
Bala::Bala(SDL_Renderer* renderer)
|
||||
: renderer_(renderer),
|
||||
centre_({0.0f, 0.0f}),
|
||||
angle_(0.0f),
|
||||
velocitat_(0.0f),
|
||||
esta_(false) {
|
||||
// [NUEVO] Carregar forma compartida des de fitxer
|
||||
forma_ = Graphics::ShapeLoader::load("bullet.shp");
|
||||
|
||||
if (!forma_ || !forma_->es_valida()) {
|
||||
std::cerr << "[Bala] Error: no s'ha pogut carregar bullet.shp" << std::endl;
|
||||
}
|
||||
if (!forma_ || !forma_->es_valida()) {
|
||||
std::cerr << "[Bala] Error: no s'ha pogut carregar bullet.shp" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void Bala::inicialitzar() {
|
||||
// Inicialment inactiva
|
||||
esta_ = false;
|
||||
centre_ = {0.0f, 0.0f};
|
||||
angle_ = 0.0f;
|
||||
velocitat_ = 0.0f;
|
||||
// Inicialment inactiva
|
||||
esta_ = false;
|
||||
centre_ = {0.0f, 0.0f};
|
||||
angle_ = 0.0f;
|
||||
velocitat_ = 0.0f;
|
||||
}
|
||||
|
||||
void Bala::disparar(const Punt &posicio, float angle) {
|
||||
// Activar bala i posicionar-la a la nau
|
||||
// Basat en joc_asteroides.cpp línies 188-200
|
||||
void Bala::disparar(const Punt& posicio, float angle) {
|
||||
// Activar bala i posicionar-la a la nau
|
||||
// Basat en joc_asteroides.cpp línies 188-200
|
||||
|
||||
// Activar bala
|
||||
esta_ = true;
|
||||
// Activar bala
|
||||
esta_ = true;
|
||||
|
||||
// Posició inicial = centre de la nau
|
||||
centre_.x = posicio.x;
|
||||
centre_.y = posicio.y;
|
||||
// Posició inicial = centre de la nau
|
||||
centre_.x = posicio.x;
|
||||
centre_.y = posicio.y;
|
||||
|
||||
// Angle = angle de la nau (dispara en la direcció que apunta)
|
||||
angle_ = angle;
|
||||
// Angle = angle de la nau (dispara en la direcció que apunta)
|
||||
angle_ = angle;
|
||||
|
||||
// Velocitat alta (el joc Pascal original usava 7 px/frame)
|
||||
// 7 px/frame × 20 FPS = 140 px/s
|
||||
velocitat_ = 140.0f;
|
||||
// Velocitat alta (el joc Pascal original usava 7 px/frame)
|
||||
// 7 px/frame × 20 FPS = 140 px/s
|
||||
velocitat_ = 140.0f;
|
||||
}
|
||||
|
||||
void Bala::actualitzar(float delta_time) {
|
||||
if (esta_) {
|
||||
mou(delta_time);
|
||||
}
|
||||
if (esta_) {
|
||||
mou(delta_time);
|
||||
}
|
||||
}
|
||||
|
||||
void Bala::dibuixar() const {
|
||||
if (esta_ && forma_) {
|
||||
// [NUEVO] Usar render_shape en lloc de rota_pol
|
||||
// Les bales no roten visualment (angle sempre 0.0f)
|
||||
Rendering::render_shape(renderer_, forma_, centre_, 0.0f, 1.0f, true);
|
||||
}
|
||||
if (esta_ && forma_) {
|
||||
// [NUEVO] Usar render_shape en lloc de rota_pol
|
||||
// Les bales no roten visualment (angle sempre 0.0f)
|
||||
Rendering::render_shape(renderer_, forma_, centre_, 0.0f, 1.0f, true);
|
||||
}
|
||||
}
|
||||
|
||||
void Bala::mou(float delta_time) {
|
||||
// Moviment rectilini de la bala
|
||||
// Basat en el codi Pascal original: procedure mou_bales
|
||||
// Copiat EXACTAMENT de joc_asteroides.cpp línies 396-419
|
||||
// Moviment rectilini de la bala
|
||||
// Basat en el codi Pascal original: procedure mou_bales
|
||||
// Copiat EXACTAMENT de joc_asteroides.cpp línies 396-419
|
||||
|
||||
// Calcular nova posició (moviment polar time-based)
|
||||
// velocitat ja està en px/s (140 px/s), només cal multiplicar per delta_time
|
||||
float velocitat_efectiva = velocitat_ * delta_time;
|
||||
// Calcular nova posició (moviment polar time-based)
|
||||
// velocitat ja està en px/s (140 px/s), només cal multiplicar per delta_time
|
||||
float velocitat_efectiva = velocitat_ * delta_time;
|
||||
|
||||
// Calcular desplaçament (angle-PI/2 perquè angle=0 apunta amunt)
|
||||
float dy = velocitat_efectiva * std::sin(angle_ - Constants::PI / 2.0f);
|
||||
float dx = velocitat_efectiva * std::cos(angle_ - Constants::PI / 2.0f);
|
||||
// Calcular desplaçament (angle-PI/2 perquè angle=0 apunta amunt)
|
||||
float dy = velocitat_efectiva * std::sin(angle_ - Constants::PI / 2.0f);
|
||||
float dx = velocitat_efectiva * std::cos(angle_ - Constants::PI / 2.0f);
|
||||
|
||||
// Acumulació directa amb precisió subpíxel
|
||||
centre_.y += dy;
|
||||
centre_.x += dx;
|
||||
// Acumulació directa amb precisió subpíxel
|
||||
centre_.y += dy;
|
||||
centre_.x += dx;
|
||||
|
||||
// Desactivar si surt de la zona de joc (no rebota com els ORNIs)
|
||||
if (!Constants::dins_zona_joc(centre_.x, centre_.y)) {
|
||||
esta_ = false;
|
||||
}
|
||||
// Desactivar si surt de la zona de joc (no rebota com els ORNIs)
|
||||
if (!Constants::dins_zona_joc(centre_.x, centre_.y)) {
|
||||
esta_ = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,37 +3,40 @@
|
||||
// © 2025 Port a C++20 amb SDL3
|
||||
|
||||
#pragma once
|
||||
#include "core/graphics/shape.hpp"
|
||||
#include "core/types.hpp"
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "core/graphics/shape.hpp"
|
||||
#include "core/types.hpp"
|
||||
|
||||
class Bala {
|
||||
public:
|
||||
Bala() : renderer_(nullptr) {}
|
||||
Bala(SDL_Renderer *renderer);
|
||||
public:
|
||||
Bala()
|
||||
: renderer_(nullptr) {}
|
||||
Bala(SDL_Renderer* renderer);
|
||||
|
||||
void inicialitzar();
|
||||
void disparar(const Punt &posicio, float angle);
|
||||
void actualitzar(float delta_time);
|
||||
void dibuixar() const;
|
||||
void inicialitzar();
|
||||
void disparar(const Punt& posicio, float angle);
|
||||
void actualitzar(float delta_time);
|
||||
void dibuixar() const;
|
||||
|
||||
// Getters (API pública sense canvis)
|
||||
bool esta_activa() const { return esta_; }
|
||||
const Punt &get_centre() const { return centre_; }
|
||||
void desactivar() { esta_ = false; }
|
||||
// Getters (API pública sense canvis)
|
||||
bool esta_activa() const { return esta_; }
|
||||
const Punt& get_centre() const { return centre_; }
|
||||
void desactivar() { esta_ = false; }
|
||||
|
||||
private:
|
||||
SDL_Renderer *renderer_;
|
||||
private:
|
||||
SDL_Renderer* renderer_;
|
||||
|
||||
// [NUEVO] Forma vectorial (compartida entre totes les bales)
|
||||
std::shared_ptr<Graphics::Shape> forma_;
|
||||
// [NUEVO] Forma vectorial (compartida entre totes les bales)
|
||||
std::shared_ptr<Graphics::Shape> forma_;
|
||||
|
||||
// [NUEVO] Estat de la instància (separat de la geometria)
|
||||
Punt centre_;
|
||||
float angle_;
|
||||
float velocitat_;
|
||||
bool esta_;
|
||||
// [NUEVO] Estat de la instància (separat de la geometria)
|
||||
Punt centre_;
|
||||
float angle_;
|
||||
float velocitat_;
|
||||
bool esta_;
|
||||
|
||||
void mou(float delta_time);
|
||||
void mou(float delta_time);
|
||||
};
|
||||
|
||||
@@ -3,117 +3,123 @@
|
||||
// © 2025 Port a C++20 amb SDL3
|
||||
|
||||
#include "game/entities/enemic.hpp"
|
||||
#include "core/graphics/shape_loader.hpp"
|
||||
#include "core/rendering/shape_renderer.hpp"
|
||||
#include "game/constants.hpp"
|
||||
|
||||
#include <cmath>
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
|
||||
Enemic::Enemic(SDL_Renderer *renderer)
|
||||
: renderer_(renderer), centre_({0.0f, 0.0f}), angle_(0.0f),
|
||||
velocitat_(0.0f), drotacio_(0.0f), rotacio_(0.0f), esta_(false) {
|
||||
#include "core/graphics/shape_loader.hpp"
|
||||
#include "core/rendering/shape_renderer.hpp"
|
||||
#include "game/constants.hpp"
|
||||
|
||||
// [NUEVO] Carregar forma compartida des de fitxer
|
||||
forma_ = Graphics::ShapeLoader::load("enemy_pentagon.shp");
|
||||
Enemic::Enemic(SDL_Renderer* renderer)
|
||||
: renderer_(renderer),
|
||||
centre_({0.0f, 0.0f}),
|
||||
angle_(0.0f),
|
||||
velocitat_(0.0f),
|
||||
drotacio_(0.0f),
|
||||
rotacio_(0.0f),
|
||||
esta_(false) {
|
||||
// [NUEVO] Carregar forma compartida des de fitxer
|
||||
forma_ = Graphics::ShapeLoader::load("enemy_pentagon.shp");
|
||||
|
||||
if (!forma_ || !forma_->es_valida()) {
|
||||
std::cerr << "[Enemic] Error: no s'ha pogut carregar enemy_pentagon.shp"
|
||||
<< std::endl;
|
||||
}
|
||||
if (!forma_ || !forma_->es_valida()) {
|
||||
std::cerr << "[Enemic] Error: no s'ha pogut carregar enemy_pentagon.shp"
|
||||
<< std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void Enemic::inicialitzar() {
|
||||
// Inicialitzar enemic (pentàgon)
|
||||
// Copiat de joc_asteroides.cpp línies 41-54
|
||||
// Inicialitzar enemic (pentàgon)
|
||||
// Copiat de joc_asteroides.cpp línies 41-54
|
||||
|
||||
// [NUEVO] Ja no cal crear_poligon_regular - la geometria es carrega del
|
||||
// fitxer Només inicialitzem l'estat de la instància
|
||||
// [NUEVO] Ja no cal crear_poligon_regular - la geometria es carrega del
|
||||
// fitxer Només inicialitzem l'estat de la instància
|
||||
|
||||
// Posició aleatòria dins de l'àrea de joc
|
||||
centre_.x = static_cast<float>((std::rand() % 580) + 30); // 30-610
|
||||
centre_.y = static_cast<float>((std::rand() % 420) + 30); // 30-450
|
||||
// Posició aleatòria dins de l'àrea de joc
|
||||
centre_.x = static_cast<float>((std::rand() % 580) + 30); // 30-610
|
||||
centre_.y = static_cast<float>((std::rand() % 420) + 30); // 30-450
|
||||
|
||||
// Angle aleatori de moviment
|
||||
angle_ = (std::rand() % 360) * Constants::PI / 180.0f;
|
||||
// Angle aleatori de moviment
|
||||
angle_ = (std::rand() % 360) * Constants::PI / 180.0f;
|
||||
|
||||
// Velocitat (2 px/frame original * 20 FPS = 40 px/s)
|
||||
velocitat_ = 40.0f;
|
||||
// Velocitat (2 px/frame original * 20 FPS = 40 px/s)
|
||||
velocitat_ = 40.0f;
|
||||
|
||||
// Rotació visual aleatòria (rad/s)
|
||||
// Original Pascal: random * 0.1 rad/frame * 20 FPS ≈ 2 rad/s
|
||||
drotacio_ = (static_cast<float>(std::rand()) / RAND_MAX) * 2.0f;
|
||||
rotacio_ = 0.0f;
|
||||
// Rotació visual aleatòria (rad/s)
|
||||
// Original Pascal: random * 0.1 rad/frame * 20 FPS ≈ 2 rad/s
|
||||
drotacio_ = (static_cast<float>(std::rand()) / RAND_MAX) * 2.0f;
|
||||
rotacio_ = 0.0f;
|
||||
|
||||
// Activar
|
||||
esta_ = true;
|
||||
// Activar
|
||||
esta_ = true;
|
||||
}
|
||||
|
||||
void Enemic::actualitzar(float delta_time) {
|
||||
if (esta_) {
|
||||
// Moviment autònom
|
||||
mou(delta_time);
|
||||
if (esta_) {
|
||||
// Moviment autònom
|
||||
mou(delta_time);
|
||||
|
||||
// Rotació visual (time-based: drotacio_ està en rad/s)
|
||||
rotacio_ += drotacio_ * delta_time;
|
||||
}
|
||||
// Rotació visual (time-based: drotacio_ està en rad/s)
|
||||
rotacio_ += drotacio_ * delta_time;
|
||||
}
|
||||
}
|
||||
|
||||
void Enemic::dibuixar() const {
|
||||
if (esta_ && forma_) {
|
||||
// [NUEVO] Usar render_shape en lloc de rota_pol
|
||||
Rendering::render_shape(renderer_, forma_, centre_, rotacio_, 1.0f, true);
|
||||
}
|
||||
if (esta_ && forma_) {
|
||||
// [NUEVO] Usar render_shape en lloc de rota_pol
|
||||
Rendering::render_shape(renderer_, forma_, centre_, rotacio_, 1.0f, true);
|
||||
}
|
||||
}
|
||||
|
||||
void Enemic::mou(float delta_time) {
|
||||
// Moviment autònom d'ORNI (enemic pentàgon)
|
||||
// Basat EXACTAMENT en el codi Pascal original: ASTEROID.PAS lines 279-293
|
||||
// Copiat EXACTAMENT de joc_asteroides.cpp línies 348-394
|
||||
//
|
||||
// IMPORTANT: El Pascal original NO té canvi aleatori continu!
|
||||
// Només ajusta l'angle quan toca una paret.
|
||||
// Moviment autònom d'ORNI (enemic pentàgon)
|
||||
// Basat EXACTAMENT en el codi Pascal original: ASTEROID.PAS lines 279-293
|
||||
// Copiat EXACTAMENT de joc_asteroides.cpp línies 348-394
|
||||
//
|
||||
// IMPORTANT: El Pascal original NO té canvi aleatori continu!
|
||||
// Només ajusta l'angle quan toca una paret.
|
||||
|
||||
// Calcular nova posició PROPUESTA (time-based, però lògica Pascal)
|
||||
// velocitat_ ja està en px/s (40 px/s), multiplicar per delta_time
|
||||
float velocitat_efectiva = velocitat_ * delta_time;
|
||||
// Calcular nova posició PROPUESTA (time-based, però lògica Pascal)
|
||||
// velocitat_ ja està en px/s (40 px/s), multiplicar per delta_time
|
||||
float velocitat_efectiva = velocitat_ * delta_time;
|
||||
|
||||
// Calcular desplaçament (angle-PI/2 perquè angle=0 apunta amunt)
|
||||
float dy = velocitat_efectiva * std::sin(angle_ - Constants::PI / 2.0f);
|
||||
float dx = velocitat_efectiva * std::cos(angle_ - Constants::PI / 2.0f);
|
||||
// Calcular desplaçament (angle-PI/2 perquè angle=0 apunta amunt)
|
||||
float dy = velocitat_efectiva * std::sin(angle_ - Constants::PI / 2.0f);
|
||||
float dx = velocitat_efectiva * std::cos(angle_ - Constants::PI / 2.0f);
|
||||
|
||||
float new_y = centre_.y + dy;
|
||||
float new_x = centre_.x + dx;
|
||||
float new_y = centre_.y + dy;
|
||||
float new_x = centre_.x + dx;
|
||||
|
||||
// Obtenir límits de la zona de joc
|
||||
float min_x, max_x, min_y, max_y;
|
||||
Constants::obtenir_limits_zona(min_x, max_x, min_y, max_y);
|
||||
// Obtenir límits de la zona de joc
|
||||
float min_x, max_x, min_y, max_y;
|
||||
Constants::obtenir_limits_zona(min_x, max_x, min_y, max_y);
|
||||
|
||||
// Lògica Pascal: Actualitza Y si dins, sinó ajusta angle aleatòriament
|
||||
// if (dy>marge_dalt) and (dy<marge_baix) then orni.centre.y:=round(Dy)
|
||||
// else orni.angle:=orni.angle+(random(256)/512)*(random(3)-1);
|
||||
if (new_y > min_y && new_y < max_y) {
|
||||
centre_.y = new_y;
|
||||
} else {
|
||||
// Pequeño ajuste aleatorio: (random(256)/512)*(random(3)-1)
|
||||
// random(256) = 0..255, /512 = 0..0.498
|
||||
// random(3) = 0,1,2, -1 = -1,0,1
|
||||
// Resultado: ±0.5 rad aprox
|
||||
float rand1 = (static_cast<float>(std::rand() % 256) / 512.0f);
|
||||
int rand2 = (std::rand() % 3) - 1; // -1, 0, o 1
|
||||
angle_ += rand1 * static_cast<float>(rand2);
|
||||
}
|
||||
// Lògica Pascal: Actualitza Y si dins, sinó ajusta angle aleatòriament
|
||||
// if (dy>marge_dalt) and (dy<marge_baix) then orni.centre.y:=round(Dy)
|
||||
// else orni.angle:=orni.angle+(random(256)/512)*(random(3)-1);
|
||||
if (new_y > min_y && new_y < max_y) {
|
||||
centre_.y = new_y;
|
||||
} else {
|
||||
// Pequeño ajuste aleatorio: (random(256)/512)*(random(3)-1)
|
||||
// random(256) = 0..255, /512 = 0..0.498
|
||||
// random(3) = 0,1,2, -1 = -1,0,1
|
||||
// Resultado: ±0.5 rad aprox
|
||||
float rand1 = (static_cast<float>(std::rand() % 256) / 512.0f);
|
||||
int rand2 = (std::rand() % 3) - 1; // -1, 0, o 1
|
||||
angle_ += rand1 * static_cast<float>(rand2);
|
||||
}
|
||||
|
||||
// Lògica Pascal: Actualitza X si dins, sinó ajusta angle aleatòriament
|
||||
// if (dx>marge_esq) and (dx<marge_dret) then orni.centre.x:=round(Dx)
|
||||
// else orni.angle:=orni.angle+(random(256)/512)*(random(3)-1);
|
||||
if (new_x > min_x && new_x < max_x) {
|
||||
centre_.x = new_x;
|
||||
} else {
|
||||
float rand1 = (static_cast<float>(std::rand() % 256) / 512.0f);
|
||||
int rand2 = (std::rand() % 3) - 1;
|
||||
angle_ += rand1 * static_cast<float>(rand2);
|
||||
}
|
||||
// Lògica Pascal: Actualitza X si dins, sinó ajusta angle aleatòriament
|
||||
// if (dx>marge_esq) and (dx<marge_dret) then orni.centre.x:=round(Dx)
|
||||
// else orni.angle:=orni.angle+(random(256)/512)*(random(3)-1);
|
||||
if (new_x > min_x && new_x < max_x) {
|
||||
centre_.x = new_x;
|
||||
} else {
|
||||
float rand1 = (static_cast<float>(std::rand() % 256) / 512.0f);
|
||||
int rand2 = (std::rand() % 3) - 1;
|
||||
angle_ += rand1 * static_cast<float>(rand2);
|
||||
}
|
||||
|
||||
// Nota: La rotació visual (rotacio_ += drotacio_) ja es fa a actualitzar()
|
||||
// Nota: La rotació visual (rotacio_ += drotacio_) ja es fa a actualitzar()
|
||||
}
|
||||
|
||||
@@ -3,39 +3,42 @@
|
||||
// © 2025 Port a C++20 amb SDL3
|
||||
|
||||
#pragma once
|
||||
#include "core/graphics/shape.hpp"
|
||||
#include "core/types.hpp"
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "core/graphics/shape.hpp"
|
||||
#include "core/types.hpp"
|
||||
|
||||
class Enemic {
|
||||
public:
|
||||
Enemic() : renderer_(nullptr) {}
|
||||
Enemic(SDL_Renderer *renderer);
|
||||
public:
|
||||
Enemic()
|
||||
: renderer_(nullptr) {}
|
||||
Enemic(SDL_Renderer* renderer);
|
||||
|
||||
void inicialitzar();
|
||||
void actualitzar(float delta_time);
|
||||
void dibuixar() const;
|
||||
void inicialitzar();
|
||||
void actualitzar(float delta_time);
|
||||
void dibuixar() const;
|
||||
|
||||
// Getters (API pública sense canvis)
|
||||
bool esta_actiu() const { return esta_; }
|
||||
const Punt &get_centre() const { return centre_; }
|
||||
const std::shared_ptr<Graphics::Shape> &get_forma() const { return forma_; }
|
||||
void destruir() { esta_ = false; }
|
||||
// Getters (API pública sense canvis)
|
||||
bool esta_actiu() const { return esta_; }
|
||||
const Punt& get_centre() const { return centre_; }
|
||||
const std::shared_ptr<Graphics::Shape>& get_forma() const { return forma_; }
|
||||
void destruir() { esta_ = false; }
|
||||
|
||||
private:
|
||||
SDL_Renderer *renderer_;
|
||||
private:
|
||||
SDL_Renderer* renderer_;
|
||||
|
||||
// [NUEVO] Forma vectorial (compartida entre tots els enemics)
|
||||
std::shared_ptr<Graphics::Shape> forma_;
|
||||
// [NUEVO] Forma vectorial (compartida entre tots els enemics)
|
||||
std::shared_ptr<Graphics::Shape> forma_;
|
||||
|
||||
// [NUEVO] Estat de la instància (separat de la geometria)
|
||||
Punt centre_;
|
||||
float angle_; // Angle de moviment
|
||||
float velocitat_;
|
||||
float drotacio_; // Delta rotació visual (rad/s)
|
||||
float rotacio_; // Rotació visual acumulada
|
||||
bool esta_;
|
||||
// [NUEVO] Estat de la instància (separat de la geometria)
|
||||
Punt centre_;
|
||||
float angle_; // Angle de moviment
|
||||
float velocitat_;
|
||||
float drotacio_; // Delta rotació visual (rad/s)
|
||||
float rotacio_; // Rotació visual acumulada
|
||||
bool esta_;
|
||||
|
||||
void mou(float delta_time);
|
||||
void mou(float delta_time);
|
||||
};
|
||||
|
||||
@@ -3,137 +3,142 @@
|
||||
// © 2025 Port a C++20 amb SDL3
|
||||
|
||||
#include "game/entities/nau.hpp"
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
|
||||
#include "core/defaults.hpp"
|
||||
#include "core/graphics/shape_loader.hpp"
|
||||
#include "core/rendering/shape_renderer.hpp"
|
||||
#include "game/constants.hpp"
|
||||
#include <SDL3/SDL.h>
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
|
||||
Nau::Nau(SDL_Renderer *renderer)
|
||||
: renderer_(renderer), centre_({0.0f, 0.0f}), angle_(0.0f),
|
||||
velocitat_(0.0f), esta_tocada_(false) {
|
||||
Nau::Nau(SDL_Renderer* renderer)
|
||||
: renderer_(renderer),
|
||||
centre_({0.0f, 0.0f}),
|
||||
angle_(0.0f),
|
||||
velocitat_(0.0f),
|
||||
esta_tocada_(false) {
|
||||
// [NUEVO] Carregar forma compartida des de fitxer
|
||||
forma_ = Graphics::ShapeLoader::load("ship.shp");
|
||||
|
||||
// [NUEVO] Carregar forma compartida des de fitxer
|
||||
forma_ = Graphics::ShapeLoader::load("ship.shp");
|
||||
|
||||
if (!forma_ || !forma_->es_valida()) {
|
||||
std::cerr << "[Nau] Error: no s'ha pogut carregar ship.shp" << std::endl;
|
||||
}
|
||||
if (!forma_ || !forma_->es_valida()) {
|
||||
std::cerr << "[Nau] Error: no s'ha pogut carregar ship.shp" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void Nau::inicialitzar() {
|
||||
// Inicialització de la nau (triangle)
|
||||
// Basat en el codi Pascal original: lines 380-384
|
||||
// Copiat de joc_asteroides.cpp línies 30-44
|
||||
// Inicialització de la nau (triangle)
|
||||
// Basat en el codi Pascal original: lines 380-384
|
||||
// Copiat de joc_asteroides.cpp línies 30-44
|
||||
|
||||
// [NUEVO] Ja no cal configurar punts polars - la geometria es carrega del
|
||||
// fitxer Només inicialitzem l'estat de la instància
|
||||
// [NUEVO] Ja no cal configurar punts polars - la geometria es carrega del
|
||||
// fitxer Només inicialitzem l'estat de la instància
|
||||
|
||||
// Posició inicial al centre de la pantalla
|
||||
centre_.x = 320.0f;
|
||||
centre_.y = 240.0f;
|
||||
// Posició inicial al centre de la pantalla
|
||||
centre_.x = 320.0f;
|
||||
centre_.y = 240.0f;
|
||||
|
||||
// Estat inicial
|
||||
angle_ = 0.0f;
|
||||
velocitat_ = 0.0f;
|
||||
esta_tocada_ = false;
|
||||
// Estat inicial
|
||||
angle_ = 0.0f;
|
||||
velocitat_ = 0.0f;
|
||||
esta_tocada_ = false;
|
||||
}
|
||||
|
||||
void Nau::processar_input(float delta_time) {
|
||||
// Processar input continu (com teclapuls() del Pascal original)
|
||||
// Basat en joc_asteroides.cpp línies 66-85
|
||||
// Només processa input si la nau està viva
|
||||
if (esta_tocada_)
|
||||
return;
|
||||
// Processar input continu (com teclapuls() del Pascal original)
|
||||
// Basat en joc_asteroides.cpp línies 66-85
|
||||
// Només processa input si la nau està viva
|
||||
if (esta_tocada_)
|
||||
return;
|
||||
|
||||
// Obtenir estat actual del teclat (no events, sinó estat continu)
|
||||
const bool *keyboard_state = SDL_GetKeyboardState(nullptr);
|
||||
// Obtenir estat actual del teclat (no events, sinó estat continu)
|
||||
const bool* keyboard_state = SDL_GetKeyboardState(nullptr);
|
||||
|
||||
// Rotació
|
||||
if (keyboard_state[SDL_SCANCODE_RIGHT]) {
|
||||
angle_ += Defaults::Physics::ROTATION_SPEED * delta_time;
|
||||
}
|
||||
|
||||
if (keyboard_state[SDL_SCANCODE_LEFT]) {
|
||||
angle_ -= Defaults::Physics::ROTATION_SPEED * delta_time;
|
||||
}
|
||||
|
||||
// Acceleració
|
||||
if (keyboard_state[SDL_SCANCODE_UP]) {
|
||||
if (velocitat_ < Defaults::Physics::MAX_VELOCITY) {
|
||||
velocitat_ += Defaults::Physics::ACCELERATION * delta_time;
|
||||
if (velocitat_ > Defaults::Physics::MAX_VELOCITY) {
|
||||
velocitat_ = Defaults::Physics::MAX_VELOCITY;
|
||||
}
|
||||
// Rotació
|
||||
if (keyboard_state[SDL_SCANCODE_RIGHT]) {
|
||||
angle_ += Defaults::Physics::ROTATION_SPEED * delta_time;
|
||||
}
|
||||
|
||||
if (keyboard_state[SDL_SCANCODE_LEFT]) {
|
||||
angle_ -= Defaults::Physics::ROTATION_SPEED * delta_time;
|
||||
}
|
||||
|
||||
// Acceleració
|
||||
if (keyboard_state[SDL_SCANCODE_UP]) {
|
||||
if (velocitat_ < Defaults::Physics::MAX_VELOCITY) {
|
||||
velocitat_ += Defaults::Physics::ACCELERATION * delta_time;
|
||||
if (velocitat_ > Defaults::Physics::MAX_VELOCITY) {
|
||||
velocitat_ = Defaults::Physics::MAX_VELOCITY;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Nau::actualitzar(float delta_time) {
|
||||
// Només actualitzar si la nau està viva
|
||||
if (esta_tocada_)
|
||||
return;
|
||||
// Només actualitzar si la nau està viva
|
||||
if (esta_tocada_)
|
||||
return;
|
||||
|
||||
// Aplicar física (moviment + fricció)
|
||||
aplicar_fisica(delta_time);
|
||||
// Aplicar física (moviment + fricció)
|
||||
aplicar_fisica(delta_time);
|
||||
}
|
||||
|
||||
void Nau::dibuixar() const {
|
||||
// Només dibuixar si la nau està viva
|
||||
if (esta_tocada_)
|
||||
return;
|
||||
// Només dibuixar si la nau està viva
|
||||
if (esta_tocada_)
|
||||
return;
|
||||
|
||||
if (!forma_)
|
||||
return;
|
||||
if (!forma_)
|
||||
return;
|
||||
|
||||
// Escalar velocitat per l'efecte visual (200 px/s → ~6 px d'efecte)
|
||||
// El codi Pascal original sumava velocitat (0-6) al radi per donar
|
||||
// sensació de "empenta". Ara velocitat està en px/s (0-200).
|
||||
// Basat en joc_asteroides.cpp línies 127-134
|
||||
//
|
||||
// [NUEVO] Convertir suma de velocitat_visual a escala multiplicativa
|
||||
// Radio base del ship = 12 px
|
||||
// velocitat_visual = 0-6 → r = 12-18 → escala = 1.0-1.5
|
||||
float velocitat_visual = velocitat_ / 33.33f;
|
||||
float escala = 1.0f + (velocitat_visual / 12.0f);
|
||||
// Escalar velocitat per l'efecte visual (200 px/s → ~6 px d'efecte)
|
||||
// El codi Pascal original sumava velocitat (0-6) al radi per donar
|
||||
// sensació de "empenta". Ara velocitat està en px/s (0-200).
|
||||
// Basat en joc_asteroides.cpp línies 127-134
|
||||
//
|
||||
// [NUEVO] Convertir suma de velocitat_visual a escala multiplicativa
|
||||
// Radio base del ship = 12 px
|
||||
// velocitat_visual = 0-6 → r = 12-18 → escala = 1.0-1.5
|
||||
float velocitat_visual = velocitat_ / 33.33f;
|
||||
float escala = 1.0f + (velocitat_visual / 12.0f);
|
||||
|
||||
Rendering::render_shape(renderer_, forma_, centre_, angle_, escala, true);
|
||||
Rendering::render_shape(renderer_, forma_, centre_, angle_, escala, true);
|
||||
}
|
||||
|
||||
void Nau::aplicar_fisica(float delta_time) {
|
||||
// Aplicar física de moviment
|
||||
// Basat en joc_asteroides.cpp línies 87-113
|
||||
// Aplicar física de moviment
|
||||
// Basat en joc_asteroides.cpp línies 87-113
|
||||
|
||||
// Calcular nova posició basada en velocitat i angle
|
||||
// S'usa (angle - PI/2) perquè angle=0 apunta cap amunt, no cap a la dreta
|
||||
// velocitat_ està en px/s, així que multipliquem per delta_time
|
||||
float dy =
|
||||
(velocitat_ * delta_time) * std::sin(angle_ - Constants::PI / 2.0f) +
|
||||
centre_.y;
|
||||
float dx =
|
||||
(velocitat_ * delta_time) * std::cos(angle_ - Constants::PI / 2.0f) +
|
||||
centre_.x;
|
||||
// Calcular nova posició basada en velocitat i angle
|
||||
// S'usa (angle - PI/2) perquè angle=0 apunta cap amunt, no cap a la dreta
|
||||
// velocitat_ està en px/s, així que multipliquem per delta_time
|
||||
float dy =
|
||||
(velocitat_ * delta_time) * std::sin(angle_ - Constants::PI / 2.0f) +
|
||||
centre_.y;
|
||||
float dx =
|
||||
(velocitat_ * delta_time) * std::cos(angle_ - Constants::PI / 2.0f) +
|
||||
centre_.x;
|
||||
|
||||
// Boundary checking - només actualitzar si dins de la zona de joc
|
||||
// Acumulació directa amb precisió subpíxel
|
||||
float min_x, max_x, min_y, max_y;
|
||||
Constants::obtenir_limits_zona(min_x, max_x, min_y, max_y);
|
||||
// Boundary checking - només actualitzar si dins de la zona de joc
|
||||
// Acumulació directa amb precisió subpíxel
|
||||
float min_x, max_x, min_y, max_y;
|
||||
Constants::obtenir_limits_zona(min_x, max_x, min_y, max_y);
|
||||
|
||||
if (dy > min_y && dy < max_y) {
|
||||
centre_.y = dy;
|
||||
}
|
||||
|
||||
if (dx > min_x && dx < max_x) {
|
||||
centre_.x = dx;
|
||||
}
|
||||
|
||||
// Fricció - desacceleració gradual (time-based)
|
||||
if (velocitat_ > 0.1f) {
|
||||
velocitat_ -= Defaults::Physics::FRICTION * delta_time;
|
||||
if (velocitat_ < 0.0f) {
|
||||
velocitat_ = 0.0f;
|
||||
if (dy > min_y && dy < max_y) {
|
||||
centre_.y = dy;
|
||||
}
|
||||
|
||||
if (dx > min_x && dx < max_x) {
|
||||
centre_.x = dx;
|
||||
}
|
||||
|
||||
// Fricció - desacceleració gradual (time-based)
|
||||
if (velocitat_ > 0.1f) {
|
||||
velocitat_ -= Defaults::Physics::FRICTION * delta_time;
|
||||
if (velocitat_ < 0.0f) {
|
||||
velocitat_ = 0.0f;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,41 +3,44 @@
|
||||
// © 2025 Port a C++20 amb SDL3
|
||||
|
||||
#pragma once
|
||||
#include "core/graphics/shape.hpp"
|
||||
#include "core/types.hpp"
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "core/graphics/shape.hpp"
|
||||
#include "core/types.hpp"
|
||||
|
||||
class Nau {
|
||||
public:
|
||||
Nau() : renderer_(nullptr) {}
|
||||
Nau(SDL_Renderer *renderer);
|
||||
public:
|
||||
Nau()
|
||||
: renderer_(nullptr) {}
|
||||
Nau(SDL_Renderer* renderer);
|
||||
|
||||
void inicialitzar();
|
||||
void processar_input(float delta_time);
|
||||
void actualitzar(float delta_time);
|
||||
void dibuixar() const;
|
||||
void inicialitzar();
|
||||
void processar_input(float delta_time);
|
||||
void actualitzar(float delta_time);
|
||||
void dibuixar() const;
|
||||
|
||||
// Getters (API pública sense canvis)
|
||||
const Punt &get_centre() const { return centre_; }
|
||||
float get_angle() const { return angle_; }
|
||||
bool esta_viva() const { return !esta_tocada_; }
|
||||
// Getters (API pública sense canvis)
|
||||
const Punt& get_centre() const { return centre_; }
|
||||
float get_angle() const { return angle_; }
|
||||
bool esta_viva() const { return !esta_tocada_; }
|
||||
|
||||
// Col·lisions (Fase 10)
|
||||
void marcar_tocada() { esta_tocada_ = true; }
|
||||
// Col·lisions (Fase 10)
|
||||
void marcar_tocada() { esta_tocada_ = true; }
|
||||
|
||||
private:
|
||||
SDL_Renderer *renderer_;
|
||||
private:
|
||||
SDL_Renderer* renderer_;
|
||||
|
||||
// [NUEVO] Forma vectorial (compartida, només 1 instància de Nau però preparat
|
||||
// per reutilització)
|
||||
std::shared_ptr<Graphics::Shape> forma_;
|
||||
// [NUEVO] Forma vectorial (compartida, només 1 instància de Nau però preparat
|
||||
// per reutilització)
|
||||
std::shared_ptr<Graphics::Shape> forma_;
|
||||
|
||||
// [NUEVO] Estat de la instància (separat de la geometria)
|
||||
Punt centre_;
|
||||
float angle_; // Angle d'orientació
|
||||
float velocitat_; // Velocitat (px/s)
|
||||
bool esta_tocada_;
|
||||
// [NUEVO] Estat de la instància (separat de la geometria)
|
||||
Punt centre_;
|
||||
float angle_; // Angle d'orientació
|
||||
float velocitat_; // Velocitat (px/s)
|
||||
bool esta_tocada_;
|
||||
|
||||
void aplicar_fisica(float delta_time);
|
||||
void aplicar_fisica(float delta_time);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user