From dee5bcb4e47b385954138681434657b9fad9d174 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Valor=20Mart=C3=ADnez?= Date: Mon, 30 Sep 2024 23:06:16 +0200 Subject: [PATCH] =?UTF-8?q?Canviada=20la=20classe=20bullet=20per=20la=20de?= =?UTF-8?q?=20chatGPT,=20aixina=20dem=C3=A0=20ho=20mire=20amb=20calma?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/balloon.cpp | 1 - source/bullet.cpp | 245 ++++++++++++--------------------------------- source/bullet.h | 104 ++++++++----------- source/game.cpp | 18 ++-- source/game.h | 3 +- 5 files changed, 116 insertions(+), 255 deletions(-) diff --git a/source/balloon.cpp b/source/balloon.cpp index 4a68491..6031391 100644 --- a/source/balloon.cpp +++ b/source/balloon.cpp @@ -1,5 +1,4 @@ #include "balloon.h" -#include // for abs #include // for abs #include "animated_sprite.h" // for AnimatedSprite #include "moving_sprite.h" // for MovingSprite diff --git a/source/bullet.cpp b/source/bullet.cpp index 3f62c9a..11dfec0 100644 --- a/source/bullet.cpp +++ b/source/bullet.cpp @@ -1,217 +1,98 @@ #include "bullet.h" #include "param.h" // for param #include "sprite.h" // for Sprite -class Texture; +#include // for std::unique_ptr + +// Constantes evaluables en tiempo de compilación +constexpr int BULLET_WIDTH = 12; +constexpr int BULLET_HEIGHT = 12; +constexpr int BULLET_VELY = -3; +constexpr int BULLET_VELX_LEFT = -2; +constexpr int BULLET_VELX_RIGHT = 2; // Constructor -Bullet::Bullet(int x, int y, int kind, bool poweredUp, int owner, SDL_Rect *playArea, Texture *texture) +Bullet::Bullet(int x, int y, BulletType kind, bool poweredUp, int owner, SDL_Rect* playArea, Texture* texture) + : posX(x), posY(y), width(BULLET_WIDTH), height(BULLET_HEIGHT), velX(0), velY(BULLET_VELY), + kind(kind), owner(owner), playArea(playArea), + sprite(std::unique_ptr(new Sprite(SDL_Rect{x, y, BULLET_WIDTH, BULLET_HEIGHT}, texture))) // Crear manualmente el std::unique_ptr { - // Rectangulo con la zona de juego - this->playArea = playArea; + velX = (kind == BulletType::LEFT) ? BULLET_VELX_LEFT : (kind == BulletType::RIGHT) ? BULLET_VELX_RIGHT : 0; - // Posición inicial del objeto - posX = x; - posY = y; + auto spriteOffset = poweredUp ? 3 : 0; + auto kindIndex = static_cast(kind); + sprite->setSpriteClip((kindIndex + spriteOffset) * width, 0, sprite->getWidth(), sprite->getHeight()); - // Alto y ancho del objeto - width = 12; - height = 12; - - // Crea el sprite - sprite = new Sprite({x, y, width, height}, texture); - - // 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(); + collider.r = width / 2; + shiftColliders(); } -// Destructor -Bullet::~Bullet() -{ - delete sprite; +// Implementación de render (llama al render del sprite) +void Bullet::render() { + sprite->render(); } -// Pinta el objeto en pantalla -void Bullet::render() -{ - sprite->render(); +// Implementación del movimiento usando BulletMoveStatus +BulletMoveStatus Bullet::move() { + posX += velX; + if (posX < param.game.playArea.rect.x - width || posX > playArea->w) { + disable(); + return BulletMoveStatus::OUT; + } + + posY += velY; + if (posY < param.game.playArea.rect.y - height) { + disable(); + return BulletMoveStatus::OUT; + } + + sprite->setPosX(posX); + sprite->setPosY(posY); + shiftColliders(); + + return BulletMoveStatus::OK; } -// 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 < param.game.playArea.rect.x - width) || (posX > playArea->w)) - { - // Se deshabilita - kind = BULLET_NULL; - - // 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 < param.game.playArea.rect.y - height) - { - // Se deshabilita - kind = BULLET_NULL; - - // 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; +bool Bullet::isEnabled() const { + return kind != BulletType::NULL_TYPE; } -// Comprueba si el objeto está habilitado -bool Bullet::isEnabled() -{ - if (kind == BULLET_NULL) - { - return false; - } - else - { - return true; - } +void Bullet::disable() { + kind = BulletType::NULL_TYPE; } -// Deshabilita el objeto -void Bullet::disable() -{ - kind = BULLET_NULL; +int Bullet::getPosX() const { + return posX; } -// Obtiene el valor de la variable -int Bullet::getPosX() -{ - return posX; +int Bullet::getPosY() const { + return posY; } -// Obtiene el valor de la variable -int Bullet::getPosY() -{ - return posY; +void Bullet::setPosX(int x) { + posX = x; } -// Establece el valor de la variable -void Bullet::setPosX(int x) -{ - posX = x; +void Bullet::setPosY(int y) { + posY = y; } -// Establece el valor de la variable -void Bullet::setPosY(int y) -{ - posY = y; +int Bullet::getVelY() const { + return velY; } -// Obtiene el valor de la variable -int Bullet::getVelY() -{ - return velY; +BulletType Bullet::getKind() const { + return kind; } -// Obtiene el valor de la variable -int Bullet::getKind() -{ - return kind; +int Bullet::getOwner() const { + return owner; } -// Obtiene el valor de la variable -int Bullet::getOwner() -{ - return owner; +circle_t& Bullet::getCollider() { + return collider; } -// 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; +void Bullet::shiftColliders() { + collider.x = posX + collider.r; + collider.y = posY + collider.r; } diff --git a/source/bullet.h b/source/bullet.h index c1722bf..5bcf444 100644 --- a/source/bullet.h +++ b/source/bullet.h @@ -1,83 +1,63 @@ #pragma once -#include // for SDL_Rect -#include // for Uint8 -#include "utils.h" // for circle_t +#include // for SDL_Rect +#include // for Uint8 +#include "utils.h" // for circle_t +#include // for std::unique_ptr + class Sprite; class Texture; -// Tipos de bala -#define BULLET_UP 1 -#define BULLET_LEFT 2 -#define BULLET_RIGHT 3 -#define BULLET_NULL 4 +// Enumeración para los diferentes tipos de balas +enum class BulletType +{ + UP, + LEFT, + RIGHT, + NULL_TYPE +}; -// Tipos de retorno de la funcion move de la bala -#define BULLET_MOVE_OK 0 -#define BULLET_MOVE_OUT 1 +// Enumeración para los resultados del movimiento de la bala +enum class BulletMoveStatus : Uint8 +{ + OK = 0, + OUT = 1 +}; // Clase Bullet class Bullet { private: - // Objetos y punteros - Sprite *sprite; // Sprite con los graficos y métodos de pintado + 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 + BulletType kind; // Tipo de objeto + int owner; // Identificador del dueño del objeto + circle_t collider; // Círculo de colisión del objeto + SDL_Rect *playArea; // Rectángulo con la zona de juego + std::unique_ptr sprite; // Sprite con los gráficos 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 - SDL_Rect *playArea; // Rectangulo con la zona de juego - - // Alinea el circulo de colisión con el objeto - void shiftColliders(); + void shiftColliders(); // Alinea el círculo de colisión con el objeto public: - // Constructor - Bullet(int x, int y, int kind, bool poweredUp, int owner, SDL_Rect *playArea, Texture *texture); + Bullet(int x, int y, BulletType kind, bool poweredUp, int owner, SDL_Rect *playArea, Texture *texture); + ~Bullet() = default; - // Destructor - ~Bullet(); + void render(); // Pinta el objeto en pantalla + BulletMoveStatus move(); // Actualiza la posición y estado del objeto - // Pinta el objeto en pantalla - void render(); + bool isEnabled() const; // Comprueba si el objeto está habilitado + void disable(); // Deshabilita el objeto - // 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 + int getPosX() const; + int getPosY() const; 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 + int getVelY() const; + BulletType getKind() const; + int getOwner() const; circle_t &getCollider(); }; diff --git a/source/game.cpp b/source/game.cpp index e125f1a..523a2f5 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -12,7 +12,7 @@ #include "asset.h" // for Asset #include "background.h" // for Background #include "balloon.h" // for Balloon, BALLOON_SPEED_1, BALLOON_... -#include "bullet.h" // for Bullet, BULLET_LEFT, BULLET_RIGHT +#include "bullet.h" // for Bullet, BulletType::LEFT, BulletType::RIGHT #include "enemy_formations.h" // for stage_t, EnemyFormations, enemyIni... #include "explosions.h" // for Explosions #include "fade.h" // for Fade, FADE_RANDOM_SQUARE, FADE_VEN... @@ -1476,7 +1476,7 @@ void Game::moveBullets() { if (bullet->isEnabled()) { - if (bullet->move() == BULLET_MOVE_OUT) + if (bullet->move() == BulletMoveStatus::OUT) { Player *player = getPlayer(bullet->getOwner()); player->decScoreMultiplier(); @@ -1498,7 +1498,7 @@ void Game::renderBullets() } // Crea un objeto bala -void Game::createBullet(int x, int y, int kind, bool poweredUp, int owner) +void Game::createBullet(int x, int y, BulletType kind, bool poweredUp, int owner) { Bullet *b = new Bullet(x, y, kind, poweredUp, owner, &(param.game.playArea.rect), bulletTexture); bullets.push_back(b); @@ -2096,7 +2096,7 @@ void Game::checkInput() if (player->canFire()) { player->setInput(input_fire_center); - createBullet(player->getPosX() + (player->getWidth() / 2) - 4, player->getPosY() + (player->getHeight() / 2), BULLET_UP, player->isPowerUp(), player->getId()); + createBullet(player->getPosX() + (player->getWidth() / 2) - 4, player->getPosY() + (player->getHeight() / 2), BulletType::UP, player->isPowerUp(), player->getId()); player->setFireCooldown(10); } } @@ -2106,7 +2106,7 @@ void Game::checkInput() if (player->canFire()) { player->setInput(input_fire_left); - createBullet(player->getPosX() + (player->getWidth() / 2) - 4, player->getPosY() + (player->getHeight() / 2), BULLET_LEFT, player->isPowerUp(), player->getId()); + createBullet(player->getPosX() + (player->getWidth() / 2) - 4, player->getPosY() + (player->getHeight() / 2), BulletType::LEFT, player->isPowerUp(), player->getId()); player->setFireCooldown(10); } } @@ -2116,7 +2116,7 @@ void Game::checkInput() if (player->canFire()) { player->setInput(input_fire_right); - createBullet(player->getPosX() + (player->getWidth() / 2) - 4, player->getPosY() + (player->getHeight() / 2), BULLET_RIGHT, player->isPowerUp(), player->getId()); + createBullet(player->getPosX() + (player->getWidth() / 2) - 4, player->getPosY() + (player->getHeight() / 2), BulletType::RIGHT, player->isPowerUp(), player->getId()); player->setFireCooldown(10); } } @@ -2183,7 +2183,7 @@ void Game::checkInput() if (player->canFire()) { player->setInput(input_fire_center); - createBullet(player->getPosX() + (player->getWidth() / 2) - 4, player->getPosY() + (player->getHeight() / 2), BULLET_UP, player->isPowerUp(), player->getId()); + createBullet(player->getPosX() + (player->getWidth() / 2) - 4, player->getPosY() + (player->getHeight() / 2), BulletType::UP, player->isPowerUp(), player->getId()); player->setFireCooldown(10); // Reproduce el sonido de disparo @@ -2200,7 +2200,7 @@ void Game::checkInput() if (player->canFire()) { player->setInput(input_fire_left); - createBullet(player->getPosX() + (player->getWidth() / 2) - 4, player->getPosY() + (player->getHeight() / 2), BULLET_LEFT, player->isPowerUp(), player->getId()); + createBullet(player->getPosX() + (player->getWidth() / 2) - 4, player->getPosY() + (player->getHeight() / 2), BulletType::LEFT, player->isPowerUp(), player->getId()); player->setFireCooldown(10); // Reproduce el sonido de disparo @@ -2217,7 +2217,7 @@ void Game::checkInput() if (player->canFire()) { player->setInput(input_fire_right); - createBullet(player->getPosX() + (player->getWidth() / 2) - 4, player->getPosY() + (player->getHeight() / 2), BULLET_RIGHT, player->isPowerUp(), player->getId()); + createBullet(player->getPosX() + (player->getWidth() / 2) - 4, player->getPosY() + (player->getHeight() / 2), BulletType::RIGHT, player->isPowerUp(), player->getId()); player->setFireCooldown(10); // Reproduce el sonido de disparo diff --git a/source/game.h b/source/game.h index 7f150e6..eb059ee 100644 --- a/source/game.h +++ b/source/game.h @@ -7,6 +7,7 @@ #include // for vector #include "section.h" // for options_e #include "utils.h" // for demoKeys_t, color_t, hiScoreEntry_t +#include "bullet.h" class Asset; class Background; class Balloon; @@ -312,7 +313,7 @@ private: void renderBullets(); // Crea un objeto bala - void createBullet(int x, int y, int kind, bool poweredUp, int owner); + void createBullet(int x, int y, BulletType kind, bool poweredUp, int owner); // Vacia el vector de balas void freeBullets();