169 lines
3.4 KiB
C++
169 lines
3.4 KiB
C++
#include "game.h"
|
|
|
|
// Constructor
|
|
Game::Game(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, Lang *lang, Input *input)
|
|
{
|
|
// Copia los punteros
|
|
mRenderer = renderer;
|
|
mAsset = asset;
|
|
mLang = lang;
|
|
mInput = input;
|
|
|
|
mScreen = new Screen(window, renderer);
|
|
mRoom = new Room(mAsset->get("01.room"), mRenderer, mAsset);
|
|
mEventHandler = new SDL_Event();
|
|
mTextureText = new LTexture();
|
|
mText = new Text(mAsset->get("smb2.txt"), mTextureText, renderer);
|
|
mFade = new Fade(renderer);
|
|
}
|
|
|
|
Game::~Game()
|
|
{
|
|
mRenderer = nullptr;
|
|
mAsset = nullptr;
|
|
mLang = nullptr;
|
|
mInput = nullptr;
|
|
|
|
delete mEventHandler;
|
|
mEventHandler = nullptr;
|
|
|
|
mTextureText->unload();
|
|
delete mTextureText;
|
|
mTextureText = nullptr;
|
|
|
|
delete mScreen;
|
|
mScreen = nullptr;
|
|
|
|
delete mRoom;
|
|
mRoom = nullptr;
|
|
|
|
delete mText;
|
|
mText = nullptr;
|
|
|
|
delete mFade;
|
|
mFade = nullptr;
|
|
}
|
|
|
|
// Inicializa las variables necesarias para la sección 'Game'
|
|
void Game::init()
|
|
{
|
|
// Carga los recursos
|
|
loadMedia();
|
|
|
|
mTicks = 0;
|
|
mTicksSpeed = 15;
|
|
|
|
mSection.name = SECTION_PROG_GAME;
|
|
mSection.subsection = SECTION_GAME_PLAY;
|
|
}
|
|
|
|
// Carga los recursos necesarios para la sección 'Game'
|
|
bool Game::loadMedia()
|
|
{
|
|
bool success = true;
|
|
|
|
// Texturas
|
|
success &= loadTextureFromFile(mTextureText, mAsset->get("smb2.png"), mRenderer);
|
|
|
|
return success;
|
|
}
|
|
|
|
// Bucle para el juego
|
|
section_t Game::run()
|
|
{
|
|
init();
|
|
|
|
while (mSection.name == SECTION_PROG_GAME)
|
|
{
|
|
// Sección juego jugando
|
|
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
|
|
if (SDL_GetTicks() - mTicks > mTicksSpeed)
|
|
{
|
|
// Actualiza el contador de ticks
|
|
mTicks = SDL_GetTicks();
|
|
|
|
// Comprueba los eventos que hay en la cola
|
|
while (SDL_PollEvent(mEventHandler) != 0)
|
|
{
|
|
// Evento de salida de la aplicación
|
|
if (mEventHandler->type == SDL_QUIT)
|
|
{
|
|
mSection.name = SECTION_PROG_QUIT;
|
|
break;
|
|
}
|
|
}
|
|
|
|
checkInput();
|
|
}
|
|
}
|
|
|
|
// Pinta los objetos en pantalla
|
|
void Game::draw()
|
|
{
|
|
// Prepara para dibujar el frame
|
|
const color_t color = {0xAA, 0x55, 0x55};
|
|
mScreen->start();
|
|
mScreen->clean(color);
|
|
mScreen->clean(mRoom->getBGColor());
|
|
|
|
mRoom->drawMap();
|
|
mRoom->drawEnemies();
|
|
|
|
// Texto en el centro de la pantalla
|
|
mText->writeCentered(GAMECANVAS_CENTER_X, 18 * 8, mRoom->getName());
|
|
|
|
// Actualiza la pantalla
|
|
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());
|
|
}
|
|
|
|
// 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;
|
|
} |