forked from jaildesigner-jailgames/jaildoctors_dilemma
migracio de Title a time based
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
#include "game/scene_manager.hpp" // Para SceneManager
|
||||
|
||||
#include "game/scenes/game.hpp"
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
@@ -14,17 +12,18 @@
|
||||
#include "core/resources/asset.hpp" // Para Asset
|
||||
#include "core/resources/resource.hpp" // Para ResourceRoom, Resource
|
||||
#include "core/system/debug.hpp" // Para Debug
|
||||
#include "core/system/global_events.hpp" // Para check
|
||||
#include "external/jail_audio.h" // Para JA_PauseMusic, JA_GetMusicState, JA_P...
|
||||
#include "game/gameplay/cheevos.hpp" // Para Cheevos
|
||||
#include "game/gameplay/item_tracker.hpp" // Para ItemTracker
|
||||
#include "game/options.hpp" // Para Options, options, Cheat, SectionState
|
||||
#include "game/gameplay/room.hpp" // Para Room, RoomData
|
||||
#include "game/gameplay/room_tracker.hpp" // Para RoomTracker
|
||||
#include "game/gameplay/scoreboard.hpp" // Para ScoreboardData, Scoreboard
|
||||
#include "game/gameplay/stats.hpp" // Para Stats
|
||||
#include "game/options.hpp" // Para Options, options, Cheat, SectionState
|
||||
#include "game/scene_manager.hpp" // Para SceneManager
|
||||
#include "game/ui/notifier.hpp" // Para Notifier, NotificationText, CHEEVO_NO...
|
||||
#include "utils/defines.hpp" // Para BLOCK, PLAY_AREA_HEIGHT, RoomBorder::BOTTOM
|
||||
#include "core/system/global_events.hpp" // Para check
|
||||
#include "utils/utils.hpp" // Para PaletteColor, stringToColor
|
||||
|
||||
// Constructor
|
||||
@@ -34,7 +33,7 @@ Game::Game(GameMode mode)
|
||||
room_tracker_(std::make_shared<RoomTracker>()),
|
||||
stats_(std::make_shared<Stats>(Asset::get()->get("stats.csv"), Asset::get()->get("stats_buffer.csv"))),
|
||||
mode_(mode),
|
||||
#ifdef DEBUG
|
||||
#ifdef _DEBUG
|
||||
current_room_("03.room"),
|
||||
spawn_point_(PlayerSpawn(25 * BLOCK, 13 * BLOCK, 0, 0, 0, PlayerState::STANDING, SDL_FLIP_HORIZONTAL))
|
||||
#else
|
||||
@@ -42,7 +41,7 @@ Game::Game(GameMode mode)
|
||||
spawn_point_(PlayerSpawn(25 * BLOCK, 13 * BLOCK, 0, 0, 0, PlayerState::STANDING, SDL_FLIP_HORIZONTAL))
|
||||
#endif
|
||||
{
|
||||
#ifdef DEBUG
|
||||
#ifdef _DEBUG
|
||||
Debug::get()->setEnabled(false);
|
||||
#endif
|
||||
|
||||
@@ -73,7 +72,7 @@ void Game::checkEvents() {
|
||||
SDL_Event event;
|
||||
while (SDL_PollEvent(&event)) {
|
||||
globalEvents::check(event);
|
||||
#ifdef DEBUG
|
||||
#ifdef _DEBUG
|
||||
checkDebugEvents(event);
|
||||
#endif
|
||||
}
|
||||
@@ -123,7 +122,7 @@ void Game::update() {
|
||||
// Comprueba el teclado
|
||||
checkInput();
|
||||
|
||||
#ifdef DEBUG
|
||||
#ifdef _DEBUG
|
||||
Debug::get()->clear();
|
||||
#endif
|
||||
|
||||
@@ -147,7 +146,7 @@ void Game::update() {
|
||||
|
||||
Screen::get()->update();
|
||||
|
||||
#ifdef DEBUG
|
||||
#ifdef _DEBUG
|
||||
updateDebugInfo();
|
||||
#endif
|
||||
}
|
||||
@@ -169,7 +168,7 @@ void Game::render() {
|
||||
scoreboard_->render();
|
||||
renderBlackScreen();
|
||||
|
||||
#ifdef DEBUG
|
||||
#ifdef _DEBUG
|
||||
// Debug info
|
||||
renderDebugInfo();
|
||||
#endif
|
||||
@@ -178,7 +177,7 @@ void Game::render() {
|
||||
Screen::get()->render();
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
#ifdef _DEBUG
|
||||
// Pasa la información de debug
|
||||
void Game::updateDebugInfo() {
|
||||
Debug::get()->add("X = " + std::to_string(static_cast<int>(player_->x_)) + ", Y = " + std::to_string(static_cast<int>(player_->y_)));
|
||||
|
||||
@@ -74,7 +74,7 @@ class Game {
|
||||
// Comprueba los eventos de la cola
|
||||
void checkEvents();
|
||||
|
||||
#ifdef DEBUG
|
||||
#ifdef _DEBUG
|
||||
// Pone la información de debug en pantalla
|
||||
void updateDebugInfo();
|
||||
|
||||
|
||||
@@ -24,7 +24,11 @@ Title::Title()
|
||||
title_logo_sprite_(std::make_shared<SurfaceSprite>(title_logo_surface_, 29, 9, title_logo_surface_->getWidth(), title_logo_surface_->getHeight())),
|
||||
loading_screen_surface_(Resource::get()->getSurface("loading_screen_color.gif")),
|
||||
loading_screen_sprite_(std::make_shared<SurfaceSprite>(loading_screen_surface_, 0, 0, loading_screen_surface_->getWidth(), loading_screen_surface_->getHeight())),
|
||||
bg_surface_(std::make_shared<Surface>(Options::game.width, Options::game.height)) {
|
||||
bg_surface_(std::make_shared<Surface>(Options::game.width, Options::game.height)),
|
||||
delta_timer_(std::make_unique<DeltaTimer>()),
|
||||
state_time_(0.0f),
|
||||
fade_accumulator_(0.0f),
|
||||
current_delta_(0.0f) {
|
||||
// Inicializa variables
|
||||
state_ = SceneManager::options == SceneManager::Options::TITLE_WITH_LOADING_SCREEN ? TitleState::SHOW_LOADING_SCREEN : TitleState::SHOW_MENU;
|
||||
SceneManager::current = SceneManager::Scene::TITLE;
|
||||
@@ -51,7 +55,7 @@ void Title::initMarquee() {
|
||||
for (int i = 0; i < (int)long_text_.length(); ++i) {
|
||||
TitleLetter l;
|
||||
l.letter = long_text_.substr(i, 1);
|
||||
l.x = 256;
|
||||
l.x = 256.0f;
|
||||
l.enabled = false;
|
||||
letters_.push_back(l);
|
||||
}
|
||||
@@ -89,18 +93,17 @@ void Title::checkEvents() {
|
||||
void Title::checkInput() {
|
||||
if (show_cheevos_) {
|
||||
if (Input::get()->checkInput(InputAction::DOWN, INPUT_ALLOW_REPEAT)) {
|
||||
moveCheevosList(1);
|
||||
moveCheevosList(1, current_delta_);
|
||||
} else if (Input::get()->checkInput(InputAction::UP, INPUT_ALLOW_REPEAT)) {
|
||||
moveCheevosList(0);
|
||||
moveCheevosList(0, current_delta_);
|
||||
} else if (Input::get()->checkInput(InputAction::ACCEPT, INPUT_DO_NOT_ALLOW_REPEAT)) {
|
||||
hideCheevosList();
|
||||
counter_ = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (Input::get()->checkInput(InputAction::ACCEPT, INPUT_DO_NOT_ALLOW_REPEAT)) {
|
||||
if (state_ == TitleState::SHOW_LOADING_SCREEN) {
|
||||
state_ = TitleState::FADE_LOADING_SCREEN;
|
||||
transitionToState(TitleState::FADE_LOADING_SCREEN);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -108,25 +111,26 @@ void Title::checkInput() {
|
||||
}
|
||||
|
||||
// Actualiza la marquesina
|
||||
void Title::updateMarquee() {
|
||||
void Title::updateMarquee(float delta_time) {
|
||||
const auto TEXT = Resource::get()->getText("gauntlet");
|
||||
const float displacement = MARQUEE_SPEED * delta_time;
|
||||
|
||||
for (int i = 0; i < (int)letters_.size(); ++i) {
|
||||
if (letters_[i].enabled) {
|
||||
letters_[i].x -= marquee_speed_;
|
||||
if (letters_[i].x < -10) {
|
||||
letters_[i].x -= displacement;
|
||||
if (letters_[i].x < -10.0f) {
|
||||
letters_[i].enabled = false;
|
||||
}
|
||||
} else {
|
||||
if (i > 0 && letters_[i - 1].x < 256 && letters_[i - 1].enabled) {
|
||||
if (i > 0 && letters_[i - 1].x < 256.0f && letters_[i - 1].enabled) {
|
||||
letters_[i].enabled = true;
|
||||
letters_[i].x = letters_[i - 1].x + TEXT->lenght(letters_[i - 1].letter) + 1;
|
||||
letters_[i].x = letters_[i - 1].x + TEXT->lenght(letters_[i - 1].letter) + 1.0f;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Comprueba si ha terminado la marquesina y la reinicia
|
||||
if (letters_[letters_.size() - 1].x < -10) {
|
||||
if (letters_[letters_.size() - 1].x < -10.0f) {
|
||||
// Inicializa la marquesina
|
||||
initMarquee();
|
||||
}
|
||||
@@ -144,55 +148,63 @@ void Title::renderMarquee() {
|
||||
|
||||
// Actualiza las variables
|
||||
void Title::update() {
|
||||
// Comprueba que la diferencia de ticks sea mayor a la velocidad del juego
|
||||
if (SDL_GetTicks() - ticks_ > GAME_SPEED) {
|
||||
// Actualiza el contador de ticks
|
||||
ticks_ = SDL_GetTicks();
|
||||
// Obtener delta time
|
||||
current_delta_ = delta_timer_->tick();
|
||||
|
||||
// Comprueba las entradas
|
||||
checkInput();
|
||||
// Comprueba las entradas
|
||||
checkInput();
|
||||
|
||||
Screen::get()->update();
|
||||
// Actualiza la pantalla
|
||||
Screen::get()->update();
|
||||
|
||||
// Incrementa el contador
|
||||
counter_++;
|
||||
// Actualiza el estado actual
|
||||
updateState(current_delta_);
|
||||
}
|
||||
|
||||
switch (state_) {
|
||||
case TitleState::SHOW_LOADING_SCREEN:
|
||||
if (counter_ == 500) {
|
||||
counter_ = 0;
|
||||
state_ = TitleState::FADE_LOADING_SCREEN;
|
||||
// Actualiza el estado actual
|
||||
void Title::updateState(float delta_time) {
|
||||
state_time_ += delta_time;
|
||||
|
||||
switch (state_) {
|
||||
case TitleState::SHOW_LOADING_SCREEN:
|
||||
if (state_time_ >= SHOW_LOADING_DURATION) {
|
||||
transitionToState(TitleState::FADE_LOADING_SCREEN);
|
||||
}
|
||||
break;
|
||||
|
||||
case TitleState::FADE_LOADING_SCREEN:
|
||||
fade_accumulator_ += delta_time;
|
||||
if (fade_accumulator_ >= FADE_STEP_INTERVAL) {
|
||||
fade_accumulator_ = 0.0f;
|
||||
if (loading_screen_surface_->fadeSubPalette()) {
|
||||
transitionToState(TitleState::SHOW_MENU);
|
||||
}
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
case TitleState::FADE_LOADING_SCREEN:
|
||||
if (counter_ % 4 == 0) {
|
||||
if (loading_screen_surface_->fadeSubPalette()) {
|
||||
counter_ = 0;
|
||||
state_ = TitleState::SHOW_MENU;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case TitleState::SHOW_MENU:
|
||||
// Actualiza la marquesina
|
||||
updateMarquee(delta_time);
|
||||
|
||||
case TitleState::SHOW_MENU:
|
||||
// Actualiza la marquesina
|
||||
updateMarquee();
|
||||
// Si el tiempo alcanza el timeout, va a créditos
|
||||
if (state_time_ >= AUTO_CREDITS_TIMEOUT && !show_cheevos_) {
|
||||
SceneManager::current = SceneManager::Scene::CREDITS;
|
||||
SceneManager::options = SceneManager::Options::NONE;
|
||||
}
|
||||
break;
|
||||
|
||||
// Si el contador alcanza cierto valor, termina la seccion
|
||||
if (counter_ == 2200) {
|
||||
if (!show_cheevos_) {
|
||||
SceneManager::current = SceneManager::Scene::CREDITS;
|
||||
SceneManager::options = SceneManager::Options::NONE;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Transiciona a un nuevo estado
|
||||
void Title::transitionToState(TitleState new_state) {
|
||||
state_ = new_state;
|
||||
state_time_ = 0.0f;
|
||||
fade_accumulator_ = 0.0f;
|
||||
}
|
||||
|
||||
// Dibuja en pantalla
|
||||
void Title::render() {
|
||||
// Prepara para empezar a dibujar en la textura de juego
|
||||
@@ -237,10 +249,14 @@ void Title::run() {
|
||||
}
|
||||
|
||||
// Desplaza la lista de logros
|
||||
void Title::moveCheevosList(int direction) {
|
||||
void Title::moveCheevosList(int direction, float delta_time) {
|
||||
// Calcula el desplazamiento basado en tiempo
|
||||
const float displacement = CHEEVOS_SCROLL_SPEED * delta_time;
|
||||
|
||||
// Modifica la posición de la ventana de vista
|
||||
constexpr int SPEED = 2;
|
||||
cheevos_surface_view_.y = direction == 0 ? cheevos_surface_view_.y - SPEED : cheevos_surface_view_.y + SPEED;
|
||||
cheevos_surface_view_.y = direction == 0
|
||||
? cheevos_surface_view_.y - displacement
|
||||
: cheevos_surface_view_.y + displacement;
|
||||
|
||||
// Ajusta los limites
|
||||
const float BOTTOM = cheevos_surface_->getHeight() - cheevos_surface_view_.h;
|
||||
|
||||
@@ -2,17 +2,19 @@
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <memory> // Para shared_ptr
|
||||
#include <string> // Para string
|
||||
#include <vector> // Para vector
|
||||
class SurfaceSprite; // lines 9-9
|
||||
class Surface; // lines 10-10
|
||||
#include <memory> // Para shared_ptr
|
||||
#include <string> // Para string
|
||||
#include <vector> // Para vector
|
||||
|
||||
#include "utils/delta_timer.hpp" // Para DeltaTimer
|
||||
class SurfaceSprite; // Forward declaration
|
||||
class Surface; // Forward declaration
|
||||
|
||||
class Title {
|
||||
private:
|
||||
struct TitleLetter {
|
||||
std::string letter; // Letra a escribir
|
||||
int x; // Posición en el eje x
|
||||
float x; // Posición en el eje x (float para precisión con delta time)
|
||||
bool enabled; // Solo se escriben y mueven si estan habilitadas
|
||||
};
|
||||
|
||||
@@ -22,6 +24,13 @@ class Title {
|
||||
SHOW_MENU
|
||||
};
|
||||
|
||||
// --- Constantes de tiempo (en segundos) ---
|
||||
static constexpr float SHOW_LOADING_DURATION = 5.0f; // Tiempo mostrando loading screen (antes 500 frames)
|
||||
static constexpr float FADE_STEP_INTERVAL = 0.033f; // Intervalo entre pasos de fade (antes cada 4 frames)
|
||||
static constexpr float AUTO_CREDITS_TIMEOUT = 22.0f; // Timeout para ir a créditos (antes 2200 frames)
|
||||
static constexpr float MARQUEE_SPEED = 100.0f; // Velocidad de marquesina (pixels/segundo)
|
||||
static constexpr float CHEEVOS_SCROLL_SPEED = 120.0f; // Velocidad de scroll de logros (pixels/segundo)
|
||||
|
||||
// Objetos y punteros
|
||||
std::shared_ptr<Surface> title_logo_surface_; // Textura con los graficos
|
||||
std::shared_ptr<SurfaceSprite> title_logo_sprite_; // SSprite para manejar la surface
|
||||
@@ -31,39 +40,28 @@ class Title {
|
||||
std::shared_ptr<Surface> cheevos_surface_; // Textura con la lista de logros
|
||||
std::shared_ptr<SurfaceSprite> cheevos_sprite_; // SSprite para manejar la surface con la lista de logros
|
||||
|
||||
// Variables
|
||||
int counter_ = 0; // Contador
|
||||
std::string long_text_; // Texto que aparece en la parte inferior del titulo
|
||||
Uint32 ticks_ = 0; // Contador de ticks para ajustar la velocidad del programa
|
||||
std::vector<TitleLetter> letters_; // Vector con las letras de la marquesina
|
||||
int marquee_speed_ = 2; // Velocidad de desplazamiento de la marquesina
|
||||
bool show_cheevos_ = false; // Indica si se muestra por pantalla el listado de logros
|
||||
SDL_FRect cheevos_surface_view_; // Zona visible de la surface con el listado de logros
|
||||
TitleState state_; // Estado en el que se encuentra el bucle principal
|
||||
// --- Variables de estado ---
|
||||
std::unique_ptr<DeltaTimer> delta_timer_; // Timer para delta time
|
||||
std::string long_text_; // Texto que aparece en la parte inferior del titulo
|
||||
std::vector<TitleLetter> letters_; // Vector con las letras de la marquesina
|
||||
bool show_cheevos_ = false; // Indica si se muestra por pantalla el listado de logros
|
||||
SDL_FRect cheevos_surface_view_; // Zona visible de la surface con el listado de logros
|
||||
TitleState state_; // Estado en el que se encuentra el bucle principal
|
||||
float state_time_; // Tiempo acumulado en el estado actual
|
||||
float fade_accumulator_; // Acumulador para controlar el fade por tiempo
|
||||
float current_delta_; // Delta time del frame actual
|
||||
|
||||
// Actualiza las variables
|
||||
void update();
|
||||
|
||||
// Dibuja en pantalla
|
||||
void render();
|
||||
|
||||
// Comprueba el manejador de eventos
|
||||
void checkEvents();
|
||||
|
||||
// Comprueba las entradas
|
||||
void checkInput();
|
||||
|
||||
// Inicializa la marquesina
|
||||
void initMarquee();
|
||||
|
||||
// Actualiza la marquesina
|
||||
void updateMarquee();
|
||||
|
||||
// Dibuja la marquesina
|
||||
void renderMarquee();
|
||||
|
||||
// Desplaza la lista de logros
|
||||
void moveCheevosList(int direction);
|
||||
// --- Funciones ---
|
||||
void update(); // Actualiza las variables
|
||||
void render(); // Dibuja en pantalla
|
||||
void checkEvents(); // Comprueba el manejador de eventos
|
||||
void checkInput(); // Comprueba las entradas
|
||||
void updateState(float delta_time); // Actualiza el estado actual
|
||||
void transitionToState(TitleState new_state); // Transiciona a un nuevo estado
|
||||
void initMarquee(); // Inicializa la marquesina
|
||||
void updateMarquee(float delta_time); // Actualiza la marquesina (time-based)
|
||||
void renderMarquee(); // Dibuja la marquesina
|
||||
void moveCheevosList(int direction, float delta_time); // Desplaza la lista de logros (time-based)
|
||||
|
||||
// Rellena la surface de fondo con todos los gráficos
|
||||
void fillSurface();
|
||||
|
||||
Reference in New Issue
Block a user