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_);
}