Depurando los saltos con colisiones
This commit is contained in:
@@ -113,9 +113,9 @@ void Game::update()
|
||||
}
|
||||
}
|
||||
|
||||
checkInput();
|
||||
mRoom->update();
|
||||
mPlayer->update();
|
||||
checkPlayerAndWalls(); // Debe ir detras del player update, por si se ha metido en algun muro, sacarlo
|
||||
checkPlayerOnBorder();
|
||||
checkPlayerOnFloor();
|
||||
}
|
||||
@@ -125,9 +125,7 @@ void Game::update()
|
||||
void Game::draw()
|
||||
{
|
||||
// Prepara para dibujar el frame
|
||||
const color_t color = {0xAA, 0x55, 0x55};
|
||||
mScreen->start();
|
||||
mScreen->clean(color);
|
||||
mScreen->clean(mRoom->getBGColor());
|
||||
|
||||
mRoom->drawMap();
|
||||
@@ -135,29 +133,39 @@ void Game::draw()
|
||||
mPlayer->draw();
|
||||
|
||||
// Texto en el centro de la pantalla
|
||||
mText->writeCentered(GAMECANVAS_CENTER_X, 18 * 8, mRoom->getName());
|
||||
SDL_Rect rect = {0, 16 * 8, PLAY_AREA_RIGHT, 8};
|
||||
color_t color = stringToColor("light_black");
|
||||
SDL_SetRenderDrawColor(mRenderer, color.r, color.g, color.b, 0xFF);
|
||||
SDL_RenderFillRect(mRenderer, &rect);
|
||||
|
||||
mText->writeCentered(GAMECANVAS_CENTER_X, 16 * 8, mRoom->getName());
|
||||
|
||||
// Debug info
|
||||
std::string text;
|
||||
text = "status: " + std::to_string(mPlayer->status);
|
||||
mText->write(0, 17 * 8, text);
|
||||
|
||||
// Actualiza la pantalla
|
||||
mScreen->blit();
|
||||
}
|
||||
|
||||
// Comprueba la entrada
|
||||
/*
|
||||
void Game::checkInput()
|
||||
{
|
||||
/*
|
||||
if (mInput->checkInput(INPUT_UP, REPEAT_FALSE))
|
||||
changeRoom(mRoom->getRoomUp());
|
||||
if (mInput->checkInput(INPUT_UP, REPEAT_FALSE))
|
||||
changeRoom(mRoom->getRoomUp());
|
||||
|
||||
if (mInput->checkInput(INPUT_DOWN, REPEAT_FALSE))
|
||||
changeRoom(mRoom->getRoomDown());
|
||||
if (mInput->checkInput(INPUT_DOWN, REPEAT_FALSE))
|
||||
changeRoom(mRoom->getRoomDown());
|
||||
|
||||
if (mInput->checkInput(INPUT_LEFT, REPEAT_FALSE))
|
||||
changeRoom(mRoom->getRoomLeft());
|
||||
if (mInput->checkInput(INPUT_LEFT, REPEAT_FALSE))
|
||||
changeRoom(mRoom->getRoomLeft());
|
||||
|
||||
if (mInput->checkInput(INPUT_RIGHT, REPEAT_FALSE))
|
||||
changeRoom(mRoom->getRoomRight());
|
||||
*/
|
||||
if (mInput->checkInput(INPUT_RIGHT, REPEAT_FALSE))
|
||||
changeRoom(mRoom->getRoomRight());
|
||||
}
|
||||
*/
|
||||
|
||||
// Cambia de habitación
|
||||
bool Game::changeRoom(std::string file)
|
||||
@@ -207,4 +215,36 @@ void Game::checkPlayerOnFloor()
|
||||
{
|
||||
mPlayer->setStatus(STATUS_FALLING);
|
||||
}
|
||||
}
|
||||
|
||||
// Comprueba que el jugador no atraviese ninguna pared
|
||||
void Game::checkPlayerAndWalls()
|
||||
{
|
||||
// Hayque comprobar las cuatro esquinas del tile superior
|
||||
// y las cuatro del tile inferior
|
||||
SDL_Rect rect = mPlayer->getRect();
|
||||
SDL_Point p1 = {rect.x, rect.y};
|
||||
SDL_Point p2 = {rect.x + 7, rect.y};
|
||||
SDL_Point p3 = {rect.x + 7, rect.y + 7};
|
||||
SDL_Point p4 = {rect.x, rect.y + 7};
|
||||
|
||||
SDL_Point p5 = {rect.x, rect.y + 8};
|
||||
SDL_Point p6 = {rect.x + 7, rect.y + 8};
|
||||
SDL_Point p7 = {rect.x + 7, rect.y + 15};
|
||||
SDL_Point p8 = {rect.x, rect.y + 15};
|
||||
|
||||
bool test = mRoom->isFloor(p1);
|
||||
test |= mRoom->isFloor(p2);
|
||||
test |= mRoom->isFloor(p3);
|
||||
test |= mRoom->isFloor(p4);
|
||||
test |= mRoom->isFloor(p5);
|
||||
test |= mRoom->isFloor(p6);
|
||||
test |= mRoom->isFloor(p7);
|
||||
test |= mRoom->isFloor(p8);
|
||||
|
||||
if (test)
|
||||
{
|
||||
mPlayer->undoLastMove();
|
||||
// mPlayer->setStatus(STATUS_STANDING);
|
||||
}
|
||||
}
|
||||
@@ -51,7 +51,7 @@ private:
|
||||
void draw();
|
||||
|
||||
// Comprueba la entrada y actua
|
||||
void checkInput();
|
||||
//void checkInput();
|
||||
|
||||
// Cambia de habitación
|
||||
bool changeRoom(std::string file);
|
||||
@@ -62,6 +62,9 @@ private:
|
||||
// Comprueba si el jugador esta sobre el suelo
|
||||
void checkPlayerOnFloor();
|
||||
|
||||
// Comprueba que el jugador no atraviese ninguna pared
|
||||
void checkPlayerAndWalls();
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
Game(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, Lang *lang, Input *input);
|
||||
|
||||
@@ -44,6 +44,8 @@ Player::Player(std::string _tileset, SDL_Renderer *_renderer, Asset *_asset, Inp
|
||||
sprite->setAnimationFrames(0, 2, 8 * 2, 0, 8, 16);
|
||||
sprite->setAnimationFrames(0, 3, 8 * 3, 0, 8, 16);
|
||||
sprite->setSpriteClip(sprite->getAnimationClip(0, 0));
|
||||
|
||||
lastPosition = getRect();
|
||||
}
|
||||
|
||||
// Destructor
|
||||
@@ -68,6 +70,7 @@ void Player::draw()
|
||||
// Actualiza las variables del objeto
|
||||
void Player::update()
|
||||
{
|
||||
setLastPosition();
|
||||
checkInput();
|
||||
sprite->update();
|
||||
checkBorders();
|
||||
@@ -77,12 +80,6 @@ void Player::update()
|
||||
// Comprueba las entradas y modifica variables
|
||||
void Player::checkInput()
|
||||
{
|
||||
// if (mInput->checkInput(INPUT_UP, REPEAT_FALSE))
|
||||
// changeRoom(mRoom->getRoomUp());
|
||||
//
|
||||
// if (mInput->checkInput(INPUT_DOWN, REPEAT_FALSE))
|
||||
// changeRoom(mRoom->getRoomDown());
|
||||
|
||||
// Se mueve en horizontal solo cuando no esté cayendo
|
||||
if ((input->checkInput(INPUT_LEFT, REPEAT_TRUE)) && (status != STATUS_FALLING))
|
||||
{
|
||||
@@ -187,7 +184,7 @@ SDL_Point Player::getLeftFoot()
|
||||
// Obtiene el valor del pixel inferior derecho del jugador
|
||||
SDL_Point Player::getRightFoot()
|
||||
{
|
||||
SDL_Point point = {(int)sprite->getPosX() + sprite->getWidth(), (int)sprite->getPosY() + sprite->getHeight()};
|
||||
SDL_Point point = {(int)sprite->getPosX() + sprite->getWidth() - 1, (int)sprite->getPosY() + sprite->getHeight()};
|
||||
return point;
|
||||
}
|
||||
|
||||
@@ -233,6 +230,19 @@ void Player::applyGravity()
|
||||
// Obtiene el rectangulo que delimita al jugador
|
||||
SDL_Rect Player::getRect()
|
||||
{
|
||||
SDL_Rect rect = {sprite->getPosX(), sprite->getPosY(), sprite->getWidth(), sprite->getHeight()};
|
||||
SDL_Rect rect = {(int)sprite->getPosX(), (int)sprite->getPosY(), sprite->getWidth(), sprite->getHeight()};
|
||||
return rect;
|
||||
}
|
||||
|
||||
// Guarda la posición actual en la variable lastPosition
|
||||
void Player::setLastPosition()
|
||||
{
|
||||
lastPosition = getRect();
|
||||
}
|
||||
|
||||
// Deshace el ultimo movimiento
|
||||
void Player::undoLastMove()
|
||||
{
|
||||
sprite->setPosX(lastPosition.x);
|
||||
sprite->setPosY(lastPosition.y);
|
||||
}
|
||||
@@ -28,7 +28,6 @@ private:
|
||||
SDL_Renderer *renderer; // El renderizador de la ventana
|
||||
Asset *asset; // Objeto con la ruta a todos los ficheros de recursos
|
||||
color_t color; // Color del jugador
|
||||
int status; // Estado en el que se encuentra el jugador. Util apara saber si está saltando o cayendo
|
||||
SDL_Rect lastPosition; // Contiene la ultima posición del jugador, por si hay que deshacer algun movimiento
|
||||
|
||||
bool onBorder; // Indica si el jugador esta en uno de los cuatro bordes de la pantalla
|
||||
@@ -46,7 +45,12 @@ private:
|
||||
// Aplica gravedad al jugador
|
||||
void applyGravity();
|
||||
|
||||
// Guarda la posición actual en la variable lastPosition
|
||||
void setLastPosition();
|
||||
|
||||
public:
|
||||
int status; // Estado en el que se encuentra el jugador. Util apara saber si está saltando o cayendo
|
||||
|
||||
// Constructor
|
||||
Player(std::string _tileset, SDL_Renderer *_renderer, Asset *_asset, Input *_input, Room *_room);
|
||||
|
||||
@@ -82,6 +86,9 @@ public:
|
||||
|
||||
// Obtiene el rectangulo que delimita al jugador
|
||||
SDL_Rect getRect();
|
||||
|
||||
// Deshace el ultimo movimiento
|
||||
void undoLastMove();
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user