437 lines
9.8 KiB
C++
437 lines
9.8 KiB
C++
#include "intro.h"
|
|
#include <SDL2/SDL_events.h> // for SDL_PollEvent, SDL_Event, SDL_QUIT, SDL...
|
|
#include <SDL2/SDL_timer.h> // for SDL_GetTicks
|
|
#include <SDL2/SDL_video.h> // for SDL_WINDOWEVENT_SIZE_CHANGED
|
|
#include <utility> // for move
|
|
#include "asset.h" // for Asset
|
|
#include "global_inputs.h" // for check
|
|
#include "input.h" // for Input
|
|
#include "jail_audio.h" // for JA_StopMusic, JA_PlayMusic
|
|
#include "lang.h" // for getText
|
|
#include "param.h" // for param
|
|
#include "resource.h" // for Resource
|
|
#include "screen.h" // for Screen
|
|
#include "section.h" // for Name, name, Options, options
|
|
#include "smart_sprite.h" // for SpriteSmart
|
|
#include "text.h" // for Text
|
|
#include "texture.h" // for Texture
|
|
#include "utils.h" // for Param, ParamGame, Zone, BLOCK, Color
|
|
#include "writer.h" // for Writer
|
|
struct JA_Music_t; // lines 19-19
|
|
|
|
// Constructor
|
|
Intro::Intro()
|
|
{
|
|
// Copia los punteros
|
|
auto renderer = Screen::get()->getRenderer();
|
|
|
|
// Reserva memoria para los objetos
|
|
texture_ = std::make_shared<Texture>(renderer, Asset::get()->get("intro.png"));
|
|
text_ = std::make_shared<Text>(Asset::get()->get("nokia.png"), Asset::get()->get("nokia.txt"), renderer);
|
|
|
|
// Inicializa variables
|
|
section::name = section::Name::INTRO;
|
|
section::options = section::Options::NONE;
|
|
ticks_ = 0;
|
|
ticks_speed_ = 15;
|
|
scene_ = 1;
|
|
|
|
// Inicializa los bitmaps de la intro
|
|
constexpr int totalBitmaps = 6;
|
|
for (int i = 0; i < totalBitmaps; ++i)
|
|
{
|
|
auto ss = std::make_unique<SmartSprite>(texture_);
|
|
ss->setWidth(128);
|
|
ss->setHeight(96);
|
|
ss->setFinishedCounter(20);
|
|
ss->setDestX(param.game.game_area.center_x - 64);
|
|
ss->setDestY(param.game.game_area.first_quarter_y - 24);
|
|
bitmaps_.push_back(std::move(ss));
|
|
}
|
|
|
|
bitmaps_[0]->setPosX(-128);
|
|
bitmaps_[0]->setPosY(param.game.game_area.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(param.game.width);
|
|
bitmaps_[1]->setPosY(param.game.game_area.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(param.game.game_area.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]->setFinishedCounter(250);
|
|
|
|
bitmaps_[3]->setPosX(param.game.game_area.center_x - 64);
|
|
bitmaps_[3]->setPosY(param.game.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(param.game.game_area.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(param.game.width);
|
|
bitmaps_[5]->setPosY(param.game.game_area.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
|
|
constexpr int totalTexts = 9;
|
|
for (int i = 0; i < totalTexts; ++i)
|
|
{
|
|
auto w = std::make_unique<Writer>(text_);
|
|
w->setPosX(BLOCK * 0);
|
|
w->setPosY(param.game.height - (BLOCK * 6));
|
|
w->setKerning(-1);
|
|
w->setEnabled(false);
|
|
w->setFinishedCounter(180);
|
|
texts_.push_back(std::move(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(param.game.game_area.center_x);
|
|
}
|
|
}
|
|
|
|
// Recarga todas las texturas
|
|
void Intro::reloadTextures()
|
|
{
|
|
texture_->reLoad();
|
|
text_->reLoadTexture();
|
|
}
|
|
|
|
// Comprueba los eventos
|
|
void Intro::checkEvents()
|
|
{
|
|
SDL_Event event;
|
|
|
|
// Comprueba los eventos que hay en la cola
|
|
while (SDL_PollEvent(&event))
|
|
{
|
|
switch (event.type)
|
|
{
|
|
case SDL_QUIT:
|
|
{
|
|
section::name = section::Name::QUIT;
|
|
break;
|
|
}
|
|
|
|
case SDL_WINDOWEVENT:
|
|
{
|
|
if (event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED)
|
|
{
|
|
reloadTextures();
|
|
}
|
|
break;
|
|
}
|
|
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Comprueba las entradas
|
|
void Intro::checkInput()
|
|
{
|
|
// Comprueba si se ha pulsado cualquier botón (de los usados para jugar)
|
|
if (Input::get()->checkAnyButtonPressed())
|
|
{
|
|
JA_StopMusic();
|
|
section::name = section::Name::TITLE;
|
|
section::options = section::Options::TITLE_1;
|
|
return;
|
|
}
|
|
|
|
// Comprueba el input para el resto de objetos
|
|
Screen::get()->checkInput();
|
|
|
|
// Comprueba los inputs que se pueden introducir en cualquier sección del juego
|
|
globalInputs::check();
|
|
}
|
|
|
|
// 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::Name::TITLE;
|
|
section::options = section::Options::TITLE_1;
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
// Actualiza las variables del objeto
|
|
void Intro::update()
|
|
{
|
|
if (SDL_GetTicks() - ticks_ > ticks_speed_)
|
|
{
|
|
// Actualiza el contador de ticks
|
|
ticks_ = SDL_GetTicks();
|
|
|
|
// Actualiza el objeto screen
|
|
Screen::get()->update();
|
|
|
|
// 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::get()->start();
|
|
|
|
// Limpia la pantalla
|
|
Screen::get()->clean(bg_color);
|
|
|
|
// Dibuja los objetos
|
|
for (const auto &bitmap : bitmaps_)
|
|
{
|
|
bitmap->render();
|
|
}
|
|
|
|
for (const auto &text : texts_)
|
|
{
|
|
text->render();
|
|
}
|
|
|
|
// Vuelca el contenido del renderizador en pantalla
|
|
Screen::get()->blit();
|
|
}
|
|
|
|
// Bucle principal
|
|
void Intro::run()
|
|
{
|
|
JA_PlayMusic(Resource::get()->getMusic("intro.ogg"), 0);
|
|
|
|
while (section::name == section::Name::INTRO)
|
|
{
|
|
checkInput();
|
|
update();
|
|
checkEvents(); // Tiene que ir antes del render
|
|
render();
|
|
}
|
|
} |