Repasada la classe Bullet

This commit is contained in:
2024-10-26 09:01:32 +02:00
parent bffd2bdace
commit 4f095ab018
4 changed files with 48 additions and 90 deletions

View File

@@ -4,33 +4,23 @@
#include "sprite.h" // para Sprite
class Texture;
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 powered_up, int owner, SDL_Rect *play_area, std::shared_ptr<Texture> texture)
: sprite_(std::make_unique<Sprite>(texture, SDL_Rect{x, y, BULLET_WIDTH, BULLET_HEIGHT})),
Bullet::Bullet(int x, int y, BulletType bullet_type, bool powered_up, int owner, std::shared_ptr<Texture> texture)
: sprite_(std::make_unique<Sprite>(texture, SDL_Rect{x, y, BULLET_WIDTH_, BULLET_HEIGHT_})),
pos_x_(x),
pos_y_(y),
width_(BULLET_WIDTH),
height_(BULLET_HEIGHT),
vel_x_(0),
vel_y_(BULLET_VELY),
kind_(kind),
owner_(owner),
play_area_(play_area)
bullet_type_(bullet_type),
owner_(owner)
{
vel_x_ = (kind_ == BulletType::LEFT) ? BULLET_VELX_LEFT : (kind_ == BulletType::RIGHT) ? BULLET_VELX_RIGHT
: 0;
vel_x_ = (bullet_type_ == BulletType::LEFT) ? BULLET_VEL_X_LEFT_
: (bullet_type_ == BulletType::RIGHT) ? BULLET_VEL_X_RIGHT_
: 0;
auto sprite_offset = powered_up ? 3 : 0;
auto kind_index = static_cast<int>(kind);
sprite_->setSpriteClip((kind_index + sprite_offset) * width_, 0, sprite_->getWidth(), sprite_->getHeight());
int sprite_offset = powered_up ? 3 : 0;
int offset = (static_cast<int>(bullet_type) + sprite_offset) * BULLET_WIDTH_;
sprite_->setSpriteClip(offset, 0, BULLET_WIDTH_, BULLET_HEIGHT_);
collider_.r = width_ / 2;
collider_.r = BULLET_WIDTH_ / 2;
shiftColliders();
}
@@ -44,21 +34,20 @@ void Bullet::render()
BulletMoveStatus Bullet::move()
{
pos_x_ += vel_x_;
if (pos_x_ < param.game.play_area.rect.x - width_ || pos_x_ > play_area_->w)
if (pos_x_ < param.game.play_area.rect.x - BULLET_WIDTH_ || pos_x_ > param.game.play_area.rect.w)
{
disable();
return BulletMoveStatus::OUT;
}
pos_y_ += vel_y_;
if (pos_y_ < param.game.play_area.rect.y - height_)
pos_y_ += BULLET_VEL_Y_;
if (pos_y_ < param.game.play_area.rect.y - BULLET_HEIGHT_)
{
disable();
return BulletMoveStatus::OUT;
}
sprite_->setX(pos_x_);
sprite_->setY(pos_y_);
shiftSprite();
shiftColliders();
return BulletMoveStatus::OK;
@@ -66,42 +55,12 @@ BulletMoveStatus Bullet::move()
bool Bullet::isEnabled() const
{
return kind_ != BulletType::NONE;
return bullet_type_ != BulletType::NONE;
}
void Bullet::disable()
{
kind_ = BulletType::NONE;
}
int Bullet::getPosX() const
{
return pos_x_;
}
int Bullet::getPosY() const
{
return pos_y_;
}
void Bullet::setPosX(int x)
{
pos_x_ = x;
}
void Bullet::setPosY(int y)
{
pos_y_ = y;
}
int Bullet::getVelY() const
{
return vel_y_;
}
BulletType Bullet::getKind() const
{
return kind_;
bullet_type_ = BulletType::NONE;
}
int Bullet::getOwner() const
@@ -119,3 +78,9 @@ void Bullet::shiftColliders()
collider_.x = pos_x_ + collider_.r;
collider_.y = pos_y_ + collider_.r;
}
void Bullet::shiftSprite()
{
sprite_->setX(pos_x_);
sprite_->setY(pos_y_);
}

View File

@@ -7,8 +7,8 @@
#include "utils.h" // para Circle
class Texture;
// Enumeración para los diferentes tipos de balas
enum class BulletType
// Tipos de balas
enum class BulletType : Uint8
{
UP,
LEFT,
@@ -16,7 +16,7 @@ enum class BulletType
NONE
};
// Enumeración para los resultados del movimiento de la bala
// Resultado del movimiento de la bala
enum class BulletMoveStatus : Uint8
{
OK = 0,
@@ -27,26 +27,29 @@ enum class BulletMoveStatus : Uint8
class Bullet
{
private:
// Constantes
static constexpr int BULLET_WIDTH_ = 12;
static constexpr int BULLET_HEIGHT_ = 12;
static constexpr int BULLET_VEL_Y_ = -3;
static constexpr int BULLET_VEL_X_LEFT_ = -2;
static constexpr int BULLET_VEL_X_RIGHT_ = 2;
std::unique_ptr<Sprite> sprite_; // Sprite con los gráficos y métodos de pintado
int pos_x_; // Posición en el eje X
int pos_y_; // Posición en el eje Y
Uint8 width_; // Ancho del objeto
Uint8 height_; // Alto del objeto
int pos_x_; // Posición en el eje X
int pos_y_; // Posición en el eje Y
int vel_x_; // Velocidad en el eje X
int vel_x_; // Velocidad en el eje X
int vel_y_; // Velocidad en el eje Y
BulletType kind_; // Tipo de objeto
int owner_; // Identificador del dueño del objeto
Circle collider_; // Círculo de colisión del objeto
SDL_Rect *play_area_; // Rectángulo con la zona de juego
BulletType bullet_type_; // Tipo de objeto
int owner_; // Identificador del dueño del objeto
Circle collider_; // Círculo de colisión del objeto
void shiftColliders(); // Alinea el círculo de colisión con el objeto
void shiftSprite(); // Alinea el sprite con el objeto
public:
// Constructor
Bullet(int x, int y, BulletType kind, bool powered_up, int owner, SDL_Rect *play_area, std::shared_ptr<Texture> texture);
Bullet(int x, int y, BulletType bullet_type, bool powered_up, int owner, std::shared_ptr<Texture> texture);
// Destructor
~Bullet() = default;
@@ -63,17 +66,7 @@ public:
// Deshabilita el objeto
void disable();
// Obtiene la posición
int getPosX() const;
int getPosY() const;
// Establece la posición
void setPosX(int x);
void setPosY(int y);
// Obtiene parámetros
int getVelY() const;
BulletType getKind() const;
int getOwner() const;
Circle &getCollider();
};

View File

@@ -841,7 +841,7 @@ void Game::renderBullets()
// Crea un objeto bala
void Game::createBullet(int x, int y, BulletType kind, bool powered_up, int owner)
{
bullets_.emplace_back(std::make_unique<Bullet>(x, y, kind, powered_up, owner, &(param.game.play_area.rect), bullet_texture_));
bullets_.emplace_back(std::make_unique<Bullet>(x, y, kind, powered_up, owner, bullet_texture_));
}
// Vacia el vector de balas
@@ -1943,7 +1943,7 @@ void Game::handleFireInput(const std::shared_ptr<Player> &player, BulletType bul
{
player->setInput(bulletType == BulletType::UP ? InputType::FIRE_CENTER : bulletType == BulletType::LEFT ? InputType::FIRE_LEFT
: InputType::FIRE_RIGHT);
createBullet(player->getPosX() + (player->getWidth() / 2) - 4, player->getPosY() + (player->getHeight() / 2), bulletType, player->isPowerUp(), player->getId());
createBullet(player->getPosX() + (player->getWidth() / 2) - 6, player->getPosY() + (player->getHeight() / 2), bulletType, player->isPowerUp(), player->getId());
JA_PlaySound(Resource::get()->getSound("bullet.wav"));
player->setFireCooldown(10); // Establece un tiempo de espera para el próximo disparo.
}

View File

@@ -22,9 +22,9 @@ class Screen;
class SmartSprite;
class Text;
class Texture;
enum class BulletType; // lines 26-26
struct JA_Music_t; // lines 27-27
struct JA_Sound_t; // lines 28-28
enum class BulletType : Uint8; // lines 26-26
struct JA_Music_t; // lines 27-27
struct JA_Sound_t; // lines 28-28
enum class ItemType;
// Modo demo
@@ -82,7 +82,7 @@ private:
static constexpr int ITEM_COFFEE_ODDS_ = 5;
static constexpr int ITEM_POWER_BALL_ODDS_ = 0;
static constexpr int ITEM_COFFEE_MACHINE_ODDS_ = 4;
// Estructuras
struct Helper
{
@@ -171,7 +171,7 @@ private:
float default_balloon_speed_; // Velocidad base de los enemigos, sin incrementar
float difficulty_score_multiplier_; // Multiplicador de puntos en función de la dificultad
float get_ready_bitmap_path_[STAGE_COUNTER_]; // Vector con los puntos X por donde se desplaza el texto
int stage_bitmap_path_[STAGE_COUNTER_]; // Vector con los puntos Y por donde se desplaza el texto
int stage_bitmap_path_[STAGE_COUNTER_]; // Vector con los puntos Y por donde se desplaza el texto
int balloon_deploy_counter_ = 0; // Cuando se lanza una formación, se le da un valor y no sale otra hasta que llegue a cero
int balloons_popped_ = 0; // Lleva la cuenta de los globos explotados
int counter_ = 0; // Contador para el juego