Compare commits

..

2 Commits

4 changed files with 68 additions and 10 deletions

45
source/cooldown.hpp Normal file
View File

@@ -0,0 +1,45 @@
#pragma once
class Cooldown {
public:
Cooldown(float first_delay_s = 0.0f, float repeat_delay_s = 0.0f)
: first_delay_s_(first_delay_s), repeat_delay_s_(repeat_delay_s),
remaining_s_(0.0f), held_before_(false) {}
// Llamar cada frame con delta en segundos (float)
void update(float delta_s) {
if (remaining_s_ <= 0.0f) {
remaining_s_ = 0.0f;
return;
}
remaining_s_ -= delta_s;
if (remaining_s_ < 0.0f) remaining_s_ = 0.0f;
}
// Llamar cuando el input está activo. Devuelve true si debe ejecutarse la acción ahora.
bool tryConsumeOnHeld() {
if (remaining_s_ > 0.0f) return false;
float delay = held_before_ ? repeat_delay_s_ : first_delay_s_;
remaining_s_ = delay;
held_before_ = true;
return true;
}
// Llamar cuando el input se suelta
void onReleased() {
held_before_ = false;
remaining_s_ = 0.0f;
}
bool empty() const { return remaining_s_ == 0.0f; }
// Fuerza un valor en segundos (útil para tests o resets)
void forceSet(float seconds) { remaining_s_ = seconds > 0.0f ? seconds : 0.0f; }
private:
float first_delay_s_;
float repeat_delay_s_;
float remaining_s_;
bool held_before_;
};

View File

@@ -9,6 +9,7 @@
#include "animated_sprite.hpp" // Para AnimatedSprite
#include "asset.hpp" // Para Asset
#include "audio.hpp" // Para Audio
#include "cooldown.hpp" // Para Cooldown
#include "input.hpp" // Para Input
#include "input_types.hpp" // Para InputAction
#include "manage_hiscore_table.hpp" // Para ManageHiScoreTable, HiScoreEntry
@@ -22,6 +23,7 @@ Player::Player(const Config& config)
: player_sprite_(std::make_unique<AnimatedSprite>(config.texture.at(0), config.animations.at(0))),
power_sprite_(std::make_unique<AnimatedSprite>(config.texture.at(4), config.animations.at(1))),
enter_name_(std::make_unique<EnterName>()),
cooldown_(std::make_unique<Cooldown>(0.300F, 0.140F)),
hi_score_table_(config.hi_score_table),
glowing_entry_(config.glowing_entry),
stage_info_(config.stage_info),
@@ -150,15 +152,19 @@ void Player::setInputEnteringName(Input::Action action) {
}
break;
case Input::Action::RIGHT:
if (!enter_name_->nameIsFull()) {
enter_name_->incIndex();
playSound("service_menu_move.wav");
if (!isShowingName() && !enter_name_->nameIsFull()) {
if (cooldown_->tryConsumeOnHeld()) {
enter_name_->incIndex();
playSound("service_menu_move.wav");
}
}
break;
case Input::Action::LEFT:
if (!enter_name_->nameIsFull()) {
enter_name_->decIndex();
playSound("service_menu_move.wav");
if (!isShowingName() && !enter_name_->nameIsFull()) {
if (cooldown_->tryConsumeOnHeld()) {
enter_name_->decIndex();
playSound("service_menu_move.wav");
}
}
break;
case Input::Action::START:
@@ -171,6 +177,7 @@ void Player::setInputEnteringName(Input::Action action) {
}
break;
default:
cooldown_->onReleased();
break;
}
name_entry_idle_time_accumulator_ = 0.0F;
@@ -543,6 +550,7 @@ void Player::update(float delta_time) {
updateContinueCounter(delta_time); // Sistema de continue
updateEnterNameCounter(delta_time); // Sistema de name entry
updateShowingName(delta_time); // Sistema de showing name
cooldown_->update(delta_time);
}
void Player::passShowingName() {

View File

@@ -9,8 +9,9 @@
#include <utility> // Para move, pair
#include <vector> // Para vector
#include "animated_sprite.hpp" // for AnimatedSprite
#include "bullet.hpp" // for Bullet
#include "animated_sprite.hpp" // for AnimatedSprite
#include "bullet.hpp" // for Bullet
#include "cooldown.hpp"
#include "enter_name.hpp" // for EnterName
#include "input.hpp" // for Input
#include "manage_hiscore_table.hpp" // for Table
@@ -249,6 +250,7 @@ class Player {
std::unique_ptr<AnimatedSprite> player_sprite_; // Sprite para dibujar el jugador
std::unique_ptr<AnimatedSprite> power_sprite_; // Sprite para dibujar el aura del jugador con el poder a tope
std::unique_ptr<EnterName> enter_name_; // Clase utilizada para introducir el nombre
std::unique_ptr<Cooldown> cooldown_ = nullptr; // Objeto para gestionar cooldowns de teclado
std::shared_ptr<Input::Gamepad> gamepad_ = nullptr; // Dispositivo asociado
Table* hi_score_table_ = nullptr; // Tabla de máximas puntuaciones
int* glowing_entry_ = nullptr; // Entrada de la tabla de puntuaciones para hacerla brillar

View File

@@ -1448,12 +1448,12 @@ void Game::handleNameInput(const std::shared_ptr<Player>& player) {
return;
}
if (input_->checkAction(Input::Action::LEFT, Input::DO_NOT_ALLOW_REPEAT, player->getUsesKeyboard(), player->getGamepad())) {
if (input_->checkAction(Input::Action::LEFT, Input::ALLOW_REPEAT, player->getUsesKeyboard(), player->getGamepad())) {
player->setInput(Input::Action::LEFT);
return;
}
if (input_->checkAction(Input::Action::RIGHT, Input::DO_NOT_ALLOW_REPEAT, player->getUsesKeyboard(), player->getGamepad())) {
if (input_->checkAction(Input::Action::RIGHT, Input::ALLOW_REPEAT, player->getUsesKeyboard(), player->getGamepad())) {
player->setInput(Input::Action::RIGHT);
return;
}
@@ -1461,7 +1461,10 @@ void Game::handleNameInput(const std::shared_ptr<Player>& player) {
if (input_->checkAction(Input::Action::START, Input::DO_NOT_ALLOW_REPEAT, player->getUsesKeyboard(), player->getGamepad())) {
player->setInput(Input::Action::START);
updateHiScoreName();
return;
}
player->setInput(Input::Action::NONE);
}
// Inicializa las variables para el modo DEMO