From 89302a2ee3277510f040c64c13e9cb9991223a84 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Sat, 29 Nov 2025 09:04:18 +0100 Subject: [PATCH] afegides zones --- source/game/constants.hpp | 24 +++++++++++++++++++----- source/game/entities/bala.cpp | 5 ++--- source/game/entities/enemic.cpp | 8 ++++++-- source/game/entities/nau.cpp | 9 ++++++--- source/game/escenes/escena_joc.cpp | 29 +++++++++++++++++++++-------- source/game/escenes/escena_joc.hpp | 1 + 6 files changed, 55 insertions(+), 21 deletions(-) diff --git a/source/game/constants.hpp b/source/game/constants.hpp index 56ee835..961ce92 100644 --- a/source/game/constants.hpp +++ b/source/game/constants.hpp @@ -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(Defaults::Zones::GAME.x); +constexpr int MARGE_DRET = static_cast(Defaults::Zones::GAME.x + Defaults::Zones::GAME.w); +constexpr int MARGE_DALT = static_cast(Defaults::Zones::GAME.y); +constexpr int MARGE_BAIX = static_cast(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(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 diff --git a/source/game/entities/bala.cpp b/source/game/entities/bala.cpp index 4e54ec6..a6a5070 100644 --- a/source/game/entities/bala.cpp +++ b/source/game/entities/bala.cpp @@ -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; } } diff --git a/source/game/entities/enemic.cpp b/source/game/entities/enemic.cpp index e35c195..b12da8e 100644 --- a/source/game/entities/enemic.cpp +++ b/source/game/entities/enemic.cpp @@ -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 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 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(std::rand() % 256) / 512.0f); diff --git a/source/game/entities/nau.cpp b/source/game/entities/nau.cpp index b5c247c..dd54286 100644 --- a/source/game/entities/nau.cpp +++ b/source/game/entities/nau.cpp @@ -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; } diff --git a/source/game/escenes/escena_joc.cpp b/source/game/escenes/escena_joc.cpp index 0344688..5ac5c5c 100644 --- a/source/game/escenes/escena_joc.cpp +++ b/source/game/escenes/escena_joc.cpp @@ -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 @@ -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(zona.x); + int y1 = static_cast(zona.y); + int x2 = static_cast(zona.x + zona.w); + int y2 = static_cast(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 +} diff --git a/source/game/escenes/escena_joc.hpp b/source/game/escenes/escena_joc.hpp index fc64690..586ab21 100644 --- a/source/game/escenes/escena_joc.hpp +++ b/source/game/escenes/escena_joc.hpp @@ -43,6 +43,7 @@ private: // Funcions privades void tocado(); + void dibuixar_marges() const; // Dibuixar vores de la zona de joc }; #endif // ESCENA_JOC_HPP