From af1c1051e649c756a87cf491f71a6209f58ff1fc Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Mon, 30 Sep 2024 20:28:20 +0200 Subject: [PATCH] Afegides implementacions (fora del codi) que ha fet chatGPT de la classe bullet --- bullet-chatGPT.cpp | 114 +++++++++++++++++++++++++++++++++++++++++++++ bullet-chatGPT.h | 62 ++++++++++++++++++++++++ 2 files changed, 176 insertions(+) create mode 100644 bullet-chatGPT.cpp create mode 100644 bullet-chatGPT.h diff --git a/bullet-chatGPT.cpp b/bullet-chatGPT.cpp new file mode 100644 index 0000000..5693401 --- /dev/null +++ b/bullet-chatGPT.cpp @@ -0,0 +1,114 @@ +#include "bullet.h" +#include "param.h" // for param +#include "sprite.h" // for Sprite +#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, BulletType kind, bool poweredUp, int owner, SDL_Rect *playArea, Texture *texture) + : posX(x), posY(y), kind(kind), owner(owner), playArea(playArea), + width(BULLET_WIDTH), height(BULLET_HEIGHT), velY(BULLET_VELY), + sprite(std::make_unique(SDL_Rect{x, y, BULLET_WIDTH, BULLET_HEIGHT}, texture)) +{ + velX = (kind == BulletType::LEFT) ? BULLET_VELX_LEFT : (kind == BulletType::RIGHT) ? BULLET_VELX_RIGHT + : 0; + + auto spriteOffset = poweredUp ? 3 : 0; + auto kindIndex = static_cast(kind); + sprite->setSpriteClip((kindIndex + spriteOffset) * width, 0, sprite->getWidth(), sprite->getHeight()); + + collider.r = width / 2; + shiftColliders(); +} + +// Implementación de render (llama al render del sprite) +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; +} + +bool Bullet::isEnabled() const +{ + return kind != BulletType::NULL_TYPE; +} + +void Bullet::disable() +{ + kind = BulletType::NULL_TYPE; +} + +int Bullet::getPosX() const +{ + return posX; +} + +int Bullet::getPosY() const +{ + return posY; +} + +void Bullet::setPosX(int x) +{ + posX = x; +} + +void Bullet::setPosY(int y) +{ + posY = y; +} + +int Bullet::getVelY() const +{ + return velY; +} + +BulletType Bullet::getKind() const +{ + return kind; +} + +int Bullet::getOwner() const +{ + return owner; +} + +circle_t &Bullet::getCollider() +{ + return collider; +} + +void Bullet::shiftColliders() +{ + collider.x = posX + collider.r; + collider.y = posY + collider.r; +} diff --git a/bullet-chatGPT.h b/bullet-chatGPT.h new file mode 100644 index 0000000..be23b96 --- /dev/null +++ b/bullet-chatGPT.h @@ -0,0 +1,62 @@ +#pragma once + +#include // for SDL_Rect +#include // for Uint8 +#include "utils.h" // for circle_t +#include // for std::unique_ptr + +class Sprite; +class Texture; + +// Enumeración para los diferentes tipos de balas +enum class BulletType { + UP, + LEFT, + RIGHT, + NULL_TYPE +}; + +// Enumeración para los resultados del movimiento de la bala +enum class BulletMoveStatus : Uint8 { + OK = 0, + OUT = 1 +}; + +// Clase Bullet +class Bullet +{ +private: + std::unique_ptr sprite; // Sprite con los gráficos 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 + + void shiftColliders(); // Alinea el círculo de colisión con el objeto + +public: + Bullet(int x, int y, BulletType kind, bool poweredUp, int owner, SDL_Rect *playArea, Texture *texture); + ~Bullet() = default; + + void render(); // Pinta el objeto en pantalla + BulletMoveStatus move(); // Actualiza la posición y estado del objeto + + bool isEnabled() const; // Comprueba si el objeto está habilitado + void disable(); // Deshabilita el objeto + + int getPosX() const; + int getPosY() const; + void setPosX(int x); + void setPosY(int y); + int getVelY() const; + BulletType getKind() const; + int getOwner() const; + circle_t& getCollider(); +};