680 lines
20 KiB
C++
680 lines
20 KiB
C++
#include "game/entities/player.h"
|
|
|
|
#include <algorithm>
|
|
#include <cmath> // for fmod
|
|
#include <cstdlib> // for rand
|
|
|
|
#include "core/input/input.h" // for InputAction
|
|
#include "core/rendering/animatedsprite.h" // for AnimatedSprite
|
|
#include "core/rendering/texture.h" // for Texture
|
|
#include "game/defaults.hpp" // for PLAY_AREA_LEFT, PLAY_AREA_RIGHT
|
|
|
|
// Constructor
|
|
Player::Player(float x, int y, SDL_Renderer *renderer, const std::vector<Texture *> &texture, const std::vector<std::vector<std::string> *> &animations) {
|
|
// Copia los punteros
|
|
this->renderer_ = renderer;
|
|
|
|
// Reserva memoria para los objetos
|
|
head_sprite_ = new AnimatedSprite(texture[0], renderer, "", animations[0]);
|
|
body_sprite_ = new AnimatedSprite(texture[1], renderer, "", animations[1]);
|
|
legs_sprite_ = new AnimatedSprite(texture[2], renderer, "", animations[2]);
|
|
death_sprite_ = new AnimatedSprite(texture[3], renderer, "", animations[3]);
|
|
fire_sprite_ = new AnimatedSprite(texture[4], renderer, "", animations[4]);
|
|
fire_sprite_->getTexture()->setAlpha(224);
|
|
|
|
// Establece la posición inicial del jugador
|
|
pos_x_ = x;
|
|
pos_y_ = y;
|
|
|
|
init();
|
|
}
|
|
|
|
// Destructor
|
|
Player::~Player() {
|
|
delete head_sprite_;
|
|
delete body_sprite_;
|
|
delete legs_sprite_;
|
|
delete death_sprite_;
|
|
delete fire_sprite_;
|
|
}
|
|
|
|
// Iniciador
|
|
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;
|
|
|
|
// Establece la altura y el ancho del jugador
|
|
width_ = 24;
|
|
height_ = 24;
|
|
|
|
// Establece el tamaño del circulo de colisión
|
|
collider_.r = 7;
|
|
|
|
// Actualiza la posición del circulo de colisión
|
|
shiftColliders();
|
|
|
|
// 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;
|
|
|
|
// Establece el multiplicador de puntos inicial
|
|
score_multiplier_ = 1.0F;
|
|
|
|
// 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_);
|
|
legs_sprite_->setPosY(pos_y_);
|
|
|
|
body_sprite_->setPosX(pos_x_);
|
|
body_sprite_->setPosY(pos_y_);
|
|
|
|
head_sprite_->setPosX(pos_x_);
|
|
head_sprite_->setPosY(pos_y_);
|
|
|
|
// Selecciona un frame para pintar
|
|
legs_sprite_->setCurrentAnimation("stand");
|
|
body_sprite_->setCurrentAnimation("stand");
|
|
head_sprite_->setCurrentAnimation("stand");
|
|
}
|
|
|
|
// Actua en consecuencia de la entrada recibida
|
|
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;
|
|
|
|
case Input::Action::FIRE_CENTER:
|
|
setFiringStatus(STATUS_FIRING_UP);
|
|
break;
|
|
|
|
case Input::Action::FIRE_LEFT:
|
|
setFiringStatus(STATUS_FIRING_LEFT);
|
|
break;
|
|
|
|
case Input::Action::FIRE_RIGHT:
|
|
setFiringStatus(STATUS_FIRING_RIGHT);
|
|
break;
|
|
|
|
default:
|
|
vel_x_ = 0;
|
|
vel_x_s_ = 0.0F;
|
|
setWalkingStatus(STATUS_WALKING_STOP);
|
|
break;
|
|
}
|
|
}
|
|
|
|
// Mueve el jugador a la posición y animación que le corresponde
|
|
void Player::move() {
|
|
if (isAlive()) {
|
|
// Mueve el jugador a derecha o izquierda
|
|
pos_x_ += vel_x_;
|
|
|
|
// Si el jugador abandona el area de juego por los laterales
|
|
if ((pos_x_ < PLAY_AREA_LEFT - 5) || (pos_x_ + width_ > PLAY_AREA_RIGHT + 5)) { // Restaura su posición
|
|
pos_x_ -= vel_x_;
|
|
}
|
|
|
|
// Actualiza la posición del sprite
|
|
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();
|
|
|
|
// Si el cadaver abandona el area de juego por los laterales
|
|
if ((death_sprite_->getPosX() < PLAY_AREA_LEFT) || (death_sprite_->getPosX() + width_ > PLAY_AREA_RIGHT)) { // Restaura su posición
|
|
const float VX = death_sprite_->getVelX();
|
|
death_sprite_->setPosX(death_sprite_->getPosX() - VX);
|
|
|
|
// Rebota
|
|
death_sprite_->setVelX(-VX);
|
|
}
|
|
}
|
|
}
|
|
|
|
// 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()) {
|
|
if (invulnerable_) {
|
|
if ((invulnerable_counter_ % 10) > 4) {
|
|
if (power_up_) {
|
|
fire_sprite_->render();
|
|
}
|
|
legs_sprite_->render();
|
|
body_sprite_->render();
|
|
head_sprite_->render();
|
|
}
|
|
} else {
|
|
if (power_up_) {
|
|
fire_sprite_->render();
|
|
}
|
|
legs_sprite_->render();
|
|
body_sprite_->render();
|
|
head_sprite_->render();
|
|
}
|
|
} else {
|
|
death_sprite_->render();
|
|
}
|
|
}
|
|
|
|
// Establece el estado del jugador cuando camina
|
|
void Player::setWalkingStatus(Uint8 status) {
|
|
status_walking_ = status;
|
|
}
|
|
|
|
// Establece el estado del jugador cuando dispara
|
|
void Player::setFiringStatus(Uint8 status) {
|
|
status_firing_ = status;
|
|
}
|
|
|
|
// Establece la animación correspondiente al estado
|
|
void Player::setAnimation() {
|
|
// Crea cadenas de texto para componer el nombre de la animación
|
|
std::string body_coffees;
|
|
std::string head_coffees;
|
|
if (coffees_ > 0) {
|
|
body_coffees = coffees_ == 1 ? "_1C" : "_2C";
|
|
head_coffees = "_1C";
|
|
}
|
|
|
|
const std::string POWER_UP = power_up_ ? "_pwr" : "";
|
|
const std::string WALKING = status_walking_ == STATUS_WALKING_STOP ? "stand" : "walk";
|
|
const std::string FIRING = status_firing_ == STATUS_FIRING_UP ? "centershoot" : "sideshoot";
|
|
|
|
const SDL_FlipMode FLIP_WALK = status_walking_ == STATUS_WALKING_RIGHT ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE;
|
|
const SDL_FlipMode FLIP_FIRE = status_firing_ == STATUS_FIRING_RIGHT ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE;
|
|
|
|
// Establece la animación a partir de las cadenas
|
|
legs_sprite_->setCurrentAnimation(WALKING);
|
|
legs_sprite_->setFlip(FLIP_WALK);
|
|
if (status_firing_ == STATUS_FIRING_NO) { // No esta disparando
|
|
body_sprite_->setCurrentAnimation(WALKING + body_coffees + POWER_UP);
|
|
body_sprite_->setFlip(FLIP_WALK);
|
|
head_sprite_->setCurrentAnimation(WALKING + head_coffees + POWER_UP);
|
|
head_sprite_->setFlip(FLIP_WALK);
|
|
} else { // Está disparando
|
|
body_sprite_->setCurrentAnimation(FIRING + body_coffees + POWER_UP);
|
|
body_sprite_->setFlip(FLIP_FIRE);
|
|
head_sprite_->setCurrentAnimation(FIRING + head_coffees + POWER_UP);
|
|
head_sprite_->setFlip(FLIP_FIRE);
|
|
}
|
|
|
|
// Actualiza las animaciones de los sprites
|
|
legs_sprite_->animate();
|
|
body_sprite_->animate();
|
|
head_sprite_->animate();
|
|
|
|
fire_sprite_->animate();
|
|
fire_sprite_->setFlip(FLIP_WALK);
|
|
}
|
|
|
|
// Establece la animación correspondiente al estado (time-based)
|
|
void Player::setAnimation(float dt_s) {
|
|
std::string body_coffees;
|
|
std::string head_coffees;
|
|
if (coffees_ > 0) {
|
|
body_coffees = coffees_ == 1 ? "_1C" : "_2C";
|
|
head_coffees = "_1C";
|
|
}
|
|
|
|
const std::string POWER_UP = power_up_ ? "_pwr" : "";
|
|
const std::string WALKING = status_walking_ == STATUS_WALKING_STOP ? "stand" : "walk";
|
|
const std::string FIRING = status_firing_ == STATUS_FIRING_UP ? "centershoot" : "sideshoot";
|
|
|
|
const SDL_FlipMode FLIP_WALK = status_walking_ == STATUS_WALKING_RIGHT ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE;
|
|
const SDL_FlipMode FLIP_FIRE = status_firing_ == STATUS_FIRING_RIGHT ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE;
|
|
|
|
legs_sprite_->setCurrentAnimation(WALKING);
|
|
legs_sprite_->setFlip(FLIP_WALK);
|
|
if (status_firing_ == STATUS_FIRING_NO) {
|
|
body_sprite_->setCurrentAnimation(WALKING + body_coffees + POWER_UP);
|
|
body_sprite_->setFlip(FLIP_WALK);
|
|
head_sprite_->setCurrentAnimation(WALKING + head_coffees + POWER_UP);
|
|
head_sprite_->setFlip(FLIP_WALK);
|
|
} else {
|
|
body_sprite_->setCurrentAnimation(FIRING + body_coffees + POWER_UP);
|
|
body_sprite_->setFlip(FLIP_FIRE);
|
|
head_sprite_->setCurrentAnimation(FIRING + head_coffees + POWER_UP);
|
|
head_sprite_->setFlip(FLIP_FIRE);
|
|
}
|
|
|
|
legs_sprite_->animate(dt_s);
|
|
body_sprite_->animate(dt_s);
|
|
head_sprite_->animate(dt_s);
|
|
|
|
fire_sprite_->animate(dt_s);
|
|
fire_sprite_->setFlip(FLIP_WALK);
|
|
}
|
|
|
|
// Obtiene el valor de la variable
|
|
auto Player::getPosX() const -> int {
|
|
return int(pos_x_);
|
|
}
|
|
|
|
// Obtiene el valor de la variable
|
|
auto Player::getPosY() const -> int {
|
|
return pos_y_;
|
|
}
|
|
|
|
// Obtiene el valor de la variable
|
|
auto Player::getWidth() const -> int {
|
|
return width_;
|
|
}
|
|
|
|
// Obtiene el valor de la variable
|
|
auto Player::getHeight() const -> int {
|
|
return height_;
|
|
}
|
|
|
|
// Indica si el jugador puede disparar
|
|
auto Player::canFire() const -> bool {
|
|
// Si el contador a llegado a cero, podemos disparar. En caso contrario decrementamos el contador
|
|
return cooldown_ <= 0;
|
|
}
|
|
|
|
// 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
|
|
void Player::updateCooldown() {
|
|
if (cooldown_ > 0) {
|
|
cooldown_--;
|
|
if (power_up_) {
|
|
cooldown_--;
|
|
}
|
|
} else {
|
|
setFiringStatus(STATUS_FIRING_NO);
|
|
}
|
|
}
|
|
|
|
// 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();
|
|
setAnimation();
|
|
shiftColliders();
|
|
updateCooldown();
|
|
updatePowerUpCounter();
|
|
updateInvulnerableCounter();
|
|
updateDeathCounter();
|
|
updatePowerUpHeadOffset();
|
|
}
|
|
|
|
// Actualiza al jugador (time-based)
|
|
void Player::update(float dt_s) {
|
|
move(dt_s);
|
|
setAnimation(dt_s);
|
|
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_;
|
|
}
|
|
|
|
// Asigna un valor a la puntuación del jugador
|
|
void Player::setScore(Uint32 score) {
|
|
this->score_ = score;
|
|
}
|
|
|
|
// Incrementa la puntuación del jugador
|
|
void Player::addScore(Uint32 score) {
|
|
this->score_ += score;
|
|
}
|
|
|
|
// Obtiene el valor de la variable
|
|
auto Player::isAlive() const -> bool {
|
|
return alive_;
|
|
}
|
|
|
|
// Establece el valor de la variable
|
|
void Player::setAlive(bool value) {
|
|
alive_ = value;
|
|
|
|
if (!value) {
|
|
death_sprite_->setPosX(head_sprite_->getRect().x);
|
|
death_sprite_->setPosY(head_sprite_->getRect().y);
|
|
// Física del cadàver en px/s i px/s² — Game crida Player::update(dt_s)
|
|
// que delega a death_sprite_->update(dt_s) (time-based).
|
|
death_sprite_->setAccelY(DEATH_ACCEL_Y_PX_PER_S2);
|
|
death_sprite_->setVelY(DEATH_VEL_Y_PX_PER_S);
|
|
death_sprite_->setVelX(DEATH_VEL_X_PX_PER_S);
|
|
if (rand() % 2 == 0) {
|
|
death_sprite_->setVelX(-DEATH_VEL_X_PX_PER_S);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Obtiene el valor de la variable
|
|
auto Player::getScoreMultiplier() const -> float {
|
|
return score_multiplier_;
|
|
}
|
|
|
|
// Establece el valor de la variable
|
|
void Player::setScoreMultiplier(float value) {
|
|
score_multiplier_ = value;
|
|
}
|
|
|
|
// Aumenta el valor de la variable hasta un máximo
|
|
void Player::incScoreMultiplier() {
|
|
if (score_multiplier_ < 5.0F) {
|
|
score_multiplier_ += 0.1F;
|
|
} else {
|
|
score_multiplier_ = 5.0F;
|
|
}
|
|
}
|
|
|
|
// Decrementa el valor de la variable hasta un mínimo
|
|
void Player::decScoreMultiplier() {
|
|
if (score_multiplier_ > 1.0F) {
|
|
score_multiplier_ -= 0.1F;
|
|
} else {
|
|
score_multiplier_ = 1.0F;
|
|
}
|
|
}
|
|
|
|
// Obtiene el valor de la variable
|
|
auto Player::isInvulnerable() const -> bool {
|
|
return invulnerable_;
|
|
}
|
|
|
|
// Establece el valor de la variable
|
|
void Player::setInvulnerable(bool value) {
|
|
invulnerable_ = value;
|
|
}
|
|
|
|
// Obtiene el valor de la variable
|
|
auto Player::getInvulnerableCounter() const -> Uint16 {
|
|
return invulnerable_counter_;
|
|
}
|
|
|
|
// 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
|
|
void Player::updateInvulnerableCounter() {
|
|
if (invulnerable_) {
|
|
if (invulnerable_counter_ > 0) {
|
|
invulnerable_counter_--;
|
|
} else {
|
|
invulnerable_ = false;
|
|
invulnerable_counter_ = INVULNERABLE_COUNTER;
|
|
}
|
|
}
|
|
}
|
|
|
|
// 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_) {
|
|
if (death_counter_ > 0) {
|
|
death_counter_--;
|
|
}
|
|
}
|
|
}
|
|
|
|
// 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_;
|
|
}
|
|
|
|
// Establece el valor de la variable
|
|
void Player::setPowerUp(bool value) {
|
|
power_up_ = value;
|
|
}
|
|
|
|
// Obtiene el valor de la variable
|
|
auto Player::getPowerUpCounter() const -> Uint16 {
|
|
return power_up_counter_;
|
|
}
|
|
|
|
// 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
|
|
void Player::updatePowerUpCounter() {
|
|
if ((power_up_counter_ > 0) && (power_up_)) {
|
|
power_up_counter_--;
|
|
} else {
|
|
power_up_ = false;
|
|
power_up_counter_ = POWERUP_COUNTER;
|
|
}
|
|
}
|
|
|
|
// 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_;
|
|
}
|
|
|
|
// Concede un toque extra al jugador
|
|
void Player::giveExtraHit() {
|
|
extra_hit_ = true;
|
|
coffees_++;
|
|
coffees_ = std::min<int>(coffees_, 2);
|
|
}
|
|
|
|
// Quita el toque extra al jugador
|
|
void Player::removeExtraHit() {
|
|
if (coffees_ > 0) {
|
|
coffees_--;
|
|
}
|
|
if (coffees_ == 0) {
|
|
extra_hit_ = false;
|
|
}
|
|
invulnerable_ = true;
|
|
invulnerable_counter_ = INVULNERABLE_COUNTER;
|
|
invulnerable_counter_s_ = INVULNERABLE_DURATION_S;
|
|
}
|
|
|
|
// Habilita la entrada de ordenes
|
|
void Player::enableInput() {
|
|
input_ = true;
|
|
}
|
|
|
|
// Deshabilita la entrada de ordenes
|
|
void Player::disableInput() {
|
|
input_ = false;
|
|
}
|
|
|
|
// Devuelve el numero de cafes actuales
|
|
auto Player::getCoffees() const -> Uint8 {
|
|
return coffees_;
|
|
}
|
|
|
|
// Obtiene el circulo de colisión
|
|
auto Player::getCollider() -> Circle & {
|
|
return collider_;
|
|
}
|
|
|
|
// Actualiza el circulo de colisión a la posición del jugador
|
|
void Player::shiftColliders() {
|
|
collider_.x = int(pos_x_ + (width_ / 2));
|
|
collider_.y = (pos_y_ + (height_ / 2));
|
|
}
|
|
|
|
// Obtiene el puntero a la textura con los gráficos de la animación de morir
|
|
auto Player::getDeadTexture() -> Texture * {
|
|
return death_sprite_->getTexture();
|
|
;
|
|
}
|
|
|
|
// Obtiene el valor de la variable
|
|
auto Player::getDeathCounter() const -> Uint16 {
|
|
return death_counter_;
|
|
}
|
|
|
|
// Actualiza el valor de la variable
|
|
void Player::updatePowerUpHeadOffset() {
|
|
if (!power_up_) {
|
|
// powerUpHeadOffset = 0;
|
|
} else {
|
|
// powerUpHeadOffset = 96;
|
|
if (power_up_counter_ < 300) {
|
|
if (power_up_counter_ % 10 > 4) {
|
|
// powerUpHeadOffset = 96;
|
|
fire_sprite_->setEnabled(false);
|
|
} else {
|
|
// powerUpHeadOffset = 0;
|
|
fire_sprite_->setEnabled(true);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// 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]);
|
|
body_sprite_->setTexture(texture[1]);
|
|
legs_sprite_->setTexture(texture[2]);
|
|
death_sprite_->setTexture(texture[3]);
|
|
fire_sprite_->setTexture(texture[4]);
|
|
} |