Ya detecta las plataformas móviles bajo los pies

This commit is contained in:
2022-08-26 20:41:48 +02:00
parent 5ff62a6950
commit dda1e049c6
9 changed files with 122 additions and 18 deletions

View File

@@ -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;
}

View File

@@ -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

View File

@@ -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);
}

View File

@@ -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;
}

View File

@@ -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

View File

@@ -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()
{

View File

@@ -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();

View File

@@ -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)
{

View File

@@ -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);