migracio de Title a time based
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user