451 lines
11 KiB
C++
451 lines
11 KiB
C++
#include "intro.h"
|
|
#include <SDL2/SDL_events.h> // Para SDL_PollEvent, SDL_Event, SDL_QUIT, SDL...
|
|
#include <SDL2/SDL_timer.h> // Para SDL_GetTicks
|
|
#include <SDL2/SDL_video.h> // Para SDL_WINDOWEVENT_SIZE_CHANGED
|
|
#include <utility> // Para move
|
|
#include "global_inputs.h" // Para check
|
|
#include "input.h" // Para Input
|
|
#include "jail_audio.h" // Para JA_StopMusic, JA_PlayMusic
|
|
#include "lang.h" // Para getText
|
|
#include "param.h" // Para Param, ParamGame, param
|
|
#include "resource.h" // Para Resource
|
|
#include "screen.h" // Para Screen
|
|
#include "section.h" // Para Name, name, Options, options
|
|
#include "smart_sprite.h" // Para SmartSprite
|
|
#include "text.h" // Para Text
|
|
#include "texture.h" // Para Texture
|
|
#include "utils.h" // Para Zone, BLOCK, Color, bg_color
|
|
#include "writer.h" // Para Writer
|
|
#include "global_events.h"
|
|
#include <array>
|
|
|
|
// Constructor
|
|
Intro::Intro()
|
|
{
|
|
// Inicializa variables
|
|
section::name = section::Name::INTRO;
|
|
section::options = section::Options::NONE;
|
|
|
|
// Inicializa las imagens
|
|
initSprites();
|
|
|
|
// Inicializa los textos
|
|
initTexts();
|
|
}
|
|
|
|
// Comprueba los eventos
|
|
void Intro::checkEvents()
|
|
{
|
|
SDL_Event event;
|
|
while (SDL_PollEvent(&event))
|
|
{
|
|
globalEvents::check(event);
|
|
}
|
|
}
|
|
|
|
// Comprueba las entradas
|
|
void Intro::checkInput() { globalInputs::check(); }
|
|
|
|
// Actualiza las escenas de la intro
|
|
void Intro::updateScenes()
|
|
{
|
|
switch (scene_)
|
|
{
|
|
case 0:
|
|
{
|
|
// Primera imagen - UPV
|
|
if (!bitmaps_.at(0)->hasFinished())
|
|
{
|
|
bitmaps_.at(0)->setEnabled(true);
|
|
}
|
|
|
|
// Primer texto de la primera imagen
|
|
if (bitmaps_.at(0)->hasFinished() && !texts_.at(0)->hasFinished())
|
|
{
|
|
texts_.at(0)->setEnabled(true);
|
|
}
|
|
|
|
// Segundo texto de la primera imagen
|
|
if (texts_.at(0)->hasFinished() && !texts_.at(1)->hasFinished())
|
|
{
|
|
texts_.at(0)->setEnabled(false);
|
|
texts_.at(1)->setEnabled(true);
|
|
}
|
|
|
|
// Tercer texto de la primera imagen
|
|
if (texts_.at(1)->hasFinished() && !texts_.at(2)->hasFinished())
|
|
{
|
|
texts_.at(1)->setEnabled(false);
|
|
texts_.at(2)->setEnabled(true);
|
|
}
|
|
|
|
// Fin de la primera escena
|
|
if (texts_.at(2)->hasFinished())
|
|
{
|
|
bitmaps_.at(0)->setEnabled(false);
|
|
texts_.at(2)->setEnabled(false);
|
|
scene_++;
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
case 1:
|
|
{
|
|
// Segunda imagen - Máquina
|
|
if (!bitmaps_.at(1)->hasFinished())
|
|
{
|
|
bitmaps_.at(1)->setEnabled(true);
|
|
}
|
|
|
|
// Primer texto de la segunda imagen
|
|
if (bitmaps_.at(1)->hasFinished() && !texts_.at(3)->hasFinished())
|
|
{
|
|
texts_.at(3)->setEnabled(true);
|
|
}
|
|
|
|
// Fin de la segunda escena
|
|
if (texts_.at(3)->hasFinished())
|
|
{
|
|
bitmaps_.at(1)->setEnabled(false);
|
|
texts_.at(3)->setEnabled(false);
|
|
scene_++;
|
|
}
|
|
|
|
break;
|
|
}
|
|
case 2:
|
|
{
|
|
// Tercera imagen junto con primer texto - GRITO
|
|
if (!bitmaps_.at(2)->hasFinished() && !texts_.at(4)->hasFinished())
|
|
{
|
|
bitmaps_.at(2)->setEnabled(true);
|
|
texts_.at(4)->setEnabled(true);
|
|
}
|
|
|
|
// Fin de la tercera escena
|
|
if (bitmaps_.at(2)->hasFinished() && texts_.at(4)->hasFinished())
|
|
{
|
|
bitmaps_.at(2)->setEnabled(false);
|
|
texts_.at(4)->setEnabled(false);
|
|
scene_++;
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
case 3:
|
|
{
|
|
// Cuarta imagen junto con primer texto - Reflexión
|
|
if (!bitmaps_.at(3)->hasFinished() && !texts_.at(5)->hasFinished())
|
|
{
|
|
bitmaps_.at(3)->setEnabled(true);
|
|
texts_.at(5)->setEnabled(true);
|
|
}
|
|
|
|
// Segundo texto de la cuarta imagen
|
|
if (texts_.at(5)->hasFinished() && !texts_.at(6)->hasFinished())
|
|
{
|
|
texts_.at(5)->setEnabled(false);
|
|
texts_.at(6)->setEnabled(true);
|
|
}
|
|
|
|
// Fin de la cuarta escena
|
|
if (bitmaps_.at(3)->hasFinished() && texts_.at(6)->hasFinished())
|
|
{
|
|
bitmaps_.at(3)->setEnabled(false);
|
|
texts_.at(6)->setEnabled(false);
|
|
scene_++;
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
case 4:
|
|
{
|
|
// Quinta imagen - Patada
|
|
if (!bitmaps_.at(4)->hasFinished())
|
|
{
|
|
bitmaps_.at(4)->setEnabled(true);
|
|
}
|
|
|
|
// Primer texto de la quinta imagen
|
|
if (bitmaps_.at(4)->hasFinished() && !texts_.at(7)->hasFinished())
|
|
{
|
|
texts_.at(7)->setEnabled(true);
|
|
}
|
|
|
|
// Fin de la quinta escena
|
|
if (bitmaps_.at(4)->hasFinished() && texts_.at(7)->hasFinished())
|
|
{
|
|
bitmaps_.at(4)->setEnabled(false);
|
|
texts_.at(7)->setEnabled(false);
|
|
scene_++;
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
case 5:
|
|
{
|
|
// Sexta imagen junto con texto - Globos de café
|
|
if (!bitmaps_.at(5)->hasFinished() && !texts_.at(8)->hasFinished())
|
|
{
|
|
bitmaps_.at(5)->setEnabled(true);
|
|
texts_.at(8)->setEnabled(true);
|
|
}
|
|
|
|
// Acaba el último texto
|
|
if (bitmaps_.at(5)->hasFinished() && texts_.at(8)->hasFinished())
|
|
{
|
|
bitmaps_.at(5)->setEnabled(false);
|
|
texts_.at(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_ > param.game.speed)
|
|
{
|
|
// Actualiza el contador de ticks
|
|
ticks_ = SDL_GetTicks();
|
|
|
|
// Actualiza el objeto screen
|
|
Screen::get()->update();
|
|
|
|
// Actualiza los sprites
|
|
updateSprites();
|
|
|
|
// Actualiza los textos
|
|
updateTexts();
|
|
|
|
// Actualiza las escenas de la intro
|
|
updateScenes();
|
|
|
|
// Actualiza las variables de globalInputs
|
|
globalInputs::update();
|
|
}
|
|
}
|
|
|
|
// 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 sprites
|
|
renderSprites();
|
|
|
|
// Dibuja los textos
|
|
renderTexts();
|
|
|
|
// Vuelca el contenido del renderizador en pantalla
|
|
Screen::get()->render();
|
|
}
|
|
|
|
// 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();
|
|
}
|
|
}
|
|
|
|
// Inicializa las imagens
|
|
void Intro::initSprites()
|
|
{
|
|
const std::array<std::string, 6> BITMAP_LIST = {
|
|
"intro1.png",
|
|
"intro2.png",
|
|
"intro3.png",
|
|
"intro4.png",
|
|
"intro5.png",
|
|
"intro6.png"};
|
|
|
|
auto texture = Resource::get()->getTexture(BITMAP_LIST.front());
|
|
const int BITMAP_WIDTH = texture->getWidth();
|
|
const int BITMAP_HEIGHT = texture->getHeight();
|
|
|
|
// Inicializa los bitmaps de la intro
|
|
constexpr int TOTAL_BITMAPS = 6;
|
|
for (int i = 0; i < TOTAL_BITMAPS; ++i)
|
|
{
|
|
auto ss = std::make_unique<SmartSprite>(Resource::get()->getTexture(BITMAP_LIST.at(i)));
|
|
ss->setWidth(BITMAP_WIDTH);
|
|
ss->setHeight(BITMAP_HEIGHT);
|
|
ss->setSpriteClip(0, 0, BITMAP_WIDTH, BITMAP_HEIGHT);
|
|
ss->setFinishedCounter(20);
|
|
ss->setDestX(param.game.game_area.center_x - (BITMAP_WIDTH / 2));
|
|
ss->setDestY(param.game.game_area.first_quarter_y - (BITMAP_HEIGHT / 4));
|
|
bitmaps_.push_back(std::move(ss));
|
|
}
|
|
|
|
bitmaps_.at(0)->setPosX(-BITMAP_WIDTH);
|
|
bitmaps_.at(0)->setPosY(param.game.game_area.first_quarter_y - (BITMAP_HEIGHT / 4));
|
|
bitmaps_.at(0)->setVelX(0.0f);
|
|
bitmaps_.at(0)->setVelY(0.0f);
|
|
bitmaps_.at(0)->setAccelX(0.6f);
|
|
bitmaps_.at(0)->setAccelY(0.0f);
|
|
|
|
bitmaps_.at(1)->setPosX(param.game.width);
|
|
bitmaps_.at(1)->setPosY(param.game.game_area.first_quarter_y - (BITMAP_HEIGHT / 4));
|
|
bitmaps_.at(1)->setVelX(-1.0f);
|
|
bitmaps_.at(1)->setVelY(0.0f);
|
|
bitmaps_.at(1)->setAccelX(-0.3f);
|
|
bitmaps_.at(1)->setAccelY(0.0f);
|
|
|
|
bitmaps_.at(2)->setPosX(param.game.game_area.center_x - (BITMAP_WIDTH / 2));
|
|
bitmaps_.at(2)->setPosY(-BITMAP_HEIGHT);
|
|
bitmaps_.at(2)->setVelX(0.0f);
|
|
bitmaps_.at(2)->setVelY(3.0f);
|
|
bitmaps_.at(2)->setAccelX(0.1f);
|
|
bitmaps_.at(2)->setAccelY(0.3f);
|
|
bitmaps_.at(2)->setFinishedCounter(250);
|
|
|
|
bitmaps_.at(3)->setPosX(param.game.game_area.center_x - (BITMAP_WIDTH / 2));
|
|
bitmaps_.at(3)->setPosY(param.game.height);
|
|
bitmaps_.at(3)->setVelX(0.0f);
|
|
bitmaps_.at(3)->setVelY(-0.7f);
|
|
bitmaps_.at(3)->setAccelX(0.0f);
|
|
bitmaps_.at(3)->setAccelY(0.0f);
|
|
|
|
bitmaps_.at(4)->setPosX(param.game.game_area.center_x - (BITMAP_WIDTH / 2));
|
|
bitmaps_.at(4)->setPosY(-BITMAP_HEIGHT);
|
|
bitmaps_.at(4)->setVelX(0.0f);
|
|
bitmaps_.at(4)->setVelY(3.0f);
|
|
bitmaps_.at(4)->setAccelX(0.1f);
|
|
bitmaps_.at(4)->setAccelY(0.3f);
|
|
|
|
bitmaps_.at(5)->setPosX(param.game.width);
|
|
bitmaps_.at(5)->setPosY(param.game.game_area.first_quarter_y - (BITMAP_HEIGHT / 4));
|
|
bitmaps_.at(5)->setVelX(-0.7f);
|
|
bitmaps_.at(5)->setVelY(0.0f);
|
|
bitmaps_.at(5)->setAccelX(0.0f);
|
|
bitmaps_.at(5)->setAccelY(0.0f);
|
|
|
|
// Crea la textura para la sombra
|
|
auto shadow_texture = std::make_shared<Texture>(Screen::get()->getRenderer());
|
|
shadow_texture->createBlank(BITMAP_WIDTH, BITMAP_HEIGHT, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET);
|
|
shadow_texture->setBlendMode(SDL_BLENDMODE_BLEND);
|
|
auto temp = SDL_GetRenderTarget(Screen::get()->getRenderer());
|
|
shadow_texture->setAsRenderTarget(Screen::get()->getRenderer());
|
|
SDL_SetRenderDrawColor(Screen::get()->getRenderer(), 0x54, 0x31, 0x49, 0xFF);
|
|
SDL_RenderClear(Screen::get()->getRenderer());
|
|
SDL_SetRenderTarget(Screen::get()->getRenderer(), temp);
|
|
|
|
// Inicializa el sprite para la sombra
|
|
shadow_sprite_ = std::make_unique<Sprite>(shadow_texture, bitmaps_.front()->getRect());
|
|
}
|
|
|
|
// Inicializa los textos
|
|
void Intro::initTexts()
|
|
{
|
|
constexpr int TOTAL_TEXTS = 9;
|
|
for (int i = 0; i < TOTAL_TEXTS; ++i)
|
|
{
|
|
auto w = std::make_unique<Writer>(Resource::get()->getText("04b_25_metal"));
|
|
w->setPosX(BLOCK * 0);
|
|
w->setPosY(param.game.height - (BLOCK * 6));
|
|
w->setKerning(-2);
|
|
w->setEnabled(false);
|
|
w->setFinishedCounter(180);
|
|
texts_.push_back(std::move(w));
|
|
}
|
|
|
|
// Un dia qualsevol de l'any 2000
|
|
texts_.at(0)->setCaption(lang::getText(27));
|
|
texts_.at(0)->setSpeed(8);
|
|
|
|
// Tot esta tranquil a la UPV
|
|
texts_.at(1)->setCaption(lang::getText(28));
|
|
texts_.at(1)->setSpeed(8);
|
|
|
|
// Fins que un desaprensiu...
|
|
texts_.at(2)->setCaption(lang::getText(29));
|
|
texts_.at(2)->setSpeed(12);
|
|
|
|
// HEY! ME ANE A FERME UN CORTAET...
|
|
texts_.at(3)->setCaption(lang::getText(30));
|
|
texts_.at(3)->setSpeed(8);
|
|
|
|
// UAAAAAAAAAAAAA!!!
|
|
texts_.at(4)->setCaption(lang::getText(31));
|
|
texts_.at(4)->setSpeed(1);
|
|
|
|
// Espera un moment...
|
|
texts_.at(5)->setCaption(lang::getText(32));
|
|
texts_.at(5)->setSpeed(16);
|
|
|
|
// Si resulta que no tinc solt!
|
|
texts_.at(6)->setCaption(lang::getText(33));
|
|
texts_.at(6)->setSpeed(2);
|
|
|
|
// MERDA DE MAQUINA!
|
|
texts_.at(7)->setCaption(lang::getText(34));
|
|
texts_.at(7)->setSpeed(3);
|
|
|
|
// Blop... blop... blop...
|
|
texts_.at(8)->setCaption(lang::getText(35));
|
|
texts_.at(8)->setSpeed(16);
|
|
|
|
for (auto &text : texts_)
|
|
{
|
|
text->center(param.game.game_area.center_x);
|
|
}
|
|
}
|
|
|
|
// Actualiza los sprites
|
|
void Intro::updateSprites()
|
|
{
|
|
for (auto &bitmap : bitmaps_)
|
|
{
|
|
bitmap->update();
|
|
}
|
|
const int X = static_cast<int>(bitmaps_.at(scene_)->getPosX()) + 2;
|
|
const int Y = static_cast<int>(bitmaps_.at(scene_)->getPosY()) + 2;
|
|
shadow_sprite_->setPosition(X, Y);
|
|
}
|
|
|
|
// Actualiza los textos
|
|
void Intro::updateTexts()
|
|
{
|
|
for (auto &text : texts_)
|
|
{
|
|
text->update();
|
|
}
|
|
}
|
|
|
|
// Dibuja los sprites
|
|
void Intro::renderSprites()
|
|
{
|
|
shadow_sprite_->render();
|
|
bitmaps_.at(scene_)->render();
|
|
}
|
|
|
|
// Dibuja los textos
|
|
void Intro::renderTexts()
|
|
{
|
|
for (const auto &text : texts_)
|
|
{
|
|
text->render();
|
|
}
|
|
} |