Terminados los tiles atravesables

This commit is contained in:
2022-08-20 09:15:51 +02:00
parent 8765049b69
commit 7e93b3150f
6 changed files with 102 additions and 58 deletions

View File

@@ -12,23 +12,23 @@
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,159,160,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,159,160,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,259,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,191,192,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,191,192,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,259,0,0,0,
0,0,0,0,0,0,0,263,264,0,0,0,0,0,0,0,0,257,258,0,0,0,0,0,0,0,0,0,263,264,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,263,264,0,0,0,0,0,0,0,0,257,258,0,0,0,0,0,0,0,0,0,263,264,0,0,0,0,0,0,259,0,0,0,
0,0,0,0,0,0,0,295,296,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,265,266,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,295,296,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,265,266,0,0,0,0,0,0,259,0,0,0,
0,263,264,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,297,298,0,0,0,0,0,0,0,0,0,0, 0,263,264,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,259,0,0,0,
0,295,296,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,263,0,0, 0,295,296,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,577,578,579,580,581,582,0,0,0,0,0,0,259,0,0,259,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,577,578,579,580,581,582,0,0,0,0,0,0,259,0,0,259,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,263,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,577,578,579,580,581,582,0,0,0,0,0,0,259,0,0,259,0,0,0,
0,0,0,0,239,240,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,265,266,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,239,240,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,577,578,579,580,581,582,0,265,266,0,0,0,259,0,0,259,0,0,0,
0,0,0,257,258,257,258,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,93,94,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,257,258,257,258,0,0,0,0,0,0,0,0,0,0,0,0,0,0,577,578,579,580,581,582,0,0,0,0,0,0,259,0,0,259,0,0,0,
0,0,0,289,290,289,290,0,0,0,0,577,578,579,580,581,582,0,0,0,0,0,0,0,0,125,126,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,289,290,289,290,0,0,0,0,577,578,579,580,581,582,0,0,0,0,577,578,579,580,581,582,0,0,0,0,0,0,259,0,0,259,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,609,610,611,612,613,614,0,0,0,0,0,0,0,0,157,158,0,0,0,0,263,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,577,578,579,580,581,582,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,189,190,0,0,0,0,0,0,0,0,0,0,189,190,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,577,578,579,580,581,582,0,0,0,0,0,0,0,0,0,0,0,0,0,
263,264,0,0,0,0,0,193,194,0,0,159,160,189,190,0,0,0,0,0,0,0,281,282,277,278,279,280,0,0,0,0,0,0,0,0,0,0,0,0, 263,264,0,0,0,0,0,193,194,0,0,0,0,0,0,0,0,0,0,0,0,0,281,282,277,278,279,280,0,0,0,0,0,0,0,0,0,0,0,0,
295,296,0,0,0,0,0,225,226,0,0,191,192,189,190,0,0,281,275,275,275,275,313,314,309,310,311,312,0,0,0,0,0,0,0,0,0,0,0,0, 295,296,0,0,0,0,0,225,226,0,0,0,0,0,0,0,0,281,275,275,275,275,313,314,309,310,311,312,0,0,0,0,0,0,0,0,0,0,0,0,
265,264,263,264,263,264,263,264,263,264,263,264,263,264,263,264,263,264,263,266,266,266,283,284,266,265,266,264,263,264,263,264,263,264,263,264,263,264,263,264, 265,264,263,264,263,264,263,264,263,0,263,264,263,264,263,264,263,264,263,266,266,266,283,284,266,265,266,264,263,264,263,264,263,264,263,264,263,264,263,264,
295,296,295,296,295,296,295,296,295,296,295,296,295,296,295,296,295,296,295,296,295,296,315,316,298,297,298,296,295,296,295,296,295,296,295,296,295,296,295,296 295,296,295,296,295,296,295,296,295,0,295,296,295,296,295,296,295,296,295,296,295,296,315,316,298,297,298,296,295,296,295,296,295,296,295,296,295,296,295,296
</data> </data>
</layer> </layer>
</map> </map>

View File

@@ -118,6 +118,18 @@ void Game::renderDebugInfo()
return; return;
} }
// Pinta la rejilla
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 16);
for (int i = 0; i < 240; i += 8)
{
SDL_RenderDrawLine(renderer, 0, i, 320, i);
}
for (int i = 0; i < 320; i += 8)
{
SDL_RenderDrawLine(renderer, i, 0, i, 240);
}
// Pinta el texto
int line = 0; int line = 0;
std::string text = ""; std::string text = "";
@@ -136,7 +148,10 @@ void Game::renderDebugInfo()
text = "isOnFloor " + std::to_string(player->isOnFloor()); text = "isOnFloor " + std::to_string(player->isOnFloor());
debugText->write(0, line += 6, text, -1); debugText->write(0, line += 6, text, -1);
text = "getTile " + std::to_string(player->map->getTile(player->collider[0])); const std::string foot_x = std::to_string(player->underFeet[0].x);
const std::string foot_y = std::to_string(player->underFeet[0].y);
const std::string gettile = std::to_string(player->map->getTile(player->underFeet[0]));
text = "getTile(" + foot_x + "," + foot_y + ") = " + gettile;
debugText->write(0, line += 6, text, -1); debugText->write(0, line += 6, text, -1);
text = "state " + std::to_string(player->state); text = "state " + std::to_string(player->state);

View File

@@ -226,13 +226,13 @@ void Map::render()
t_tile_map Map::getTile(SDL_Point p) t_tile_map Map::getTile(SDL_Point p)
{ {
const int tile = tilemap[((p.y / tile_size) * map_width) + (p.x / tile_size)]; const int tile = tilemap[((p.y / tile_size) * map_width) + (p.x / tile_size)];
const int png_width = 16 * 2; const int png_width = 32;
if (tile >= 0 && tile < 4 * 2 * png_width) if (tile >= 0 && tile < 8 * png_width)
{ {
return nothing; return nothing;
} }
else if (tile >= (4 * 2 * png_width) && tile < 8 * 2 * png_width) else if (tile >= (8 * png_width) && tile < 16 * png_width)
{ {
return wall; return wall;
} }
@@ -247,3 +247,9 @@ int Map::getTileSize()
{ {
return tile_size; return tile_size;
} }
// Devuelve el indice del tile correspondiente a un punto del mapa
int Map::getTileIndex(SDL_Point p)
{
return tilemap[((p.y / tile_size) * map_width) + (p.x / tile_size)];
}

View File

@@ -68,6 +68,9 @@ public:
// Devuelve el valor de la variable // Devuelve el valor de la variable
int getTileSize(); int getTileSize();
// Devuelve el indice del tile correspondiente a un punto del mapa
int getTileIndex(SDL_Point p);
}; };
#endif #endif

View File

@@ -23,6 +23,7 @@ Player::Player(SDL_Renderer *renderer, Asset *asset, Input *input, Map *map)
y = 168; y = 168;
vx = 0; vx = 0;
vy = 0; vy = 0;
lastPosition = {(int)x, (int)y};
const SDL_Rect rect = {(int)x, (int)y, 16, 24}; const SDL_Rect rect = {(int)x, (int)y, 16, 24};
sprite->setPos(rect); sprite->setPos(rect);
sprite->setCurrentAnimation("stand"); sprite->setCurrentAnimation("stand");
@@ -64,7 +65,7 @@ Player::~Player()
void Player::update() void Player::update()
{ {
checkInput(); checkInput();
addGravity(); // addGravity();
move(); move();
animate(); animate();
} }
@@ -136,13 +137,10 @@ void Player::checkInput()
// Aplica la gravedad // Aplica la gravedad
void Player::addGravity() void Player::addGravity()
{ {
if (!isOnFloor()) // *** Falta ver pq la gravedad empuja al muñeco hacia abajo en los tiles atravesables
if (state != standing)
{ {
vy = std::min(vy += gravity, maxVY); vy = std::min(vy += gravity, maxVY);
if (state == standing)
{
state = falling;
}
} }
} }
@@ -198,48 +196,74 @@ bool Player::checkMapCollisions()
// Mueve al jugador en función de la velocidad/desplazamiento // Mueve al jugador en función de la velocidad/desplazamiento
void Player::move() void Player::move()
{ {
const int tile = map->getTileSize(); const int tileSize = map->getTileSize();
lastPosition = {(int)x, (int)y};
addGravity();
// Mueve en el eje X y comprueba colisiones con muros
x += vx; x += vx;
if (checkMapCollisions()) if (checkMapCollisions())
{ {
// Recoloca // Recoloca
if (vx > 0) if (vx > 0)
{ {
x -= ((int)x + w) % tile; x -= ((int)x + w) % tileSize;
} }
else else
{ {
x += tile - ((int)x % tile); x += tileSize - ((int)x % tileSize);
} }
vx = 0.0f; vx = 0.0f;
} }
const bool wasOnFloor = isOnFloor(); // Mueve en el eje Y y comprueba colisiones con muros
y += vy; y += vy;
if (checkMapCollisions()) if (checkMapCollisions())
{ {
// Recoloca // Recoloca
if (vy > 0.0f) if (vy > 0.0f)
{ {
y -= ((int)y + h) % tile; y -= ((int)y + h) % tileSize;
state = standing; state = standing;
} }
else else
{ {
y += tile - ((int)y % tile); y += tileSize - ((int)y % tileSize);
state = falling; state = falling;
} }
vy = 0.0f; vy = 0.0f;
} }
else if ((!wasOnFloor) && (isOnFloor()) && (vy > 0.0f))
// Si no hay colisiones con los muros en el eje Y, comprueba no haya atravesado
// el suelo de un tile atravesble
else
{
const int a = (lastPosition.y + h) / tileSize;
const int b = ((int)y + h) / tileSize;
const bool tile_change = a != b;
const bool going_down = vy >= 0.0f;
const bool tile_aligned = ((int)y + h) % tileSize == 0;
if (((going_down) && (tile_aligned)) || ((going_down) && (tile_change)))
{
// Tiene uno de los pies sobre una superficie
if (isOnFloor())
{ {
state = standing; state = standing;
vy = 0.0f; vy = 0.0f;
y -= ((int)y + h) % tile; y -= ((int)y + h) % tileSize;
}
// Tiene ambos pies sobre el vacío
else
{
state = falling;
}
}
} }
// Actualiza la posición del sprite
sprite->setPosX(x); sprite->setPosX(x);
sprite->setPosY(y); sprite->setPosY(y);
} }
@@ -273,11 +297,6 @@ bool Player::isOnFloor()
updateFeet(); updateFeet();
if (underFeet[0].y % map->getTileSize() != 0)
{
return false;
}
for (auto f : underFeet) for (auto f : underFeet)
{ {
onFloor |= ((map->getTile(f) == wall) || (map->getTile(f) == passable)); onFloor |= ((map->getTile(f) == wall) || (map->getTile(f) == passable));

View File

@@ -41,6 +41,7 @@ public:
int w; // Ancho del jugador int w; // Ancho del jugador
int h; // ALto del jugador int h; // ALto del jugador
t_player_state state; // Estado actual del jugador t_player_state state; // Estado actual del jugador
SDL_Point lastPosition; // Posición anterior
// Variables que afectan a la inercia del movimiento // Variables que afectan a la inercia del movimiento
float jumpStrenght; // Cantidad de pixels a desplazarse y velocidad que pilla al saltar float jumpStrenght; // Cantidad de pixels a desplazarse y velocidad que pilla al saltar