Files
coffee_crisis_arcade_edition/source/bullet.cpp

87 lines
2.0 KiB
C++

#include "bullet.h"
#include <memory> // para unique_ptr, make_unique, shared_ptr
#include "param.h" // para param
#include "sprite.h" // para Sprite
class Texture;
// Constructor
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),
bullet_type_(bullet_type),
owner_(owner)
{
vel_x_ = (bullet_type_ == BulletType::LEFT) ? BULLET_VEL_X_LEFT_
: (bullet_type_ == BulletType::RIGHT) ? BULLET_VEL_X_RIGHT_
: 0;
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 = BULLET_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()
{
pos_x_ += vel_x_;
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_ += BULLET_VEL_Y_;
if (pos_y_ < param.game.play_area.rect.y - BULLET_HEIGHT_)
{
disable();
return BulletMoveStatus::OUT;
}
shiftSprite();
shiftColliders();
return BulletMoveStatus::OK;
}
bool Bullet::isEnabled() const
{
return bullet_type_ != BulletType::NONE;
}
void Bullet::disable()
{
bullet_type_ = BulletType::NONE;
}
int Bullet::getOwner() const
{
return owner_;
}
Circle &Bullet::getCollider()
{
return collider_;
}
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_);
}