518 lines
14 KiB
C++
518 lines
14 KiB
C++
#include "game/entities/player.h"
|
|
|
|
#include <algorithm>
|
|
#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;
|
|
status_walking_ = STATUS_WALKING_STOP;
|
|
status_firing_ = STATUS_FIRING_NO;
|
|
invulnerable_ = false;
|
|
invulnerable_counter_ = INVULNERABLE_COUNTER;
|
|
power_up_ = false;
|
|
power_up_counter_ = POWERUP_COUNTER;
|
|
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;
|
|
|
|
// Establece la velocidad base
|
|
base_speed_ = 1.5;
|
|
|
|
// 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;
|
|
|
|
// 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_;
|
|
setWalkingStatus(STATUS_WALKING_LEFT);
|
|
break;
|
|
|
|
case Input::Action::RIGHT:
|
|
vel_x_ = base_speed_;
|
|
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;
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
|
|
// 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);
|
|
}
|
|
|
|
// 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;
|
|
}
|
|
|
|
// Actualiza el valor de la variable
|
|
void Player::updateCooldown() {
|
|
if (cooldown_ > 0) {
|
|
cooldown_--;
|
|
if (power_up_) {
|
|
cooldown_--;
|
|
}
|
|
} 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();
|
|
}
|
|
|
|
// 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);
|
|
death_sprite_->setAccelY(0.2F);
|
|
death_sprite_->setVelY(-6.6F);
|
|
death_sprite_->setVelX(3.3F);
|
|
if (rand() % 2 == 0) {
|
|
death_sprite_->setVelX(-3.3F);
|
|
}
|
|
}
|
|
}
|
|
|
|
// 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;
|
|
}
|
|
|
|
// 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 valor de la variable
|
|
void Player::updateDeathCounter() {
|
|
if (!alive_) {
|
|
if (death_counter_ > 0) {
|
|
death_counter_--;
|
|
}
|
|
}
|
|
}
|
|
|
|
// 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;
|
|
}
|
|
|
|
// 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;
|
|
}
|
|
}
|
|
|
|
// 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;
|
|
}
|
|
|
|
// 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);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// 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]);
|
|
} |