afegides zones

This commit is contained in:
2025-11-29 09:04:18 +01:00
parent 832f77de80
commit 89302a2ee3
6 changed files with 55 additions and 21 deletions

View File

@@ -6,11 +6,11 @@
// Permet usar Constants::MARGE_ESQ en lloc de Defaults::Game::MARGIN_LEFT
namespace Constants {
// Marges de l'àrea de joc
constexpr int MARGE_DALT = Defaults::Game::MARGIN_TOP;
constexpr int MARGE_BAIX = Defaults::Game::MARGIN_BOTTOM;
constexpr int MARGE_ESQ = Defaults::Game::MARGIN_LEFT;
constexpr int MARGE_DRET = Defaults::Game::MARGIN_RIGHT;
// Marges de l'àrea de joc (derivats de Defaults::Zones::GAME)
constexpr int MARGE_ESQ = static_cast<int>(Defaults::Zones::GAME.x);
constexpr int MARGE_DRET = static_cast<int>(Defaults::Zones::GAME.x + Defaults::Zones::GAME.w);
constexpr int MARGE_DALT = static_cast<int>(Defaults::Zones::GAME.y);
constexpr int MARGE_BAIX = static_cast<int>(Defaults::Zones::GAME.y + Defaults::Zones::GAME.h);
// Límits de polígons i objectes
constexpr int MAX_IPUNTS = Defaults::Entities::MAX_IPUNTS;
@@ -23,4 +23,18 @@ constexpr int VELOCITAT_MAX = static_cast<int>(Defaults::Physics::BULLET_SPEED);
// Matemàtiques
constexpr float PI = Defaults::Math::PI;
// Helpers per comprovar límits de zona
inline bool dins_zona_joc(float x, float y) {
const SDL_FPoint punt = {x, y};
return SDL_PointInRectFloat(&punt, &Defaults::Zones::GAME);
}
inline void obtenir_limits_zona(float& min_x, float& max_x, float& min_y, float& max_y) {
const auto& zona = Defaults::Zones::GAME;
min_x = zona.x;
max_x = zona.x + zona.w;
min_y = zona.y;
max_y = zona.y + zona.h;
}
} // namespace Constants

View File

@@ -79,9 +79,8 @@ void Bala::mou(float delta_time) {
centre_.y += dy;
centre_.x += dx;
// Desactivar si surt dels marges (no rebota com els ORNIs)
if (centre_.x < Constants::MARGE_ESQ || centre_.x > Constants::MARGE_DRET ||
centre_.y < Constants::MARGE_DALT || centre_.y > Constants::MARGE_BAIX) {
// Desactivar si surt de la zona de joc (no rebota com els ORNIs)
if (!Constants::dins_zona_joc(centre_.x, centre_.y)) {
esta_ = false;
}
}

View File

@@ -85,10 +85,14 @@ void Enemic::mou(float delta_time) {
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);
// 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) {
if (new_y > min_y && new_y < max_y) {
centre_.y = new_y;
} else {
// Pequeño ajuste aleatorio: (random(256)/512)*(random(3)-1)
@@ -103,7 +107,7 @@ void Enemic::mou(float delta_time) {
// 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) {
if (new_x > min_x && new_x < max_x) {
centre_.x = new_x;
} else {
float rand1 = (static_cast<float>(std::rand() % 256) / 512.0f);

View File

@@ -116,13 +116,16 @@ void Nau::aplicar_fisica(float delta_time) {
(velocitat_ * delta_time) * std::cos(angle_ - Constants::PI / 2.0f) +
centre_.x;
// Boundary checking - només actualitzar si dins dels marges
// Boundary checking - només actualitzar si dins de la zona de joc
// Acumulació directa amb precisió subpíxel
if (dy > Constants::MARGE_DALT && dy < Constants::MARGE_BAIX) {
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 > Constants::MARGE_ESQ && dx < Constants::MARGE_DRET) {
if (dx > min_x && dx < max_x) {
centre_.x = dx;
}

View File

@@ -3,6 +3,7 @@
// © 2025 Port a C++20 amb SDL3
#include "escena_joc.hpp"
#include "../../core/rendering/line_renderer.hpp"
#include "../../core/system/gestor_escenes.hpp"
#include "../../core/system/global_events.hpp"
#include <cmath>
@@ -117,6 +118,9 @@ void EscenaJoc::actualitzar(float delta_time) {
}
void EscenaJoc::dibuixar() {
// Dibuixar marges de la zona de joc
dibuixar_marges();
// Dibuixar nau
nau_.dibuixar();
@@ -129,14 +133,6 @@ void EscenaJoc::dibuixar() {
for (const auto &bala : bales_) {
bala.dibuixar();
}
// [PRUEBA] Text vectorial
text_.render("0123456789", {10, 10}, 1.5f);
text_.render("SCORE: 1234", {10, 40}, 1.0f);
text_.render("10:45", {10, 70}, 2.0f);
text_.render("LEVEL-3", {10, 110}, 1.5f);
// TODO: Dibuixar marges (Fase 11)
}
void EscenaJoc::processar_input(const SDL_Event &event) {
@@ -169,3 +165,20 @@ void EscenaJoc::processar_input(const SDL_Event &event) {
void EscenaJoc::tocado() {
// TODO: Implementar seqüència de mort
}
void EscenaJoc::dibuixar_marges() const {
// Dibuixar rectangle de la zona de joc
const SDL_FRect& zona = Defaults::Zones::GAME;
// Coordenades dels cantons
int x1 = static_cast<int>(zona.x);
int y1 = static_cast<int>(zona.y);
int x2 = static_cast<int>(zona.x + zona.w);
int y2 = static_cast<int>(zona.y + zona.h);
// 4 línies per formar el rectangle
Rendering::linea(sdl_.obte_renderer(), x1, y1, x2, y1, true); // Top
Rendering::linea(sdl_.obte_renderer(), x1, y2, x2, y2, true); // Bottom
Rendering::linea(sdl_.obte_renderer(), x1, y1, x1, y2, true); // Left
Rendering::linea(sdl_.obte_renderer(), x2, y1, x2, y2, true); // Right
}

View File

@@ -43,6 +43,7 @@ private:
// Funcions privades
void tocado();
void dibuixar_marges() const; // Dibuixar vores de la zona de joc
};
#endif // ESCENA_JOC_HPP