- [NEW] Mogut tot el codi de lloc

This commit is contained in:
2025-07-02 13:36:48 +02:00
parent 4670b52378
commit 81c5011bc7
34 changed files with 52 additions and 52 deletions

View File

@@ -0,0 +1,12 @@
#pragma once
namespace gamestate
{
namespace sequence { void init(); }
namespace menu { void init(); }
namespace prefase { void init(); }
namespace play { void init(); }
namespace postfase { void init(); }
namespace mort { void init(); }
namespace password { void init(); }
}

View File

@@ -0,0 +1,85 @@
#include "gamestates.h"
#include "../japi/game.h"
#include "../aux/font.h"
namespace gamestate
{
namespace menu
{
namespace eixir
{
const int no = 0;
const int sequence = 1;
const int password = 2;
}
draw::surface *fondo = nullptr;
draw::surface *cursor = nullptr;
int exit = 0;
bool loop();
void init()
{
menu::exit = menu::eixir::no;
menu::fondo = draw::loadSurface("menuprin.gif", true);
menu::cursor = draw::loadSurface("cursor.gif");
audio::loadAndPlayMusic("mus3.ogg");
font::selectFont(font::type::colored);
font::setColor(font::color::white);
draw::fadein();
game::setState(&gamestate::menu::loop);
}
bool loop()
{
if (exit) {
if (!draw::isfading()) {
draw::freeSurface(fondo);
draw::freeSurface(cursor);
if (exit==menu::eixir::sequence) gamestate::sequence::init();
if (exit==menu::eixir::password) gamestate::password::init();
}
draw::render();
return true;
}
const int x = input::mouseX();
const int y = input::mouseY();
draw::draw(fondo);
#ifdef VERSION
font::print(277,192, VERSION);
#endif
draw::setSource(cursor);
draw::draw(x, y);
draw::render();
if (input::mouseClk(1))
{
if (x >= 200 && y >= 100 && x <= 270 && y <= 120) {
draw::fadeout();
game::setConfig("fase", 0);
exit = menu::eixir::sequence;
return true;
} else if (x >= 175 && y >= 125 && x <= 290 && y <= 145) {
draw::fadeout();
game::setConfig("fase", 0);
exit = menu::eixir::password;
return true;
} else if (x >= 200 && y >= 150 && x <= 265 && y <= 170) {
return false;
}
}
return true;
}
}
}

View File

@@ -0,0 +1,75 @@
#include "gamestates.h"
#include "../japi/game.h"
namespace gamestate
{
namespace mort
{
namespace eixir
{
const int no = 0;
const int prefase = 1;
const int menu = 2;
}
draw::surface *fondo = nullptr;
draw::surface *cursor = nullptr;
int exit = 0;
bool loop();
void init()
{
exit = mort::eixir::no;
fondo = draw::loadSurface("mort.gif", true);
cursor = draw::loadSurface("cursor.gif");
audio::loadAndPlayMusic("mus5.ogg");
draw::fadein();
game::setState(&gamestate::mort::loop);
}
bool loop()
{
if (exit) {
if (!draw::isfading()) {
draw::freeSurface(fondo);
draw::freeSurface(cursor);
if (exit==mort::eixir::prefase) gamestate::prefase::init();
if (exit==mort::eixir::menu) gamestate::menu::init();
}
draw::render();
return true;
}
const int x = input::mouseX();
const int y = input::mouseY();
draw::draw(fondo);
draw::setSource(cursor);
draw::draw(x, y);
draw::render();
if (input::mouseClk(1))
{
if (x >= 100 && y >= 50 && x <= 210 && y <= 70) {
draw::fadeout();
exit = mort::eixir::prefase;
return true;
} else if (x >= 120 && y >= 72 && x <= 190 && y <= 95) {
draw::fadeout();
exit = mort::eixir::menu;
return true;
} else if (x >= 120 && y >= 95 && x <= 190 && y <= 115) {
return false;
}
}
return true;
}
}
}

View File

@@ -0,0 +1,148 @@
#include "gamestates.h"
#include "../japi/game.h"
#include <string>
#include "../aux/font.h"
#include "../entities/mapa.h"
#include <SDL3/SDL.h>
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;
for (int i=0; i<10; ++i) password[i] = 32;
password[10] = 0;
indice = 0;
// 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()
{
password[10] = 0;
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++) {
passFile[i] = buffer[punter++] - (101+i);
}
salir = true;
for (int i=0;i<10;i++) {
if (passFile[i] != password[i]) salir = false;
}
numPassword++;
}
if (!salir) numPassword = 0;
return numPassword;
}
}
}

359
source/gamestates/play.cpp Normal file
View File

@@ -0,0 +1,359 @@
#include "gamestates.h"
#include "../japi/game.h"
#include <SDL3/SDL.h>
#include <string>
#include "../aux/font.h"
#include "../entities/mapa.h"
#include "../entities/arounders.h"
namespace gamestate
{
namespace play
{
namespace eixir
{
const int no = 0;
const int postfase = 1;
const int mort = 2;
}
draw::surface *faded = nullptr;
draw::surface *fondo = nullptr;
draw::surface *cursor = nullptr;
draw::surface *aigua = nullptr;
uint32_t *original_palette = nullptr;
uint32_t arounderCount;
uint32_t startTicks;
uint32_t currentTicks;
int aigua_frame1 = 0;
int aigua_frame2 = 5;
int exit = 0;
bool loop();
bool loop_pause();
bool loop_menu();
void backToLoop();
void draw();
void draw_aigua();
void finalize();
void init()
{
// Ara comença tot, per tant no volem eixir
play::exit = play::eixir::no;
// Carregar el fondo que toque i la seua paleta (que nomes usa els colors del 128 al 255)
char arxiuFondo[10] = "BKG00.GIF";
arxiuFondo[4] = (game::getConfig("fase") % 10) + 48;
play::fondo = draw::loadSurface(arxiuFondo, true);
// Carregar la paleta estandar (colors del 0 al 128)
uint32_t *pal = draw::loadPalette("tiles.gif");
draw::setPalette(pal, 128);
free(pal);
// Carregar els gifs auxiliars i crear la superficie per al fondo de la pausa i el menu
play::cursor = draw::loadSurface("cursor.gif");
play::aigua = draw::loadSurface("aigua.gif");
play::faded = draw::createSurface(320, 200);
arounders::init();
// [TODO] Crear el primer Arounder
// REALMENT NO. PREFEREIXC QUE PASEN UNS SEGONS ABANS QUE ARRIBE EL PRIMER
// mapa::arounders::eixits++;
// arounder_seleccionat = primerArounders
// Enxufa el arradio
audio::loadAndPlayMusic((game::getConfig("fase")+1) % 5 == 0 ? "mus6.ogg" : "mus4.ogg");
// Fiquem a contar el cronómetre de arounders
play::arounderCount = play::startTicks = game::getTicks();
// Fade in i anem al bucle principal
draw::fadein();
game::setState(gamestate::play::loop);
}
bool loop()
{
// Si ja s'ha demanat l'eixida...
if (play::exit) {
// I si ja s'ha acabat el fadeout...
if (!draw::isfading()) {
// Alliberem memòria
play::finalize();
// I anem on toque
if (play::exit==play::eixir::postfase) gamestate::postfase::init();
else if (play::exit==play::eixir::mort) gamestate::mort::init();
}
// En qualsevol cas, renderitzem i eixim ja del bucle (fins que acabe el fadeout)
draw::render();
return true;
}
// Pintar tot menys el cursor
play::draw();
// Si se pulsa 'P', o el botó del mig del ratolí, o si la finestra perd el foco...
if (input::keyPressed(SDL_SCANCODE_P) || input::mouseClk(input::mouse::button::middle) || !game::windowHasFocus)
{
// Parem el cronòmetre d'arounders
play::arounderCount = game::getTicks() - play::arounderCount;
// Pintem el que hi ha a la pantalla en la surface "faded", li afegim el text "PAUSA" i aixó es tot el que pintarem fins eixir de la pausa
draw::setSource(nullptr);
draw::setDestination(play::faded);
draw::draw();
font::selectFont(font::type::fade);
font::print(136, 80, "PAUSA");
draw::setDestination(nullptr);
// Reduïm tots els colors de la paleta (menys els epecials) a la meitat del seu color
play::original_palette = draw::getPalette();
for (int i=0; i<256; ++i) if ( i!=61 && i!= 62 && (i<68 || i >75) )
{
uint32_t p = play::original_palette[i];
uint8_t r = (p >> 16) & 0xff;
uint8_t g = (p >> 8) & 0xff;
uint8_t b = p & 0xff;
draw::setPaletteEntry(i, r >> 1, g >> 1, b >> 1);
}
// Canviem el bucle principal a "loop_pausa()"
game::setState(gamestate::play::loop_pause);
return true;
}
// Si se pulsa ESC...
if (input::keyPressed(SDL_SCANCODE_ESCAPE))
{
// Parem el cronòmetre d'arounders
play::arounderCount = game::getTicks() - play::arounderCount;
// Pintem el que hi ha a la pantalla en la surface "faded", li afegim el menú i aixó es tot el que pintarem fins eixir del menú
draw::setSource(nullptr);
draw::setDestination(play::faded);
draw::draw();
draw::surface *menu = draw::loadSurface("menu.gif");
draw::setSource(menu);
draw::draw(97, 52);
draw::freeSurface(menu);
font::selectFont(font::type::fade);
font::print(129, 60, "CONTINUAR");
font::print(129, 71, "REINICIAR");
font::print(112, 82, "MENU PRINCIPAL");
font::print(143, 93, "EIXIR");
draw::setDestination(nullptr);
// Reduïm tots els colors de la paleta (menys els epecials) a la meitat del seu color
play::original_palette = draw::getPalette();
for (int i=0; i<256; ++i) if ( i!=61 && i!= 62 && (i<68 || i >75) )
{
uint32_t p = play::original_palette[i];
uint8_t r = (p >> 16) & 0xff;
uint8_t g = (p >> 8) & 0xff;
uint8_t b = p & 0xff;
draw::setPaletteEntry(i, r >> 1, g >> 1, b >> 1);
}
// Canviem el bucle principal a "loop_menu()"
game::setState(gamestate::play::loop_menu);
return true;
}
// Ara ja, pintem el cursor i enviem tot a pantalla
// No ho havem fet abans perque en el fondo de la pausa o el menú no volem que aparega el cursor
draw::setSource(cursor);
draw::draw(input::mouseX(), input::mouseY());
draw::render();
// Si pulsem el botó de la dreta, el arounder seleccionat deixa de fer l'acció que està fent
if (input::mouseClk(input::mouse::button::right))
{
arounders::abortarAccio();
}
if (input::mouseClk(input::mouse::button::left))
{
if (!arounders::seleccionar())
{
if (input::mouseY()<165 && arounders::seleccionat != nullptr && arounders::seleccionat->accio == arounders::accions::caminar)
{
arounders::seleccionat->orientacio = input::mouseX() > arounders::seleccionat->x ? arounders::orientacions::dreta : arounders::orientacions::esquerra;
}
}
const int botoPulsat = mapa::procesar();
if (botoPulsat != -1 && arounders::seleccionat != nullptr)
{
if (botoPulsat == arounders::seleccionat->prevista) {
arounders::seleccionat->prevista = arounders::seleccionat->accio;
} else {
arounders::seleccionat->prevista = botoPulsat;
}
}
}
currentTicks = game::getTicks() - startTicks;
if( currentTicks >= mapa::velocitat ) {
startTicks = game::getTicks();
aigua_frame1 = (aigua_frame1+1)%10;
aigua_frame2 = (aigua_frame2+1)%10;
arounders::procesar();
}
if ( (game::getTicks() - arounderCount) >= mapa::velocitat*58) {
if (mapa::arounders::eixits < mapa::arounders::totals) {
arounders::afegir();
mapa::arounders::eixits++;
arounderCount = game::getTicks();
}
}
if (mapa::arounders::arrivats + mapa::arounders::morts == mapa::arounders::totals) {
if (mapa::arounders::arrivats >= mapa::arounders::necessaris) {
game::setConfig("fase", game::getConfig("fase")+1);
draw::fadeout();
audio::fadeOutMusic();
play::exit = 1;
} else {
draw::fadeout();
audio::fadeOutMusic();
play::exit = 2;
}
}
return true;
}
bool loop_pause()
{
draw::draw(faded);
draw::setSource(cursor);
draw::draw(input::mouseX(), input::mouseY());
draw::render();
if (input::keyPressed(SDL_SCANCODE_P) || input::keyPressed(SDL_SCANCODE_ESCAPE))
{
play::backToLoop();
}
return true;
}
bool loop_menu()
{
if (play::exit) {
if (!draw::isfading()) {
play::finalize();
if (exit==1) {
mapa::carregar();
gamestate::play::init();
}
if (exit==2) gamestate::menu::init();
}
draw::render();
return true;
}
draw::draw(faded);
draw::setSource(cursor);
draw::draw(input::mouseX(), input::mouseY());
draw::render();
if (input::keyPressed(SDL_SCANCODE_ESCAPE))
{
play::backToLoop();
}
if (input::mouseClk(input::mouse::button::left))
{
if (input::mouseX() >= 97 && input::mouseX() <= 223) {
if (input::mouseY() >= 60 && input::mouseY() <= 65) {
play::backToLoop();
}
if (input::mouseY() >= 71 && input::mouseY() <= 76) {
free(play::original_palette);
draw::fadeout();
audio::fadeOutMusic();
play::exit = 1;
}
if (input::mouseY() >= 82 && input::mouseY() <= 87) {
free(play::original_palette);
draw::fadeout();
audio::fadeOutMusic();
play::exit = 2;
}
if (input::mouseY() >= 93 && input::mouseY() <= 98) {
return false;
}
}
}
return true;
}
void backToLoop()
{
draw::setPalette(play::original_palette, 256);
free(play::original_palette);
game::setState(gamestate::play::loop);
arounderCount = game::getTicks() - arounderCount;
}
void draw()
{
draw::draw(play::fondo);
int accio = 0;
int prevista = 0;
if (arounders::seleccionat) { accio = arounders::seleccionat->accio; prevista = arounders::seleccionat->prevista; }
mapa::pintar(accio, prevista);
draw_aigua();
arounders::pintar();
// [TODO] de fet, pintar la marca en el modul "arounders"
/*
if (arounders::seleccionat) {
draw::setSource(marca);
draw::draw(arounderSeleccionat->X-3, arounderSeleccionat->Y-3);
}
*/
}
void draw_aigua()
{
static int frames1[10] = {0,1,2,1,0,3,4,5,4,3};
static int frames2[10] = {6,7,8,7,6,9,10,11,10,9};
int *frames;
frames = (game::getConfig("fase")+1) % 5 == 0 ? frames2 : frames1;
draw::setSource(aigua);
for (int i=0;i<10;i++) {
draw::draw(i*32, 150, 16, 15, frames[aigua_frame1]*16, 0);
draw::draw(16+i*32, 150, 16, 15, frames[aigua_frame2]*16, 0);
}
}
void finalize()
{
draw::freeSurface(faded);
draw::freeSurface(fondo);
draw::freeSurface(cursor);
draw::freeSurface(aigua);
}
}
}

View File

@@ -0,0 +1,119 @@
#include "gamestates.h"
#include "../japi/game.h"
#include <string>
#include "../aux/font.h"
namespace gamestate
{
namespace postfase
{
namespace state
{
const int initial = 0;
const int victoria = 1;
const int password = 2;
}
// Variables del gamestate
static int sub_state = postfase::state::initial;
void initVictoria();
void initPassword();
bool loop();
std::string getPassword();
void init()
{
sub_state = postfase::state::initial;
if (game::getConfig("fase") == 30) {
gamestate::sequence::init();
return;
} else {
audio::loadAndPlayMusic("mus3.ogg");
}
if (game::getConfig("fase") % 5 == 0) {
gamestate::postfase::initVictoria();
} else {
gamestate::postfase::initPassword();
}
}
void initVictoria()
{
sub_state = postfase::state::victoria;
char filename[12] = "final00.GIF";
filename[6]= 48 + (game::getConfig("fase") / 5);
draw::surface *fondo = draw::loadSurface(filename, true);
draw::draw(fondo);
draw::freeSurface(fondo);
draw::fadein();
game::setState(gamestate::postfase::loop);
}
void initPassword()
{
sub_state = postfase::state::password;
std::string password = getPassword();
draw::surface *fondo = draw::loadSurface("postfase.gif", true);
draw::draw(fondo);
draw::freeSurface(fondo);
font::selectFont(font::type::colored);
font::setColor(font::color::red);
font::print(175, 166, password);
draw::fadein();
game::setState(gamestate::postfase::loop);
}
bool loop()
{
static bool salir = false;
draw::render();
if (draw::isfading()) return true;
if (salir)
{
salir = false;
if (sub_state == postfase::state::victoria) {
gamestate::postfase::initPassword();
} else {
gamestate::sequence::init();
}
return true;
}
if (input::anyKeyPressed() || input::mouseBtn(1))
{
draw::fadeout();
salir = true;
return true;
}
return true;
}
std::string getPassword()
{
int size;
char *buffer = file::getFileBuffer("offsets.bal", size);
int punter = (game::getConfig("fase")-1)*10;
char passFile[11];
for (int i=0;i<10;i++) passFile[i] = uint8_t(buffer[punter++]) - (101+i);
passFile[10] = 0;
free(buffer);
return passFile;
}
}
}

View File

@@ -0,0 +1,77 @@
#include "gamestates.h"
#include "../japi/game.h"
#include <string>
#include "../aux/font.h"
#include "../entities/mapa.h"
namespace gamestate
{
namespace prefase
{
// Variables del gamestate
draw::surface *fondo = nullptr;
draw::surface *cursor = nullptr;
// Mètodes del gamestate
bool loop();
void init()
{
// Carrega el gif del fondo
fondo = draw::loadSurface("prefase.gif", true);
cursor = draw::loadSurface("cursor.gif");
// Carrega el mapa
mapa::carregar();
// Pinta el text en el fondo
draw::setDestination(fondo);
font::selectFont(font::type::normal);
font::print(130, 60, "NIVELL");
font::print(179, 60, game::getConfig("fase")+1);
font::print(80, 100, mapa::arounders::totals);
font::print(101, 100, "AROUNDERS DISPONIBLES");
font::print(80, 110, mapa::arounders::necessaris);
font::print(101, 110, "AROUNDERS NECESSARIS");
draw::setDestination(nullptr);
// Comencem el bucle
draw::fadein();
game::setState(gamestate::prefase::loop);
}
bool loop()
{
static bool salir = false;
draw::draw(fondo);
draw::setSource(cursor);
draw::draw(input::mouseX(), input::mouseY());
draw::render();
if (draw::isfading()) return true;
if (salir)
{
salir = false;
gamestate::play::init();
return true;
}
if (input::anyKeyPressed() || input::mouseBtn(1))
{
draw::fadeout();
salir = true;
return true;
}
return true;
}
}
}

View File

@@ -0,0 +1,134 @@
#include "gamestates.h"
#include "../japi/game.h"
#include <string>
#include <SDL3/SDL.h>
#include "../aux/font.h"
namespace gamestate
{
namespace sequence
{
FILE *sequence_file = nullptr;
uint32_t wait_until = 0;
void drawPic(std::string filename);
bool loop();
void init()
{
font::selectFont(font::type::colored);
std::string filename;
switch (game::getConfig("fase"))
{
case -1: filename = "seqIN.txt"; break;
case 0: filename = "seq00.txt"; break;
case 5: filename = "seq05.txt"; break;
case 10: filename = "seq10.txt"; break;
case 15: filename = "seq15.txt"; break;
case 20: filename = "seq20.txt"; break;
case 25: filename = "seq25.txt"; break;
case 30: filename = "seq30.txt"; break;
default: gamestate::prefase::init(); return; break;
}
int size;
sequence_file = file::getFilePointer(filename.c_str(), size);
game::setState(&gamestate::sequence::loop);
}
bool loop()
{
if (draw::isfading()) {
draw::render();
return true;
}
if ( (wait_until > 0) && (SDL_GetTicks() < wait_until) )
{
if (input::anyKeyPressed() || input::mouseBtn(1)) {
wait_until=0;
if (input::keyPressed(SDL_SCANCODE_ESCAPE)) fseek(sequence_file, 0, SEEK_END);
} else {
draw::render();
return true;
}
}
if (feof(sequence_file))
{
fclose(sequence_file);
const int fase = game::getConfig("fase");
if ( fase == -1 || fase == 30) {
gamestate::menu::init();
} else {
gamestate::prefase::init();
}
return true;
} else {
char text[100];
int x, y, val;
int res = fscanf(sequence_file, "%s", text);
if (res==1)
{
std::string command(text);
if (command=="ESPERAR") {
fscanf(sequence_file, "%i", &val);
wait_until = SDL_GetTicks() + val;
} else if (command=="FADEIN") {
fscanf(sequence_file, " '%[^']'", text);
drawPic(text);
draw::fadein();
} else if (command=="SHOW") {
fscanf(sequence_file, " '%[^']'", text);
drawPic(text);
draw::render();
} else if (command=="PRINT") {
fscanf(sequence_file, " %i %i %i '%[^']'", &x, &y, &val, text);
font::setColor(val);
font::print(x, y, text);
} else if (command=="PLAYMUSIC") {
fscanf(sequence_file, " '%[^']'", text);
audio::loadAndPlayMusic(text);
} else if (command=="FADEOUT") {
draw::fadeout();
} else if (command=="FADEOUTMUSIC") {
draw::fadeout();
audio::fadeOutMusic();
} else if (command=="END") {
fclose(sequence_file);
const int fase = game::getConfig("fase");
if ( fase == -1 || fase == 30) {
gamestate::menu::init();
} else {
gamestate::prefase::init();
}
return true;
}
}
}
return true;
}
void drawPic(std::string filename)
{
draw::surface *pic = draw::loadSurface(filename.c_str(), true);
draw::draw(pic);
draw::freeSurface(pic);
}
}
}