reestructuració
This commit is contained in:
781
source/game/entities/balloon.cpp
Normal file
781
source/game/entities/balloon.cpp
Normal file
@@ -0,0 +1,781 @@
|
||||
#include "game/entities/balloon.h"
|
||||
|
||||
#include <math.h> // for abs
|
||||
|
||||
#include "core/rendering/animatedsprite.h" // for AnimatedSprite
|
||||
#include "core/rendering/movingsprite.h" // for MovingSprite
|
||||
#include "core/rendering/sprite.h" // for Sprite
|
||||
#include "core/rendering/texture.h" // for Texture
|
||||
#include "game/defaults.hpp" // for PLAY_AREA_LEFT, PLAY_AREA_RIGHT, PLAY_AR...
|
||||
|
||||
// Constructor
|
||||
Balloon::Balloon(float x, float y, Uint8 kind, float velx, float speed, Uint16 creationtimer, Texture *texture, std::vector<std::string> *animation, SDL_Renderer *renderer) {
|
||||
sprite = new AnimatedSprite(texture, renderer, "", animation);
|
||||
disable();
|
||||
|
||||
enabled = true;
|
||||
|
||||
switch (kind) {
|
||||
case BALLOON_1:
|
||||
// Alto y ancho del objeto
|
||||
width = BALLOON_WIDTH_1;
|
||||
height = BALLOON_WIDTH_1;
|
||||
size = BALLOON_SIZE_1;
|
||||
power = 1;
|
||||
|
||||
// Inicializa los valores de velocidad y gravedad
|
||||
this->velX = velx;
|
||||
velY = 0;
|
||||
maxVelY = 3.0f;
|
||||
gravity = 0.09f;
|
||||
defaultVelY = 2.6f;
|
||||
|
||||
// Puntos que da el globo al ser destruido
|
||||
score = BALLOON_SCORE_1;
|
||||
|
||||
// Amenaza que genera el globo
|
||||
menace = 1;
|
||||
|
||||
break;
|
||||
|
||||
case BALLOON_2:
|
||||
// Alto y ancho del objeto
|
||||
width = BALLOON_WIDTH_2;
|
||||
height = BALLOON_WIDTH_2;
|
||||
size = BALLOON_SIZE_2;
|
||||
power = 3;
|
||||
|
||||
// Inicializa los valores de velocidad y gravedad
|
||||
this->velX = velx;
|
||||
velY = 0;
|
||||
maxVelY = 3.0f;
|
||||
gravity = 0.10f;
|
||||
defaultVelY = 3.5f;
|
||||
|
||||
// Puntos que da el globo al ser destruido
|
||||
score = BALLOON_SCORE_2;
|
||||
|
||||
// Amenaza que genera el globo
|
||||
menace = 2;
|
||||
|
||||
break;
|
||||
|
||||
case BALLOON_3:
|
||||
// Alto y ancho del objeto
|
||||
width = BALLOON_WIDTH_3;
|
||||
height = BALLOON_WIDTH_3;
|
||||
size = BALLOON_SIZE_3;
|
||||
power = 7;
|
||||
|
||||
// Inicializa los valores de velocidad y gravedad
|
||||
this->velX = velx;
|
||||
velY = 0;
|
||||
maxVelY = 3.0f;
|
||||
gravity = 0.10f;
|
||||
defaultVelY = 4.50f;
|
||||
|
||||
// Puntos que da el globo al ser destruido
|
||||
score = BALLOON_SCORE_3;
|
||||
|
||||
// Amenaza que genera el globo
|
||||
menace = 4;
|
||||
|
||||
break;
|
||||
|
||||
case BALLOON_4:
|
||||
// Alto y ancho del objeto
|
||||
width = BALLOON_WIDTH_4;
|
||||
height = BALLOON_WIDTH_4;
|
||||
size = BALLOON_SIZE_4;
|
||||
power = 15;
|
||||
|
||||
// Inicializa los valores de velocidad y gravedad
|
||||
this->velX = velx;
|
||||
velY = 0;
|
||||
maxVelY = 3.0f;
|
||||
gravity = 0.10f;
|
||||
defaultVelY = 4.95f;
|
||||
|
||||
// Puntos que da el globo al ser destruido
|
||||
score = BALLOON_SCORE_4;
|
||||
|
||||
// Amenaza que genera el globo
|
||||
menace = 8;
|
||||
|
||||
break;
|
||||
|
||||
case HEXAGON_1:
|
||||
// Alto y ancho del objeto
|
||||
width = BALLOON_WIDTH_1;
|
||||
height = BALLOON_WIDTH_1;
|
||||
size = BALLOON_SIZE_1;
|
||||
power = 1;
|
||||
|
||||
// Inicializa los valores de velocidad y gravedad
|
||||
this->velX = velx;
|
||||
velY = abs(velx) * 2;
|
||||
maxVelY = abs(velx) * 2;
|
||||
gravity = 0.00f;
|
||||
defaultVelY = abs(velx) * 2;
|
||||
|
||||
// Puntos que da el globo al ser destruido
|
||||
score = BALLOON_SCORE_1;
|
||||
|
||||
// Amenaza que genera el globo
|
||||
menace = 1;
|
||||
|
||||
break;
|
||||
|
||||
case HEXAGON_2:
|
||||
// Alto y ancho del objeto
|
||||
width = BALLOON_WIDTH_2;
|
||||
height = BALLOON_WIDTH_2;
|
||||
size = BALLOON_SIZE_2;
|
||||
power = 3;
|
||||
|
||||
// Inicializa los valores de velocidad y gravedad
|
||||
this->velX = velx;
|
||||
velY = abs(velx) * 2;
|
||||
maxVelY = abs(velx) * 2;
|
||||
gravity = 0.00f;
|
||||
defaultVelY = abs(velx) * 2;
|
||||
|
||||
// Puntos que da el globo al ser destruido
|
||||
score = BALLOON_SCORE_2;
|
||||
|
||||
// Amenaza que genera el globo
|
||||
menace = 2;
|
||||
|
||||
break;
|
||||
|
||||
case HEXAGON_3:
|
||||
// Alto y ancho del objeto
|
||||
width = BALLOON_WIDTH_3;
|
||||
height = BALLOON_WIDTH_3;
|
||||
size = BALLOON_SIZE_3;
|
||||
power = 7;
|
||||
|
||||
// Inicializa los valores de velocidad y gravedad
|
||||
this->velX = velx;
|
||||
velY = abs(velx) * 2;
|
||||
maxVelY = abs(velx) * 2;
|
||||
gravity = 0.00f;
|
||||
defaultVelY = abs(velx) * 2;
|
||||
|
||||
// Puntos que da el globo al ser destruido
|
||||
score = BALLOON_SCORE_3;
|
||||
|
||||
// Amenaza que genera el globo
|
||||
menace = 4;
|
||||
|
||||
break;
|
||||
|
||||
case HEXAGON_4:
|
||||
// Alto y ancho del objeto
|
||||
width = BALLOON_WIDTH_4;
|
||||
height = BALLOON_WIDTH_4;
|
||||
size = BALLOON_SIZE_4;
|
||||
power = 15;
|
||||
|
||||
// Inicializa los valores de velocidad y gravedad
|
||||
this->velX = velx;
|
||||
velY = abs(velx) * 2;
|
||||
maxVelY = abs(velx) * 2;
|
||||
gravity = 0.00f;
|
||||
defaultVelY = abs(velx) * 2;
|
||||
|
||||
// Puntos que da el globo al ser destruido
|
||||
score = BALLOON_SCORE_4;
|
||||
|
||||
// Amenaza que genera el globo
|
||||
menace = 8;
|
||||
|
||||
break;
|
||||
|
||||
case POWER_BALL:
|
||||
// Alto y ancho del objeto
|
||||
width = BALLOON_WIDTH_4;
|
||||
height = BALLOON_WIDTH_4;
|
||||
size = 4;
|
||||
power = 0;
|
||||
|
||||
// Inicializa los valores de velocidad y gravedad
|
||||
this->velX = velx;
|
||||
velY = 0;
|
||||
maxVelY = 3.0f;
|
||||
gravity = 0.10f;
|
||||
defaultVelY = 4.95f;
|
||||
|
||||
// Puntos que da el globo al ser destruido
|
||||
score = 0;
|
||||
|
||||
// Amenaza que genera el globo
|
||||
menace = 0;
|
||||
|
||||
// Añade rotación al sprite
|
||||
sprite->setRotate(false);
|
||||
sprite->setRotateSpeed(0);
|
||||
if (velX > 0.0f) {
|
||||
sprite->setRotateAmount(2.0);
|
||||
} else {
|
||||
sprite->setRotateAmount(-2.0);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
// Posición inicial
|
||||
posX = x;
|
||||
posY = y;
|
||||
|
||||
// Valores para el efecto de rebote
|
||||
bouncing.enabled = false;
|
||||
bouncing.counter = 0;
|
||||
bouncing.speed = 2;
|
||||
bouncing.zoomW = 1.0f;
|
||||
bouncing.zoomH = 1.0f;
|
||||
bouncing.despX = 0.0f;
|
||||
bouncing.despY = 0.0f;
|
||||
bouncing.w = {1.10f, 1.05f, 1.00f, 0.95f, 0.90f, 0.95f, 1.00f, 1.02f, 1.05f, 1.02f};
|
||||
bouncing.h = {0.90f, 0.95f, 1.00f, 1.05f, 1.10f, 1.05f, 1.00f, 0.98f, 0.95f, 0.98f};
|
||||
|
||||
// Alto y ancho del sprite
|
||||
sprite->setWidth(width);
|
||||
sprite->setHeight(height);
|
||||
|
||||
// Posición X,Y del sprite
|
||||
sprite->setPosX((int)posX);
|
||||
sprite->setPosY((int)posY);
|
||||
|
||||
// Tamaño del circulo de colisión
|
||||
collider.r = width / 2;
|
||||
|
||||
// Alinea el circulo de colisión con el objeto
|
||||
updateColliders();
|
||||
|
||||
// Inicializa variables
|
||||
stopped = true;
|
||||
stoppedCounter = 0;
|
||||
blinking = false;
|
||||
visible = true;
|
||||
invulnerable = true;
|
||||
beingCreated = true;
|
||||
creationCounter = creationtimer;
|
||||
creationCounterIni = creationtimer;
|
||||
popping = false;
|
||||
|
||||
// Actualiza valores
|
||||
beingCreated = creationCounter == 0 ? false : true;
|
||||
invulnerable = beingCreated == false ? false : true;
|
||||
|
||||
counter = 0;
|
||||
travelY = 1.0f;
|
||||
this->speed = speed;
|
||||
|
||||
// Tipo
|
||||
this->kind = kind;
|
||||
}
|
||||
|
||||
// Destructor
|
||||
Balloon::~Balloon() {
|
||||
delete sprite;
|
||||
}
|
||||
|
||||
// Centra el globo en la posición X
|
||||
void Balloon::allignTo(int x) {
|
||||
posX = float(x - (width / 2));
|
||||
|
||||
if (posX < PLAY_AREA_LEFT)
|
||||
posX = PLAY_AREA_LEFT + 1;
|
||||
else if ((posX + width) > PLAY_AREA_RIGHT)
|
||||
posX = float(PLAY_AREA_RIGHT - width - 1);
|
||||
|
||||
// Posición X,Y del sprite
|
||||
sprite->setPosX(getPosX());
|
||||
sprite->setPosY(getPosY());
|
||||
|
||||
// Alinea el circulo de colisión con el objeto
|
||||
updateColliders();
|
||||
}
|
||||
|
||||
// Pinta el globo en la pantalla
|
||||
void Balloon::render() {
|
||||
if ((visible) && (enabled)) {
|
||||
if (bouncing.enabled) {
|
||||
if (kind != POWER_BALL) {
|
||||
// Aplica desplazamiento para el zoom
|
||||
sprite->setPosX(getPosX() + bouncing.despX);
|
||||
sprite->setPosY(getPosY() + bouncing.despY);
|
||||
sprite->render();
|
||||
sprite->setPosX(getPosX() - bouncing.despX);
|
||||
sprite->setPosY(getPosY() - bouncing.despY);
|
||||
}
|
||||
} else if (isBeingCreated()) {
|
||||
// Aplica alpha blending
|
||||
sprite->getTexture()->setAlpha(255 - (int)((float)creationCounter * (255.0f / (float)creationCounterIni)));
|
||||
sprite->render();
|
||||
if (kind == POWER_BALL) {
|
||||
Sprite *sp = new Sprite(sprite->getRect(), sprite->getTexture(), sprite->getRenderer());
|
||||
sp->setSpriteClip(407, 0, 37, 37);
|
||||
sp->render();
|
||||
delete sp;
|
||||
}
|
||||
sprite->getTexture()->setAlpha(255);
|
||||
} else {
|
||||
sprite->render();
|
||||
|
||||
if (kind == POWER_BALL and !popping) {
|
||||
Sprite *sp = new Sprite(sprite->getRect(), sprite->getTexture(), sprite->getRenderer());
|
||||
sp->setSpriteClip(407, 0, 37, 37);
|
||||
sp->render();
|
||||
delete sp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza la posición y estados del globo
|
||||
void Balloon::move() {
|
||||
// Comprueba si se puede mover
|
||||
if (!isStopped()) {
|
||||
// Lo mueve a izquierda o derecha
|
||||
posX += (velX * speed);
|
||||
|
||||
// Si queda fuera de pantalla, corregimos su posición y cambiamos su sentido
|
||||
if ((posX < PLAY_AREA_LEFT) || (posX + width > PLAY_AREA_RIGHT)) {
|
||||
// Corrige posición
|
||||
posX -= (velX * speed);
|
||||
|
||||
// Invierte sentido
|
||||
velX = -velX;
|
||||
|
||||
// Invierte la rotación
|
||||
sprite->switchRotate();
|
||||
|
||||
// Activa el efecto de rebote
|
||||
if (kind != POWER_BALL) {
|
||||
bounceStart();
|
||||
}
|
||||
}
|
||||
|
||||
// Mueve el globo hacia arriba o hacia abajo
|
||||
posY += (velY * speed);
|
||||
|
||||
// Si se sale por arriba
|
||||
if (posY < PLAY_AREA_TOP) {
|
||||
// Corrige
|
||||
posY = PLAY_AREA_TOP;
|
||||
|
||||
// Invierte sentido
|
||||
velY = -velY;
|
||||
|
||||
// Activa el efecto de rebote
|
||||
if (kind != POWER_BALL) {
|
||||
bounceStart();
|
||||
}
|
||||
}
|
||||
|
||||
// Si el globo se sale por la parte inferior
|
||||
if (posY + height > PLAY_AREA_BOTTOM) {
|
||||
// Corrige
|
||||
posY = PLAY_AREA_BOTTOM - height;
|
||||
|
||||
// Invierte colocando una velocidad por defecto
|
||||
velY = -defaultVelY;
|
||||
|
||||
// Activa el efecto de rebote
|
||||
if (kind != POWER_BALL) {
|
||||
bounceStart();
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Para aplicar la gravedad, el diseño original la aplicaba en cada iteración del bucle
|
||||
Al añadir el modificador de velocidad se reduce la distancia que recorre el objeto y por
|
||||
tanto recibe mas gravedad. Para solucionarlo se va a aplicar la gravedad cuando se haya
|
||||
recorrido una distancia igual a la velocidad en Y, que era el cálculo inicial
|
||||
|
||||
*/
|
||||
|
||||
// Incrementa la variable que calcula la distancia acumulada en Y
|
||||
travelY += speed;
|
||||
|
||||
// Si la distancia acumulada en Y es igual a la velocidad, se aplica la gravedad
|
||||
if (travelY >= 1.0f) {
|
||||
// Quita el excedente
|
||||
travelY -= 1.0f;
|
||||
|
||||
// Aplica la gravedad al objeto sin pasarse de una velocidad máxima
|
||||
velY += gravity;
|
||||
|
||||
// Al parecer esta asignación se quedó sin hacer y ahora el juego no funciona
|
||||
// correctamente si se aplica, así que se deja sin efecto
|
||||
// velY = std::min(velY, maxVelY);
|
||||
}
|
||||
|
||||
// Actualiza la posición del sprite
|
||||
sprite->setPosX(getPosX());
|
||||
sprite->setPosY(getPosY());
|
||||
}
|
||||
}
|
||||
|
||||
// Deshabilita el globo y pone a cero todos los valores
|
||||
void Balloon::disable() {
|
||||
beingCreated = false;
|
||||
blinking = false;
|
||||
collider.r = 0;
|
||||
collider.x = 0;
|
||||
collider.y = 0;
|
||||
counter = 0;
|
||||
creationCounter = 0;
|
||||
creationCounterIni = 0;
|
||||
defaultVelY = 0.0f;
|
||||
enabled = false;
|
||||
gravity = 0.0f;
|
||||
height = 0;
|
||||
invulnerable = false;
|
||||
kind = 0;
|
||||
maxVelY = 0.0f;
|
||||
menace = 0;
|
||||
popping = false;
|
||||
posX = 0.0f;
|
||||
posY = 0.0f;
|
||||
power = 0;
|
||||
score = 0;
|
||||
size = 0;
|
||||
speed = 0;
|
||||
stopped = false;
|
||||
stoppedCounter = 0;
|
||||
travelY = 0;
|
||||
velX = 0.0f;
|
||||
velY = 0.0f;
|
||||
visible = false;
|
||||
width = 0;
|
||||
sprite->clear();
|
||||
}
|
||||
|
||||
// Explosiona el globo
|
||||
void Balloon::pop() {
|
||||
setPopping(true);
|
||||
sprite->disableRotate();
|
||||
setStop(true);
|
||||
setStoppedTimer(2000);
|
||||
setInvulnerable(true);
|
||||
menace = 0;
|
||||
}
|
||||
|
||||
// Actualiza al globo a su posicion, animación y controla los contadores
|
||||
void Balloon::update() {
|
||||
if (enabled) {
|
||||
sprite->MovingSprite::update();
|
||||
move();
|
||||
updateAnimation();
|
||||
updateColliders();
|
||||
updateState();
|
||||
updateBounce();
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza los estados del globo
|
||||
void Balloon::updateState() {
|
||||
// Si está explotando
|
||||
if (isPopping()) {
|
||||
setInvulnerable(true);
|
||||
setStop(true);
|
||||
if (sprite->animationIsCompleted()) {
|
||||
disable();
|
||||
}
|
||||
}
|
||||
|
||||
// Si se está creando
|
||||
if (isBeingCreated()) {
|
||||
// Actualiza el valor de las variables
|
||||
setStop(true);
|
||||
setInvulnerable(true);
|
||||
|
||||
// Todavia tiene tiempo en el contador
|
||||
if (creationCounter > 0) {
|
||||
// Desplaza lentamente el globo hacia abajo y hacia un lado
|
||||
if (creationCounter % 10 == 0) {
|
||||
posY++;
|
||||
posX += velX;
|
||||
|
||||
// Comprueba no se salga por los laterales
|
||||
if ((posX < PLAY_AREA_LEFT) || (posX > (PLAY_AREA_RIGHT - width))) {
|
||||
// Corrige y cambia el sentido de la velocidad
|
||||
posX -= velX;
|
||||
velX = -velX;
|
||||
}
|
||||
|
||||
// Actualiza la posición del sprite
|
||||
sprite->setPosX(getPosX());
|
||||
sprite->setPosY(getPosY());
|
||||
|
||||
// Actualiza la posición del circulo de colisión
|
||||
updateColliders();
|
||||
}
|
||||
|
||||
creationCounter--;
|
||||
}
|
||||
// El contador ha llegado a cero
|
||||
else {
|
||||
setBeingCreated(false);
|
||||
setStop(false);
|
||||
setVisible(true);
|
||||
setInvulnerable(false);
|
||||
if (kind == POWER_BALL) {
|
||||
sprite->setRotate(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Solo comprueba el estado detenido cuando no se está creando
|
||||
else if (isStopped()) {
|
||||
// Si es una powerball deja de rodar
|
||||
if (kind == POWER_BALL) {
|
||||
sprite->setRotate(false);
|
||||
}
|
||||
|
||||
// Reduce el contador
|
||||
if (stoppedCounter > 0) {
|
||||
stoppedCounter--;
|
||||
}
|
||||
// Quitarles el estado "detenido" si no estan explosionando
|
||||
else if (!isPopping()) {
|
||||
// Si es una powerball vuelve a rodar
|
||||
if (kind == POWER_BALL) {
|
||||
sprite->setRotate(true);
|
||||
}
|
||||
|
||||
setStop(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Establece la animación correspondiente al estado
|
||||
void Balloon::updateAnimation() {
|
||||
std::string creatingAnimation = "blue";
|
||||
std::string normalAnimation = "orange";
|
||||
|
||||
if (kind == POWER_BALL) {
|
||||
creatingAnimation = "powerball";
|
||||
normalAnimation = "powerball";
|
||||
} else if (getClass() == HEXAGON_CLASS) {
|
||||
creatingAnimation = "red";
|
||||
normalAnimation = "green";
|
||||
}
|
||||
|
||||
// Establece el frame de animación
|
||||
if (isPopping()) {
|
||||
sprite->setCurrentAnimation("pop");
|
||||
} else if (isBeingCreated()) {
|
||||
sprite->setCurrentAnimation(creatingAnimation);
|
||||
} else {
|
||||
sprite->setCurrentAnimation(normalAnimation);
|
||||
}
|
||||
|
||||
sprite->animate();
|
||||
}
|
||||
|
||||
// Comprueba si el globo está habilitado
|
||||
bool Balloon::isEnabled() {
|
||||
return enabled;
|
||||
}
|
||||
|
||||
// Obtiene del valor de la variable
|
||||
float Balloon::getPosX() {
|
||||
return posX;
|
||||
}
|
||||
|
||||
// Obtiene del valor de la variable
|
||||
float Balloon::getPosY() {
|
||||
return posY;
|
||||
}
|
||||
|
||||
// Obtiene del valor de la variable
|
||||
float Balloon::getVelY() {
|
||||
return velY;
|
||||
}
|
||||
|
||||
// Obtiene del valor de la variable
|
||||
int Balloon::getWidth() {
|
||||
return width;
|
||||
}
|
||||
|
||||
// Obtiene del valor de la variable
|
||||
int Balloon::getHeight() {
|
||||
return height;
|
||||
}
|
||||
|
||||
// Establece el valor de la variable
|
||||
void Balloon::setVelY(float velY) {
|
||||
this->velY = velY;
|
||||
}
|
||||
|
||||
// Establece el valor de la variable
|
||||
void Balloon::setSpeed(float speed) {
|
||||
this->speed = speed;
|
||||
}
|
||||
|
||||
// Obtiene del valor de la variable
|
||||
int Balloon::getKind() {
|
||||
return kind;
|
||||
}
|
||||
|
||||
// Obtiene del valor de la variable
|
||||
Uint8 Balloon::getSize() {
|
||||
return size;
|
||||
}
|
||||
|
||||
// Obtiene la clase a la que pertenece el globo
|
||||
Uint8 Balloon::getClass() {
|
||||
if ((kind >= BALLOON_1) && (kind <= BALLOON_4)) {
|
||||
return BALLOON_CLASS;
|
||||
}
|
||||
|
||||
else if ((kind >= HEXAGON_1) && (kind <= HEXAGON_4)) {
|
||||
return HEXAGON_CLASS;
|
||||
}
|
||||
|
||||
return BALLOON_CLASS;
|
||||
}
|
||||
|
||||
// Establece el valor de la variable
|
||||
void Balloon::setStop(bool state) {
|
||||
stopped = state;
|
||||
}
|
||||
|
||||
// Obtiene del valor de la variable
|
||||
bool Balloon::isStopped() {
|
||||
return stopped;
|
||||
}
|
||||
|
||||
// Establece el valor de la variable
|
||||
void Balloon::setBlink(bool value) {
|
||||
blinking = value;
|
||||
}
|
||||
|
||||
// Obtiene del valor de la variable
|
||||
bool Balloon::isBlinking() {
|
||||
return blinking;
|
||||
}
|
||||
|
||||
// Establece el valor de la variable
|
||||
void Balloon::setVisible(bool value) {
|
||||
visible = value;
|
||||
}
|
||||
|
||||
// Obtiene del valor de la variable
|
||||
bool Balloon::isVisible() {
|
||||
return visible;
|
||||
}
|
||||
|
||||
// Establece el valor de la variable
|
||||
void Balloon::setInvulnerable(bool value) {
|
||||
invulnerable = value;
|
||||
}
|
||||
|
||||
// Obtiene del valor de la variable
|
||||
bool Balloon::isInvulnerable() {
|
||||
return invulnerable;
|
||||
}
|
||||
|
||||
// Establece el valor de la variable
|
||||
void Balloon::setBeingCreated(bool value) {
|
||||
beingCreated = value;
|
||||
}
|
||||
|
||||
// Obtiene del valor de la variable
|
||||
bool Balloon::isBeingCreated() {
|
||||
return beingCreated;
|
||||
}
|
||||
|
||||
// Establece el valor de la variable
|
||||
void Balloon::setPopping(bool value) {
|
||||
popping = value;
|
||||
}
|
||||
|
||||
// Obtiene del valor de la variable
|
||||
bool Balloon::isPopping() {
|
||||
return popping;
|
||||
}
|
||||
|
||||
// Establece el valor de la variable
|
||||
void Balloon::setStoppedTimer(Uint16 time) {
|
||||
stoppedCounter = time;
|
||||
}
|
||||
|
||||
// Obtiene del valor de la variable
|
||||
Uint16 Balloon::getStoppedTimer() {
|
||||
return stoppedCounter;
|
||||
}
|
||||
|
||||
// Obtiene del valor de la variable
|
||||
Uint16 Balloon::getScore() {
|
||||
return score;
|
||||
}
|
||||
|
||||
// Obtiene el circulo de colisión
|
||||
circle_t &Balloon::getCollider() {
|
||||
return collider;
|
||||
}
|
||||
|
||||
// Alinea el circulo de colisión con la posición del objeto globo
|
||||
void Balloon::updateColliders() {
|
||||
collider.x = Uint16(posX + collider.r);
|
||||
collider.y = posY + collider.r;
|
||||
}
|
||||
|
||||
// Obtiene le valor de la variable
|
||||
Uint8 Balloon::getMenace() {
|
||||
if (isEnabled()) {
|
||||
return menace;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Obtiene le valor de la variable
|
||||
Uint8 Balloon::getPower() {
|
||||
return power;
|
||||
}
|
||||
|
||||
void Balloon::bounceStart() {
|
||||
bouncing.enabled = true;
|
||||
bouncing.zoomW = 1;
|
||||
bouncing.zoomH = 1;
|
||||
sprite->setZoomW(bouncing.zoomW);
|
||||
sprite->setZoomH(bouncing.zoomH);
|
||||
bouncing.despX = 0;
|
||||
bouncing.despY = 0;
|
||||
}
|
||||
|
||||
void Balloon::bounceStop() {
|
||||
bouncing.enabled = false;
|
||||
bouncing.counter = 0;
|
||||
bouncing.zoomW = 1.0f;
|
||||
bouncing.zoomH = 1.0f;
|
||||
sprite->setZoomW(bouncing.zoomW);
|
||||
sprite->setZoomH(bouncing.zoomH);
|
||||
bouncing.despX = 0.0f;
|
||||
bouncing.despY = 0.0f;
|
||||
}
|
||||
|
||||
void Balloon::updateBounce() {
|
||||
if (bouncing.enabled) {
|
||||
bouncing.zoomW = bouncing.w[bouncing.counter / bouncing.speed];
|
||||
bouncing.zoomH = bouncing.h[bouncing.counter / bouncing.speed];
|
||||
sprite->setZoomW(bouncing.zoomW);
|
||||
sprite->setZoomH(bouncing.zoomH);
|
||||
bouncing.despX = (sprite->getSpriteClip().w - (sprite->getSpriteClip().w * bouncing.zoomW));
|
||||
bouncing.despY = (sprite->getSpriteClip().h - (sprite->getSpriteClip().h * bouncing.zoomH));
|
||||
bouncing.counter++;
|
||||
if ((bouncing.counter / bouncing.speed) > (MAX_BOUNCE - 1)) {
|
||||
bounceStop();
|
||||
}
|
||||
}
|
||||
}
|
||||
250
source/game/entities/balloon.h
Normal file
250
source/game/entities/balloon.h
Normal file
@@ -0,0 +1,250 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <string> // for string
|
||||
#include <vector> // for vector
|
||||
|
||||
#include "utils/utils.h" // for circle_t
|
||||
class AnimatedSprite;
|
||||
class Texture;
|
||||
|
||||
// Cantidad de elementos del vector con los valores de la deformación del globo al rebotar
|
||||
constexpr int MAX_BOUNCE = 10;
|
||||
|
||||
// Tipos de globo
|
||||
constexpr int BALLOON_1 = 1;
|
||||
constexpr int BALLOON_2 = 2;
|
||||
constexpr int BALLOON_3 = 3;
|
||||
constexpr int BALLOON_4 = 4;
|
||||
constexpr int HEXAGON_1 = 5;
|
||||
constexpr int HEXAGON_2 = 6;
|
||||
constexpr int HEXAGON_3 = 7;
|
||||
constexpr int HEXAGON_4 = 8;
|
||||
constexpr int POWER_BALL = 9;
|
||||
|
||||
// Puntos de globo
|
||||
constexpr int BALLOON_SCORE_1 = 50;
|
||||
constexpr int BALLOON_SCORE_2 = 100;
|
||||
constexpr int BALLOON_SCORE_3 = 200;
|
||||
constexpr int BALLOON_SCORE_4 = 400;
|
||||
|
||||
// Tamaños de globo
|
||||
constexpr int BALLOON_SIZE_1 = 1;
|
||||
constexpr int BALLOON_SIZE_2 = 2;
|
||||
constexpr int BALLOON_SIZE_3 = 3;
|
||||
constexpr int BALLOON_SIZE_4 = 4;
|
||||
|
||||
// Clases de globo
|
||||
constexpr int BALLOON_CLASS = 0;
|
||||
constexpr int HEXAGON_CLASS = 1;
|
||||
|
||||
// Velocidad del globo
|
||||
constexpr float BALLOON_VELX_POSITIVE = 0.7f;
|
||||
constexpr float BALLOON_VELX_NEGATIVE = -0.7f;
|
||||
|
||||
// Índice para las animaciones de los globos
|
||||
constexpr int BALLOON_MOVING_ANIMATION = 0;
|
||||
constexpr int BALLOON_POP_ANIMATION = 1;
|
||||
constexpr int BALLOON_BORN_ANIMATION = 2;
|
||||
|
||||
// Cantidad posible de globos
|
||||
constexpr int MAX_BALLOONS = 100;
|
||||
|
||||
// Velocidades a las que se mueven los globos
|
||||
constexpr float BALLOON_SPEED_1 = 0.60f;
|
||||
constexpr float BALLOON_SPEED_2 = 0.70f;
|
||||
constexpr float BALLOON_SPEED_3 = 0.80f;
|
||||
constexpr float BALLOON_SPEED_4 = 0.90f;
|
||||
constexpr float BALLOON_SPEED_5 = 1.00f;
|
||||
|
||||
// Tamaño de los globos
|
||||
constexpr int BALLOON_WIDTH_1 = 8;
|
||||
constexpr int BALLOON_WIDTH_2 = 13;
|
||||
constexpr int BALLOON_WIDTH_3 = 21;
|
||||
constexpr int BALLOON_WIDTH_4 = 37;
|
||||
|
||||
// PowerBall
|
||||
constexpr int POWERBALL_SCREENPOWER_MINIMUM = 10;
|
||||
constexpr int POWERBALL_COUNTER = 8;
|
||||
|
||||
// Clase Balloon
|
||||
class Balloon {
|
||||
private:
|
||||
// Estructura para las variables para el efecto de los rebotes
|
||||
struct bouncing {
|
||||
bool enabled; // Si el efecto está activo
|
||||
Uint8 counter; // Countador para el efecto
|
||||
Uint8 speed; // Velocidad a la que transcurre el efecto
|
||||
float zoomW; // Zoom aplicado a la anchura
|
||||
float zoomH; // Zoom aplicado a la altura
|
||||
float despX; // Desplazamiento de pixeles en el eje X antes de pintar el objeto con zoom
|
||||
float despY; // Desplazamiento de pixeles en el eje Y antes de pintar el objeto con zoom
|
||||
std::vector<float> w; // Vector con los valores de zoom para el ancho del globo
|
||||
std::vector<float> h; // Vector con los valores de zoom para el alto del globo
|
||||
};
|
||||
|
||||
// Objetos y punteros
|
||||
AnimatedSprite *sprite; // Sprite del objeto globo
|
||||
|
||||
// Variables
|
||||
float posX; // Posición en el eje X
|
||||
float posY; // Posición en el eje Y
|
||||
Uint8 width; // Ancho
|
||||
Uint8 height; // Alto
|
||||
float velX; // Velocidad en el eje X. Cantidad de pixeles a desplazarse
|
||||
float velY; // Velocidad en el eje Y. Cantidad de pixeles a desplazarse
|
||||
float gravity; // Aceleración en el eje Y. Modifica la velocidad
|
||||
float defaultVelY; // Velocidad inicial que tienen al rebotar contra el suelo
|
||||
float maxVelY; // Máxima velocidad que puede alcanzar el objeto en el eje Y
|
||||
bool beingCreated; // Indica si el globo se está creando
|
||||
bool blinking; // Indica si el globo está intermitente
|
||||
bool enabled; // Indica si el globo esta activo
|
||||
bool invulnerable; // Indica si el globo es invulnerable
|
||||
bool popping; // Indica si el globo está explotando
|
||||
bool stopped; // Indica si el globo está parado
|
||||
bool visible; // Indica si el globo es visible
|
||||
circle_t collider; // Circulo de colisión del objeto
|
||||
Uint16 creationCounter; // Temporizador para controlar el estado "creandose"
|
||||
Uint16 creationCounterIni; // Valor inicial para el temporizador para controlar el estado "creandose"
|
||||
Uint16 score; // Puntos que da el globo al ser destruido
|
||||
Uint16 stoppedCounter; // Contador para controlar el estado "parado"
|
||||
Uint8 kind; // Tipo de globo
|
||||
Uint8 menace; // Cantidad de amenaza que genera el globo
|
||||
Uint32 counter; // Contador interno
|
||||
float travelY; // Distancia que ha de recorrer el globo en el eje Y antes de que se le aplique la gravedad
|
||||
float speed; // Velocidad a la que se mueven los globos
|
||||
Uint8 size; // Tamaño del globo
|
||||
Uint8 power; // Cantidad de poder que alberga el globo
|
||||
bouncing bouncing; // Contiene las variables para el efecto de rebote
|
||||
|
||||
// Alinea el circulo de colisión con la posición del objeto globo
|
||||
void updateColliders();
|
||||
|
||||
// Activa el efecto
|
||||
void bounceStart();
|
||||
|
||||
// Detiene el efecto
|
||||
void bounceStop();
|
||||
|
||||
// Aplica el efecto
|
||||
void updateBounce();
|
||||
|
||||
// Actualiza los estados del globo
|
||||
void updateState();
|
||||
|
||||
// Establece la animación correspondiente
|
||||
void updateAnimation();
|
||||
|
||||
// Establece el valor de la variable
|
||||
void setBeingCreated(bool value);
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
Balloon(float x, float y, Uint8 kind, float velx, float speed, Uint16 creationtimer, Texture *texture, std::vector<std::string> *animation, SDL_Renderer *renderer);
|
||||
|
||||
// Destructor
|
||||
~Balloon();
|
||||
|
||||
// Centra el globo en la posición X
|
||||
void allignTo(int x);
|
||||
|
||||
// Pinta el globo en la pantalla
|
||||
void render();
|
||||
|
||||
// Actualiza la posición y estados del globo
|
||||
void move();
|
||||
|
||||
// Deshabilita el globo y pone a cero todos los valores
|
||||
void disable();
|
||||
|
||||
// Explosiona el globo
|
||||
void pop();
|
||||
|
||||
// Actualiza al globo a su posicion, animación y controla los contadores
|
||||
void update();
|
||||
|
||||
// Comprueba si el globo está habilitado
|
||||
bool isEnabled();
|
||||
|
||||
// Obtiene del valor de la variable
|
||||
float getPosX();
|
||||
|
||||
// Obtiene del valor de la variable
|
||||
float getPosY();
|
||||
|
||||
// Obtiene del valor de la variable
|
||||
float getVelY();
|
||||
|
||||
// Obtiene del valor de la variable
|
||||
int getWidth();
|
||||
|
||||
// Obtiene del valor de la variable
|
||||
int getHeight();
|
||||
|
||||
// Establece el valor de la variable
|
||||
void setVelY(float velY);
|
||||
|
||||
// Establece el valor de la variable
|
||||
void setSpeed(float speed);
|
||||
|
||||
// Obtiene del valor de la variable
|
||||
int getKind();
|
||||
|
||||
// Obtiene del valor de la variable
|
||||
Uint8 getSize();
|
||||
|
||||
// Obtiene la clase a la que pertenece el globo
|
||||
Uint8 getClass();
|
||||
|
||||
// Establece el valor de la variable
|
||||
void setStop(bool value);
|
||||
|
||||
// Obtiene del valor de la variable
|
||||
bool isStopped();
|
||||
|
||||
// Establece el valor de la variable
|
||||
void setBlink(bool value);
|
||||
|
||||
// Obtiene del valor de la variable
|
||||
bool isBlinking();
|
||||
|
||||
// Establece el valor de la variable
|
||||
void setVisible(bool value);
|
||||
|
||||
// Obtiene del valor de la variable
|
||||
bool isVisible();
|
||||
|
||||
// Establece el valor de la variable
|
||||
void setInvulnerable(bool value);
|
||||
|
||||
// Obtiene del valor de la variable
|
||||
bool isInvulnerable();
|
||||
|
||||
// Obtiene del valor de la variable
|
||||
bool isBeingCreated();
|
||||
|
||||
// Establece el valor de la variable
|
||||
void setPopping(bool value);
|
||||
|
||||
// Obtiene del valor de la variable
|
||||
bool isPopping();
|
||||
|
||||
// Establece el valor de la variable
|
||||
void setStoppedTimer(Uint16 time);
|
||||
|
||||
// Obtiene del valor de la variable
|
||||
Uint16 getStoppedTimer();
|
||||
|
||||
// Obtiene del valor de la variable
|
||||
Uint16 getScore();
|
||||
|
||||
// Obtiene el circulo de colisión
|
||||
circle_t &getCollider();
|
||||
|
||||
// Obtiene le valor de la variable
|
||||
Uint8 getMenace();
|
||||
|
||||
// Obtiene le valor de la variable
|
||||
Uint8 getPower();
|
||||
};
|
||||
184
source/game/entities/bullet.cpp
Normal file
184
source/game/entities/bullet.cpp
Normal file
@@ -0,0 +1,184 @@
|
||||
#include "game/entities/bullet.h"
|
||||
|
||||
#include "core/rendering/sprite.h" // for Sprite
|
||||
#include "game/defaults.hpp" // for NO_KIND, PLAY_AREA_LEFT, PLAY_AREA_RIGHT, PLAY_A...
|
||||
class Texture;
|
||||
|
||||
// Constructor
|
||||
Bullet::Bullet(int x, int y, int kind, bool poweredUp, int owner, Texture *texture, SDL_Renderer *renderer) {
|
||||
sprite = new Sprite({x, y, 10, 10}, texture, renderer);
|
||||
|
||||
// Posición inicial del objeto
|
||||
posX = x;
|
||||
posY = y;
|
||||
|
||||
// Alto y ancho del objeto
|
||||
width = 10;
|
||||
height = 10;
|
||||
|
||||
// Velocidad inicial en el eje Y
|
||||
velY = -3;
|
||||
|
||||
// Tipo de bala
|
||||
this->kind = kind;
|
||||
|
||||
// Identificador del dueño del objeto
|
||||
this->owner = owner;
|
||||
|
||||
// Valores especificos según el tipo
|
||||
switch (kind) {
|
||||
case BULLET_UP:
|
||||
// Establece la velocidad inicial
|
||||
velX = 0;
|
||||
|
||||
// Rectangulo con los gráficos del objeto
|
||||
if (!poweredUp) {
|
||||
sprite->setSpriteClip(0 * width, 0, sprite->getWidth(), sprite->getHeight());
|
||||
} else {
|
||||
sprite->setSpriteClip((0 + 3) * width, 0, sprite->getWidth(), sprite->getHeight());
|
||||
}
|
||||
break;
|
||||
|
||||
case BULLET_LEFT:
|
||||
// Establece la velocidad inicial
|
||||
velX = -2;
|
||||
|
||||
// Rectangulo con los gráficos del objeto
|
||||
if (!poweredUp) {
|
||||
sprite->setSpriteClip(1 * width, 0, sprite->getWidth(), sprite->getHeight());
|
||||
} else {
|
||||
sprite->setSpriteClip((1 + 3) * width, 0, sprite->getWidth(), sprite->getHeight());
|
||||
}
|
||||
break;
|
||||
|
||||
case BULLET_RIGHT:
|
||||
// Establece la velocidad inicial
|
||||
velX = 2;
|
||||
|
||||
// Rectangulo con los gráficos del objeto
|
||||
if (!poweredUp) {
|
||||
sprite->setSpriteClip(2 * width, 0, sprite->getWidth(), sprite->getHeight());
|
||||
} else {
|
||||
sprite->setSpriteClip((2 + 3) * width, 0, sprite->getWidth(), sprite->getHeight());
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
// Establece el tamaño del circulo de colisión
|
||||
collider.r = width / 2;
|
||||
|
||||
// Alinea el circulo de colisión con el objeto
|
||||
shiftColliders();
|
||||
}
|
||||
|
||||
// Destructor
|
||||
Bullet::~Bullet() {
|
||||
delete sprite;
|
||||
}
|
||||
|
||||
// Pinta el objeto en pantalla
|
||||
void Bullet::render() {
|
||||
sprite->render();
|
||||
}
|
||||
|
||||
// Actualiza la posición y estado del objeto en horizontal
|
||||
Uint8 Bullet::move() {
|
||||
// Variable con el valor de retorno
|
||||
Uint8 msg = BULLET_MOVE_OK;
|
||||
|
||||
// Mueve el objeto a su nueva posición
|
||||
posX += velX;
|
||||
|
||||
// Si el objeto se sale del area de juego por los laterales
|
||||
if ((posX < PLAY_AREA_LEFT - width) || (posX > PLAY_AREA_RIGHT)) {
|
||||
// Se deshabilita
|
||||
kind = NO_KIND;
|
||||
|
||||
// Mensaje de salida
|
||||
msg = BULLET_MOVE_OUT;
|
||||
}
|
||||
|
||||
// Mueve el objeto a su nueva posición en vertical
|
||||
posY += int(velY);
|
||||
|
||||
// Si el objeto se sale del area de juego por la parte superior
|
||||
if (posY < PLAY_AREA_TOP - height) {
|
||||
// Se deshabilita
|
||||
kind = NO_KIND;
|
||||
|
||||
// Mensaje de salida
|
||||
msg = BULLET_MOVE_OUT;
|
||||
}
|
||||
|
||||
// Actualiza la posición del sprite
|
||||
sprite->setPosX(posX);
|
||||
sprite->setPosY(posY);
|
||||
|
||||
// Alinea el circulo de colisión con el objeto
|
||||
shiftColliders();
|
||||
|
||||
return msg;
|
||||
}
|
||||
|
||||
// Comprueba si el objeto está habilitado
|
||||
bool Bullet::isEnabled() {
|
||||
if (kind == NO_KIND) {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// Deshabilita el objeto
|
||||
void Bullet::disable() {
|
||||
kind = NO_KIND;
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
int Bullet::getPosX() {
|
||||
return posX;
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
int Bullet::getPosY() {
|
||||
return posY;
|
||||
}
|
||||
|
||||
// Establece el valor de la variable
|
||||
void Bullet::setPosX(int x) {
|
||||
posX = x;
|
||||
}
|
||||
|
||||
// Establece el valor de la variable
|
||||
void Bullet::setPosY(int y) {
|
||||
posY = y;
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
int Bullet::getVelY() {
|
||||
return velY;
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
int Bullet::getKind() {
|
||||
return kind;
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
int Bullet::getOwner() {
|
||||
return owner;
|
||||
}
|
||||
|
||||
// Obtiene el circulo de colisión
|
||||
circle_t &Bullet::getCollider() {
|
||||
return collider;
|
||||
}
|
||||
|
||||
// Alinea el circulo de colisión con el objeto
|
||||
void Bullet::shiftColliders() {
|
||||
collider.x = posX + collider.r;
|
||||
collider.y = posY + collider.r;
|
||||
}
|
||||
80
source/game/entities/bullet.h
Normal file
80
source/game/entities/bullet.h
Normal file
@@ -0,0 +1,80 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include "utils/utils.h" // for circle_t
|
||||
class Sprite;
|
||||
class Texture;
|
||||
|
||||
// Tipos de bala
|
||||
constexpr int BULLET_UP = 1;
|
||||
constexpr int BULLET_LEFT = 2;
|
||||
constexpr int BULLET_RIGHT = 3;
|
||||
|
||||
// Tipos de retorno de la función move de la bala
|
||||
constexpr int BULLET_MOVE_OK = 0;
|
||||
constexpr int BULLET_MOVE_OUT = 1;
|
||||
|
||||
// Clase Bullet
|
||||
class Bullet {
|
||||
private:
|
||||
// Objetos y punteros
|
||||
Sprite *sprite; // Sprite con los graficos y métodos de pintado
|
||||
|
||||
// Variables
|
||||
int posX; // Posición en el eje X
|
||||
int posY; // Posición en el eje Y
|
||||
Uint8 width; // Ancho del objeto
|
||||
Uint8 height; // Alto del objeto
|
||||
int velX; // Velocidad en el eje X
|
||||
int velY; // Velocidad en el eje Y
|
||||
int kind; // Tipo de objeto
|
||||
int owner; // Identificador del dueño del objeto
|
||||
circle_t collider; // Circulo de colisión del objeto
|
||||
|
||||
// Alinea el circulo de colisión con el objeto
|
||||
void shiftColliders();
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
Bullet(int x, int y, int kind, bool poweredUp, int owner, Texture *texture, SDL_Renderer *renderer);
|
||||
|
||||
// Destructor
|
||||
~Bullet();
|
||||
|
||||
// Pinta el objeto en pantalla
|
||||
void render();
|
||||
|
||||
// Actualiza la posición y estado del objeto
|
||||
Uint8 move();
|
||||
|
||||
// Comprueba si el objeto está habilitado
|
||||
bool isEnabled();
|
||||
|
||||
// Deshabilita el objeto
|
||||
void disable();
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
int getPosX();
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
int getPosY();
|
||||
|
||||
// Establece el valor de la variable
|
||||
void setPosX(int x);
|
||||
|
||||
// Establece el valor de la variable
|
||||
void setPosY(int y);
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
int getVelY();
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
int getKind();
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
int getOwner();
|
||||
|
||||
// Obtiene el circulo de colisión
|
||||
circle_t &getCollider();
|
||||
};
|
||||
200
source/game/entities/item.cpp
Normal file
200
source/game/entities/item.cpp
Normal file
@@ -0,0 +1,200 @@
|
||||
#include "game/entities/item.h"
|
||||
|
||||
#include <stdlib.h> // for rand
|
||||
|
||||
#include "core/rendering/animatedsprite.h" // for AnimatedSprite
|
||||
#include "game/defaults.hpp" // for PLAY_AREA_LEFT, PLAY_AREA_RIGHT, PLAY_AR...
|
||||
class Texture;
|
||||
|
||||
// Constructor
|
||||
Item::Item(Uint8 kind, float x, float y, Texture *texture, std::vector<std::string> *animation, SDL_Renderer *renderer) {
|
||||
sprite = new AnimatedSprite(texture, renderer, "", animation);
|
||||
|
||||
this->kind = kind;
|
||||
enabled = true;
|
||||
timeToLive = 600;
|
||||
accelX = 0.0f;
|
||||
floorCollision = false;
|
||||
|
||||
if (kind == ITEM_COFFEE_MACHINE) {
|
||||
width = 23;
|
||||
height = 29;
|
||||
posX = (((int)x + (PLAY_AREA_WIDTH / 2)) % (PLAY_AREA_WIDTH - width - 5)) + 2;
|
||||
posY = PLAY_AREA_TOP - height;
|
||||
velX = 0.0f;
|
||||
velY = -0.1f;
|
||||
accelY = 0.1f;
|
||||
collider.r = 10;
|
||||
} else {
|
||||
width = 16;
|
||||
height = 16;
|
||||
posX = x;
|
||||
posY = y;
|
||||
velX = -1.0f + ((rand() % 5) * 0.5f);
|
||||
velY = -4.0f;
|
||||
accelY = 0.2f;
|
||||
collider.r = width / 2;
|
||||
}
|
||||
|
||||
sprite->setPosX(posX);
|
||||
sprite->setPosY(posY);
|
||||
shiftColliders();
|
||||
}
|
||||
|
||||
// Destructor
|
||||
Item::~Item() {
|
||||
delete sprite;
|
||||
}
|
||||
|
||||
// Centra el objeto en la posición X
|
||||
void Item::allignTo(int x) {
|
||||
posX = float(x - (width / 2));
|
||||
|
||||
if (posX < PLAY_AREA_LEFT) {
|
||||
posX = PLAY_AREA_LEFT + 1;
|
||||
} else if ((posX + width) > PLAY_AREA_RIGHT) {
|
||||
posX = float(PLAY_AREA_RIGHT - width - 1);
|
||||
}
|
||||
|
||||
// Posición X,Y del sprite
|
||||
sprite->setPosX(int(posX));
|
||||
sprite->setPosY(int(posY));
|
||||
|
||||
// Alinea el circulo de colisión con el objeto
|
||||
shiftColliders();
|
||||
}
|
||||
|
||||
// Pinta el objeto en la pantalla
|
||||
void Item::render() {
|
||||
if (enabled) {
|
||||
if (timeToLive > 200) {
|
||||
sprite->render();
|
||||
} else if (timeToLive % 20 > 10) {
|
||||
sprite->render();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza la posición y estados del objeto
|
||||
void Item::move() {
|
||||
floorCollision = false;
|
||||
|
||||
// Calcula la nueva posición
|
||||
posX += velX;
|
||||
posY += velY;
|
||||
|
||||
// Aplica las aceleraciones a la velocidad
|
||||
velX += accelX;
|
||||
velY += accelY;
|
||||
|
||||
// Si queda fuera de pantalla, corregimos su posición y cambiamos su sentido
|
||||
if ((posX < PLAY_AREA_LEFT) || (posX + width > PLAY_AREA_RIGHT)) {
|
||||
// Corregir posición
|
||||
posX -= velX;
|
||||
|
||||
// Invertir sentido
|
||||
velX = -velX;
|
||||
}
|
||||
|
||||
// Si se sale por arriba rebota (excepto la maquina de café)
|
||||
if ((posY < PLAY_AREA_TOP) && !(kind == ITEM_COFFEE_MACHINE)) {
|
||||
// Corrige
|
||||
posY -= velY;
|
||||
|
||||
// Invierte el sentido
|
||||
velY = -velY;
|
||||
}
|
||||
|
||||
// Si el objeto se sale por la parte inferior
|
||||
if (posY + height > PLAY_AREA_BOTTOM) {
|
||||
// Corrige
|
||||
posY -= velY;
|
||||
|
||||
// Detiene el objeto
|
||||
velY = 0;
|
||||
velX = 0;
|
||||
accelX = 0;
|
||||
accelY = 0;
|
||||
posY = PLAY_AREA_BOTTOM - height;
|
||||
if (kind == ITEM_COFFEE_MACHINE) {
|
||||
floorCollision = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza la posición del sprite
|
||||
sprite->setPosX(int(posX));
|
||||
sprite->setPosY(int(posY));
|
||||
shiftColliders();
|
||||
}
|
||||
|
||||
// Pone a cero todos los valores del objeto
|
||||
void Item::disable() {
|
||||
enabled = false;
|
||||
}
|
||||
|
||||
// Actualiza el objeto a su posicion, animación y controla los contadores
|
||||
void Item::update() {
|
||||
move();
|
||||
sprite->animate();
|
||||
updateTimeToLive();
|
||||
checkTimeToLive();
|
||||
}
|
||||
|
||||
// Actualiza el contador
|
||||
void Item::updateTimeToLive() {
|
||||
if (timeToLive > 0) {
|
||||
timeToLive--;
|
||||
}
|
||||
}
|
||||
|
||||
// Comprueba si el objeto sigue vivo
|
||||
void Item::checkTimeToLive() {
|
||||
if (timeToLive == 0)
|
||||
disable();
|
||||
}
|
||||
|
||||
// Obtiene del valor de la variable
|
||||
float Item::getPosX() {
|
||||
return posX;
|
||||
}
|
||||
|
||||
// Obtiene del valor de la variable
|
||||
float Item::getPosY() {
|
||||
return posY;
|
||||
}
|
||||
|
||||
// Obtiene del valor de la variable
|
||||
int Item::getWidth() {
|
||||
return width;
|
||||
}
|
||||
|
||||
// Obtiene del valor de la variable
|
||||
int Item::getHeight() {
|
||||
return height;
|
||||
}
|
||||
|
||||
// Obtiene del valor de la variable
|
||||
int Item::getClass() {
|
||||
return kind;
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
bool Item::isEnabled() {
|
||||
return enabled;
|
||||
}
|
||||
|
||||
// Obtiene el circulo de colisión
|
||||
circle_t &Item::getCollider() {
|
||||
return collider;
|
||||
}
|
||||
|
||||
// Alinea el circulo de colisión con la posición del objeto
|
||||
void Item::shiftColliders() {
|
||||
collider.x = int(posX + (width / 2));
|
||||
collider.y = int(posY + (height / 2));
|
||||
}
|
||||
|
||||
// Informa si el objeto ha colisionado con el suelo
|
||||
bool Item::isOnFloor() {
|
||||
return floorCollision;
|
||||
}
|
||||
96
source/game/entities/item.h
Normal file
96
source/game/entities/item.h
Normal file
@@ -0,0 +1,96 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <string> // for string
|
||||
#include <vector> // for vector
|
||||
|
||||
#include "utils/utils.h" // for circle_t
|
||||
class AnimatedSprite;
|
||||
class Texture;
|
||||
|
||||
// Tipos de objetos
|
||||
constexpr int ITEM_POINTS_1_DISK = 1;
|
||||
constexpr int ITEM_POINTS_2_GAVINA = 2;
|
||||
constexpr int ITEM_POINTS_3_PACMAR = 3;
|
||||
constexpr int ITEM_CLOCK = 4;
|
||||
constexpr int ITEM_COFFEE = 5;
|
||||
constexpr int ITEM_COFFEE_MACHINE = 6;
|
||||
|
||||
// Clase Item
|
||||
class Item {
|
||||
private:
|
||||
// Objetos y punteros
|
||||
AnimatedSprite *sprite; // Sprite con los graficos del objeto
|
||||
|
||||
// Variables
|
||||
float posX; // Posición X del objeto
|
||||
float posY; // Posición Y del objeto
|
||||
Uint8 width; // Ancho del objeto
|
||||
Uint8 height; // Alto del objeto
|
||||
float velX; // Velocidad en el eje X
|
||||
float velY; // Velocidad en el eje Y
|
||||
float accelX; // Aceleración en el eje X
|
||||
float accelY; // Aceleración en el eje Y
|
||||
bool floorCollision; // Indica si el objeto colisiona con el suelo
|
||||
Uint8 kind; // Especifica el tipo de objeto que es
|
||||
bool enabled; // Especifica si el objeto está habilitado
|
||||
circle_t collider; // Circulo de colisión del objeto
|
||||
|
||||
// Alinea el circulo de colisión con la posición del objeto
|
||||
void shiftColliders();
|
||||
|
||||
// Actualiza la posición y estados del objeto
|
||||
void move();
|
||||
|
||||
public:
|
||||
Uint16 timeToLive; // Temporizador con el tiempo que el objeto está presente
|
||||
|
||||
// Constructor
|
||||
Item(Uint8 kind, float x, float y, Texture *texture, std::vector<std::string> *animation, SDL_Renderer *renderer);
|
||||
|
||||
// Destructor
|
||||
~Item();
|
||||
|
||||
// Centra el objeto en la posición X
|
||||
void allignTo(int x);
|
||||
|
||||
// Pinta el objeto en la pantalla
|
||||
void render();
|
||||
|
||||
// Pone a cero todos los valores del objeto
|
||||
void disable();
|
||||
|
||||
// Actualiza al objeto a su posicion, animación y controla los contadores
|
||||
void update();
|
||||
|
||||
// Actualiza el contador
|
||||
void updateTimeToLive();
|
||||
|
||||
// Comprueba si el objeto sigue vivo
|
||||
void checkTimeToLive();
|
||||
|
||||
// Obtiene del valor de la variable
|
||||
float getPosX();
|
||||
|
||||
// Obtiene del valor de la variable
|
||||
float getPosY();
|
||||
|
||||
// Obtiene del valor de la variable
|
||||
int getWidth();
|
||||
|
||||
// Obtiene del valor de la variable
|
||||
int getHeight();
|
||||
|
||||
// Obtiene del valor de la variable
|
||||
int getClass();
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
bool isEnabled();
|
||||
|
||||
// Obtiene el circulo de colisión
|
||||
circle_t &getCollider();
|
||||
|
||||
// Informa si el objeto ha colisionado con el suelo
|
||||
bool isOnFloor();
|
||||
};
|
||||
530
source/game/entities/player.cpp
Normal file
530
source/game/entities/player.cpp
Normal file
@@ -0,0 +1,530 @@
|
||||
#include "game/entities/player.h"
|
||||
|
||||
#include <stdlib.h> // for rand
|
||||
|
||||
#include "core/input/input.h" // for inputs_e
|
||||
#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, std::vector<Texture *> texture, std::vector<std::vector<std::string> *> animations) {
|
||||
// Copia los punteros
|
||||
this->renderer = renderer;
|
||||
|
||||
// Reserva memoria para los objetos
|
||||
headSprite = new AnimatedSprite(texture[0], renderer, "", animations[0]);
|
||||
bodySprite = new AnimatedSprite(texture[1], renderer, "", animations[1]);
|
||||
legsSprite = new AnimatedSprite(texture[2], renderer, "", animations[2]);
|
||||
deathSprite = new AnimatedSprite(texture[3], renderer, "", animations[3]);
|
||||
fireSprite = new AnimatedSprite(texture[4], renderer, "", animations[4]);
|
||||
fireSprite->getTexture()->setAlpha(224);
|
||||
|
||||
// Establece la posición inicial del jugador
|
||||
posX = x;
|
||||
posY = y;
|
||||
|
||||
init();
|
||||
}
|
||||
|
||||
// Destructor
|
||||
Player::~Player() {
|
||||
delete headSprite;
|
||||
delete bodySprite;
|
||||
delete legsSprite;
|
||||
delete deathSprite;
|
||||
delete fireSprite;
|
||||
}
|
||||
|
||||
// Iniciador
|
||||
void Player::init() {
|
||||
// Inicializa variables de estado
|
||||
alive = true;
|
||||
deathCounter = DEATH_COUNTER;
|
||||
statusWalking = PLAYER_STATUS_WALKING_STOP;
|
||||
statusFiring = PLAYER_STATUS_FIRING_NO;
|
||||
invulnerable = false;
|
||||
invulnerableCounter = PLAYER_INVULNERABLE_COUNTER;
|
||||
powerUp = false;
|
||||
powerUpCounter = PLAYER_POWERUP_COUNTER;
|
||||
extraHit = 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
|
||||
velX = 0;
|
||||
velY = 0;
|
||||
|
||||
// Establece la velocidad base
|
||||
baseSpeed = 1.5;
|
||||
|
||||
// Establece la puntuación inicial
|
||||
score = 0;
|
||||
|
||||
// Establece el multiplicador de puntos inicial
|
||||
scoreMultiplier = 1.0f;
|
||||
|
||||
// Inicia el contador para la cadencia de disparo
|
||||
cooldown = 10;
|
||||
|
||||
// Establece la posición del sprite
|
||||
legsSprite->setPosX(posX);
|
||||
legsSprite->setPosY(posY);
|
||||
|
||||
bodySprite->setPosX(posX);
|
||||
bodySprite->setPosY(posY);
|
||||
|
||||
headSprite->setPosX(posX);
|
||||
headSprite->setPosY(posY);
|
||||
|
||||
// Selecciona un frame para pintar
|
||||
legsSprite->setCurrentAnimation("stand");
|
||||
bodySprite->setCurrentAnimation("stand");
|
||||
headSprite->setCurrentAnimation("stand");
|
||||
}
|
||||
|
||||
// Actua en consecuencia de la entrada recibida
|
||||
void Player::setInput(Uint8 input) {
|
||||
switch (input) {
|
||||
case input_left:
|
||||
velX = -baseSpeed;
|
||||
setWalkingStatus(PLAYER_STATUS_WALKING_LEFT);
|
||||
break;
|
||||
|
||||
case input_right:
|
||||
velX = baseSpeed;
|
||||
setWalkingStatus(PLAYER_STATUS_WALKING_RIGHT);
|
||||
break;
|
||||
|
||||
case input_fire_center:
|
||||
setFiringStatus(PLAYER_STATUS_FIRING_UP);
|
||||
break;
|
||||
|
||||
case input_fire_left:
|
||||
setFiringStatus(PLAYER_STATUS_FIRING_LEFT);
|
||||
break;
|
||||
|
||||
case input_fire_right:
|
||||
setFiringStatus(PLAYER_STATUS_FIRING_RIGHT);
|
||||
break;
|
||||
|
||||
default:
|
||||
velX = 0;
|
||||
setWalkingStatus(PLAYER_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
|
||||
posX += velX;
|
||||
|
||||
// Si el jugador abandona el area de juego por los laterales
|
||||
if ((posX < PLAY_AREA_LEFT - 5) || (posX + width > PLAY_AREA_RIGHT + 5)) { // Restaura su posición
|
||||
posX -= velX;
|
||||
}
|
||||
|
||||
// Actualiza la posición del sprite
|
||||
legsSprite->setPosX(getPosX());
|
||||
legsSprite->setPosY(posY);
|
||||
|
||||
bodySprite->setPosX(getPosX());
|
||||
bodySprite->setPosY(posY);
|
||||
|
||||
headSprite->setPosX(getPosX());
|
||||
headSprite->setPosY(posY);
|
||||
|
||||
fireSprite->setPosX(getPosX() - 2);
|
||||
fireSprite->setPosY(posY - 8);
|
||||
} else {
|
||||
deathSprite->update();
|
||||
|
||||
// Si el cadaver abandona el area de juego por los laterales
|
||||
if ((deathSprite->getPosX() < PLAY_AREA_LEFT) || (deathSprite->getPosX() + width > PLAY_AREA_RIGHT)) { // Restaura su posición
|
||||
const float vx = deathSprite->getVelX();
|
||||
deathSprite->setPosX(deathSprite->getPosX() - vx);
|
||||
|
||||
// Rebota
|
||||
deathSprite->setVelX(-vx);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Pinta el jugador en pantalla
|
||||
void Player::render() {
|
||||
if (isAlive()) {
|
||||
if (invulnerable) {
|
||||
if ((invulnerableCounter % 10) > 4) {
|
||||
if (powerUp) {
|
||||
fireSprite->render();
|
||||
}
|
||||
legsSprite->render();
|
||||
bodySprite->render();
|
||||
headSprite->render();
|
||||
}
|
||||
} else {
|
||||
if (powerUp) {
|
||||
fireSprite->render();
|
||||
}
|
||||
legsSprite->render();
|
||||
bodySprite->render();
|
||||
headSprite->render();
|
||||
}
|
||||
} else {
|
||||
deathSprite->render();
|
||||
}
|
||||
}
|
||||
|
||||
// Establece el estado del jugador cuando camina
|
||||
void Player::setWalkingStatus(Uint8 status) {
|
||||
// Si cambiamos de estado, reiniciamos la animación
|
||||
if (statusWalking != status) {
|
||||
statusWalking = status;
|
||||
// legsSprite->setCurrentFrame(0);
|
||||
}
|
||||
}
|
||||
|
||||
// Establece el estado del jugador cuando dispara
|
||||
void Player::setFiringStatus(Uint8 status) {
|
||||
// Si cambiamos de estado, reiniciamos la animación
|
||||
if (statusFiring != status) {
|
||||
statusFiring = 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 aBodyCoffees = "";
|
||||
std::string aHeadCoffees = "";
|
||||
if (coffees > 0) {
|
||||
aBodyCoffees = coffees == 1 ? "_1C" : "_2C";
|
||||
aHeadCoffees = "_1C";
|
||||
}
|
||||
|
||||
const std::string aPowerUp = powerUp ? "_pwr" : "";
|
||||
const std::string aWalking = statusWalking == PLAYER_STATUS_WALKING_STOP ? "stand" : "walk";
|
||||
const std::string aFiring = statusFiring == PLAYER_STATUS_FIRING_UP ? "centershoot" : "sideshoot";
|
||||
|
||||
const SDL_FlipMode flipWalk = statusWalking == PLAYER_STATUS_WALKING_RIGHT ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE;
|
||||
const SDL_FlipMode flipFire = statusFiring == PLAYER_STATUS_FIRING_RIGHT ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE;
|
||||
|
||||
// Establece la animación a partir de las cadenas
|
||||
legsSprite->setCurrentAnimation(aWalking);
|
||||
legsSprite->setFlip(flipWalk);
|
||||
if (statusFiring == PLAYER_STATUS_FIRING_NO) { // No esta disparando
|
||||
bodySprite->setCurrentAnimation(aWalking + aBodyCoffees + aPowerUp);
|
||||
bodySprite->setFlip(flipWalk);
|
||||
headSprite->setCurrentAnimation(aWalking + aHeadCoffees + aPowerUp);
|
||||
headSprite->setFlip(flipWalk);
|
||||
} else { // Está disparando
|
||||
bodySprite->setCurrentAnimation(aFiring + aBodyCoffees + aPowerUp);
|
||||
bodySprite->setFlip(flipFire);
|
||||
headSprite->setCurrentAnimation(aFiring + aHeadCoffees + aPowerUp);
|
||||
headSprite->setFlip(flipFire);
|
||||
}
|
||||
|
||||
// Actualiza las animaciones de los sprites
|
||||
legsSprite->animate();
|
||||
bodySprite->animate();
|
||||
headSprite->animate();
|
||||
|
||||
fireSprite->animate();
|
||||
fireSprite->setFlip(flipWalk);
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
int Player::getPosX() {
|
||||
return int(posX);
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
int Player::getPosY() {
|
||||
return posY;
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
int Player::getWidth() {
|
||||
return width;
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
int Player::getHeight() {
|
||||
return height;
|
||||
}
|
||||
|
||||
// Indica si el jugador puede disparar
|
||||
bool Player::canFire() {
|
||||
// Si el contador a llegado a cero, podemos disparar. En caso contrario decrementamos el contador
|
||||
if (cooldown > 0) {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// 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 (powerUp) {
|
||||
cooldown--;
|
||||
}
|
||||
} else {
|
||||
setFiringStatus(PLAYER_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
|
||||
Uint32 Player::getScore() {
|
||||
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
|
||||
bool Player::isAlive() {
|
||||
return alive;
|
||||
}
|
||||
|
||||
// Establece el valor de la variable
|
||||
void Player::setAlive(bool value) {
|
||||
alive = value;
|
||||
|
||||
if (!value) {
|
||||
deathSprite->setPosX(headSprite->getRect().x);
|
||||
deathSprite->setPosY(headSprite->getRect().y);
|
||||
deathSprite->setAccelY(0.2f);
|
||||
deathSprite->setVelY(-6.6f);
|
||||
deathSprite->setVelX(3.3f);
|
||||
if (rand() % 2 == 0) {
|
||||
deathSprite->setVelX(-3.3f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
float Player::getScoreMultiplier() {
|
||||
return scoreMultiplier;
|
||||
}
|
||||
|
||||
// Establece el valor de la variable
|
||||
void Player::setScoreMultiplier(float value) {
|
||||
scoreMultiplier = value;
|
||||
}
|
||||
|
||||
// Aumenta el valor de la variable hasta un máximo
|
||||
void Player::incScoreMultiplier() {
|
||||
if (scoreMultiplier < 5.0f) {
|
||||
scoreMultiplier += 0.1f;
|
||||
} else {
|
||||
scoreMultiplier = 5.0f;
|
||||
}
|
||||
}
|
||||
|
||||
// Decrementa el valor de la variable hasta un mínimo
|
||||
void Player::decScoreMultiplier() {
|
||||
if (scoreMultiplier > 1.0f) {
|
||||
scoreMultiplier -= 0.1f;
|
||||
} else {
|
||||
scoreMultiplier = 1.0f;
|
||||
}
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
bool Player::isInvulnerable() {
|
||||
return invulnerable;
|
||||
}
|
||||
|
||||
// Establece el valor de la variable
|
||||
void Player::setInvulnerable(bool value) {
|
||||
invulnerable = value;
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
Uint16 Player::getInvulnerableCounter() {
|
||||
return invulnerableCounter;
|
||||
}
|
||||
|
||||
// Establece el valor de la variable
|
||||
void Player::setInvulnerableCounter(Uint16 value) {
|
||||
invulnerableCounter = value;
|
||||
}
|
||||
|
||||
// Actualiza el valor de la variable
|
||||
void Player::updateInvulnerableCounter() {
|
||||
if (invulnerable) {
|
||||
if (invulnerableCounter > 0) {
|
||||
invulnerableCounter--;
|
||||
} else {
|
||||
invulnerable = false;
|
||||
invulnerableCounter = PLAYER_INVULNERABLE_COUNTER;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza el valor de la variable
|
||||
void Player::updateDeathCounter() {
|
||||
if (!alive) {
|
||||
if (deathCounter > 0) {
|
||||
deathCounter--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
bool Player::isPowerUp() {
|
||||
return powerUp;
|
||||
}
|
||||
|
||||
// Establece el valor de la variable
|
||||
void Player::setPowerUp(bool value) {
|
||||
powerUp = value;
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
Uint16 Player::getPowerUpCounter() {
|
||||
return powerUpCounter;
|
||||
}
|
||||
|
||||
// Establece el valor de la variable
|
||||
void Player::setPowerUpCounter(Uint16 value) {
|
||||
powerUpCounter = value;
|
||||
}
|
||||
|
||||
// Actualiza el valor de la variable
|
||||
void Player::updatePowerUpCounter() {
|
||||
if ((powerUpCounter > 0) && (powerUp)) {
|
||||
powerUpCounter--;
|
||||
} else {
|
||||
powerUp = false;
|
||||
powerUpCounter = PLAYER_POWERUP_COUNTER;
|
||||
}
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
bool Player::hasExtraHit() {
|
||||
return extraHit;
|
||||
}
|
||||
|
||||
// Concede un toque extra al jugador
|
||||
void Player::giveExtraHit() {
|
||||
extraHit = true;
|
||||
coffees++;
|
||||
if (coffees > 2) {
|
||||
coffees = 2;
|
||||
}
|
||||
}
|
||||
|
||||
// Quita el toque extra al jugador
|
||||
void Player::removeExtraHit() {
|
||||
if (coffees > 0) {
|
||||
coffees--;
|
||||
}
|
||||
if (coffees == 0) {
|
||||
extraHit = false;
|
||||
}
|
||||
invulnerable = true;
|
||||
invulnerableCounter = PLAYER_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
|
||||
Uint8 Player::getCoffees() {
|
||||
return coffees;
|
||||
}
|
||||
|
||||
// Obtiene el circulo de colisión
|
||||
circle_t &Player::getCollider() {
|
||||
return collider;
|
||||
}
|
||||
|
||||
// Actualiza el circulo de colisión a la posición del jugador
|
||||
void Player::shiftColliders() {
|
||||
collider.x = int(posX + (width / 2));
|
||||
collider.y = int(posY + (height / 2));
|
||||
}
|
||||
|
||||
// Obtiene el puntero a la textura con los gráficos de la animación de morir
|
||||
Texture *Player::getDeadTexture() {
|
||||
return deathSprite->getTexture();
|
||||
;
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
Uint16 Player::getDeathCounter() {
|
||||
return deathCounter;
|
||||
}
|
||||
|
||||
// Actualiza el valor de la variable
|
||||
void Player::updatePowerUpHeadOffset() {
|
||||
if (!powerUp) {
|
||||
// powerUpHeadOffset = 0;
|
||||
} else {
|
||||
// powerUpHeadOffset = 96;
|
||||
if (powerUpCounter < 300) {
|
||||
if (powerUpCounter % 10 > 4) {
|
||||
// powerUpHeadOffset = 96;
|
||||
fireSprite->setEnabled(false);
|
||||
} else {
|
||||
// powerUpHeadOffset = 0;
|
||||
fireSprite->setEnabled(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Pone las texturas del jugador
|
||||
void Player::setPlayerTextures(std::vector<Texture *> texture) {
|
||||
headSprite->setTexture(texture[0]);
|
||||
bodySprite->setTexture(texture[1]);
|
||||
legsSprite->setTexture(texture[2]);
|
||||
deathSprite->setTexture(texture[3]);
|
||||
fireSprite->setTexture(texture[4]);
|
||||
}
|
||||
217
source/game/entities/player.h
Normal file
217
source/game/entities/player.h
Normal file
@@ -0,0 +1,217 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <string> // for string
|
||||
#include <vector> // for vector
|
||||
|
||||
#include "utils/utils.h" // for circle_t
|
||||
class AnimatedSprite;
|
||||
class Texture;
|
||||
|
||||
// Contadores
|
||||
constexpr int DEATH_COUNTER = 350;
|
||||
|
||||
// Estados del jugador
|
||||
constexpr int PLAYER_STATUS_WALKING_LEFT = 0;
|
||||
constexpr int PLAYER_STATUS_WALKING_RIGHT = 1;
|
||||
constexpr int PLAYER_STATUS_WALKING_STOP = 2;
|
||||
|
||||
constexpr int PLAYER_STATUS_FIRING_UP = 0;
|
||||
constexpr int PLAYER_STATUS_FIRING_LEFT = 1;
|
||||
constexpr int PLAYER_STATUS_FIRING_RIGHT = 2;
|
||||
constexpr int PLAYER_STATUS_FIRING_NO = 3;
|
||||
|
||||
// Variables del jugador
|
||||
constexpr int PLAYER_INVULNERABLE_COUNTER = 200;
|
||||
constexpr int PLAYER_POWERUP_COUNTER = 1500;
|
||||
|
||||
// Clase Player
|
||||
class Player {
|
||||
private:
|
||||
// Objetos y punteros
|
||||
SDL_Renderer *renderer; // El renderizador de la ventana
|
||||
AnimatedSprite *headSprite; // Sprite para dibujar la cabeza
|
||||
AnimatedSprite *bodySprite; // Sprite para dibujar el cuerpo
|
||||
AnimatedSprite *legsSprite; // Sprite para dibujar las piernas
|
||||
AnimatedSprite *deathSprite; // Sprite para dibujar el jugador derrotado
|
||||
AnimatedSprite *fireSprite; // Sprite para dibujar el aura del jugador con el poder a tope
|
||||
|
||||
// Variables
|
||||
float posX; // Posicion en el eje X
|
||||
int posY; // Posicion en el eje Y
|
||||
|
||||
Uint8 width; // Anchura
|
||||
Uint8 height; // Altura
|
||||
|
||||
float velX; // Cantidad de pixeles a desplazarse en el eje X
|
||||
int velY; // Cantidad de pixeles a desplazarse en el eje Y
|
||||
|
||||
float baseSpeed; // Velocidad base del jugador
|
||||
int cooldown; // Contador durante el cual no puede disparar
|
||||
|
||||
Uint32 score; // Puntos del jugador
|
||||
float scoreMultiplier; // Multiplicador de puntos
|
||||
|
||||
Uint8 statusWalking; // Estado del jugador
|
||||
Uint8 statusFiring; // Estado del jugador
|
||||
|
||||
bool alive; // Indica si el jugador está vivo
|
||||
Uint16 deathCounter; // Contador para la animación de morirse
|
||||
bool invulnerable; // Indica si el jugador es invulnerable
|
||||
Uint16 invulnerableCounter; // Contador para la invulnerabilidad
|
||||
bool extraHit; // Indica si el jugador tiene un toque extra
|
||||
Uint8 coffees; // Indica cuantos cafes lleva acumulados
|
||||
bool powerUp; // Indica si el jugador tiene activo el modo PowerUp
|
||||
Uint16 powerUpCounter; // Temporizador para el modo PowerUp
|
||||
bool input; // Indica si puede recibir ordenes de entrada
|
||||
circle_t collider; // Circulo de colisión del jugador
|
||||
|
||||
// Actualiza el circulo de colisión a la posición del jugador
|
||||
void shiftColliders();
|
||||
|
||||
// Actualiza el valor de la variable
|
||||
void updateInvulnerableCounter();
|
||||
|
||||
// Actualiza el valor de la variable
|
||||
void updateDeathCounter();
|
||||
|
||||
// Actualiza el valor de la variable
|
||||
void updatePowerUpHeadOffset();
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
Player(float x, int y, SDL_Renderer *renderer, std::vector<Texture *> texture, std::vector<std::vector<std::string> *> animations);
|
||||
|
||||
// Destructor
|
||||
~Player();
|
||||
|
||||
// Iniciador
|
||||
void init();
|
||||
|
||||
// Actualiza al jugador a su posicion, animación y controla los contadores
|
||||
void update();
|
||||
|
||||
// Pinta el jugador en pantalla
|
||||
void render();
|
||||
|
||||
// Pone las texturas del jugador
|
||||
void setPlayerTextures(std::vector<Texture *> texture);
|
||||
|
||||
// Actua en consecuencia de la entrada recibida
|
||||
void setInput(Uint8 input);
|
||||
|
||||
// Mueve el jugador a la posición y animación que le corresponde
|
||||
void move();
|
||||
|
||||
// Establece el estado del jugador
|
||||
void setWalkingStatus(Uint8 status);
|
||||
|
||||
// Establece el estado del jugador
|
||||
void setFiringStatus(Uint8 status);
|
||||
|
||||
// Establece la animación correspondiente al estado
|
||||
void setAnimation();
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
int getPosX();
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
int getPosY();
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
int getWidth();
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
int getHeight();
|
||||
|
||||
// Indica si el jugador puede disparar
|
||||
bool canFire();
|
||||
|
||||
// Establece el valor de la variable
|
||||
void setFireCooldown(int time);
|
||||
|
||||
// Actualiza el valor de la variable
|
||||
void updateCooldown();
|
||||
|
||||
// Obtiene la puntuación del jugador
|
||||
Uint32 getScore();
|
||||
|
||||
// Asigna un valor a la puntuación del jugador
|
||||
void setScore(Uint32 score);
|
||||
|
||||
// Incrementa la puntuación del jugador
|
||||
void addScore(Uint32 score);
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
bool isAlive();
|
||||
|
||||
// Establece el valor de la variable
|
||||
void setAlive(bool value);
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
float getScoreMultiplier();
|
||||
|
||||
// Establece el valor de la variable
|
||||
void setScoreMultiplier(float value);
|
||||
|
||||
// Aumenta el valor de la variable hasta un máximo
|
||||
void incScoreMultiplier();
|
||||
|
||||
// Decrementa el valor de la variable hasta un mínimo
|
||||
void decScoreMultiplier();
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
bool isInvulnerable();
|
||||
|
||||
// Establece el valor de la variable
|
||||
void setInvulnerable(bool value);
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
Uint16 getInvulnerableCounter();
|
||||
|
||||
// Establece el valor de la variable
|
||||
void setInvulnerableCounter(Uint16 value);
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
bool isPowerUp();
|
||||
|
||||
// Establece el valor de la variable
|
||||
void setPowerUp(bool value);
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
Uint16 getPowerUpCounter();
|
||||
|
||||
// Establece el valor de la variable
|
||||
void setPowerUpCounter(Uint16 value);
|
||||
|
||||
// Actualiza el valor de la variable
|
||||
void updatePowerUpCounter();
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
bool hasExtraHit();
|
||||
|
||||
// Concede un toque extra al jugador
|
||||
void giveExtraHit();
|
||||
|
||||
// Quita el toque extra al jugador
|
||||
void removeExtraHit();
|
||||
|
||||
// Habilita la entrada de ordenes
|
||||
void enableInput();
|
||||
|
||||
// Deshabilita la entrada de ordenes
|
||||
void disableInput();
|
||||
|
||||
// Devuelve el numero de cafes actuales
|
||||
Uint8 getCoffees();
|
||||
|
||||
// Obtiene el circulo de colisión
|
||||
circle_t &getCollider();
|
||||
|
||||
// Obtiene el puntero a la textura con los gráficos de la animación de morir
|
||||
Texture *getDeadTexture();
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
Uint16 getDeathCounter();
|
||||
};
|
||||
Reference in New Issue
Block a user