Ya detecta las plataformas móviles bajo los pies
This commit is contained in:
@@ -24,11 +24,12 @@ Actor::Actor(actor_t actor)
|
|||||||
sprite->setPosY(actor.y);
|
sprite->setPosY(actor.y);
|
||||||
sprite->setWidth(actor.w);
|
sprite->setWidth(actor.w);
|
||||||
sprite->setHeight(actor.h);
|
sprite->setHeight(actor.h);
|
||||||
|
|
||||||
sprite->setVelX(actor.vx);
|
sprite->setVelX(actor.vx);
|
||||||
sprite->setVelY(actor.vy);
|
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
|
// Destructor
|
||||||
@@ -60,4 +61,10 @@ SDL_Rect &Actor::getCollider()
|
|||||||
{
|
{
|
||||||
collider = sprite->getRect();
|
collider = sprite->getRect();
|
||||||
return collider;
|
return collider;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Obtiene el nombre del actor
|
||||||
|
actor_name_e Actor::getName()
|
||||||
|
{
|
||||||
|
return name;
|
||||||
}
|
}
|
||||||
@@ -9,7 +9,7 @@
|
|||||||
#ifndef ACTOR_H
|
#ifndef ACTOR_H
|
||||||
#define ACTOR_H
|
#define ACTOR_H
|
||||||
|
|
||||||
// Tipos de actores
|
// Nombres de actores
|
||||||
enum actor_name_e
|
enum actor_name_e
|
||||||
{
|
{
|
||||||
a_moving_platform,
|
a_moving_platform,
|
||||||
@@ -32,7 +32,7 @@ struct actor_t
|
|||||||
float y; // Posición inicial en el eje Y
|
float y; // Posición inicial en el eje Y
|
||||||
float vx; // Velocidad en el eje X
|
float vx; // Velocidad en el eje X
|
||||||
float vy; // Velocidad en el eje Y
|
float vy; // Velocidad en el eje Y
|
||||||
actor_name_e name; // Tipo de actor
|
actor_name_e name; // Nombre del actor
|
||||||
};
|
};
|
||||||
|
|
||||||
// Clase Actor
|
// Clase Actor
|
||||||
@@ -44,6 +44,7 @@ protected:
|
|||||||
LTexture *texture; // Textura con los graficos del enemigo
|
LTexture *texture; // Textura con los graficos del enemigo
|
||||||
AnimatedSprite *sprite; // Sprite del enemigo
|
AnimatedSprite *sprite; // Sprite del enemigo
|
||||||
SDL_Rect collider; // Caja de colisión
|
SDL_Rect collider; // Caja de colisión
|
||||||
|
actor_name_e name; // Nombre del actor
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// Constructor
|
// Constructor
|
||||||
@@ -61,6 +62,9 @@ public:
|
|||||||
|
|
||||||
// Obtiene el rectangulo de colision del enemigo
|
// Obtiene el rectangulo de colision del enemigo
|
||||||
SDL_Rect &getCollider();
|
SDL_Rect &getCollider();
|
||||||
|
|
||||||
|
// Obtiene el nombre del actor
|
||||||
|
actor_name_e getName();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -114,6 +114,11 @@ void Game::checkInput()
|
|||||||
delete player;
|
delete player;
|
||||||
player = new Player(renderer, asset, input, map);
|
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
|
// 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);
|
text = map->getRoomFileName(b_top) + " " + map->getRoomFileName(b_right) + " " + map->getRoomFileName(b_bottom) + " " + map->getRoomFileName(b_left);
|
||||||
debugText->write(0, line += 6, text, -1);
|
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);
|
debugText->write(0, line += 6, text, -1);
|
||||||
|
|
||||||
// Pinta mascaras
|
// Pinta mascaras
|
||||||
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 128);
|
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 128);
|
||||||
SDL_Rect rect = player->sprite->getRect();
|
SDL_Rect rect = player->sprite->getRect();
|
||||||
SDL_RenderFillRect(renderer, &rect);
|
SDL_RenderFillRect(renderer, &rect);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -502,5 +502,32 @@ int Map::actorCollision(SDL_Rect &rect)
|
|||||||
index++;
|
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;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -94,6 +94,12 @@ public:
|
|||||||
|
|
||||||
// Indica si hay colision con un actor a partir de un rectangulo
|
// Indica si hay colision con un actor a partir de un rectangulo
|
||||||
int actorCollision(SDL_Rect &rect);
|
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
|
#endif
|
||||||
|
|||||||
@@ -304,6 +304,20 @@ bool Player::isOnFloor()
|
|||||||
return onFloor;
|
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
|
// Comprueba si está situado en alguno de los cuatro bordes de la habitación
|
||||||
bool Player::isOnScreenBorder()
|
bool Player::isOnScreenBorder()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
#include "animatedsprite.h"
|
#include "animatedsprite.h"
|
||||||
#include "asset.h"
|
#include "asset.h"
|
||||||
#include "map.h"
|
#include "map.h"
|
||||||
|
#include "actor.h"
|
||||||
|
|
||||||
#ifndef PLAYER_H
|
#ifndef PLAYER_H
|
||||||
#define PLAYER_H
|
#define PLAYER_H
|
||||||
@@ -83,6 +84,9 @@ public:
|
|||||||
// Comprueba si el jugador tiene suelo debajo de los pies
|
// Comprueba si el jugador tiene suelo debajo de los pies
|
||||||
bool isOnFloor();
|
bool isOnFloor();
|
||||||
|
|
||||||
|
// Comprueba si el jugador tiene una plataforma movil bajo sus pies
|
||||||
|
bool isOnMovingPlatform();
|
||||||
|
|
||||||
// Comprueba las interacciones con los actores
|
// Comprueba las interacciones con los actores
|
||||||
int checkActors();
|
int checkActors();
|
||||||
|
|
||||||
|
|||||||
@@ -3,8 +3,8 @@
|
|||||||
// Calcula el cuadrado de la distancia entre dos puntos
|
// Calcula el cuadrado de la distancia entre dos puntos
|
||||||
double distanceSquared(int x1, int y1, int x2, int y2)
|
double distanceSquared(int x1, int y1, int x2, int y2)
|
||||||
{
|
{
|
||||||
int deltaX = x2 - x1;
|
const int deltaX = x2 - x1;
|
||||||
int deltaY = y2 - y1;
|
const int deltaY = y2 - y1;
|
||||||
return deltaX * deltaX + deltaY * deltaY;
|
return deltaX * deltaX + deltaY * deltaY;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,20 +71,20 @@ bool checkCollision(circle_t &a, SDL_Rect &b)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Detector de colisiones entre un dos rectangulos
|
// Detector de colisiones entre dos rectangulos
|
||||||
bool checkCollision(SDL_Rect &a, SDL_Rect &b)
|
bool checkCollision(SDL_Rect &a, SDL_Rect &b)
|
||||||
{
|
{
|
||||||
// Calculate the sides of rect A
|
// Calculate the sides of rect A
|
||||||
int leftA = a.x;
|
const int leftA = a.x;
|
||||||
int rightA = a.x + a.w;
|
const int rightA = a.x + a.w;
|
||||||
int topA = a.y;
|
const int topA = a.y;
|
||||||
int bottomA = a.y + a.h;
|
const int bottomA = a.y + a.h;
|
||||||
|
|
||||||
// Calculate the sides of rect B
|
// Calculate the sides of rect B
|
||||||
int leftB = b.x;
|
const int leftB = b.x;
|
||||||
int rightB = b.x + b.w;
|
const int rightB = b.x + b.w;
|
||||||
int topB = b.y;
|
const int topB = b.y;
|
||||||
int bottomB = b.y + b.h;
|
const int bottomB = b.y + b.h;
|
||||||
|
|
||||||
// If any of the sides from A are outside of B
|
// If any of the sides from A are outside of B
|
||||||
if (bottomA <= topB)
|
if (bottomA <= topB)
|
||||||
@@ -111,6 +111,35 @@ bool checkCollision(SDL_Rect &a, SDL_Rect &b)
|
|||||||
return true;
|
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
|
// Carga un archivo de imagen en una textura
|
||||||
bool loadTextureFromFile(LTexture *texture, std::string path, SDL_Renderer *renderer)
|
bool loadTextureFromFile(LTexture *texture, std::string path, SDL_Renderer *renderer)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -74,6 +74,9 @@ bool checkCollision(circle_t &a, SDL_Rect &b);
|
|||||||
// Detector de colisiones entre un dos rectangulos
|
// Detector de colisiones entre un dos rectangulos
|
||||||
bool checkCollision(SDL_Rect &a, SDL_Rect &b);
|
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
|
// Carga un archivo de imagen en una textura
|
||||||
bool loadTextureFromFile(LTexture *texture, std::string path, SDL_Renderer *renderer);
|
bool loadTextureFromFile(LTexture *texture, std::string path, SDL_Renderer *renderer);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user