Canviats defines per constexpr i enum class
Canviats punters a unique_ptr Afegit const a alguns metodes de classse fix: el segon jugador no podia unirse a la partida new: Quan els dos jugadors han decidit no continuar, ja no poden continuar i el marcador així ho reflectix fix: al posar el nom per segona vegada en la mateixa partida, no es reseteja la posició del selector fix: el fade venetian no netejava la textura i de vegades eixien gràfics corruptes fix: ara grava a disco cada vegada que es posa nom al morir
This commit is contained in:
@@ -4,19 +4,18 @@
|
||||
#include <stdlib.h> // for rand
|
||||
#include <algorithm> // for max, min
|
||||
#include "animated_sprite.h" // for AnimatedSprite
|
||||
#include "enter_name.h"
|
||||
#include "input.h" // for inputs_e
|
||||
#include "param.h" // for param
|
||||
#include "texture.h" // for Texture
|
||||
#include "input.h" // for inputs_e
|
||||
#include "param.h" // for param
|
||||
#include "texture.h" // for Texture
|
||||
|
||||
// Constructor
|
||||
Player::Player(int id, float x, int y, SDL_Rect *playArea, std::vector<Texture *> texture, std::vector<std::vector<std::string> *> animations)
|
||||
{
|
||||
// Reserva memoria para los objetos
|
||||
playerSprite = new AnimatedSprite(texture[0], "", animations[0]);
|
||||
powerSprite = new AnimatedSprite(texture[1], "", animations[1]);
|
||||
playerSprite = std::unique_ptr<AnimatedSprite>(new AnimatedSprite(texture[0], "", animations[0]));
|
||||
powerSprite = std::unique_ptr<AnimatedSprite>(new AnimatedSprite(texture[1], "", animations[1]));
|
||||
powerSprite->getTexture()->setAlpha(224);
|
||||
enterName = new EnterName();
|
||||
enterName = std::unique_ptr<EnterName>(new EnterName());
|
||||
|
||||
// Rectangulo con la zona de juego
|
||||
this->playArea = playArea;
|
||||
@@ -31,32 +30,21 @@ Player::Player(int id, float x, int y, SDL_Rect *playArea, std::vector<Texture *
|
||||
|
||||
// Inicializa variables
|
||||
this->id = id;
|
||||
statusPlaying = PLAYER_STATUS_WAITING;
|
||||
statusPlaying = playerStatus::WAITING;
|
||||
scoreBoardPanel = 0;
|
||||
name = "";
|
||||
setRecordName(enterName->getName());
|
||||
init();
|
||||
}
|
||||
|
||||
// Destructor
|
||||
Player::~Player()
|
||||
{
|
||||
delete playerSprite;
|
||||
delete powerSprite;
|
||||
if (enterName)
|
||||
{
|
||||
delete enterName;
|
||||
}
|
||||
}
|
||||
|
||||
// Iniciador
|
||||
void Player::init()
|
||||
{
|
||||
// Inicializa variables de estado
|
||||
posX = defaultPosX;
|
||||
posY = defaultPosY;
|
||||
statusWalking = PLAYER_STATUS_WALKING_STOP;
|
||||
statusFiring = PLAYER_STATUS_FIRING_NO;
|
||||
statusWalking = playerStatus::WALKING_STOP;
|
||||
statusFiring = playerStatus::FIRING_NO;
|
||||
invulnerable = true;
|
||||
invulnerableCounter = PLAYER_INVULNERABLE_COUNTER;
|
||||
powerUp = false;
|
||||
@@ -90,13 +78,16 @@ void Player::setInput(int input)
|
||||
{
|
||||
switch (statusPlaying)
|
||||
{
|
||||
case PLAYER_STATUS_PLAYING:
|
||||
case playerStatus::PLAYING:
|
||||
setInputPlaying(input);
|
||||
break;
|
||||
|
||||
case PLAYER_STATUS_ENTERING_NAME:
|
||||
case playerStatus::ENTERING_NAME:
|
||||
setInputEnteringName(input);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -107,29 +98,29 @@ void Player::setInputPlaying(int input)
|
||||
{
|
||||
case input_left:
|
||||
velX = -baseSpeed;
|
||||
setWalkingStatus(PLAYER_STATUS_WALKING_LEFT);
|
||||
setWalkingStatus(playerStatus::WALKING_LEFT);
|
||||
break;
|
||||
|
||||
case input_right:
|
||||
velX = baseSpeed;
|
||||
setWalkingStatus(PLAYER_STATUS_WALKING_RIGHT);
|
||||
setWalkingStatus(playerStatus::WALKING_RIGHT);
|
||||
break;
|
||||
|
||||
case input_fire_center:
|
||||
setFiringStatus(PLAYER_STATUS_FIRING_UP);
|
||||
setFiringStatus(playerStatus::FIRING_UP);
|
||||
break;
|
||||
|
||||
case input_fire_left:
|
||||
setFiringStatus(PLAYER_STATUS_FIRING_LEFT);
|
||||
setFiringStatus(playerStatus::FIRING_LEFT);
|
||||
break;
|
||||
|
||||
case input_fire_right:
|
||||
setFiringStatus(PLAYER_STATUS_FIRING_RIGHT);
|
||||
setFiringStatus(playerStatus::FIRING_RIGHT);
|
||||
break;
|
||||
|
||||
default:
|
||||
velX = 0;
|
||||
setWalkingStatus(PLAYER_STATUS_WALKING_STOP);
|
||||
setWalkingStatus(playerStatus::WALKING_STOP);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -156,7 +147,7 @@ void Player::setInputEnteringName(int input)
|
||||
break;
|
||||
|
||||
case input_start:
|
||||
recordName = enterName->getName();
|
||||
setRecordName(enterName->getName());
|
||||
break;
|
||||
|
||||
default:
|
||||
@@ -204,7 +195,7 @@ void Player::move()
|
||||
// Si el cadaver abandona el area de juego por abajo
|
||||
if (playerSprite->getPosY() > param.game.playArea.rect.h)
|
||||
{
|
||||
setStatusPlaying(PLAYER_STATUS_DIED);
|
||||
setStatusPlaying(playerStatus::DIED);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -220,11 +211,12 @@ void Player::render()
|
||||
}
|
||||
}
|
||||
|
||||
playerSprite->render();
|
||||
if (isRenderable())
|
||||
playerSprite->render();
|
||||
}
|
||||
|
||||
// Establece el estado del jugador cuando camina
|
||||
void Player::setWalkingStatus(int status)
|
||||
void Player::setWalkingStatus(playerStatus status)
|
||||
{
|
||||
// Si cambiamos de estado, reiniciamos la animación
|
||||
if (statusWalking != status)
|
||||
@@ -234,7 +226,7 @@ void Player::setWalkingStatus(int status)
|
||||
}
|
||||
|
||||
// Establece el estado del jugador cuando dispara
|
||||
void Player::setFiringStatus(int status)
|
||||
void Player::setFiringStatus(playerStatus status)
|
||||
{
|
||||
// Si cambiamos de estado, reiniciamos la animación
|
||||
if (statusFiring != status)
|
||||
@@ -247,16 +239,16 @@ void Player::setFiringStatus(int status)
|
||||
void Player::setAnimation()
|
||||
{
|
||||
// Crea cadenas de texto para componer el nombre de la animación
|
||||
const std::string aWalking = statusWalking == PLAYER_STATUS_WALKING_STOP ? "stand" : "walk";
|
||||
const std::string aFiring = statusFiring == PLAYER_STATUS_FIRING_UP ? "centershoot" : "sideshoot";
|
||||
const std::string aWalking = statusWalking == playerStatus::WALKING_STOP ? "stand" : "walk";
|
||||
const std::string aFiring = statusFiring == playerStatus::FIRING_UP ? "centershoot" : "sideshoot";
|
||||
|
||||
const SDL_RendererFlip flipWalk = statusWalking == PLAYER_STATUS_WALKING_RIGHT ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE;
|
||||
const SDL_RendererFlip flipFire = statusFiring == PLAYER_STATUS_FIRING_RIGHT ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE;
|
||||
const SDL_RendererFlip flipWalk = statusWalking == playerStatus::WALKING_RIGHT ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE;
|
||||
const SDL_RendererFlip flipFire = statusFiring == playerStatus::FIRING_RIGHT ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE;
|
||||
|
||||
// Establece la animación a partir de las cadenas
|
||||
if (isPlaying())
|
||||
{
|
||||
if (statusFiring == PLAYER_STATUS_FIRING_NO)
|
||||
if (statusFiring == playerStatus::FIRING_NO)
|
||||
{ // No esta disparando
|
||||
playerSprite->setCurrentAnimation(aWalking);
|
||||
playerSprite->setFlip(flipWalk);
|
||||
@@ -282,31 +274,31 @@ void Player::setAnimation()
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
int Player::getPosX()
|
||||
int Player::getPosX() const
|
||||
{
|
||||
return int(posX);
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
int Player::getPosY()
|
||||
int Player::getPosY() const
|
||||
{
|
||||
return posY;
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
int Player::getWidth()
|
||||
int Player::getWidth() const
|
||||
{
|
||||
return width;
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
int Player::getHeight()
|
||||
int Player::getHeight() const
|
||||
{
|
||||
return height;
|
||||
}
|
||||
|
||||
// Indica si el jugador puede disparar
|
||||
bool Player::canFire()
|
||||
bool Player::canFire() const
|
||||
{
|
||||
// Si el contador a llegado a cero, podemos disparar. En caso contrario decrementamos el contador
|
||||
return cooldown > 0 ? false : true;
|
||||
@@ -331,7 +323,7 @@ void Player::updateCooldown()
|
||||
}
|
||||
else
|
||||
{
|
||||
setFiringStatus(PLAYER_STATUS_FIRING_NO);
|
||||
setFiringStatus(playerStatus::FIRING_NO);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -348,7 +340,7 @@ void Player::update()
|
||||
}
|
||||
|
||||
// Obtiene la puntuación del jugador
|
||||
int Player::getScore()
|
||||
int Player::getScore() const
|
||||
{
|
||||
return score;
|
||||
}
|
||||
@@ -369,73 +361,89 @@ void Player::addScore(int score)
|
||||
}
|
||||
|
||||
// Indica si el jugador está jugando
|
||||
bool Player::isPlaying()
|
||||
bool Player::isPlaying() const
|
||||
{
|
||||
return statusPlaying == PLAYER_STATUS_PLAYING;
|
||||
return statusPlaying == playerStatus::PLAYING;
|
||||
}
|
||||
|
||||
// Indica si el jugador está continuando
|
||||
bool Player::isContinue()
|
||||
bool Player::isContinue() const
|
||||
{
|
||||
return statusPlaying == PLAYER_STATUS_CONTINUE;
|
||||
return statusPlaying == playerStatus::CONTINUE;
|
||||
}
|
||||
|
||||
// Indica si el jugador está esperando
|
||||
bool Player::isWaiting()
|
||||
bool Player::isWaiting() const
|
||||
{
|
||||
return statusPlaying == PLAYER_STATUS_WAITING;
|
||||
return statusPlaying == playerStatus::WAITING;
|
||||
}
|
||||
|
||||
// Indica si el jugador está introduciendo su nombre
|
||||
bool Player::isEnteringName()
|
||||
bool Player::isEnteringName() const
|
||||
{
|
||||
return statusPlaying == PLAYER_STATUS_ENTERING_NAME;
|
||||
return statusPlaying == playerStatus::ENTERING_NAME;
|
||||
}
|
||||
|
||||
// Indica si el jugador está muriendose
|
||||
bool Player::isDying()
|
||||
bool Player::isDying() const
|
||||
{
|
||||
return statusPlaying == PLAYER_STATUS_DYING;
|
||||
return statusPlaying == playerStatus::DYING;
|
||||
}
|
||||
|
||||
// Indica si el jugador ha terminado de morir
|
||||
bool Player::hasDied()
|
||||
bool Player::hasDied() const
|
||||
{
|
||||
return statusPlaying == PLAYER_STATUS_DIED;
|
||||
return statusPlaying == playerStatus::DIED;
|
||||
}
|
||||
|
||||
// Indica si el jugador ya ha terminado de jugar
|
||||
bool Player::isGameOver() const
|
||||
{
|
||||
return statusPlaying == playerStatus::GAME_OVER;
|
||||
}
|
||||
|
||||
// Establece el estado del jugador en el juego
|
||||
void Player::setStatusPlaying(int value)
|
||||
void Player::setStatusPlaying(playerStatus value)
|
||||
{
|
||||
statusPlaying = value;
|
||||
|
||||
switch (statusPlaying)
|
||||
{
|
||||
case PLAYER_STATUS_PLAYING:
|
||||
statusPlaying = PLAYER_STATUS_PLAYING;
|
||||
case playerStatus::PLAYING:
|
||||
{
|
||||
statusPlaying = playerStatus::PLAYING;
|
||||
init();
|
||||
break;
|
||||
}
|
||||
|
||||
case PLAYER_STATUS_CONTINUE:
|
||||
case playerStatus::CONTINUE:
|
||||
{
|
||||
// Inicializa el contador de continuar
|
||||
continueTicks = SDL_GetTicks();
|
||||
continueCounter = 9;
|
||||
enterName->init();
|
||||
break;
|
||||
}
|
||||
|
||||
case playerStatus::WAITING:
|
||||
break;
|
||||
|
||||
case PLAYER_STATUS_WAITING:
|
||||
case playerStatus::ENTERING_NAME:
|
||||
break;
|
||||
|
||||
case PLAYER_STATUS_ENTERING_NAME:
|
||||
break;
|
||||
|
||||
case PLAYER_STATUS_DYING:
|
||||
case playerStatus::DYING:
|
||||
{
|
||||
// Activa la animación de morir
|
||||
playerSprite->setAccelY(0.2f);
|
||||
playerSprite->setVelY(-6.6f);
|
||||
rand() % 2 == 0 ? playerSprite->setVelX(3.3f) : playerSprite->setVelX(-3.3f);
|
||||
break;
|
||||
}
|
||||
|
||||
case PLAYER_STATUS_DIED:
|
||||
case playerStatus::DIED:
|
||||
break;
|
||||
|
||||
case playerStatus::GAME_OVER:
|
||||
break;
|
||||
|
||||
default:
|
||||
@@ -444,13 +452,13 @@ void Player::setStatusPlaying(int value)
|
||||
}
|
||||
|
||||
// Obtiene el estado del jugador en el juego
|
||||
int Player::getStatusPlaying()
|
||||
playerStatus Player::getStatusPlaying() const
|
||||
{
|
||||
return statusPlaying;
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
float Player::getScoreMultiplier()
|
||||
float Player::getScoreMultiplier() const
|
||||
{
|
||||
return scoreMultiplier;
|
||||
}
|
||||
@@ -476,7 +484,7 @@ void Player::decScoreMultiplier()
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
bool Player::isInvulnerable()
|
||||
bool Player::isInvulnerable() const
|
||||
{
|
||||
return invulnerable;
|
||||
}
|
||||
@@ -489,7 +497,7 @@ void Player::setInvulnerable(bool value)
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
int Player::getInvulnerableCounter()
|
||||
int Player::getInvulnerableCounter() const
|
||||
{
|
||||
return invulnerableCounter;
|
||||
}
|
||||
@@ -519,7 +527,7 @@ void Player::updateInvulnerable()
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
bool Player::isPowerUp()
|
||||
bool Player::isPowerUp() const
|
||||
{
|
||||
return powerUp;
|
||||
}
|
||||
@@ -532,7 +540,7 @@ void Player::setPowerUp()
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
int Player::getPowerUpCounter()
|
||||
int Player::getPowerUpCounter() const
|
||||
{
|
||||
return powerUpCounter;
|
||||
}
|
||||
@@ -558,7 +566,7 @@ void Player::updatePowerUpCounter()
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
bool Player::hasExtraHit()
|
||||
bool Player::hasExtraHit() const
|
||||
{
|
||||
return extraHit;
|
||||
}
|
||||
@@ -600,7 +608,7 @@ void Player::disableInput()
|
||||
}
|
||||
|
||||
// Devuelve el número de cafes actuales
|
||||
int Player::getCoffees()
|
||||
int Player::getCoffees() const
|
||||
{
|
||||
return coffees;
|
||||
}
|
||||
@@ -626,7 +634,7 @@ void Player::setPlayerTextures(std::vector<Texture *> texture)
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
int Player::getContinueCounter()
|
||||
int Player::getContinueCounter() const
|
||||
{
|
||||
return continueCounter;
|
||||
}
|
||||
@@ -634,7 +642,7 @@ int Player::getContinueCounter()
|
||||
// Actualiza el contador de continue
|
||||
void Player::updateContinueCounter()
|
||||
{
|
||||
if (statusPlaying == PLAYER_STATUS_CONTINUE)
|
||||
if (statusPlaying == playerStatus::CONTINUE)
|
||||
{
|
||||
const Uint32 ticksSpeed = 1000;
|
||||
|
||||
@@ -652,7 +660,7 @@ void Player::setScoreBoardPanel(int panel)
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
int Player::getScoreBoardPanel()
|
||||
int Player::getScoreBoardPanel() const
|
||||
{
|
||||
return scoreBoardPanel;
|
||||
}
|
||||
@@ -664,7 +672,7 @@ void Player::decContinueCounter()
|
||||
continueCounter--;
|
||||
if (continueCounter < 0)
|
||||
{
|
||||
setStatusPlaying(PLAYER_STATUS_WAITING);
|
||||
setStatusPlaying(playerStatus::WAITING);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -681,19 +689,19 @@ void Player::setRecordName(std::string recordName)
|
||||
}
|
||||
|
||||
// Obtiene el nombre del jugador
|
||||
std::string Player::getName()
|
||||
std::string Player::getName() const
|
||||
{
|
||||
return name;
|
||||
}
|
||||
|
||||
// Obtiene el nombre del jugador para la tabla de mejores puntuaciones
|
||||
std::string Player::getRecordName()
|
||||
std::string Player::getRecordName() const
|
||||
{
|
||||
return recordName;
|
||||
}
|
||||
|
||||
// Obtiene la posici´´on que se está editando del nombre del jugador para la tabla de mejores puntuaciones
|
||||
int Player::getRecordNamePos()
|
||||
int Player::getRecordNamePos() const
|
||||
{
|
||||
if (enterName)
|
||||
{
|
||||
@@ -710,13 +718,19 @@ void Player::setController(int index)
|
||||
}
|
||||
|
||||
// Obtiene el mando que usa para ser controlado
|
||||
int Player::getController()
|
||||
int Player::getController() const
|
||||
{
|
||||
return controllerIndex;
|
||||
}
|
||||
|
||||
// Obtiene el "id" del jugador
|
||||
int Player::getId()
|
||||
int Player::getId() const
|
||||
{
|
||||
return id;
|
||||
}
|
||||
|
||||
// Indica si el jugador se puede dibujar
|
||||
bool Player::isRenderable() const
|
||||
{
|
||||
return isPlaying() || isDying();
|
||||
}
|
||||
Reference in New Issue
Block a user