Terminados los tiles atravesables
This commit is contained in:
@@ -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,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,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,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,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,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,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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,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,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,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,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,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,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,
|
||||
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,
|
||||
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,
|
||||
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,
|
||||
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
|
||||
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,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,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,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,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,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,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,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,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,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,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,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,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,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,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,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,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>
|
||||
</layer>
|
||||
</map>
|
||||
|
||||
@@ -118,6 +118,18 @@ void Game::renderDebugInfo()
|
||||
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;
|
||||
std::string text = "";
|
||||
|
||||
@@ -136,7 +148,10 @@ void Game::renderDebugInfo()
|
||||
text = "isOnFloor " + std::to_string(player->isOnFloor());
|
||||
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);
|
||||
|
||||
text = "state " + std::to_string(player->state);
|
||||
|
||||
@@ -226,13 +226,13 @@ void Map::render()
|
||||
t_tile_map Map::getTile(SDL_Point p)
|
||||
{
|
||||
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;
|
||||
}
|
||||
else if (tile >= (4 * 2 * png_width) && tile < 8 * 2 * png_width)
|
||||
else if (tile >= (8 * png_width) && tile < 16 * png_width)
|
||||
{
|
||||
return wall;
|
||||
}
|
||||
@@ -247,3 +247,9 @@ int Map::getTileSize()
|
||||
{
|
||||
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)];
|
||||
}
|
||||
@@ -68,6 +68,9 @@ public:
|
||||
|
||||
// Devuelve el valor de la variable
|
||||
int getTileSize();
|
||||
|
||||
// Devuelve el indice del tile correspondiente a un punto del mapa
|
||||
int getTileIndex(SDL_Point p);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -23,6 +23,7 @@ Player::Player(SDL_Renderer *renderer, Asset *asset, Input *input, Map *map)
|
||||
y = 168;
|
||||
vx = 0;
|
||||
vy = 0;
|
||||
lastPosition = {(int)x, (int)y};
|
||||
const SDL_Rect rect = {(int)x, (int)y, 16, 24};
|
||||
sprite->setPos(rect);
|
||||
sprite->setCurrentAnimation("stand");
|
||||
@@ -64,7 +65,7 @@ Player::~Player()
|
||||
void Player::update()
|
||||
{
|
||||
checkInput();
|
||||
addGravity();
|
||||
// addGravity();
|
||||
move();
|
||||
animate();
|
||||
}
|
||||
@@ -136,13 +137,10 @@ void Player::checkInput()
|
||||
// Aplica la gravedad
|
||||
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);
|
||||
if (state == standing)
|
||||
{
|
||||
state = falling;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -198,48 +196,74 @@ bool Player::checkMapCollisions()
|
||||
// Mueve al jugador en función de la velocidad/desplazamiento
|
||||
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;
|
||||
if (checkMapCollisions())
|
||||
{
|
||||
// Recoloca
|
||||
if (vx > 0)
|
||||
{
|
||||
x -= ((int)x + w) % tile;
|
||||
x -= ((int)x + w) % tileSize;
|
||||
}
|
||||
else
|
||||
{
|
||||
x += tile - ((int)x % tile);
|
||||
x += tileSize - ((int)x % tileSize);
|
||||
}
|
||||
|
||||
vx = 0.0f;
|
||||
}
|
||||
|
||||
const bool wasOnFloor = isOnFloor();
|
||||
// Mueve en el eje Y y comprueba colisiones con muros
|
||||
y += vy;
|
||||
if (checkMapCollisions())
|
||||
{
|
||||
// Recoloca
|
||||
if (vy > 0.0f)
|
||||
{
|
||||
y -= ((int)y + h) % tile;
|
||||
y -= ((int)y + h) % tileSize;
|
||||
state = standing;
|
||||
}
|
||||
else
|
||||
{
|
||||
y += tile - ((int)y % tile);
|
||||
y += tileSize - ((int)y % tileSize);
|
||||
state = falling;
|
||||
}
|
||||
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;
|
||||
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->setPosY(y);
|
||||
}
|
||||
@@ -273,11 +297,6 @@ bool Player::isOnFloor()
|
||||
|
||||
updateFeet();
|
||||
|
||||
if (underFeet[0].y % map->getTileSize() != 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
for (auto f : underFeet)
|
||||
{
|
||||
onFloor |= ((map->getTile(f) == wall) || (map->getTile(f) == passable));
|
||||
|
||||
@@ -41,6 +41,7 @@ public:
|
||||
int w; // Ancho del jugador
|
||||
int h; // ALto del jugador
|
||||
t_player_state state; // Estado actual del jugador
|
||||
SDL_Point lastPosition; // Posición anterior
|
||||
|
||||
// Variables que afectan a la inercia del movimiento
|
||||
float jumpStrenght; // Cantidad de pixels a desplazarse y velocidad que pilla al saltar
|
||||
|
||||
Reference in New Issue
Block a user