From dda1e049c61f5c97497754d73df753876c07228a Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Fri, 26 Aug 2022 20:41:48 +0200 Subject: [PATCH] =?UTF-8?q?Ya=20detecta=20las=20plataformas=20m=C3=B3viles?= =?UTF-8?q?=20bajo=20los=20pies?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/actor.cpp | 13 +++++++++--- source/actor.h | 8 ++++++-- source/game.cpp | 14 +++++++++++-- source/map.cpp | 27 +++++++++++++++++++++++++ source/map.h | 6 ++++++ source/player.cpp | 14 +++++++++++++ source/player.h | 4 ++++ source/utils.cpp | 51 +++++++++++++++++++++++++++++++++++++---------- source/utils.h | 3 +++ 9 files changed, 122 insertions(+), 18 deletions(-) diff --git a/source/actor.cpp b/source/actor.cpp index 805f385..6db6e0b 100644 --- a/source/actor.cpp +++ b/source/actor.cpp @@ -24,11 +24,12 @@ Actor::Actor(actor_t actor) sprite->setPosY(actor.y); sprite->setWidth(actor.w); sprite->setHeight(actor.h); - + sprite->setVelX(actor.vx); sprite->setVelY(actor.vy); - - sprite->setFlip(actor.vx>0?SDL_FLIP_NONE:SDL_FLIP_HORIZONTAL); + + sprite->setFlip(actor.vx > 0 ? SDL_FLIP_NONE : SDL_FLIP_HORIZONTAL); + this->name = actor.name; } // Destructor @@ -60,4 +61,10 @@ SDL_Rect &Actor::getCollider() { collider = sprite->getRect(); return collider; +} + +// Obtiene el nombre del actor +actor_name_e Actor::getName() +{ + return name; } \ No newline at end of file diff --git a/source/actor.h b/source/actor.h index 6678f1b..55681d8 100644 --- a/source/actor.h +++ b/source/actor.h @@ -9,7 +9,7 @@ #ifndef ACTOR_H #define ACTOR_H -// Tipos de actores +// Nombres de actores enum actor_name_e { a_moving_platform, @@ -32,7 +32,7 @@ struct actor_t float y; // Posición inicial en el eje Y float vx; // Velocidad en el eje X float vy; // Velocidad en el eje Y - actor_name_e name; // Tipo de actor + actor_name_e name; // Nombre del actor }; // Clase Actor @@ -44,6 +44,7 @@ protected: LTexture *texture; // Textura con los graficos del enemigo AnimatedSprite *sprite; // Sprite del enemigo SDL_Rect collider; // Caja de colisión + actor_name_e name; // Nombre del actor public: // Constructor @@ -61,6 +62,9 @@ public: // Obtiene el rectangulo de colision del enemigo SDL_Rect &getCollider(); + + // Obtiene el nombre del actor + actor_name_e getName(); }; #endif diff --git a/source/game.cpp b/source/game.cpp index b651cf1..1107b91 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -114,6 +114,11 @@ void Game::checkInput() delete player; player = new Player(renderer, asset, input, map); } + + if (input->checkInput(INPUT_BUTTON_ESCAPE, REPEAT_FALSE)) + { + section.name = SECTION_PROG_QUIT; + } } // Muestra información de depuración @@ -172,11 +177,16 @@ void Game::renderDebugInfo() text = map->getRoomFileName(b_top) + " " + map->getRoomFileName(b_right) + " " + map->getRoomFileName(b_bottom) + " " + map->getRoomFileName(b_left); debugText->write(0, line += 6, text, -1); - text = "ACTOR = " + std::to_string(player->checkActors()); + text = "isOnMovingPlatform = " + std::to_string(player->isOnMovingPlatform()); + //SDL_Point p = {76, 180}; + //SDL_Rect r = player->sprite->getRect(); + //SDL_SetRenderDrawColor(renderer, 255, 0, 0, 128); + //SDL_RenderDrawPoint(renderer, p.x, p.y); + //text = "checkCollision = " + std::to_string(checkCollision(p, r)); debugText->write(0, line += 6, text, -1); // Pinta mascaras - SDL_SetRenderDrawColor(renderer, 0, 255, 0, 128); + SDL_SetRenderDrawColor(renderer, 0, 255, 0, 128); SDL_Rect rect = player->sprite->getRect(); SDL_RenderFillRect(renderer, &rect); } diff --git a/source/map.cpp b/source/map.cpp index 81dd2ae..88daed7 100644 --- a/source/map.cpp +++ b/source/map.cpp @@ -502,5 +502,32 @@ int Map::actorCollision(SDL_Rect &rect) index++; } + return -1; +} + +// Indica si hay colision con un actor a partir de un rectangulo +int Map::actorCollision(SDL_Point &p) +{ + int index = 0; + for (auto actor : actors) + { + if (checkCollision(p, actor->getCollider())) + { + return index; + } + index++; + } + + return -1; +} + +// Devuelve el nombre del actor a pàrtir de un índice +int Map::getActorName(int index) +{ + if (index != -1) + { + return actors[index]->getName(); + } + return -1; } \ No newline at end of file diff --git a/source/map.h b/source/map.h index 8af98c9..cdc99db 100644 --- a/source/map.h +++ b/source/map.h @@ -94,6 +94,12 @@ public: // Indica si hay colision con un actor a partir de un rectangulo int actorCollision(SDL_Rect &rect); + + // Indica si hay colision con un actor a partir de un punto + int actorCollision(SDL_Point &p); + + // Devuelve el nombre del actor a pàrtir de un índice + int getActorName(int index); }; #endif diff --git a/source/player.cpp b/source/player.cpp index db1589a..b51d5dc 100644 --- a/source/player.cpp +++ b/source/player.cpp @@ -304,6 +304,20 @@ bool Player::isOnFloor() return onFloor; } +// Comprueba si el jugador tiene una plataforma movil bajo sus pies +bool Player::isOnMovingPlatform() +{ + bool onMovingPlatform = false; + + updateFeet(); + + for (auto f : underFeet) + { + onMovingPlatform |= (map->getActorName(map->actorCollision(f)) == a_moving_platform); + } + return onMovingPlatform; +} + // Comprueba si está situado en alguno de los cuatro bordes de la habitación bool Player::isOnScreenBorder() { diff --git a/source/player.h b/source/player.h index 34ab04f..0698102 100644 --- a/source/player.h +++ b/source/player.h @@ -7,6 +7,7 @@ #include "animatedsprite.h" #include "asset.h" #include "map.h" +#include "actor.h" #ifndef PLAYER_H #define PLAYER_H @@ -83,6 +84,9 @@ public: // Comprueba si el jugador tiene suelo debajo de los pies bool isOnFloor(); + // Comprueba si el jugador tiene una plataforma movil bajo sus pies + bool isOnMovingPlatform(); + // Comprueba las interacciones con los actores int checkActors(); diff --git a/source/utils.cpp b/source/utils.cpp index a90cf2d..36b15cb 100644 --- a/source/utils.cpp +++ b/source/utils.cpp @@ -3,8 +3,8 @@ // Calcula el cuadrado de la distancia entre dos puntos double distanceSquared(int x1, int y1, int x2, int y2) { - int deltaX = x2 - x1; - int deltaY = y2 - y1; + const int deltaX = x2 - x1; + const int deltaY = y2 - y1; return deltaX * deltaX + deltaY * deltaY; } @@ -71,20 +71,20 @@ bool checkCollision(circle_t &a, SDL_Rect &b) return false; } -// Detector de colisiones entre un dos rectangulos +// Detector de colisiones entre dos rectangulos bool checkCollision(SDL_Rect &a, SDL_Rect &b) { // Calculate the sides of rect A - int leftA = a.x; - int rightA = a.x + a.w; - int topA = a.y; - int bottomA = a.y + a.h; + const int leftA = a.x; + const int rightA = a.x + a.w; + const int topA = a.y; + const int bottomA = a.y + a.h; // Calculate the sides of rect B - int leftB = b.x; - int rightB = b.x + b.w; - int topB = b.y; - int bottomB = b.y + b.h; + const int leftB = b.x; + const int rightB = b.x + b.w; + const int topB = b.y; + const int bottomB = b.y + b.h; // If any of the sides from A are outside of B if (bottomA <= topB) @@ -111,6 +111,35 @@ bool checkCollision(SDL_Rect &a, SDL_Rect &b) return true; } +// Detector de colisiones entre un punto y u rectangulo +bool checkCollision(SDL_Point &p, SDL_Rect &r) +{ + // Comprueba si el punto está fuera del rectangulo en el eje X + if (p.x < r.x) + { + return false; + } + + if (p.x > r.x + r.w) + { + return false; + } + + // Comprueba si el punto está fuera del rectangulo en el eje Y + if (p.y < r.y) + { + return false; + } + + if (p.y > r.y + r.h) + { + return false; + } + + // Si ha llegado hasta aquí, es que está dentro + return true; +} + // Carga un archivo de imagen en una textura bool loadTextureFromFile(LTexture *texture, std::string path, SDL_Renderer *renderer) { diff --git a/source/utils.h b/source/utils.h index 23ed47a..2acf21f 100644 --- a/source/utils.h +++ b/source/utils.h @@ -74,6 +74,9 @@ bool checkCollision(circle_t &a, SDL_Rect &b); // Detector de colisiones entre un dos rectangulos bool checkCollision(SDL_Rect &a, SDL_Rect &b); +// Detector de colisiones entre un punto y u rectangulo +bool checkCollision(SDL_Point &p, SDL_Rect &r); + // Carga un archivo de imagen en una textura bool loadTextureFromFile(LTexture *texture, std::string path, SDL_Renderer *renderer);