Trabajando en las nuevas colisiones

This commit is contained in:
2022-09-04 15:39:12 +02:00
parent 5eaf44d0b8
commit 8630a0ae56
5 changed files with 224 additions and 161 deletions

View File

@@ -7,7 +7,7 @@ Game::Game(SDL_Renderer *renderer, Screen *screen, Asset *asset, Input *input)
clock = SDL_GetTicks();
currentRoom = "01.room";
spawnPoint = {2 * 8, 12 * 8, 0, 0, 0, STATUS_STANDING, SDL_FLIP_NONE};
debug = false;
debug = true;
// Copia los punteros
this->renderer = renderer;
@@ -161,10 +161,10 @@ void Game::update()
room->update();
{
player->update();
checkPlayerAndWalls(); // Debe ir detras del player update, por si se ha metido en algun muro
// checkPlayerAndWalls(); // Debe ir detras del player update, por si se ha metido en algun muro
}
checkPlayerOnBorder();
checkPlayerOnFloor();
// checkPlayerOnFloor();
checkPlayerAndItems();
checkPlayerAndEnemies();
scoreboard->update();
@@ -200,6 +200,11 @@ void Game::renderDebugInfo()
return;
}
// Borra el marcador
const SDL_Rect rect = {0, 17 * BLOCK, PLAY_AREA_WIDTH, GAMECANVAS_HEIGHT - PLAY_AREA_HEIGHT};
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderFillRect(renderer, &rect);
// Pinta la rejilla
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 64);
for (int i = 0; i < PLAY_AREA_BOTTOM; i += 8)
@@ -216,16 +221,19 @@ void Game::renderDebugInfo()
const int inc = debugText->getCharacterWidth() + 1;
int line = 131;
text = std::to_string((int)player->x) + "," + std::to_string((int)player->y);
debugText->write(0, line += inc, text);
text = "status: " + std::to_string(player->status);
debugText->write(0, line += inc, text);
text = "foot: " + std::to_string((int)player->getLeftFoot().y);
debugText->write(0, line += inc, text);
// text = "foot: " + std::to_string((int)player->getLeftFoot().y);
// debugText->write(0, line += inc, text);
const int a = (player->lastPosition.y + 16) / 8;
const int b = player->getLeftFoot().y / 8;
text = "tile: " + std::to_string(a) + " - " + std::to_string(b);
debugText->write(0, line += inc, text);
// const int a = (player->lastPosition.y + 16) / 8;
// const int b = player->getLeftFoot().y / 8;
// text = "tile: " + std::to_string(a) + " - " + std::to_string(b);
// debugText->write(0, line += inc, text);
const bool collision = checkPlayerAndEnemies();
text = "collision: " + std::to_string(collision);
@@ -261,6 +269,9 @@ bool Game::changeRoom(std::string file)
// Crea un objeto habitación nuevo a partir del fichero
room = new Room(asset->get(file), renderer, asset, itemTracker, &itemsPicked);
// Pasa la nueva habitación al jugador
player->setRoom(room);
success = true;
}
@@ -282,105 +293,6 @@ void Game::checkPlayerOnBorder()
}
}
// Comprueba si el jugador esta sobre el suelo
void Game::checkPlayerOnFloor()
{
// Comprueba si tiene suelo bajo los pies solo cuando no hay velocidad de subida
// y solo cuando el pie este encima de un bloque, es decir, en multiplos de 8
// *** HAY UN POSIBLE PROBLEMA y es que caiga muy rapido y viaje a mas de un pixel de velocidad,
// con lo que se saltaria la comprobación
// *** POSIBLE SOLUCION. Comprobar si el tile actual del pie es diferente al tile del pie previo.
// Esto indica que se ha saltado la comprobacion cada 8 pixeles.
// En este caso habría que recolocar al jugador en el sitio
// *** PARECE RESUELTO
const int a = (player->lastPosition.y + 16) / 8;
const int b = player->getLeftFoot().y / 8;
const bool tile_change = a != b;
const bool is_not_going_up = player->getVelY() >= 0;
const bool is_tile_aligned = player->getLeftFoot().y % 8 == 0;
if (((is_not_going_up) && (is_tile_aligned)) || ((is_not_going_up) && (tile_change)))
{
bool test = false;
test |= (room->getTile(player->getLeftFoot()) == TILE_SOLID);
test |= (room->getTile(player->getRightFoot()) == TILE_SOLID);
test |= (room->getTile(player->getLeftFoot()) == TILE_TRAVESSABLE);
test |= (room->getTile(player->getRightFoot()) == TILE_TRAVESSABLE);
// Tiene uno de los pies sobre una superficie
if (test)
{
player->setStatus(STATUS_STANDING);
// Si ha habido un cambio de tile, hay que recolocarlo
if (tile_change)
{
int offset = (int)player->sprite->getPosY() % 8;
player->sprite->setPosY((int)player->sprite->getPosY() - offset);
}
}
// Tiene ambos pies sobre el vacío
else if (player->getStatus() != STATUS_JUMPING)
{
player->setStatus(STATUS_FALLING);
}
}
}
// Comprueba que el jugador no atraviese ninguna pared
void Game::checkPlayerAndWalls()
{
// Obtiene los ocho puntos de colisión del jugador
const SDL_Rect rect = player->getRect();
const SDL_Point p1 = {rect.x, rect.y};
const SDL_Point p2 = {rect.x + 7, rect.y};
const SDL_Point p3 = {rect.x + 7, rect.y + 7};
const SDL_Point p4 = {rect.x, rect.y + 7};
const SDL_Point p5 = {rect.x, rect.y + 8};
const SDL_Point p6 = {rect.x + 7, rect.y + 8};
const SDL_Point p7 = {rect.x + 7, rect.y + 15};
const SDL_Point p8 = {rect.x, rect.y + 15};
// Comprueba si ha colisionado con un muro
bool wall = false;
wall |= (room->getTile(p1) == TILE_SOLID);
wall |= (room->getTile(p2) == TILE_SOLID);
wall |= (room->getTile(p3) == TILE_SOLID);
wall |= (room->getTile(p4) == TILE_SOLID);
wall |= (room->getTile(p5) == TILE_SOLID);
wall |= (room->getTile(p6) == TILE_SOLID);
wall |= (room->getTile(p7) == TILE_SOLID);
wall |= (room->getTile(p8) == TILE_SOLID);
if (wall)
{
// Si hay colisión, deshace el movimiento y lo pone en modo caída
player->undoLastMove();
player->setStatus(STATUS_FALLING);
}
// Comprueba si ha colisionado con un tile de los que matan al jugador
bool death = false;
death |= (room->getTile(p1) == TILE_KILL);
death |= (room->getTile(p2) == TILE_KILL);
death |= (room->getTile(p3) == TILE_KILL);
death |= (room->getTile(p4) == TILE_KILL);
death |= (room->getTile(p5) == TILE_KILL);
death |= (room->getTile(p6) == TILE_KILL);
death |= (room->getTile(p7) == TILE_KILL);
death |= (room->getTile(p8) == TILE_KILL);
if (death)
{
killPlayer();
}
}
// Comprueba las colisiones del jugador con los enemigos
bool Game::checkPlayerAndEnemies()
{