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]);
|
||||
|
||||
Reference in New Issue
Block a user