time-based: Player amb dual-API update/move/cooldown/counters(dt_s), base_speed=90 px/s, durades en s
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#include "game/entities/player.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath> // for fmod
|
||||
#include <cstdlib> // for rand
|
||||
|
||||
#include "core/input/input.h" // for InputAction
|
||||
@@ -42,12 +43,15 @@ void Player::init() {
|
||||
// Inicializa variables de estado
|
||||
alive_ = true;
|
||||
death_counter_ = DEATH_COUNTER;
|
||||
death_counter_s_ = DEATH_DURATION_S;
|
||||
status_walking_ = STATUS_WALKING_STOP;
|
||||
status_firing_ = STATUS_FIRING_NO;
|
||||
invulnerable_ = false;
|
||||
invulnerable_counter_ = INVULNERABLE_COUNTER;
|
||||
invulnerable_counter_s_ = INVULNERABLE_DURATION_S;
|
||||
power_up_ = false;
|
||||
power_up_counter_ = POWERUP_COUNTER;
|
||||
power_up_counter_s_ = POWERUP_DURATION_S;
|
||||
extra_hit_ = false;
|
||||
coffees_ = 0;
|
||||
input_ = true;
|
||||
@@ -65,9 +69,11 @@ void Player::init() {
|
||||
// Establece la velocidad inicial
|
||||
vel_x_ = 0;
|
||||
vel_y_ = 0;
|
||||
vel_x_s_ = 0.0F;
|
||||
|
||||
// Establece la velocidad base
|
||||
base_speed_ = 1.5;
|
||||
base_speed_s_ = BASE_SPEED_PX_PER_S;
|
||||
|
||||
// Establece la puntuación inicial
|
||||
score_ = 0;
|
||||
@@ -77,6 +83,7 @@ void Player::init() {
|
||||
|
||||
// Inicia el contador para la cadencia de disparo
|
||||
cooldown_ = 10;
|
||||
cooldown_s_ = COOLDOWN_S;
|
||||
|
||||
// Establece la posición del sprite
|
||||
legs_sprite_->setPosX(pos_x_);
|
||||
@@ -99,11 +106,13 @@ void Player::setInput(Input::Action input) {
|
||||
switch (input) {
|
||||
case Input::Action::LEFT:
|
||||
vel_x_ = -base_speed_;
|
||||
vel_x_s_ = -base_speed_s_;
|
||||
setWalkingStatus(STATUS_WALKING_LEFT);
|
||||
break;
|
||||
|
||||
case Input::Action::RIGHT:
|
||||
vel_x_ = base_speed_;
|
||||
vel_x_s_ = base_speed_s_;
|
||||
setWalkingStatus(STATUS_WALKING_RIGHT);
|
||||
break;
|
||||
|
||||
@@ -121,6 +130,7 @@ void Player::setInput(Input::Action input) {
|
||||
|
||||
default:
|
||||
vel_x_ = 0;
|
||||
vel_x_s_ = 0.0F;
|
||||
setWalkingStatus(STATUS_WALKING_STOP);
|
||||
break;
|
||||
}
|
||||
@@ -163,6 +173,37 @@ void Player::move() {
|
||||
}
|
||||
}
|
||||
|
||||
// Mueve el jugador a la posición y animación que le corresponde (time-based)
|
||||
void Player::move(float dt_s) {
|
||||
if (isAlive()) {
|
||||
pos_x_ += vel_x_s_ * dt_s;
|
||||
|
||||
if ((pos_x_ < PLAY_AREA_LEFT - 5) || (pos_x_ + width_ > PLAY_AREA_RIGHT + 5)) {
|
||||
pos_x_ -= vel_x_s_ * dt_s;
|
||||
}
|
||||
|
||||
legs_sprite_->setPosX(getPosX());
|
||||
legs_sprite_->setPosY(pos_y_);
|
||||
|
||||
body_sprite_->setPosX(getPosX());
|
||||
body_sprite_->setPosY(pos_y_);
|
||||
|
||||
head_sprite_->setPosX(getPosX());
|
||||
head_sprite_->setPosY(pos_y_);
|
||||
|
||||
fire_sprite_->setPosX(getPosX() - 2);
|
||||
fire_sprite_->setPosY(pos_y_ - 8);
|
||||
} else {
|
||||
death_sprite_->update(dt_s);
|
||||
|
||||
if ((death_sprite_->getPosX() < PLAY_AREA_LEFT) || (death_sprite_->getPosX() + width_ > PLAY_AREA_RIGHT)) {
|
||||
const float VX = death_sprite_->getVelX();
|
||||
death_sprite_->setPosX(death_sprite_->getPosX() - (VX * dt_s));
|
||||
death_sprite_->setVelX(-VX);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Pinta el jugador en pantalla
|
||||
void Player::render() {
|
||||
if (isAlive()) {
|
||||
@@ -268,6 +309,13 @@ auto Player::canFire() const -> bool {
|
||||
// Establece el valor de la variable
|
||||
void Player::setFireCooldown(int time) {
|
||||
cooldown_ = time;
|
||||
cooldown_s_ = static_cast<float>(time) / 60.0F;
|
||||
}
|
||||
|
||||
// Establece el valor del cooldown en segons (time-based)
|
||||
void Player::setFireCooldownS(float seconds) {
|
||||
cooldown_s_ = seconds;
|
||||
cooldown_ = static_cast<int>(seconds * 60.0F);
|
||||
}
|
||||
|
||||
// Actualiza el valor de la variable
|
||||
@@ -282,6 +330,18 @@ void Player::updateCooldown() {
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza el cooldown (time-based). Quan està en mode PowerUp, el cooldown
|
||||
// es consumeix el doble de ràpid (equivalent a decrementar 2 frames per tick).
|
||||
void Player::updateCooldown(float dt_s) {
|
||||
if (cooldown_s_ > 0.0F) {
|
||||
const float RATE = power_up_ ? 2.0F : 1.0F;
|
||||
cooldown_s_ = std::max(0.0F, cooldown_s_ - (dt_s * RATE));
|
||||
cooldown_ = static_cast<int>(cooldown_s_ * 60.0F);
|
||||
} else {
|
||||
setFiringStatus(STATUS_FIRING_NO);
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza al jugador a su posicion, animación y controla los contadores
|
||||
void Player::update() {
|
||||
move();
|
||||
@@ -294,6 +354,18 @@ void Player::update() {
|
||||
updatePowerUpHeadOffset();
|
||||
}
|
||||
|
||||
// Actualiza al jugador (time-based)
|
||||
void Player::update(float dt_s) {
|
||||
move(dt_s);
|
||||
setAnimation();
|
||||
shiftColliders();
|
||||
updateCooldown(dt_s);
|
||||
updatePowerUpCounter(dt_s);
|
||||
updateInvulnerableCounter(dt_s);
|
||||
updateDeathCounter(dt_s);
|
||||
updatePowerUpHeadOffset(dt_s);
|
||||
}
|
||||
|
||||
// Obtiene la puntuación del jugador
|
||||
auto Player::getScore() const -> Uint32 {
|
||||
return score_;
|
||||
@@ -321,6 +393,10 @@ void Player::setAlive(bool value) {
|
||||
if (!value) {
|
||||
death_sprite_->setPosX(head_sprite_->getRect().x);
|
||||
death_sprite_->setPosY(head_sprite_->getRect().y);
|
||||
// MovingSprite comparteix vx/vy/ax/ay entre frame-based i time-based.
|
||||
// De moment fixem els valors en px/frame perquè Game encara crida
|
||||
// Player::update() frame-based. Quan Game flippi a time-based caldrà
|
||||
// canviar a px/s (12.0 / -396.0 / +-198.0) — vegeu DEATH_*_PX_PER_S2/S.
|
||||
death_sprite_->setAccelY(0.2F);
|
||||
death_sprite_->setVelY(-6.6F);
|
||||
death_sprite_->setVelX(3.3F);
|
||||
@@ -376,6 +452,7 @@ auto Player::getInvulnerableCounter() const -> Uint16 {
|
||||
// Establece el valor de la variable
|
||||
void Player::setInvulnerableCounter(Uint16 value) {
|
||||
invulnerable_counter_ = value;
|
||||
invulnerable_counter_s_ = static_cast<float>(value) / 60.0F;
|
||||
}
|
||||
|
||||
// Actualiza el valor de la variable
|
||||
@@ -390,6 +467,21 @@ void Player::updateInvulnerableCounter() {
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza el contador d'invulnerabilitat (time-based). Manté el counter
|
||||
// enter sincronitzat perquè render() segueixi parpellejant igual.
|
||||
void Player::updateInvulnerableCounter(float dt_s) {
|
||||
if (invulnerable_) {
|
||||
if (invulnerable_counter_s_ > 0.0F) {
|
||||
invulnerable_counter_s_ = std::max(0.0F, invulnerable_counter_s_ - dt_s);
|
||||
invulnerable_counter_ = static_cast<Uint16>(invulnerable_counter_s_ * 60.0F);
|
||||
} else {
|
||||
invulnerable_ = false;
|
||||
invulnerable_counter_ = INVULNERABLE_COUNTER;
|
||||
invulnerable_counter_s_ = INVULNERABLE_DURATION_S;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza el valor de la variable
|
||||
void Player::updateDeathCounter() {
|
||||
if (!alive_) {
|
||||
@@ -399,6 +491,16 @@ void Player::updateDeathCounter() {
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza el comptador de mort (time-based)
|
||||
void Player::updateDeathCounter(float dt_s) {
|
||||
if (!alive_) {
|
||||
if (death_counter_s_ > 0.0F) {
|
||||
death_counter_s_ = std::max(0.0F, death_counter_s_ - dt_s);
|
||||
death_counter_ = static_cast<Uint16>(death_counter_s_ * 60.0F);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
auto Player::isPowerUp() const -> bool {
|
||||
return power_up_;
|
||||
@@ -417,6 +519,7 @@ auto Player::getPowerUpCounter() const -> Uint16 {
|
||||
// Establece el valor de la variable
|
||||
void Player::setPowerUpCounter(Uint16 value) {
|
||||
power_up_counter_ = value;
|
||||
power_up_counter_s_ = static_cast<float>(value) / 60.0F;
|
||||
}
|
||||
|
||||
// Actualiza el valor de la variable
|
||||
@@ -429,6 +532,18 @@ void Player::updatePowerUpCounter() {
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza el comptador de PowerUp (time-based)
|
||||
void Player::updatePowerUpCounter(float dt_s) {
|
||||
if ((power_up_counter_s_ > 0.0F) && (power_up_)) {
|
||||
power_up_counter_s_ = std::max(0.0F, power_up_counter_s_ - dt_s);
|
||||
power_up_counter_ = static_cast<Uint16>(power_up_counter_s_ * 60.0F);
|
||||
} else {
|
||||
power_up_ = false;
|
||||
power_up_counter_ = POWERUP_COUNTER;
|
||||
power_up_counter_s_ = POWERUP_DURATION_S;
|
||||
}
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
auto Player::hasExtraHit() const -> bool {
|
||||
return extra_hit_;
|
||||
@@ -451,6 +566,7 @@ void Player::removeExtraHit() {
|
||||
}
|
||||
invulnerable_ = true;
|
||||
invulnerable_counter_ = INVULNERABLE_COUNTER;
|
||||
invulnerable_counter_s_ = INVULNERABLE_DURATION_S;
|
||||
}
|
||||
|
||||
// Habilita la entrada de ordenes
|
||||
@@ -508,6 +624,16 @@ void Player::updatePowerUpHeadOffset() {
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza l'offset (time-based). dt_s no s'usa directament: el blink final
|
||||
// depèn de power_up_counter_s_ que ja s'està actualitzant a updatePowerUpCounter.
|
||||
void Player::updatePowerUpHeadOffset([[maybe_unused]] float dt_s) {
|
||||
if (!power_up_) { return; }
|
||||
if (power_up_counter_s_ < POWERUP_BLINK_THRESHOLD_S) {
|
||||
const float PHASE = std::fmod(power_up_counter_s_, BLINK_PERIOD_S);
|
||||
fire_sprite_->setEnabled(PHASE <= BLINK_OFF_S);
|
||||
}
|
||||
}
|
||||
|
||||
// Pone las texturas del jugador
|
||||
void Player::setPlayerTextures(const std::vector<Texture *> &texture) {
|
||||
head_sprite_->setTexture(texture[0]);
|
||||
|
||||
@@ -5,8 +5,8 @@
|
||||
#include <string> // for string
|
||||
#include <vector> // for vector
|
||||
|
||||
#include "utils/utils.h" // for Circle
|
||||
#include "core/input/input.h" // for Input::Action
|
||||
#include "utils/utils.h" // for Circle
|
||||
class AnimatedSprite;
|
||||
class Texture;
|
||||
|
||||
@@ -21,10 +21,12 @@ class Player {
|
||||
Player(const Player &) = delete;
|
||||
auto operator=(const Player &) -> Player & = delete;
|
||||
|
||||
void init(); // Iniciador
|
||||
void update(); // Actualiza al jugador a su posicion, animación y controla los contadores
|
||||
void render(); // Pinta el jugador en pantalla
|
||||
void move(); // Mueve el jugador a la posición y animación que le corresponde
|
||||
void init(); // Iniciador
|
||||
void update(); // Actualiza al jugador (frame-based)
|
||||
void update(float dt_s); // Actualiza al jugador (time-based)
|
||||
void render(); // Pinta el jugador en pantalla
|
||||
void move(); // Mueve el jugador (frame-based)
|
||||
void move(float dt_s); // Mueve el jugador (time-based)
|
||||
|
||||
void setPlayerTextures(const std::vector<Texture *> &texture); // Pone las texturas del jugador
|
||||
void setInput(Input::Action input); // Actua en consecuencia de la entrada recibida
|
||||
@@ -36,8 +38,10 @@ class Player {
|
||||
[[nodiscard]] auto getHeight() const -> int; // Obtiene el valor de la variable
|
||||
|
||||
[[nodiscard]] auto canFire() const -> bool; // Indica si el jugador puede disparar
|
||||
void setFireCooldown(int time); // Establece el valor de la variable
|
||||
void updateCooldown(); // Actualiza el valor de la variable
|
||||
void setFireCooldown(int time); // Establece el valor de la variable (frames)
|
||||
void setFireCooldownS(float seconds); // Establece el valor de la variable (segons)
|
||||
void updateCooldown(); // Actualiza el valor de la variable (frame-based)
|
||||
void updateCooldown(float dt_s); // Actualiza el valor de la variable (time-based)
|
||||
|
||||
[[nodiscard]] auto getScore() const -> Uint32; // Obtiene la puntuación del jugador
|
||||
void setScore(Uint32 score); // Asigna un valor a la puntuación del jugador
|
||||
@@ -60,7 +64,8 @@ class Player {
|
||||
void setPowerUp(bool value); // Establece el valor de la variable
|
||||
[[nodiscard]] auto getPowerUpCounter() const -> Uint16; // Obtiene el valor de la variable
|
||||
void setPowerUpCounter(Uint16 value); // Establece el valor de la variable
|
||||
void updatePowerUpCounter(); // Actualiza el valor de la variable
|
||||
void updatePowerUpCounter(); // Actualiza el valor de la variable (frame-based)
|
||||
void updatePowerUpCounter(float dt_s); // Actualiza el valor de la variable (time-based)
|
||||
|
||||
[[nodiscard]] auto hasExtraHit() const -> bool; // Obtiene el valor de la variable
|
||||
void giveExtraHit(); // Concede un toque extra al jugador
|
||||
@@ -89,6 +94,20 @@ class Player {
|
||||
static constexpr int INVULNERABLE_COUNTER = 200;
|
||||
static constexpr int POWERUP_COUNTER = 1500;
|
||||
|
||||
// Time-based: equivalents en segons/px·s a 60Hz (font de veritat).
|
||||
static constexpr float BASE_SPEED_PX_PER_S = 90.0F; // Era 1.5 px/frame
|
||||
static constexpr float COOLDOWN_S = 10.0F / 60.0F; // Era 10 frames
|
||||
static constexpr float INVULNERABLE_DURATION_S = 200.0F / 60.0F;
|
||||
static constexpr float POWERUP_DURATION_S = 1500.0F / 60.0F;
|
||||
static constexpr float DEATH_DURATION_S = 350.0F / 60.0F;
|
||||
static constexpr float POWERUP_BLINK_THRESHOLD_S = 300.0F / 60.0F; // Era power_up_counter_ < 300
|
||||
static constexpr float BLINK_PERIOD_S = 10.0F / 60.0F; // Era % 10
|
||||
static constexpr float BLINK_OFF_S = 4.0F / 60.0F; // Era % 10 > 4
|
||||
// Death sprite (cadàver) — physics convertides de px/frame a px/s.
|
||||
static constexpr float DEATH_ACCEL_Y_PX_PER_S2 = 0.2F * 60.0F * 60.0F; // = 720
|
||||
static constexpr float DEATH_VEL_Y_PX_PER_S = -6.6F * 60.0F; // = -396
|
||||
static constexpr float DEATH_VEL_X_PX_PER_S = 3.3F * 60.0F; // = 198
|
||||
|
||||
// Objetos y punteros
|
||||
SDL_Renderer *renderer_; // El renderizador de la ventana
|
||||
AnimatedSprite *head_sprite_; // Sprite para dibujar la cabeza
|
||||
@@ -104,11 +123,15 @@ class Player {
|
||||
Uint8 width_; // Anchura
|
||||
Uint8 height_; // Altura
|
||||
|
||||
float vel_x_; // Cantidad de pixeles a desplazarse en el eje X
|
||||
float vel_x_; // Cantidad de pixeles a desplazarse en el eje X (frame-based: px/frame)
|
||||
int vel_y_; // Cantidad de pixeles a desplazarse en el eje Y
|
||||
|
||||
float base_speed_; // Velocidad base del jugador
|
||||
int cooldown_; // Contador durante el cual no puede disparar
|
||||
float vel_x_s_{0.0F}; // Velocidad en el eje X (time-based: px/s)
|
||||
|
||||
float base_speed_; // Velocidad base del jugador (frame-based: px/frame)
|
||||
float base_speed_s_{0.0F}; // Velocidad base del jugador (time-based: px/s)
|
||||
int cooldown_; // Contador durante el cual no puede disparar (frames)
|
||||
float cooldown_s_{0.0F}; // Contador durante el cual no puede disparar (segons)
|
||||
|
||||
Uint32 score_; // Puntos del jugador
|
||||
float score_multiplier_; // Multiplicador de puntos
|
||||
@@ -116,22 +139,28 @@ class Player {
|
||||
Uint8 status_walking_; // Estado del jugador
|
||||
Uint8 status_firing_; // Estado del jugador
|
||||
|
||||
bool alive_; // Indica si el jugador está vivo
|
||||
Uint16 death_counter_; // Contador para la animación de morirse
|
||||
bool invulnerable_; // Indica si el jugador es invulnerable
|
||||
Uint16 invulnerable_counter_; // Contador para la invulnerabilidad
|
||||
bool extra_hit_; // Indica si el jugador tiene un toque extra
|
||||
Uint8 coffees_; // Indica cuantos cafes lleva acumulados
|
||||
bool power_up_; // Indica si el jugador tiene activo el modo PowerUp
|
||||
Uint16 power_up_counter_; // Temporizador para el modo PowerUp
|
||||
bool input_; // Indica si puede recibir ordenes de entrada
|
||||
Circle collider_; // Circulo de colisión del jugador
|
||||
bool alive_; // Indica si el jugador está vivo
|
||||
Uint16 death_counter_; // Contador (frame-based)
|
||||
float death_counter_s_{0.0F}; // Contador (time-based)
|
||||
bool invulnerable_; // Indica si el jugador es invulnerable
|
||||
Uint16 invulnerable_counter_; // Contador (frame-based)
|
||||
float invulnerable_counter_s_{0.0F}; // Contador (time-based)
|
||||
bool extra_hit_; // Indica si el jugador tiene un toque extra
|
||||
Uint8 coffees_; // Indica cuantos cafes lleva acumulados
|
||||
bool power_up_; // Indica si el jugador tiene activo el modo PowerUp
|
||||
Uint16 power_up_counter_; // Temporizador (frame-based)
|
||||
float power_up_counter_s_{0.0F}; // Temporizador (time-based)
|
||||
bool input_; // Indica si puede recibir ordenes de entrada
|
||||
Circle collider_; // Circulo de colisión del jugador
|
||||
|
||||
void setWalkingStatus(Uint8 status); // Establece el estado del jugador
|
||||
void setFiringStatus(Uint8 status); // Establece el estado del jugador
|
||||
|
||||
void shiftColliders(); // Actualiza el circulo de colisión a la posición del jugador
|
||||
void updateInvulnerableCounter(); // Actualiza el valor de la variable
|
||||
void updateDeathCounter(); // Actualiza el valor de la variable
|
||||
void updatePowerUpHeadOffset(); // Actualiza el valor de la variable
|
||||
void shiftColliders(); // Actualiza el circulo de colisión a la posición del jugador
|
||||
void updateInvulnerableCounter(); // Actualiza el valor de la variable (frame-based)
|
||||
void updateInvulnerableCounter(float dt_s); // Actualiza el valor de la variable (time-based)
|
||||
void updateDeathCounter(); // Actualiza el valor de la variable (frame-based)
|
||||
void updateDeathCounter(float dt_s); // Actualiza el valor de la variable (time-based)
|
||||
void updatePowerUpHeadOffset(); // Actualiza el valor de la variable (frame-based)
|
||||
void updatePowerUpHeadOffset(float dt_s); // Actualiza el valor de la variable (time-based)
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user