Implementado el cambio de habitación

This commit is contained in:
2022-07-03 14:53:18 +02:00
parent 7d0262a30e
commit 0bb7b6441a
6 changed files with 145 additions and 45 deletions

View File

@@ -1,11 +1,11 @@
id=1 id=1
name=Test Room name=Test Room
bg_color=cyan bg_color=black
tileset=room1.png tileset=room1.png
room_up=0 room_up=0
room_down=0 room_down=0
room_left=0 room_left=0
room_right=2 room_right=02.room
tilemap=41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,41,41,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,41,41,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,41,41,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,41,41,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,81,0,0,0,0,0,0,0,0,0,0,0,0,0,41,41,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,41,41,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,41,41,0,0,0,0,0,0,0,0,0,0,0,0,41,41,0,0,0,0,0,0,0,0,0,0,0,0,0,41,41,41,41,41,41,41,41,0,0,0,0,0,0,0,0,0,41,41,0,0,0,0,0,0,0,0,0,0,0,41,41,41,41,41,41,41,41,41,41,0,0,0,0,0,0,0,0,0,41,41,0,0,0,0,0,41,0,0,0,0,0,0,0,0,0,0,0,0,0,0,41,0,0,0,0,0,0,0,0,0,41,41,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,41,0,0,0,0,0,0,0,0,0,41,0,0,0,0,0,81,81,81,81,81,81,81,81,81,81,81,81,81,81,81,81,81,81,81,81,81,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41 tilemap=41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,41,41,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,41,41,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,41,41,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,41,41,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,81,0,0,0,0,0,0,0,0,0,0,0,0,0,41,41,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,41,41,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,41,41,0,0,0,0,0,0,0,0,0,0,0,0,41,41,0,0,0,0,0,0,0,0,0,0,0,0,0,41,41,41,41,41,41,41,41,0,0,0,0,0,0,0,0,0,41,41,0,0,0,0,0,0,0,0,0,0,0,41,41,41,41,41,41,41,41,41,41,0,0,0,0,0,0,0,0,0,41,41,0,0,0,0,0,41,0,0,0,0,0,0,0,0,0,0,0,0,0,0,41,0,0,0,0,0,0,0,0,0,41,41,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,41,0,0,0,0,0,0,0,0,0,41,0,0,0,0,0,81,81,81,81,81,81,81,81,81,81,81,81,81,81,81,81,81,81,81,81,81,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41
enemy=1,0,0,0,1 enemy=1,0,0,0,1
enemy=2,10,0,0,1 enemy=2,10,0,0,1

View File

@@ -1,12 +1,12 @@
id=2 id=2
name=Test Room 2 name=Test Room 2
bg_color=blue bg_color=black
tileset=1 tileset=room1.png
room_up=0 room_up=0
room_down= room_down=0
room_left=1 room_left=01.room
room_right=0 room_right=0
tilemap=67,67,89,92,93 tilemap=43,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,43,43,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,43,43,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,43,43,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,43,43,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,43,43,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,43,43,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,43,43,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,43,43,43,43,43,0,0,0,0,0,0,0,0,0,43,43,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,43,43,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,43,43,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,43,43,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,43,0,0,0,0,0,0,0,83,83,83,83,83,83,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,83,0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,63,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63
enemy=1,0,0,0,1 enemy=1,0,0,0,1
enemy=2,10,0,0,1 enemy=2,10,0,0,1
item=1,10,10 item=1,10,10

View File

@@ -77,6 +77,17 @@ section_t Game::run()
{ {
// Sección juego jugando // Sección juego jugando
if (mSection.subsection == SECTION_GAME_PLAY) if (mSection.subsection == SECTION_GAME_PLAY)
{
update();
draw();
}
}
return mSection;
}
// Actualiza el juego, las variables, comprueba la entrada, etc.
void Game::update()
{ {
// Comprueba que la diferencia de ticks sea mayor a la velocidad del juego // Comprueba que la diferencia de ticks sea mayor a la velocidad del juego
if (SDL_GetTicks() - mTicks > mTicksSpeed) if (SDL_GetTicks() - mTicks > mTicksSpeed)
@@ -94,8 +105,14 @@ section_t Game::run()
break; break;
} }
} }
checkInput();
}
} }
// Pinta los objetos en pantalla
void Game::draw()
{
// Prepara para dibujar el frame // Prepara para dibujar el frame
const color_t color = {0xAA, 0x55, 0x55}; const color_t color = {0xAA, 0x55, 0x55};
mScreen->start(); mScreen->start();
@@ -114,7 +131,42 @@ section_t Game::run()
// Actualiza la pantalla // Actualiza la pantalla
mScreen->blit(); mScreen->blit();
} }
// Comprueba la entrada
void Game::checkInput()
{
if (mInput->checkInput(INPUT_UP, REPEAT_FALSE))
changeRoom(mRoom->getRoomUp());
if (mInput->checkInput(INPUT_DOWN, REPEAT_FALSE))
changeRoom(mRoom->getRoomDown());
if (mInput->checkInput(INPUT_LEFT, REPEAT_FALSE))
changeRoom(mRoom->getRoomLeft());
if (mInput->checkInput(INPUT_RIGHT, REPEAT_FALSE))
changeRoom(mRoom->getRoomRight());
} }
return mSection; // Cambia de habitación
bool Game::changeRoom(std::string file)
{
bool success = false;
// En las habitaciones los limites tienen la cadena del fichero o un 0 en caso de no limitar con nada
if (file != "0")
// Verifica que exista el fichero que se va a cargar
if (mAsset->get(file) != "")
{
// Elimina la habitación actual
delete mRoom;
mRoom = nullptr;
// Crea un objeto habitación nuevo a partir del fichero
mRoom = new Room(mAsset->get(file), mRenderer, mAsset);
success = true;
}
return success;
} }

View File

@@ -45,6 +45,18 @@ private:
// Carga los recursos // Carga los recursos
bool loadMedia(); bool loadMedia();
// Actualiza el juego, las variables, comprueba la entrada, etc.
void update();
// Pinta los objetos en pantalla
void draw();
// Comprueba la entrada
void checkInput();
// Cambia de habitación
bool changeRoom(std::string file);
public: public:
// Constructor // Constructor
Game(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, Lang *lang, Input *input); Game(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, Lang *lang, Input *input);

View File

@@ -179,14 +179,14 @@ void Room::fillMapTexture()
// Los tilesets son de 20x20 tiles. El primer tile es el 1. Cuentan hacia la derecha y hacia abajo // Los tilesets son de 20x20 tiles. El primer tile es el 1. Cuentan hacia la derecha y hacia abajo
printf("Filling map\n"); // printf("Filling map\n");
SDL_Rect clip = {0, 0, 8, 8}; SDL_Rect clip = {0, 0, 8, 8};
for (int y = 0; y < 16; y++) for (int y = 0; y < 16; y++)
for (int x = 0; x < 32; x++) for (int x = 0; x < 32; x++)
{ {
clip.x = ((tilemap[(y * 32) + x] - 1) % 20) * 8; clip.x = ((tilemap[(y * 32) + x] - 1) % 20) * 8;
clip.y = ((tilemap[(y * 32) + x] - 1) / 20) * 8; clip.y = ((tilemap[(y * 32) + x] - 1) / 20) * 8;
printf("tilemap [%i] = %i | x = %i | y = %i\n", ((y * 32) + x), tilemap[(y * 32) + x], clip.x, clip.y); // printf("tilemap [%i] = %i | x = %i | y = %i\n", ((y * 32) + x), tilemap[(y * 32) + x], clip.x, clip.y);
texture->render(renderer, x * 8, y * 8, &clip); texture->render(renderer, x * 8, y * 8, &clip);
} }
@@ -201,3 +201,27 @@ void Room::drawMap()
// Dibuja la textura con el mapa en pantalla // Dibuja la textura con el mapa en pantalla
SDL_RenderCopy(renderer, map_texture, &rect, NULL); SDL_RenderCopy(renderer, map_texture, &rect, NULL);
} }
// Devuelve el valor de la variable
std::string Room::getRoomUp()
{
return room_up;
}
// Devuelve el valor de la variable
std::string Room::getRoomDown()
{
return room_down;
}
// Devuelve el valor de la variable
std::string Room::getRoomLeft()
{
return room_left;
}
// Devuelve el valor de la variable
std::string Room::getRoomRight()
{
return room_right;
}

View File

@@ -67,6 +67,18 @@ public:
// Dibuja el mapa en pantalla // Dibuja el mapa en pantalla
void drawMap(); void drawMap();
// Devuelve el valor de la variable
std::string getRoomUp();
// Devuelve el valor de la variable
std::string getRoomDown();
// Devuelve el valor de la variable
std::string getRoomLeft();
// Devuelve el valor de la variable
std::string getRoomRight();
}; };
#endif #endif