Primera implementacion de tiles atravesables. No funcionará si estan apilados

This commit is contained in:
2022-08-19 12:33:42 +02:00
parent 76696f9eb7
commit 0624cff845
6 changed files with 71 additions and 52 deletions

View File

@@ -99,7 +99,15 @@ void Game::render()
void Game::checkInput() void Game::checkInput()
{ {
if (input->checkInput(INPUT_BUTTON_2, REPEAT_FALSE)) if (input->checkInput(INPUT_BUTTON_2, REPEAT_FALSE))
{
debug = !debug; debug = !debug;
}
if (input->checkInput(INPUT_BUTTON_3, REPEAT_FALSE))
{
delete player;
player = new Player(renderer, asset, input, map);
}
} }
// Muestra información de depuración // Muestra información de depuración
@@ -117,11 +125,17 @@ void Game::renderDebugInfo()
debugText->write(0, line, text, -1); debugText->write(0, line, text, -1);
text = "VY " + std::to_string(player->vy) + " " + std::to_string(player->jumpStrenght); text = "VY " + std::to_string(player->vy) + " " + std::to_string(player->jumpStrenght);
debugText->write(0, line+=6, text, -1); debugText->write(0, line += 6, text, -1);
text = "VX " + std::to_string(player->vx); text = "VX " + std::to_string(player->vx);
debugText->write(0, line+=6, text, -1); debugText->write(0, line += 6, text, -1);
text = "jump_pressed " + std::to_string(player->jumpPressed); text = "jump_pressed " + std::to_string(player->jumpPressed);
debugText->write(0, line+=6, text, -1); debugText->write(0, line += 6, text, -1);
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]));
debugText->write(0, line += 6, text, -1);
} }

View File

@@ -3,6 +3,12 @@
// Constructor // Constructor
Map::Map(std::string file, SDL_Renderer *renderer, Asset *asset) Map::Map(std::string file, SDL_Renderer *renderer, Asset *asset)
{ {
// Inicializa variables
tile_size = 8;
map_width = 40;
map_height = 26;
tileset_width = 32;
// Copia los punteros a objetos // Copia los punteros a objetos
this->asset = asset; this->asset = asset;
this->renderer = renderer; this->renderer = renderer;
@@ -21,11 +27,6 @@ Map::Map(std::string file, SDL_Renderer *renderer, Asset *asset)
// Pinta el mapa de la habitación en la textura // Pinta el mapa de la habitación en la textura
fillMapTexture(); fillMapTexture();
// Inicializa variables
tile_width = 16/2;
map_width = 20*2;
map_height = 13*2;
} }
// Destructor // Destructor
@@ -160,16 +161,6 @@ bool Map::setVars(std::string var, std::string value)
{ {
room_right = value; room_right = value;
} }
else if (var == "tilemap")
{
// Se introducen los valores separados por comas en un vector
std::stringstream ss(value);
std::string tmp;
while (getline(ss, tmp, ','))
{
tilemap.push_back(std::stoi(tmp));
}
}
else if (var == "") else if (var == "")
{ {
} }
@@ -194,18 +185,13 @@ void Map::fillMapTexture()
texture_bg->render(renderer, 0, 0, &clip); texture_bg->render(renderer, 0, 0, &clip);
// Dibuja el mapeado de tiles // Dibuja el mapeado de tiles
const int tile_size = 16/2;
const int tileset_width_in_tiles = 16*2;
const int map_width_in_tiles = 20*2;
const int map_height_in_tiles = 13*2;
clip = {0, 0, tile_size, tile_size}; clip = {0, 0, tile_size, tile_size};
for (int y = 0; y < map_height_in_tiles; y++) for (int y = 0; y < map_height; y++)
for (int x = 0; x < map_width_in_tiles; x++) for (int x = 0; x < map_width; x++)
{ {
clip.x = ((tilemap[(y * map_width_in_tiles) + x] - 1) % tileset_width_in_tiles) * tile_size; clip.x = ((tilemap[(y * map_width) + x] - 1) % tileset_width) * tile_size;
clip.y = ((tilemap[(y * map_width_in_tiles) + x] - 1) / tileset_width_in_tiles) * tile_size; clip.y = ((tilemap[(y * map_width) + x] - 1) / tileset_width) * tile_size;
texture_tile->render(renderer, x * tile_size, y * tile_size, &clip); texture_tile->render(renderer, x * tile_size, y * tile_size, &clip);
} }
@@ -239,19 +225,25 @@ void Map::render()
// Devuelve el tipo de tile que hay en un punto // Devuelve el tipo de tile que hay en un punto
t_tile_map Map::getTile(SDL_Point p) t_tile_map Map::getTile(SDL_Point p)
{ {
const int tile = tilemap[((p.y / tile_width) * map_width) + (p.x / tile_width)]; const int tile = tilemap[((p.y / tile_size) * map_width) + (p.x / tile_size)];
const int png_width = 16*2; const int png_width = 16 * 2;
if (tile >= 0 && tile < 4*2 * png_width) if (tile >= 0 && tile < 4 * 2 * png_width)
{ {
return nothing; return nothing;
} }
else if (tile >= (4*2 * png_width) && tile < 8*2 * png_width) else if (tile >= (4 * 2 * png_width) && tile < 8 * 2 * png_width)
{ {
return wall; return wall;
} }
else else
{ {
return travessable; return passable;
} }
} }
// Devuelve el valor de la variable
int Map::getTileWidth()
{
return tile_size;
}

View File

@@ -16,7 +16,7 @@ enum t_tile_map
{ {
nothing, nothing,
wall, wall,
travessable passable
}; };
// The player // The player
@@ -36,9 +36,10 @@ 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_width; // 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
// Carga las variables desde un fichero // Carga las variables desde un fichero
bool load(std::string file); bool load(std::string file);
@@ -64,6 +65,9 @@ public:
// Devuelve el tipo de tile que hay en un punto // Devuelve el tipo de tile que hay en un punto
t_tile_map getTile(SDL_Point p); t_tile_map getTile(SDL_Point p);
// Devuelve el valor de la variable
int getTileWidth();
}; };
#endif #endif

View File

@@ -17,6 +17,8 @@ Player::Player(SDL_Renderer *renderer, Asset *asset, Input *input, Map *map)
sprite = new AnimatedSprite(texture, renderer, asset->get("player.ani")); sprite = new AnimatedSprite(texture, renderer, asset->get("player.ani"));
w = 16;
h = 24;
x = 3 * 16; x = 3 * 16;
y = 168; y = 168;
vx = 0; vx = 0;
@@ -164,9 +166,9 @@ void Player::updateFeet()
{ {
const SDL_Point p = {(int)x, (int)y}; const SDL_Point p = {(int)x, (int)y};
underFeet[0] = {p.x, p.y + 24}; underFeet[0] = {p.x, p.y + h};
underFeet[1] = {p.x + 7, p.y + 24}; underFeet[1] = {p.x + 7, p.y + h};
underFeet[2] = {p.x + 15, p.y + 24}; underFeet[2] = {p.x + 15, p.y + h};
} }
// Compruena las colisiones con el mapa // Compruena las colisiones con el mapa
@@ -187,9 +189,12 @@ 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->getTileWidth();
x += vx; x += vx;
if (checkMapCollisions()) if (checkMapCollisions())
{ {
// Recoloca
if (vx > 0) if (vx > 0)
{ {
do do
@@ -208,27 +213,28 @@ void Player::move()
vx = 0.0f; vx = 0.0f;
} }
const bool wasOnFloor = isOnFloor();
y += vy; y += vy;
if (checkMapCollisions()) if (checkMapCollisions())
{ {
if (vy > 0) // Recoloca
if (vy > 0.0f)
{ {
do y -= ((int)y + h) % tile;
{
y--;
} while (checkMapCollisions());
jumping = false;
vy = 0.0f;
} }
else else
{ {
do y += tile - ((int)y % tile);
{
y++;
} while (checkMapCollisions());
jumping = false;
vy = 0.0f;
} }
jumping = false;
vy = 0.0f;
}
else if ((!wasOnFloor) && (isOnFloor()) && (vy > 0.0f))
{
jumping = false;
vy = 0.0f;
y -= ((int)y + h) % tile;
} }
sprite->setPosX(x); sprite->setPosX(x);
@@ -266,7 +272,7 @@ bool Player::isOnFloor()
for (auto f : underFeet) for (auto f : underFeet)
{ {
onFloor |= (map->getTile(f) == wall); onFloor |= ((map->getTile(f) == wall) || (map->getTile(f) == passable));
} }
return onFloor; return onFloor;
} }

View File

@@ -33,6 +33,8 @@ public:
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 h; // ALto del jugador
// 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

View File

@@ -40,6 +40,7 @@ Prog::Prog(std::string executablePath)
input->bindKey(INPUT_CANCEL, SDL_SCANCODE_ESCAPE); input->bindKey(INPUT_CANCEL, SDL_SCANCODE_ESCAPE);
input->bindKey(INPUT_BUTTON_1, SDL_SCANCODE_SPACE); input->bindKey(INPUT_BUTTON_1, SDL_SCANCODE_SPACE);
input->bindKey(INPUT_BUTTON_2, SDL_SCANCODE_D); input->bindKey(INPUT_BUTTON_2, SDL_SCANCODE_D);
input->bindKey(INPUT_BUTTON_3, SDL_SCANCODE_R);
input->bindKey(INPUT_BUTTON_PAUSE, SDL_SCANCODE_ESCAPE); input->bindKey(INPUT_BUTTON_PAUSE, SDL_SCANCODE_ESCAPE);
input->bindKey(INPUT_BUTTON_ESCAPE, SDL_SCANCODE_ESCAPE); input->bindKey(INPUT_BUTTON_ESCAPE, SDL_SCANCODE_ESCAPE);