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,9 +99,17 @@ void Game::render()
void Game::checkInput()
{
if (input->checkInput(INPUT_BUTTON_2, REPEAT_FALSE))
{
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
void Game::renderDebugInfo()
{
@@ -124,4 +132,10 @@ void Game::renderDebugInfo()
text = "jump_pressed " + std::to_string(player->jumpPressed);
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
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
this->asset = asset;
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
fillMapTexture();
// Inicializa variables
tile_width = 16/2;
map_width = 20*2;
map_height = 13*2;
}
// Destructor
@@ -160,16 +161,6 @@ bool Map::setVars(std::string var, std::string 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 == "")
{
}
@@ -194,18 +185,13 @@ void Map::fillMapTexture()
texture_bg->render(renderer, 0, 0, &clip);
// 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};
for (int y = 0; y < map_height_in_tiles; y++)
for (int x = 0; x < map_width_in_tiles; x++)
for (int y = 0; y < map_height; y++)
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.y = ((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) + x] - 1) / tileset_width) * tile_size;
texture_tile->render(renderer, x * tile_size, y * tile_size, &clip);
}
@@ -239,7 +225,7 @@ void Map::render()
// Devuelve el tipo de tile que hay en un punto
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;
if (tile >= 0 && tile < 4 * 2 * png_width)
@@ -252,6 +238,12 @@ t_tile_map Map::getTile(SDL_Point p)
}
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,
wall,
travessable
passable
};
// The player
@@ -36,9 +36,10 @@ private:
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
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_height; // Alto del mapa en tiles
int tileset_width; // Ancho del tileset en tiles
// Carga las variables desde un fichero
bool load(std::string file);
@@ -64,6 +65,9 @@ public:
// Devuelve el tipo de tile que hay en un punto
t_tile_map getTile(SDL_Point p);
// Devuelve el valor de la variable
int getTileWidth();
};
#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"));
w = 16;
h = 24;
x = 3 * 16;
y = 168;
vx = 0;
@@ -164,9 +166,9 @@ void Player::updateFeet()
{
const SDL_Point p = {(int)x, (int)y};
underFeet[0] = {p.x, p.y + 24};
underFeet[1] = {p.x + 7, p.y + 24};
underFeet[2] = {p.x + 15, p.y + 24};
underFeet[0] = {p.x, p.y + h};
underFeet[1] = {p.x + 7, p.y + h};
underFeet[2] = {p.x + 15, p.y + h};
}
// Compruena las colisiones con el mapa
@@ -187,9 +189,12 @@ bool Player::checkMapCollisions()
// Mueve al jugador en función de la velocidad/desplazamiento
void Player::move()
{
const int tile = map->getTileWidth();
x += vx;
if (checkMapCollisions())
{
// Recoloca
if (vx > 0)
{
do
@@ -208,27 +213,28 @@ void Player::move()
vx = 0.0f;
}
const bool wasOnFloor = isOnFloor();
y += vy;
if (checkMapCollisions())
{
if (vy > 0)
// Recoloca
if (vy > 0.0f)
{
do
{
y--;
} while (checkMapCollisions());
jumping = false;
vy = 0.0f;
y -= ((int)y + h) % tile;
}
else
{
do
{
y++;
} while (checkMapCollisions());
y += tile - ((int)y % tile);
}
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);
@@ -266,7 +272,7 @@ bool Player::isOnFloor()
for (auto f : underFeet)
{
onFloor |= (map->getTile(f) == wall);
onFloor |= ((map->getTile(f) == wall) || (map->getTile(f) == passable));
}
return onFloor;
}

View File

@@ -33,6 +33,8 @@ public:
int coins; // Cantidad de monedas
int cooldown; // Tiempo de inhabilitación
int lives; // Cantidad de vidas
int w; // Ancho del jugador
int h; // ALto del jugador
// Variables que afectan a la inercia del movimiento
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_BUTTON_1, SDL_SCANCODE_SPACE);
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_ESCAPE, SDL_SCANCODE_ESCAPE);