Quitadas todas las variables globales y transformadas en punteros

This commit is contained in:
2022-10-20 18:24:12 +02:00
parent 596bf2c4a5
commit b4e76a4c7d
25 changed files with 848 additions and 781 deletions

View File

@@ -1,75 +1,91 @@
#include "player.h"
//Constructor
Player::Player()
// Constructor
Player::Player(SDL_Renderer *gRenderer)
{
this->gRenderer = gRenderer;
gPlayerTexture = new LTexture(gRenderer);
// Carga los gráficos del jugador
if (!gPlayerTexture->loadFromFile("media/gfx/player.png"))
{
printf("Failed to load player texture!\n");
}
init();
}
//Iniciador
// Destructor
Player::~Player()
{
gPlayerTexture->free();
}
// Iniciador
void Player::init()
{
//Establece la altura y el ancho del jugador
{
// Establece la altura y el ancho del jugador
mWidth = 3 * BLOCK;
mHeight = 3 * BLOCK;
//Establece la posición inicial del jugador
// Establece la posición inicial del jugador
mPosX = PLAY_AREA_LEFT + (PLAY_AREA_WIDTH / 2) - (mWidth / 2);
mPosY = PLAY_AREA_BOTTOM - mHeight;
//Establece el tamaño del circulo de colisión
// Establece el tamaño del circulo de colisión
mCollider.r = 7;
//Actualiza la posición del circulo de colisión
// Actualiza la posición del circulo de colisión
shiftColliders();
//Establece la velocidad inicial
// Establece la velocidad inicial
mVelX = 0;
mVelY = 0;
//Establece la velocidad base
// Establece la velocidad base
mBaseSpeed = 1.5;
//Establece el numero inicial de vidas
// Establece el numero inicial de vidas
mStartingLives = 3;
mLives = mStartingLives;
//Establece la puntuación inicial
// Establece la puntuación inicial
mScore = 0;
//Inicia el contador
// Inicia el contador
mCooldown = 10;
//Inicia el sprite
// Inicia el sprite
mSprite.init();
//Set width and height of the player sprite
// Set width and height of the player sprite
mSprite.setWidth(mWidth);
mSprite.setHeight(mHeight);
//Set sprite position
// Set sprite position
mSprite.setPosX(mPosX);
mSprite.setPosY(mPosY);
//Set sprite sheet
mSprite.setTexture(gPlayerTexture);
// Set sprite sheet
mSprite.setTexture(*gPlayerTexture);
//Set status
// Set status
mStatus = PLAYER_STATE_STOPPED;
//Initialize animation variables
// Initialize animation variables
mSprite.setCurrentFrame(0);
mSprite.setAnimationCounter(0);
mSprite.setAnimationNumFrames(PLAYER_STATE_STOPPED, 1);
mSprite.setAnimationNumFrames(PLAYER_STATE_WALKING_LEFT, 4);
mSprite.setAnimationNumFrames(PLAYER_STATE_WALKING_RIGHT, 4);
mSprite.setAnimationSpeed(PLAYER_STATE_STOPPED, 10);
mSprite.setAnimationSpeed(PLAYER_STATE_WALKING_LEFT, 5);
mSprite.setAnimationSpeed(PLAYER_STATE_WALKING_RIGHT, 5);
//Set animation clips
// Set animation clips
mSprite.setAnimationFrames(PLAYER_STATE_WALKING_LEFT, 0, mSprite.getWidth() * 0, mSprite.getWidth() * 0, mSprite.getWidth(), mSprite.getHeight());
mSprite.setAnimationFrames(PLAYER_STATE_WALKING_LEFT, 1, mSprite.getWidth() * 1, mSprite.getWidth() * 0, mSprite.getWidth(), mSprite.getHeight());
mSprite.setAnimationFrames(PLAYER_STATE_WALKING_LEFT, 2, mSprite.getWidth() * 2, mSprite.getWidth() * 0, mSprite.getWidth(), mSprite.getHeight());
@@ -80,11 +96,11 @@ void Player::init()
mSprite.setAnimationFrames(PLAYER_STATE_WALKING_RIGHT, 3, mSprite.getWidth() * 3, mSprite.getHeight() * 1, mSprite.getWidth(), mSprite.getHeight());
mSprite.setAnimationFrames(PLAYER_STATE_STOPPED, 0, mSprite.getWidth() * 0, mSprite.getHeight() * 2, mSprite.getWidth(), mSprite.getHeight());
//Set window for sprite sheet
// Set window for sprite sheet
mSprite.setSpriteClip(mSprite.getAnimationClip(PLAYER_STATE_STOPPED, 0));
}
//Comprueba el teclado y actua en consecuencia
// Comprueba el teclado y actua en consecuencia
void Player::checkInput(Uint8 input)
{
switch (input)
@@ -94,56 +110,56 @@ void Player::checkInput(Uint8 input)
setStatus(PLAYER_STATE_WALKING_LEFT);
break;
case INPUT_RIGHT:
case INPUT_RIGHT:
mVelX = mBaseSpeed;
setStatus(PLAYER_STATE_WALKING_RIGHT);
break;
default:
mVelX = 0;
mVelX = 0;
setStatus(PLAYER_STATE_STOPPED);
break;
}
}
//Mueve el jugador a la posición y animación que le corresponde
// Mueve el jugador a la posición y animación que le corresponde
void Player::move()
{
//Move the player left or right
// Move the player left or right
mPosX += mVelX;
//If the player went too far to the left or right
// If the player went too far to the left or right
if ((mPosX < PLAY_AREA_LEFT) || (mPosX + mWidth > PLAY_AREA_RIGHT))
{
//Move back
// Move back
mPosX -= mVelX;
}
//Move the player up or down
// Move the player up or down
mPosY += mVelY;
//If the player went too far up or down
// If the player went too far up or down
if ((mPosY < PLAY_AREA_TOP) || (mPosY + mHeight > PLAY_AREA_BOTTOM))
{
//Move back
// Move back
mPosY -= mVelY;
}
//Update sprite position
// Update sprite position
mSprite.setPosX(getPosX());
mSprite.setPosY(mPosY);
}
//Pinta el jugador en pantalla
// Pinta el jugador en pantalla
void Player::render()
{
mSprite.render();
}
//Establece el estado del jugador
// Establece el estado del jugador
void Player::setStatus(int status)
{
//Si cambiamos de estado, reiniciamos la animación
// Si cambiamos de estado, reiniciamos la animación
if (mStatus != status)
{
mStatus = status;
@@ -151,40 +167,40 @@ void Player::setStatus(int status)
}
}
//Establece la animación correspondiente al estado
// Establece la animación correspondiente al estado
void Player::setAnimation()
{
mSprite.animate(mStatus);
}
//Obtiene el valor de la variable
// Obtiene el valor de la variable
int Player::getPosX()
{
return int(mPosX);
}
//Obtiene el valor de la variable
// Obtiene el valor de la variable
int Player::getPosY()
{
return mPosY;
}
//Obtiene el valor de la variable
// Obtiene el valor de la variable
int Player::getWidth()
{
return mWidth;
}
//Obtiene el valor de la variable
// Obtiene el valor de la variable
int Player::getHeight()
{
return mHeight;
}
//Indica si el jugador puede disparar
// Indica si el jugador puede disparar
bool Player::canFire()
{
//Si el contador a llegado a cero, podemos disparar. En caso contrario decrementamos el contador
// Si el contador a llegado a cero, podemos disparar. En caso contrario decrementamos el contador
if (mCooldown > 0)
{
return false;
@@ -195,13 +211,13 @@ bool Player::canFire()
}
}
//Establece el valor de la variable
// Establece el valor de la variable
void Player::setFireCooldown(int time)
{
mCooldown = time;
}
//Actualiza el valor de la variable
// Actualiza el valor de la variable
void Player::updateCooldown()
{
if (mCooldown > 0)
@@ -210,43 +226,43 @@ void Player::updateCooldown()
}
}
//Actualiza al jugador a su posicion, animación y controla los contadores
// Actualiza al jugador a su posicion, animación y controla los contadores
void Player::update()
{
move();
setAnimation();
shiftColliders();
shiftColliders();
updateCooldown();
}
//Obtiene la puntuación del jugador
// Obtiene la puntuación del jugador
int Player::getScore()
{
return mScore;
}
//Establece la puntuación del jugador
// Establece la puntuación del jugador
void Player::setScore(int score)
{
mScore = score;
}
//Añade a la puntuación del jugador
// Añade a la puntuación del jugador
void Player::addScore(int score)
{
mScore += score;
}
//Obtiene el circulo de colisión
// Obtiene el circulo de colisión
Circle &Player::getCollider()
{
return mCollider;
}
//Actualiza el circulo de colisión a la posición del jugador
// Actualiza el circulo de colisión a la posición del jugador
void Player::shiftColliders()
{
//Align collider to center of player
// Align collider to center of player
mCollider.x = mPosX + (mWidth / 2);
mCollider.y = mPosY + (mHeight / 2);
}