Repasada la classe Bullet
This commit is contained in:
@@ -4,33 +4,23 @@
|
|||||||
#include "sprite.h" // para Sprite
|
#include "sprite.h" // para Sprite
|
||||||
class Texture;
|
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
|
// Constructor
|
||||||
Bullet::Bullet(int x, int y, BulletType kind, bool powered_up, int owner, SDL_Rect *play_area, std::shared_ptr<Texture> texture)
|
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})),
|
: sprite_(std::make_unique<Sprite>(texture, SDL_Rect{x, y, BULLET_WIDTH_, BULLET_HEIGHT_})),
|
||||||
pos_x_(x),
|
pos_x_(x),
|
||||||
pos_y_(y),
|
pos_y_(y),
|
||||||
width_(BULLET_WIDTH),
|
bullet_type_(bullet_type),
|
||||||
height_(BULLET_HEIGHT),
|
owner_(owner)
|
||||||
vel_x_(0),
|
|
||||||
vel_y_(BULLET_VELY),
|
|
||||||
kind_(kind),
|
|
||||||
owner_(owner),
|
|
||||||
play_area_(play_area)
|
|
||||||
{
|
{
|
||||||
vel_x_ = (kind_ == BulletType::LEFT) ? BULLET_VELX_LEFT : (kind_ == BulletType::RIGHT) ? BULLET_VELX_RIGHT
|
vel_x_ = (bullet_type_ == BulletType::LEFT) ? BULLET_VEL_X_LEFT_
|
||||||
: 0;
|
: (bullet_type_ == BulletType::RIGHT) ? BULLET_VEL_X_RIGHT_
|
||||||
|
: 0;
|
||||||
|
|
||||||
auto sprite_offset = powered_up ? 3 : 0;
|
int sprite_offset = powered_up ? 3 : 0;
|
||||||
auto kind_index = static_cast<int>(kind);
|
int offset = (static_cast<int>(bullet_type) + sprite_offset) * BULLET_WIDTH_;
|
||||||
sprite_->setSpriteClip((kind_index + sprite_offset) * width_, 0, sprite_->getWidth(), sprite_->getHeight());
|
sprite_->setSpriteClip(offset, 0, BULLET_WIDTH_, BULLET_HEIGHT_);
|
||||||
|
|
||||||
collider_.r = width_ / 2;
|
collider_.r = BULLET_WIDTH_ / 2;
|
||||||
shiftColliders();
|
shiftColliders();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,21 +34,20 @@ void Bullet::render()
|
|||||||
BulletMoveStatus Bullet::move()
|
BulletMoveStatus Bullet::move()
|
||||||
{
|
{
|
||||||
pos_x_ += vel_x_;
|
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();
|
disable();
|
||||||
return BulletMoveStatus::OUT;
|
return BulletMoveStatus::OUT;
|
||||||
}
|
}
|
||||||
|
|
||||||
pos_y_ += vel_y_;
|
pos_y_ += BULLET_VEL_Y_;
|
||||||
if (pos_y_ < param.game.play_area.rect.y - height_)
|
if (pos_y_ < param.game.play_area.rect.y - BULLET_HEIGHT_)
|
||||||
{
|
{
|
||||||
disable();
|
disable();
|
||||||
return BulletMoveStatus::OUT;
|
return BulletMoveStatus::OUT;
|
||||||
}
|
}
|
||||||
|
|
||||||
sprite_->setX(pos_x_);
|
shiftSprite();
|
||||||
sprite_->setY(pos_y_);
|
|
||||||
shiftColliders();
|
shiftColliders();
|
||||||
|
|
||||||
return BulletMoveStatus::OK;
|
return BulletMoveStatus::OK;
|
||||||
@@ -66,42 +55,12 @@ BulletMoveStatus Bullet::move()
|
|||||||
|
|
||||||
bool Bullet::isEnabled() const
|
bool Bullet::isEnabled() const
|
||||||
{
|
{
|
||||||
return kind_ != BulletType::NONE;
|
return bullet_type_ != BulletType::NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Bullet::disable()
|
void Bullet::disable()
|
||||||
{
|
{
|
||||||
kind_ = BulletType::NONE;
|
bullet_type_ = 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_;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int Bullet::getOwner() const
|
int Bullet::getOwner() const
|
||||||
@@ -119,3 +78,9 @@ void Bullet::shiftColliders()
|
|||||||
collider_.x = pos_x_ + collider_.r;
|
collider_.x = pos_x_ + collider_.r;
|
||||||
collider_.y = pos_y_ + collider_.r;
|
collider_.y = pos_y_ + collider_.r;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Bullet::shiftSprite()
|
||||||
|
{
|
||||||
|
sprite_->setX(pos_x_);
|
||||||
|
sprite_->setY(pos_y_);
|
||||||
|
}
|
||||||
|
|||||||
@@ -7,8 +7,8 @@
|
|||||||
#include "utils.h" // para Circle
|
#include "utils.h" // para Circle
|
||||||
class Texture;
|
class Texture;
|
||||||
|
|
||||||
// Enumeración para los diferentes tipos de balas
|
// Tipos de balas
|
||||||
enum class BulletType
|
enum class BulletType : Uint8
|
||||||
{
|
{
|
||||||
UP,
|
UP,
|
||||||
LEFT,
|
LEFT,
|
||||||
@@ -16,7 +16,7 @@ enum class BulletType
|
|||||||
NONE
|
NONE
|
||||||
};
|
};
|
||||||
|
|
||||||
// Enumeración para los resultados del movimiento de la bala
|
// Resultado del movimiento de la bala
|
||||||
enum class BulletMoveStatus : Uint8
|
enum class BulletMoveStatus : Uint8
|
||||||
{
|
{
|
||||||
OK = 0,
|
OK = 0,
|
||||||
@@ -27,26 +27,29 @@ enum class BulletMoveStatus : Uint8
|
|||||||
class Bullet
|
class Bullet
|
||||||
{
|
{
|
||||||
private:
|
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
|
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_x_; // Posición en el eje X
|
||||||
int pos_y_; // Posición en el eje Y
|
int pos_y_; // Posición en el eje Y
|
||||||
Uint8 width_; // Ancho del objeto
|
int vel_x_; // Velocidad en el eje X
|
||||||
Uint8 height_; // Alto del objeto
|
|
||||||
|
|
||||||
int vel_x_; // Velocidad en el eje X
|
BulletType bullet_type_; // Tipo de objeto
|
||||||
int vel_y_; // Velocidad en el eje Y
|
int owner_; // Identificador del dueño del objeto
|
||||||
|
Circle collider_; // Círculo de colisión del objeto
|
||||||
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
|
|
||||||
|
|
||||||
void shiftColliders(); // Alinea el círculo de colisión con el objeto
|
void shiftColliders(); // Alinea el círculo de colisión con el objeto
|
||||||
|
void shiftSprite(); // Alinea el sprite con el objeto
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// Constructor
|
// 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
|
// Destructor
|
||||||
~Bullet() = default;
|
~Bullet() = default;
|
||||||
@@ -63,17 +66,7 @@ public:
|
|||||||
// Deshabilita el objeto
|
// Deshabilita el objeto
|
||||||
void disable();
|
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
|
// Obtiene parámetros
|
||||||
int getVelY() const;
|
|
||||||
BulletType getKind() const;
|
|
||||||
int getOwner() const;
|
int getOwner() const;
|
||||||
Circle &getCollider();
|
Circle &getCollider();
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -841,7 +841,7 @@ void Game::renderBullets()
|
|||||||
// Crea un objeto bala
|
// Crea un objeto bala
|
||||||
void Game::createBullet(int x, int y, BulletType kind, bool powered_up, int owner)
|
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
|
// 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
|
player->setInput(bulletType == BulletType::UP ? InputType::FIRE_CENTER : bulletType == BulletType::LEFT ? InputType::FIRE_LEFT
|
||||||
: InputType::FIRE_RIGHT);
|
: 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"));
|
JA_PlaySound(Resource::get()->getSound("bullet.wav"));
|
||||||
player->setFireCooldown(10); // Establece un tiempo de espera para el próximo disparo.
|
player->setFireCooldown(10); // Establece un tiempo de espera para el próximo disparo.
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,9 +22,9 @@ class Screen;
|
|||||||
class SmartSprite;
|
class SmartSprite;
|
||||||
class Text;
|
class Text;
|
||||||
class Texture;
|
class Texture;
|
||||||
enum class BulletType; // lines 26-26
|
enum class BulletType : Uint8; // lines 26-26
|
||||||
struct JA_Music_t; // lines 27-27
|
struct JA_Music_t; // lines 27-27
|
||||||
struct JA_Sound_t; // lines 28-28
|
struct JA_Sound_t; // lines 28-28
|
||||||
enum class ItemType;
|
enum class ItemType;
|
||||||
|
|
||||||
// Modo demo
|
// Modo demo
|
||||||
@@ -82,7 +82,7 @@ private:
|
|||||||
static constexpr int ITEM_COFFEE_ODDS_ = 5;
|
static constexpr int ITEM_COFFEE_ODDS_ = 5;
|
||||||
static constexpr int ITEM_POWER_BALL_ODDS_ = 0;
|
static constexpr int ITEM_POWER_BALL_ODDS_ = 0;
|
||||||
static constexpr int ITEM_COFFEE_MACHINE_ODDS_ = 4;
|
static constexpr int ITEM_COFFEE_MACHINE_ODDS_ = 4;
|
||||||
|
|
||||||
// Estructuras
|
// Estructuras
|
||||||
struct Helper
|
struct Helper
|
||||||
{
|
{
|
||||||
@@ -171,7 +171,7 @@ private:
|
|||||||
float default_balloon_speed_; // Velocidad base de los enemigos, sin incrementar
|
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 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
|
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 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 balloons_popped_ = 0; // Lleva la cuenta de los globos explotados
|
||||||
int counter_ = 0; // Contador para el juego
|
int counter_ = 0; // Contador para el juego
|
||||||
|
|||||||
Reference in New Issue
Block a user