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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,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>
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|||||||
@@ -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;
|
||||||
}
|
}
|
||||||
@@ -246,4 +246,10 @@ t_tile_map Map::getTile(SDL_Point p)
|
|||||||
int Map::getTileSize()
|
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)];
|
||||||
}
|
}
|
||||||
@@ -36,9 +36,9 @@ private:
|
|||||||
LTexture *texture_bg; // Textura con los graficos de fondo de la habitación
|
LTexture *texture_bg; // Textura con los graficos de fondo de la habitación
|
||||||
SDL_Texture *map_texture; // Textura para dibujar el mapa de la habitación
|
SDL_Texture *map_texture; // Textura para dibujar el mapa de la habitación
|
||||||
|
|
||||||
int tile_size; // Ancho del tile en pixels
|
int tile_size; // Ancho del tile en pixels
|
||||||
int map_width; // Ancho del mapa en tiles
|
int map_width; // Ancho del mapa en tiles
|
||||||
int map_height; // Alto del mapa en tiles
|
int map_height; // Alto del mapa en tiles
|
||||||
int tileset_width; // Ancho del tileset en tiles
|
int tileset_width; // Ancho del tileset en tiles
|
||||||
|
|
||||||
// Carga las variables desde un fichero
|
// Carga las variables desde un fichero
|
||||||
@@ -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
|
||||||
|
|||||||
@@ -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
|
||||||
{
|
{
|
||||||
state = standing;
|
const int a = (lastPosition.y + h) / tileSize;
|
||||||
vy = 0.0f;
|
const int b = ((int)y + h) / tileSize;
|
||||||
y -= ((int)y + h) % tile;
|
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) % 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));
|
||||||
|
|||||||
@@ -28,19 +28,20 @@ public:
|
|||||||
LTexture *texture; // Textura con los graficos del jugador
|
LTexture *texture; // Textura con los graficos del jugador
|
||||||
Map *map; // Objeto con el mapa
|
Map *map; // Objeto con el mapa
|
||||||
|
|
||||||
float x; // Posición del jugador en el eje X
|
float x; // Posición del jugador en el eje X
|
||||||
float y; // Posición del jugador en el eje Y
|
float y; // Posición del jugador en el eje Y
|
||||||
float vx; // Velocidad/desplazamiento del jugador en el eje X
|
float vx; // Velocidad/desplazamiento del jugador en el eje X
|
||||||
float vy; // Velocidad/desplazamiento del jugador en el eje Y
|
float vy; // Velocidad/desplazamiento del jugador en el eje Y
|
||||||
bool jumpPressed; // Indica si esta pulsada la tecla de salto
|
bool jumpPressed; // Indica si esta pulsada la tecla de salto
|
||||||
bool enabled; // Si está habilitado
|
bool enabled; // Si está habilitado
|
||||||
bool invulnerable; // Indica si se encuentra en estado invulnerable
|
bool invulnerable; // Indica si se encuentra en estado invulnerable
|
||||||
int coins; // Cantidad de monedas
|
int coins; // Cantidad de monedas
|
||||||
int cooldown; // Tiempo de inhabilitación
|
int cooldown; // Tiempo de inhabilitación
|
||||||
int lives; // Cantidad de vidas
|
int lives; // Cantidad de vidas
|
||||||
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
|
||||||
|
|||||||
Reference in New Issue
Block a user