reestructuració
This commit is contained in:
416
source/game/scenes/intro.cpp
Normal file
416
source/game/scenes/intro.cpp
Normal file
@@ -0,0 +1,416 @@
|
||||
#include "game/scenes/intro.h"
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <string> // for basic_string
|
||||
|
||||
#include "core/audio/jail_audio.hpp" // for JA_StopMusic, JA_DeleteMusic, JA_LoadMusic
|
||||
#include "core/input/input.h" // for Input, REPEAT_FALSE, inputs_e
|
||||
#include "core/locale/lang.h" // for Lang
|
||||
#include "core/rendering/screen.h" // for Screen
|
||||
#include "core/rendering/smartsprite.h" // for SmartSprite
|
||||
#include "core/rendering/text.h" // for Text
|
||||
#include "core/rendering/texture.h" // for Texture
|
||||
#include "core/rendering/writer.h" // for Writer
|
||||
#include "core/resources/asset.h" // for Asset
|
||||
#include "core/resources/resource.h"
|
||||
#include "game/defaults.hpp" // for GAMECANVAS_CENTER_X, GAMECANVAS_FIRST_QU...
|
||||
#include "utils/utils.h" // for section_t, color_t
|
||||
|
||||
// Constructor
|
||||
Intro::Intro(SDL_Renderer *renderer, Screen *screen, Asset *asset, Input *input, Lang *lang, section_t *section) {
|
||||
// Copia los punteros
|
||||
this->renderer = renderer;
|
||||
this->screen = screen;
|
||||
this->lang = lang;
|
||||
this->asset = asset;
|
||||
this->input = input;
|
||||
this->section = section;
|
||||
|
||||
// Reserva memoria para los objetos
|
||||
eventHandler = new SDL_Event();
|
||||
texture = Resource::get()->getTexture("intro.png");
|
||||
text = Resource::get()->getText("nokia");
|
||||
music = Resource::get()->getMusic("intro.ogg");
|
||||
|
||||
// Inicializa variables
|
||||
section->name = SECTION_PROG_INTRO;
|
||||
section->subsection = 0;
|
||||
ticks = 0;
|
||||
ticksSpeed = 15;
|
||||
scene = 1;
|
||||
|
||||
// Inicializa los bitmaps de la intro
|
||||
const int totalBitmaps = 6;
|
||||
for (int i = 0; i < totalBitmaps; ++i) {
|
||||
SmartSprite *ss = new SmartSprite(texture, renderer);
|
||||
ss->setWidth(128);
|
||||
ss->setHeight(96);
|
||||
ss->setEnabledCounter(20);
|
||||
ss->setDestX(GAMECANVAS_CENTER_X - 64);
|
||||
ss->setDestY(GAMECANVAS_FIRST_QUARTER_Y - 24);
|
||||
bitmaps.push_back(ss);
|
||||
}
|
||||
|
||||
bitmaps[0]->setPosX(-128);
|
||||
bitmaps[0]->setPosY(GAMECANVAS_FIRST_QUARTER_Y - 24);
|
||||
bitmaps[0]->setVelX(0.0f);
|
||||
bitmaps[0]->setVelY(0.0f);
|
||||
bitmaps[0]->setAccelX(0.6f);
|
||||
bitmaps[0]->setAccelY(0.0f);
|
||||
bitmaps[0]->setSpriteClip(0, 0, 128, 96);
|
||||
|
||||
bitmaps[1]->setPosX(GAMECANVAS_WIDTH);
|
||||
bitmaps[1]->setPosY(GAMECANVAS_FIRST_QUARTER_Y - 24);
|
||||
bitmaps[1]->setVelX(-1.0f);
|
||||
bitmaps[1]->setVelY(0.0f);
|
||||
bitmaps[1]->setAccelX(-0.3f);
|
||||
bitmaps[1]->setAccelY(0.0f);
|
||||
bitmaps[1]->setSpriteClip(128, 0, 128, 96);
|
||||
|
||||
bitmaps[2]->setPosX(GAMECANVAS_CENTER_X - 64);
|
||||
bitmaps[2]->setPosY(-96);
|
||||
bitmaps[2]->setVelX(0.0f);
|
||||
bitmaps[2]->setVelY(3.0f);
|
||||
bitmaps[2]->setAccelX(0.1f);
|
||||
bitmaps[2]->setAccelY(0.3f);
|
||||
bitmaps[2]->setSpriteClip(0, 96, 128, 96);
|
||||
bitmaps[2]->setEnabledCounter(250);
|
||||
|
||||
bitmaps[3]->setPosX(GAMECANVAS_CENTER_X - 64);
|
||||
bitmaps[3]->setPosY(GAMECANVAS_HEIGHT);
|
||||
bitmaps[3]->setVelX(0.0f);
|
||||
bitmaps[3]->setVelY(-0.7f);
|
||||
bitmaps[3]->setAccelX(0.0f);
|
||||
bitmaps[3]->setAccelY(0.0f);
|
||||
bitmaps[3]->setSpriteClip(128, 96, 128, 96);
|
||||
|
||||
bitmaps[4]->setPosX(GAMECANVAS_CENTER_X - 64);
|
||||
bitmaps[4]->setPosY(-96);
|
||||
bitmaps[4]->setVelX(0.0f);
|
||||
bitmaps[4]->setVelY(3.0f);
|
||||
bitmaps[4]->setAccelX(0.1f);
|
||||
bitmaps[4]->setAccelY(0.3f);
|
||||
bitmaps[4]->setSpriteClip(0, 192, 128, 96);
|
||||
|
||||
bitmaps[5]->setPosX(GAMECANVAS_WIDTH);
|
||||
bitmaps[5]->setPosY(GAMECANVAS_FIRST_QUARTER_Y - 24);
|
||||
bitmaps[5]->setVelX(-0.7f);
|
||||
bitmaps[5]->setVelY(0.0f);
|
||||
bitmaps[5]->setAccelX(0.0f);
|
||||
bitmaps[5]->setAccelY(0.0f);
|
||||
bitmaps[5]->setSpriteClip(128, 192, 128, 96);
|
||||
|
||||
// Inicializa los textos de la intro
|
||||
const int totalTexts = 9;
|
||||
for (int i = 0; i < totalTexts; ++i) {
|
||||
Writer *w = new Writer(text);
|
||||
w->setPosX(BLOCK * 0);
|
||||
w->setPosY(GAMECANVAS_HEIGHT - (BLOCK * 6));
|
||||
w->setKerning(-1);
|
||||
w->setEnabled(false);
|
||||
w->setEnabledCounter(180);
|
||||
texts.push_back(w);
|
||||
}
|
||||
|
||||
// Un dia qualsevol de l'any 2000
|
||||
texts[0]->setCaption(lang->getText(27));
|
||||
texts[0]->setSpeed(8);
|
||||
|
||||
// Tot esta tranquil a la UPV
|
||||
texts[1]->setCaption(lang->getText(28));
|
||||
texts[1]->setSpeed(8);
|
||||
|
||||
// Fins que un desaprensiu...
|
||||
texts[2]->setCaption(lang->getText(29));
|
||||
texts[2]->setSpeed(12);
|
||||
|
||||
// HEY! ME ANE A FERME UN CORTAET...
|
||||
texts[3]->setCaption(lang->getText(30));
|
||||
texts[3]->setSpeed(8);
|
||||
|
||||
// UAAAAAAAAAAAAA!!!
|
||||
texts[4]->setCaption(lang->getText(31));
|
||||
texts[4]->setSpeed(1);
|
||||
|
||||
// Espera un moment...
|
||||
texts[5]->setCaption(lang->getText(32));
|
||||
texts[5]->setSpeed(16);
|
||||
|
||||
// Si resulta que no tinc solt!
|
||||
texts[6]->setCaption(lang->getText(33));
|
||||
texts[6]->setSpeed(2);
|
||||
|
||||
// MERDA DE MAQUINA!
|
||||
texts[7]->setCaption(lang->getText(34));
|
||||
texts[7]->setSpeed(3);
|
||||
|
||||
// Blop... blop... blop...
|
||||
texts[8]->setCaption(lang->getText(35));
|
||||
texts[8]->setSpeed(16);
|
||||
|
||||
for (auto text : texts) {
|
||||
text->center(GAMECANVAS_CENTER_X);
|
||||
}
|
||||
|
||||
JA_PlayMusic(music, 0);
|
||||
}
|
||||
|
||||
// Destructor
|
||||
Intro::~Intro() {
|
||||
delete eventHandler;
|
||||
|
||||
// texture, text, music son propiedad de Resource — no liberar aquí.
|
||||
for (auto bitmap : bitmaps) {
|
||||
delete bitmap;
|
||||
}
|
||||
|
||||
for (auto t : texts) {
|
||||
delete t;
|
||||
}
|
||||
}
|
||||
|
||||
// Carga los recursos (ya no carga nada, se mantiene por si hay callers)
|
||||
bool Intro::loadMedia() {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Comprueba los eventos
|
||||
void Intro::checkEvents() {
|
||||
#ifndef __EMSCRIPTEN__
|
||||
// Comprueba los eventos que hay en la cola
|
||||
while (SDL_PollEvent(eventHandler) != 0) {
|
||||
// Evento de salida de la aplicación
|
||||
if (eventHandler->type == SDL_EVENT_QUIT) {
|
||||
section->name = SECTION_PROG_QUIT;
|
||||
break;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
// Comprueba las entradas
|
||||
void Intro::checkInput() {
|
||||
#ifndef __EMSCRIPTEN__
|
||||
if (input->checkInput(input_exit, REPEAT_FALSE)) {
|
||||
section->name = SECTION_PROG_QUIT;
|
||||
} else
|
||||
#endif
|
||||
if (input->checkInput(input_window_fullscreen, REPEAT_FALSE)) {
|
||||
screen->toggleVideoMode();
|
||||
}
|
||||
|
||||
else if (input->checkInput(input_window_dec_size, REPEAT_FALSE)) {
|
||||
screen->decWindowZoom();
|
||||
}
|
||||
|
||||
else if (input->checkInput(input_window_inc_size, REPEAT_FALSE)) {
|
||||
screen->incWindowZoom();
|
||||
}
|
||||
|
||||
else if (input->checkInput(input_pause, REPEAT_FALSE) || input->checkInput(input_accept, REPEAT_FALSE) || input->checkInput(input_fire_left, REPEAT_FALSE) || input->checkInput(input_fire_center, REPEAT_FALSE) || input->checkInput(input_fire_right, REPEAT_FALSE)) {
|
||||
JA_StopMusic();
|
||||
section->name = SECTION_PROG_TITLE;
|
||||
section->subsection = SUBSECTION_TITLE_1;
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza las escenas de la intro
|
||||
void Intro::updateScenes() {
|
||||
switch (scene) {
|
||||
case 1:
|
||||
// Primera imagen - UPV
|
||||
if (!bitmaps[0]->hasFinished()) {
|
||||
bitmaps[0]->setEnabled(true);
|
||||
}
|
||||
|
||||
// Primer texto de la primera imagen
|
||||
if (bitmaps[0]->hasFinished() && !texts[0]->hasFinished()) {
|
||||
texts[0]->setEnabled(true);
|
||||
}
|
||||
|
||||
// Segundo texto de la primera imagen
|
||||
if (texts[0]->hasFinished() && !texts[1]->hasFinished()) {
|
||||
texts[0]->setEnabled(false);
|
||||
texts[1]->setEnabled(true);
|
||||
}
|
||||
|
||||
// Tercer texto de la primera imagen
|
||||
if (texts[1]->hasFinished() && !texts[2]->hasFinished()) {
|
||||
texts[1]->setEnabled(false);
|
||||
texts[2]->setEnabled(true);
|
||||
}
|
||||
|
||||
// Fin de la primera escena
|
||||
if (texts[2]->hasFinished()) {
|
||||
bitmaps[0]->setEnabled(false);
|
||||
texts[2]->setEnabled(false);
|
||||
scene++;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case 2:
|
||||
// Segunda imagen - Máquina
|
||||
if (!bitmaps[1]->hasFinished()) {
|
||||
bitmaps[1]->setEnabled(true);
|
||||
}
|
||||
|
||||
// Primer texto de la segunda imagen
|
||||
if (bitmaps[1]->hasFinished() && !texts[3]->hasFinished()) {
|
||||
texts[3]->setEnabled(true);
|
||||
}
|
||||
|
||||
// Fin de la segunda escena
|
||||
if (texts[3]->hasFinished()) {
|
||||
bitmaps[1]->setEnabled(false);
|
||||
texts[3]->setEnabled(false);
|
||||
scene++;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case 3:
|
||||
// Tercera imagen junto con primer texto - GRITO
|
||||
if (!bitmaps[2]->hasFinished() && !texts[4]->hasFinished()) {
|
||||
bitmaps[2]->setEnabled(true);
|
||||
texts[4]->setEnabled(true);
|
||||
}
|
||||
|
||||
// Fin de la tercera escena
|
||||
if (bitmaps[2]->hasFinished() && texts[4]->hasFinished()) {
|
||||
bitmaps[2]->setEnabled(false);
|
||||
texts[4]->setEnabled(false);
|
||||
scene++;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case 4:
|
||||
// Cuarta imagen junto con primer texto - Reflexión
|
||||
if (!bitmaps[3]->hasFinished() && !texts[5]->hasFinished()) {
|
||||
bitmaps[3]->setEnabled(true);
|
||||
texts[5]->setEnabled(true);
|
||||
}
|
||||
|
||||
// Segundo texto de la cuarta imagen
|
||||
if (texts[5]->hasFinished() && !texts[6]->hasFinished()) {
|
||||
texts[5]->setEnabled(false);
|
||||
texts[6]->setEnabled(true);
|
||||
}
|
||||
|
||||
// Fin de la cuarta escena
|
||||
if (bitmaps[3]->hasFinished() && texts[6]->hasFinished()) {
|
||||
bitmaps[3]->setEnabled(false);
|
||||
texts[6]->setEnabled(false);
|
||||
scene++;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case 5:
|
||||
// Quinta imagen - Patada
|
||||
if (!bitmaps[4]->hasFinished()) {
|
||||
bitmaps[4]->setEnabled(true);
|
||||
}
|
||||
|
||||
// Primer texto de la quinta imagen
|
||||
if (bitmaps[4]->hasFinished() && !texts[7]->hasFinished()) {
|
||||
texts[7]->setEnabled(true);
|
||||
}
|
||||
|
||||
// Fin de la quinta escena
|
||||
if (bitmaps[4]->hasFinished() && texts[7]->hasFinished()) {
|
||||
bitmaps[4]->setEnabled(false);
|
||||
texts[7]->setEnabled(false);
|
||||
scene++;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case 6:
|
||||
// Sexta imagen junto con texto - Globos de café
|
||||
if (!bitmaps[5]->hasFinished() && !texts[8]->hasFinished()) {
|
||||
bitmaps[5]->setEnabled(true);
|
||||
texts[8]->setEnabled(true);
|
||||
}
|
||||
|
||||
// Acaba el último texto
|
||||
if (bitmaps[5]->hasFinished() && texts[8]->hasFinished()) {
|
||||
bitmaps[5]->setEnabled(false);
|
||||
texts[8]->setEnabled(false);
|
||||
JA_StopMusic();
|
||||
section->name = SECTION_PROG_TITLE;
|
||||
section->subsection = SUBSECTION_TITLE_1;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza las variables del objeto
|
||||
void Intro::update() {
|
||||
JA_Update();
|
||||
checkInput();
|
||||
|
||||
if (SDL_GetTicks() - ticks > ticksSpeed) {
|
||||
// Actualiza el contador de ticks
|
||||
ticks = SDL_GetTicks();
|
||||
|
||||
// Actualiza los objetos
|
||||
for (auto bitmap : bitmaps) {
|
||||
bitmap->update();
|
||||
}
|
||||
|
||||
for (auto text : texts) {
|
||||
text->update();
|
||||
}
|
||||
|
||||
// Actualiza las escenas de la intro
|
||||
updateScenes();
|
||||
}
|
||||
}
|
||||
|
||||
// Dibuja el objeto en pantalla
|
||||
void Intro::render() {
|
||||
// Prepara para empezar a dibujar en la textura de juego
|
||||
screen->start();
|
||||
|
||||
// Limpia la pantalla
|
||||
screen->clean(bgColor);
|
||||
|
||||
// Dibuja los objetos
|
||||
for (auto bitmap : bitmaps) {
|
||||
bitmap->render();
|
||||
}
|
||||
|
||||
for (auto text : texts) {
|
||||
text->render();
|
||||
}
|
||||
|
||||
// Vuelca el contenido del renderizador en pantalla
|
||||
screen->blit();
|
||||
}
|
||||
|
||||
// Bucle principal
|
||||
void Intro::run() {
|
||||
JA_PlayMusic(music, 0);
|
||||
|
||||
while (section->name == SECTION_PROG_INTRO) {
|
||||
iterate();
|
||||
}
|
||||
}
|
||||
|
||||
// Ejecuta un frame
|
||||
void Intro::iterate() {
|
||||
update();
|
||||
render();
|
||||
}
|
||||
|
||||
// Procesa un evento individual
|
||||
void Intro::handleEvent(SDL_Event *event) {
|
||||
// SDL_EVENT_QUIT ya lo maneja Director
|
||||
}
|
||||
Reference in New Issue
Block a user