Files
coffee_crisis_arcade_edition/source/intro.cpp

432 lines
10 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 "mouse.h"
#include <array>
// Constructor
Intro::Intro()
{
// Inicializa variables
section::name = section::Name::INTRO;
section::options = section::Options::NONE;
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);
// Inicializa los textos de la intro
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);
}
}
// 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;
section::options = section::Options::QUIT_FROM_EVENT;
break;
}
case SDL_WINDOWEVENT:
{
if (event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED)
{
// reloadTextures();
}
break;
}
default:
break;
}
// Comprueba el cursor
Mouse::handleEvent(event);
}
}
// 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 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()
{
constexpr int TICKS_SPEED = 15;
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();
// 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 objetos
for (const auto &bitmap : bitmaps_)
{
bitmap->render();
}
for (const auto &text : texts_)
{
text->render();
}
// 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();
}
}