migrat a sistema de shapes
This commit is contained in:
@@ -40,6 +40,9 @@ set(APP_SOURCES
|
||||
source/core/rendering/color_oscillator.cpp
|
||||
source/core/rendering/polygon_renderer.cpp
|
||||
source/core/rendering/primitives.cpp
|
||||
source/core/rendering/shape_renderer.cpp
|
||||
source/core/graphics/shape.cpp
|
||||
source/core/graphics/shape_loader.cpp
|
||||
source/game/options.cpp
|
||||
source/game/joc_asteroides.cpp
|
||||
source/game/entities/nau.cpp
|
||||
|
||||
3
Makefile
3
Makefile
@@ -40,6 +40,9 @@ APP_SOURCES := \
|
||||
source/core/rendering/color_oscillator.cpp \
|
||||
source/core/rendering/polygon_renderer.cpp \
|
||||
source/core/rendering/primitives.cpp \
|
||||
source/core/rendering/shape_renderer.cpp \
|
||||
source/core/graphics/shape.cpp \
|
||||
source/core/graphics/shape_loader.cpp \
|
||||
source/game/options.cpp \
|
||||
source/game/joc_asteroides.cpp \
|
||||
source/game/entities/nau.cpp \
|
||||
|
||||
19
data/shapes/bullet.shp
Normal file
19
data/shapes/bullet.shp
Normal file
@@ -0,0 +1,19 @@
|
||||
# bullet.shp - Projectil (petit pentàgon)
|
||||
# © 1999 Visente i Sergi (versió Pascal)
|
||||
# © 2025 Port a C++20 amb SDL3
|
||||
|
||||
name: bullet
|
||||
scale: 1.0
|
||||
center: 0, 0
|
||||
|
||||
# Pentàgon petit radi=5 (1/4 del enemic)
|
||||
# Pentàgon regular amb 72° entre punts
|
||||
#
|
||||
# Conversió polar→cartesià (radi=5, SDL: Y creix cap avall):
|
||||
# angle=-90°: (0.00, -5.00)
|
||||
# angle=-18°: (4.76, -1.55)
|
||||
# angle=54°: (2.94, 4.05)
|
||||
# angle=126°: (-2.94, 4.05)
|
||||
# angle=198°: (-4.76, -1.55)
|
||||
|
||||
polyline: 0,-5 4.76,-1.55 2.94,4.05 -2.94,4.05 -4.76,-1.55 0,-5
|
||||
21
data/shapes/enemy_pentagon.shp
Normal file
21
data/shapes/enemy_pentagon.shp
Normal file
@@ -0,0 +1,21 @@
|
||||
# enemy_pentagon.shp - ORNI enemic (pentàgon regular)
|
||||
# © 1999 Visente i Sergi (versió Pascal)
|
||||
# © 2025 Port a C++20 amb SDL3
|
||||
|
||||
name: enemy_pentagon
|
||||
scale: 1.0
|
||||
center: 0, 0
|
||||
|
||||
# Pentàgon regular radi=20
|
||||
# 5 punts equidistants al voltant d'un cercle (72° entre ells)
|
||||
# Començant a angle=-90° (amunt), rotant sentit antihorari
|
||||
#
|
||||
# Angles: -90°, -18°, 54°, 126°, 198°
|
||||
# Conversió polar→cartesià (SDL: Y creix cap avall):
|
||||
# angle=-90°: (0.00, -20.00)
|
||||
# angle=-18°: (19.02, -6.18)
|
||||
# angle=54°: (11.76, 16.18)
|
||||
# angle=126°: (-11.76, 16.18)
|
||||
# angle=198°: (-19.02, -6.18)
|
||||
|
||||
polyline: 0,-20 19.02,-6.18 11.76,16.18 -11.76,16.18 -19.02,-6.18 0,-20
|
||||
20
data/shapes/ship.shp
Normal file
20
data/shapes/ship.shp
Normal file
@@ -0,0 +1,20 @@
|
||||
# ship.shp - Nau del jugador (triangle)
|
||||
# © 1999 Visente i Sergi (versió Pascal)
|
||||
# © 2025 Port a C++20 amb SDL3
|
||||
|
||||
name: ship
|
||||
scale: 1.0
|
||||
center: 0, 0
|
||||
|
||||
# Triangle: punta amunt, base avall
|
||||
# Punts originals (polar):
|
||||
# p1: r=12, angle=270° (3π/2) → punta amunt
|
||||
# p2: r=12, angle=45° (π/4) → base dreta-darrere
|
||||
# p3: r=12, angle=135° (3π/4) → base esquerra-darrere
|
||||
#
|
||||
# Conversió polar→cartesià (angle-90° perquè origen visual és amunt):
|
||||
# p1: (0, -12)
|
||||
# p2: (8.49, 8.49)
|
||||
# p3: (-8.49, 8.49)
|
||||
|
||||
polyline: 0,-12 8.49,8.49 -8.49,8.49 0,-12
|
||||
@@ -3,23 +3,30 @@
|
||||
// © 2025 Port a C++20 amb SDL3
|
||||
|
||||
#include "game/entities/bala.hpp"
|
||||
#include "core/rendering/polygon_renderer.hpp"
|
||||
#include "core/rendering/primitives.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) {}
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
void Bala::inicialitzar() {
|
||||
// Inicialitzar bala com a pentàgon petit
|
||||
// Basat en el codi Pascal original: inicialment inactiva
|
||||
// Copiat de joc_asteroides.cpp línies 58-64
|
||||
|
||||
// Crear pentàgon petit (5 costats, radi 5)
|
||||
crear_poligon_regular(dades_, 5, 5.0f);
|
||||
|
||||
// Inicialment inactiva
|
||||
dades_.esta = false;
|
||||
esta_ = false;
|
||||
centre_ = {0.0f, 0.0f};
|
||||
angle_ = 0.0f;
|
||||
velocitat_ = 0.0f;
|
||||
}
|
||||
|
||||
void Bala::disparar(const Punt &posicio, float angle) {
|
||||
@@ -27,30 +34,31 @@ void Bala::disparar(const Punt &posicio, float angle) {
|
||||
// Basat en joc_asteroides.cpp línies 188-200
|
||||
|
||||
// Activar bala
|
||||
dades_.esta = true;
|
||||
esta_ = true;
|
||||
|
||||
// Posició inicial = centre de la nau
|
||||
dades_.centre.x = posicio.x;
|
||||
dades_.centre.y = posicio.y;
|
||||
centre_.x = posicio.x;
|
||||
centre_.y = posicio.y;
|
||||
|
||||
// Angle = angle de la nau (dispara en la direcció que apunta)
|
||||
dades_.angle = angle;
|
||||
angle_ = angle;
|
||||
|
||||
// Velocitat alta (el joc Pascal original usava 7 px/frame)
|
||||
// 7 px/frame × 20 FPS = 140 px/s
|
||||
dades_.velocitat = 140.0f;
|
||||
velocitat_ = 140.0f;
|
||||
}
|
||||
|
||||
void Bala::actualitzar(float delta_time) {
|
||||
if (dades_.esta) {
|
||||
if (esta_) {
|
||||
mou(delta_time);
|
||||
}
|
||||
}
|
||||
|
||||
void Bala::dibuixar() const {
|
||||
if (dades_.esta) {
|
||||
// Dibuixar com a pentàgon petit, sense rotació visual
|
||||
Rendering::rota_pol(renderer_, dades_, 0.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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,23 +69,19 @@ void Bala::mou(float 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 = dades_.velocitat * 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(dades_.angle - Constants::PI / 2.0f);
|
||||
float dx =
|
||||
velocitat_efectiva * std::cos(dades_.angle - Constants::PI / 2.0f);
|
||||
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
|
||||
dades_.centre.y += dy;
|
||||
dades_.centre.x += dx;
|
||||
centre_.y += dy;
|
||||
centre_.x += dx;
|
||||
|
||||
// Desactivar si surt dels marges (no rebota com els ORNIs)
|
||||
if (dades_.centre.x < Constants::MARGE_ESQ ||
|
||||
dades_.centre.x > Constants::MARGE_DRET ||
|
||||
dades_.centre.y < Constants::MARGE_DALT ||
|
||||
dades_.centre.y > Constants::MARGE_BAIX) {
|
||||
dades_.esta = false;
|
||||
if (centre_.x < Constants::MARGE_ESQ || centre_.x > Constants::MARGE_DRET ||
|
||||
centre_.y < Constants::MARGE_DALT || centre_.y > Constants::MARGE_BAIX) {
|
||||
esta_ = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,8 +3,10 @@
|
||||
// © 2025 Port a C++20 amb SDL3
|
||||
|
||||
#pragma once
|
||||
#include "core/graphics/shape.hpp"
|
||||
#include "core/types.hpp"
|
||||
#include <SDL3/SDL.h>
|
||||
#include <memory>
|
||||
|
||||
class Bala {
|
||||
public:
|
||||
@@ -16,13 +18,22 @@ public:
|
||||
void actualitzar(float delta_time);
|
||||
void dibuixar() const;
|
||||
|
||||
// Getters
|
||||
bool esta_activa() const { return dades_.esta; }
|
||||
const Punt &get_centre() const { return dades_.centre; }
|
||||
void desactivar() { dades_.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_;
|
||||
Poligon dades_;
|
||||
|
||||
// [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_;
|
||||
|
||||
void mou(float delta_time);
|
||||
};
|
||||
|
||||
@@ -3,45 +3,66 @@
|
||||
// © 2025 Port a C++20 amb SDL3
|
||||
|
||||
#include "game/entities/enemic.hpp"
|
||||
#include "core/rendering/polygon_renderer.hpp"
|
||||
#include "core/rendering/primitives.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) {}
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
void Enemic::inicialitzar() {
|
||||
// Inicialitzar enemic com a pentàgon
|
||||
// Inicialitzar enemic (pentàgon)
|
||||
// Copiat de joc_asteroides.cpp línies 41-54
|
||||
|
||||
// Crear pentàgon regular (5 costats, radi 20)
|
||||
crear_poligon_regular(dades_, 5, 20.0f);
|
||||
// [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
|
||||
dades_.centre.x = static_cast<float>((std::rand() % 580) + 30); // 30-610
|
||||
dades_.centre.y = static_cast<float>((std::rand() % 420) + 30); // 30-450
|
||||
centre_.x = static_cast<float>((std::rand() % 580) + 30); // 30-610
|
||||
centre_.y = static_cast<float>((std::rand() % 420) + 30); // 30-450
|
||||
|
||||
// Angle aleatori
|
||||
dades_.angle = (std::rand() % 360) * Constants::PI / 180.0f;
|
||||
// Angle aleatori de moviment
|
||||
angle_ = (std::rand() % 360) * Constants::PI / 180.0f;
|
||||
|
||||
// Està actiu
|
||||
dades_.esta = true;
|
||||
// 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;
|
||||
|
||||
// Activar
|
||||
esta_ = true;
|
||||
}
|
||||
|
||||
void Enemic::actualitzar(float delta_time) {
|
||||
if (dades_.esta) {
|
||||
if (esta_) {
|
||||
// Moviment autònom
|
||||
mou(delta_time);
|
||||
|
||||
// Rotació visual (time-based: drotacio està en rad/s)
|
||||
dades_.rotacio += dades_.drotacio * delta_time;
|
||||
// Rotació visual (time-based: drotacio_ està en rad/s)
|
||||
rotacio_ += drotacio_ * delta_time;
|
||||
}
|
||||
}
|
||||
|
||||
void Enemic::dibuixar() const {
|
||||
if (dades_.esta) {
|
||||
Rendering::rota_pol(renderer_, dades_, dades_.rotacio, true);
|
||||
if (esta_ && forma_) {
|
||||
// [NUEVO] Usar render_shape en lloc de rota_pol
|
||||
Rendering::render_shape(renderer_, forma_, centre_, rotacio_, 1.0f, true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,23 +75,21 @@ void Enemic::mou(float delta_time) {
|
||||
// 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 = dades_.velocitat * delta_time;
|
||||
// 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(dades_.angle - Constants::PI / 2.0f);
|
||||
float dx =
|
||||
velocitat_efectiva * std::cos(dades_.angle - Constants::PI / 2.0f);
|
||||
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 = dades_.centre.y + dy;
|
||||
float new_x = dades_.centre.x + dx;
|
||||
float new_y = centre_.y + dy;
|
||||
float new_x = centre_.x + dx;
|
||||
|
||||
// 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 > Constants::MARGE_DALT && new_y < Constants::MARGE_BAIX) {
|
||||
dades_.centre.y = new_y;
|
||||
centre_.y = new_y;
|
||||
} else {
|
||||
// Pequeño ajuste aleatorio: (random(256)/512)*(random(3)-1)
|
||||
// random(256) = 0..255, /512 = 0..0.498
|
||||
@@ -78,20 +97,19 @@ void Enemic::mou(float delta_time) {
|
||||
// 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
|
||||
dades_.angle += rand1 * static_cast<float>(rand2);
|
||||
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 > Constants::MARGE_ESQ && new_x < Constants::MARGE_DRET) {
|
||||
dades_.centre.x = new_x;
|
||||
centre_.x = new_x;
|
||||
} else {
|
||||
float rand1 = (static_cast<float>(std::rand() % 256) / 512.0f);
|
||||
int rand2 = (std::rand() % 3) - 1;
|
||||
dades_.angle += rand1 * static_cast<float>(rand2);
|
||||
angle_ += rand1 * static_cast<float>(rand2);
|
||||
}
|
||||
|
||||
// Nota: La rotació visual (dades_.rotacio += dades_.drotacio) ja es fa a
|
||||
// actualitzar()
|
||||
// Nota: La rotació visual (rotacio_ += drotacio_) ja es fa a actualitzar()
|
||||
}
|
||||
|
||||
@@ -3,8 +3,10 @@
|
||||
// © 2025 Port a C++20 amb SDL3
|
||||
|
||||
#pragma once
|
||||
#include "core/graphics/shape.hpp"
|
||||
#include "core/types.hpp"
|
||||
#include <SDL3/SDL.h>
|
||||
#include <memory>
|
||||
|
||||
class Enemic {
|
||||
public:
|
||||
@@ -15,13 +17,24 @@ public:
|
||||
void actualitzar(float delta_time);
|
||||
void dibuixar() const;
|
||||
|
||||
// Getters
|
||||
bool esta_actiu() const { return dades_.esta; }
|
||||
const Punt &get_centre() const { return dades_.centre; }
|
||||
void destruir() { dades_.esta = false; }
|
||||
// Getters (API pública sense canvis)
|
||||
bool esta_actiu() const { return esta_; }
|
||||
const Punt &get_centre() const { return centre_; }
|
||||
void destruir() { esta_ = false; }
|
||||
|
||||
private:
|
||||
SDL_Renderer *renderer_;
|
||||
Poligon dades_;
|
||||
|
||||
// [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_;
|
||||
|
||||
void mou(float delta_time);
|
||||
};
|
||||
|
||||
@@ -4,36 +4,40 @@
|
||||
|
||||
#include "game/entities/nau.hpp"
|
||||
#include "core/defaults.hpp"
|
||||
#include "core/rendering/polygon_renderer.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), esta_tocada_(false) {}
|
||||
: 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");
|
||||
|
||||
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
|
||||
|
||||
// Configurar triangle amb 3 punts polars
|
||||
dades_.p1.r = 12.0f;
|
||||
dades_.p1.angle = 3.0f * Constants::PI / 2.0f; // Apunta amunt (270°)
|
||||
|
||||
dades_.p2.r = 12.0f;
|
||||
dades_.p2.angle = Constants::PI / 4.0f; // 45°
|
||||
|
||||
dades_.p3.r = 12.0f;
|
||||
dades_.p3.angle = (3.0f * Constants::PI) / 4.0f; // 135°
|
||||
// [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
|
||||
dades_.centre.x = 320.0f;
|
||||
dades_.centre.y = 240.0f;
|
||||
centre_.x = 320.0f;
|
||||
centre_.y = 240.0f;
|
||||
|
||||
// Estat inicial
|
||||
dades_.angle = 0.0f;
|
||||
dades_.velocitat = 0.0f;
|
||||
angle_ = 0.0f;
|
||||
velocitat_ = 0.0f;
|
||||
esta_tocada_ = false;
|
||||
}
|
||||
|
||||
@@ -49,19 +53,19 @@ void Nau::processar_input(float delta_time) {
|
||||
|
||||
// Rotació
|
||||
if (keyboard_state[SDL_SCANCODE_RIGHT]) {
|
||||
dades_.angle += Defaults::Physics::ROTATION_SPEED * delta_time;
|
||||
angle_ += Defaults::Physics::ROTATION_SPEED * delta_time;
|
||||
}
|
||||
|
||||
if (keyboard_state[SDL_SCANCODE_LEFT]) {
|
||||
dades_.angle -= Defaults::Physics::ROTATION_SPEED * delta_time;
|
||||
angle_ -= Defaults::Physics::ROTATION_SPEED * delta_time;
|
||||
}
|
||||
|
||||
// Acceleració
|
||||
if (keyboard_state[SDL_SCANCODE_UP]) {
|
||||
if (dades_.velocitat < Defaults::Physics::MAX_VELOCITY) {
|
||||
dades_.velocitat += Defaults::Physics::ACCELERATION * delta_time;
|
||||
if (dades_.velocitat > Defaults::Physics::MAX_VELOCITY) {
|
||||
dades_.velocitat = Defaults::Physics::MAX_VELOCITY;
|
||||
if (velocitat_ < Defaults::Physics::MAX_VELOCITY) {
|
||||
velocitat_ += Defaults::Physics::ACCELERATION * delta_time;
|
||||
if (velocitat_ > Defaults::Physics::MAX_VELOCITY) {
|
||||
velocitat_ = Defaults::Physics::MAX_VELOCITY;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -81,13 +85,21 @@ void Nau::dibuixar() const {
|
||||
if (esta_tocada_)
|
||||
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
|
||||
float velocitat_visual = dades_.velocitat / 33.33f;
|
||||
//
|
||||
// [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::rota_tri(renderer_, dades_, dades_.angle, velocitat_visual, true);
|
||||
Rendering::render_shape(renderer_, forma_, centre_, angle_, escala, true);
|
||||
}
|
||||
|
||||
void Nau::aplicar_fisica(float delta_time) {
|
||||
@@ -96,29 +108,29 @@ void Nau::aplicar_fisica(float delta_time) {
|
||||
|
||||
// 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 = (dades_.velocitat * delta_time) *
|
||||
std::sin(dades_.angle - Constants::PI / 2.0f) +
|
||||
dades_.centre.y;
|
||||
float dx = (dades_.velocitat * delta_time) *
|
||||
std::cos(dades_.angle - Constants::PI / 2.0f) +
|
||||
dades_.centre.x;
|
||||
// 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 dels marges
|
||||
// Acumulació directa amb precisió subpíxel
|
||||
if (dy > Constants::MARGE_DALT && dy < Constants::MARGE_BAIX) {
|
||||
dades_.centre.y = dy;
|
||||
centre_.y = dy;
|
||||
}
|
||||
|
||||
if (dx > Constants::MARGE_ESQ && dx < Constants::MARGE_DRET) {
|
||||
dades_.centre.x = dx;
|
||||
centre_.x = dx;
|
||||
}
|
||||
|
||||
// Fricció - desacceleració gradual (time-based)
|
||||
if (dades_.velocitat > 0.1f) {
|
||||
dades_.velocitat -= Defaults::Physics::FRICTION * delta_time;
|
||||
if (dades_.velocitat < 0.0f) {
|
||||
dades_.velocitat = 0.0f;
|
||||
if (velocitat_ > 0.1f) {
|
||||
velocitat_ -= Defaults::Physics::FRICTION * delta_time;
|
||||
if (velocitat_ < 0.0f) {
|
||||
velocitat_ = 0.0f;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,8 +3,10 @@
|
||||
// © 2025 Port a C++20 amb SDL3
|
||||
|
||||
#pragma once
|
||||
#include "core/graphics/shape.hpp"
|
||||
#include "core/types.hpp"
|
||||
#include <SDL3/SDL.h>
|
||||
#include <memory>
|
||||
|
||||
class Nau {
|
||||
public:
|
||||
@@ -16,9 +18,9 @@ public:
|
||||
void actualitzar(float delta_time);
|
||||
void dibuixar() const;
|
||||
|
||||
// Getters
|
||||
const Punt &get_centre() const { return dades_.centre; }
|
||||
float get_angle() const { return dades_.angle; }
|
||||
// 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)
|
||||
@@ -26,7 +28,14 @@ public:
|
||||
|
||||
private:
|
||||
SDL_Renderer *renderer_;
|
||||
Triangle dades_;
|
||||
|
||||
// [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_;
|
||||
|
||||
void aplicar_fisica(float delta_time);
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
// © 2025 Port a C++20 amb SDL3
|
||||
|
||||
#include "joc_asteroides.hpp"
|
||||
#include "core/rendering/primitives.hpp"
|
||||
#include <cmath>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
|
||||
@@ -5,7 +5,6 @@
|
||||
#ifndef JOC_ASTEROIDES_HPP
|
||||
#define JOC_ASTEROIDES_HPP
|
||||
|
||||
#include "core/rendering/polygon_renderer.hpp"
|
||||
#include "core/types.hpp"
|
||||
#include "game/constants.hpp"
|
||||
#include "game/entities/bala.hpp"
|
||||
|
||||
Reference in New Issue
Block a user