From 0624cff8456079a98f96fc00c072aadff3d9a5e9 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Fri, 19 Aug 2022 12:33:42 +0200 Subject: [PATCH] =?UTF-8?q?Primera=20implementacion=20de=20tiles=20atraves?= =?UTF-8?q?ables.=20No=20funcionar=C3=A1=20si=20estan=20apilados?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/game.cpp | 22 +++++++++++++++++---- source/map.cpp | 50 ++++++++++++++++++++--------------------------- source/map.h | 8 ++++++-- source/player.cpp | 40 +++++++++++++++++++++---------------- source/player.h | 2 ++ source/prog.cpp | 1 + 6 files changed, 71 insertions(+), 52 deletions(-) diff --git a/source/game.cpp b/source/game.cpp index 05e8b21..272d673 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -99,7 +99,15 @@ 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 @@ -117,11 +125,17 @@ void Game::renderDebugInfo() debugText->write(0, line, text, -1); 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); - debugText->write(0, line+=6, text, -1); - + debugText->write(0, line += 6, text, -1); + 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); } \ No newline at end of file diff --git a/source/map.cpp b/source/map.cpp index 39146da..714e638 100644 --- a/source/map.cpp +++ b/source/map.cpp @@ -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,19 +225,25 @@ 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 png_width = 16*2; + 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) + if (tile >= 0 && tile < 4 * 2 * png_width) { 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; } else { - return travessable; + return passable; } +} + +// Devuelve el valor de la variable +int Map::getTileWidth() +{ + return tile_size; } \ No newline at end of file diff --git a/source/map.h b/source/map.h index 0ab2064..e78cbef 100644 --- a/source/map.h +++ b/source/map.h @@ -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 diff --git a/source/player.cpp b/source/player.cpp index b5fe7db..e7a0f06 100644 --- a/source/player.cpp +++ b/source/player.cpp @@ -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()); - jumping = false; - vy = 0.0f; + 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; } \ No newline at end of file diff --git a/source/player.h b/source/player.h index 6be18be..b766e95 100644 --- a/source/player.h +++ b/source/player.h @@ -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 diff --git a/source/prog.cpp b/source/prog.cpp index f57859c..87c6426 100644 --- a/source/prog.cpp +++ b/source/prog.cpp @@ -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);