diff --git a/source/gamestate_menu.cpp b/source/gamestate_menu.cpp index fa216c7..2677c58 100644 --- a/source/gamestate_menu.cpp +++ b/source/gamestate_menu.cpp @@ -20,9 +20,9 @@ namespace gamestate void init() { - exit = 0; - fondo = draw::loadSurface("menuprin.gif", true); - cursor = draw::loadSurface("cursor.gif"); + menu::exit = menu::eixir::no; + menu::fondo = draw::loadSurface("menuprin.gif", true); + menu::cursor = draw::loadSurface("cursor.gif"); if (audio::getMusicState() != audio::music_state::MUSIC_PLAYING || audio::whichMusic() != "mus3.ogg") { audio::playMusic(audio::loadMusic("mus3.ogg")); @@ -40,7 +40,7 @@ namespace gamestate draw::freeSurface(fondo); draw::freeSurface(cursor); if (exit==menu::eixir::sequence) gamestate::sequence::init(); - //if (exit==menu::eixir::password) gamestate::password::init(); + if (exit==menu::eixir::password) gamestate::password::init(); } draw::render(); return true; diff --git a/source/gamestate_password.cpp b/source/gamestate_password.cpp new file mode 100644 index 0000000..a018671 --- /dev/null +++ b/source/gamestate_password.cpp @@ -0,0 +1,146 @@ +#include "gamestates.h" +#include "jgame.h" +#include +#include "aux_font.h" +#include "proc_mapa.h" +#include + +namespace gamestate +{ + namespace password + { + namespace eixir + { + const int no = 0; + const int sequence = 1; + const int menu = 2; + } + + // Variables del gamestate + draw::surface *fondo = nullptr; + draw::surface *cursor = nullptr; + + int exit = password::eixir::no; + char password[11] = " "; + uint8_t indice = 0; + + // Mètodes del gamestate + bool loop(); + const int getFaseFromPassword(); + + void init() + { + exit = password::eixir::no; + + // Carrega el gif del fondo + fondo = draw::loadSurface("prefase.gif", true); + cursor = draw::loadSurface("cursor.gif"); + + // Pinta el text en el fondo + draw::setDestination(fondo); + font::selectFont(font::type::normal); + font::print(95, 80, "ESCRIU EL PASSWORD"); + + draw::setDestination(nullptr); + + // Comencem el bucle + draw::fadein(); + game::setState(gamestate::password::loop); + } + + bool loop() + { + draw::draw(fondo); + font::print(123, 140, password); + + draw::setSource(cursor); + draw::draw(input::mouseX(), input::mouseY()); + + draw::render(); + + if (draw::isfading()) return true; + + if (exit) + { + if (!draw::isfading()) { + draw::freeSurface(fondo); + draw::freeSurface(cursor); + if (exit==password::eixir::sequence) gamestate::sequence::init(); + if (exit==password::eixir::menu) gamestate::menu::init(); + } + draw::render(); + return true; + } + + if (input::anyKeyPressed()) + { + if (input::keyPressed(SDL_SCANCODE_ESCAPE)) + { + draw::fadeout(); + game::setConfig("fase", 0); + exit = password::eixir::menu; + return true; + } + else if (input::keyPressed(SDL_SCANCODE_BACKSPACE)) + { + if (indice>0) password[--indice] = 32; + } + else + { + const uint8_t tecla = input::getKeyPressed(); + if (tecla == SDL_SCANCODE_0) { + password[indice++] = '0'; + } + else if (tecla >= SDL_SCANCODE_1 && tecla <= SDL_SCANCODE_9) + { + password[indice++] = tecla + 18; + } + else if (tecla >= SDL_SCANCODE_A && tecla <= SDL_SCANCODE_Z) + { + password[indice++] = tecla + 61; + } + + if (indice == 10) + { + draw::fadeout(); + game::setConfig("fase", password::getFaseFromPassword()); + exit = password::eixir::sequence; + return true; + } + } + + } + return true; + } + + const int getFaseFromPassword() + { + int filesize = 0; + const char *buffer = file::getFileBuffer("offsets.bal", &filesize); + + int punter = 0; + + bool salir = false; + char passFile[11] = " "; + int numPassword = 0; + + while ( numPassword < 30 && !salir ) { + for (int i=0;i<10;i++) { + punter++; + passFile[i] = buffer[punter] - (101+i); + } + punter++; + + salir = true; + for (int i=0;i<10;i++) { + if (passFile[i] != password[i]) salir = false; + } + numPassword++; + } + + if (!salir) numPassword = 0; + + return numPassword; + } + } +} diff --git a/source/gamestates.h b/source/gamestates.h index 0bc56be..b174fdc 100644 --- a/source/gamestates.h +++ b/source/gamestates.h @@ -8,4 +8,5 @@ namespace gamestate namespace play { void init(); } namespace postfase { void init(); } namespace mort { void init(); } + namespace password { void init(); } }