Files
coffee_crisis_arcade_edition/source/bullet.cpp

112 lines
2.5 KiB
C++

#include "bullet.h"
#include <SDL3/SDL_rect.h> // Para SDL_FRect
#include <memory> // Para unique_ptr, make_unique, shared_ptr
#include "param.h" // Para Param, ParamGame, param
#include "sprite.h" // Para Sprite
#include "resource.h"
class Texture; // lines 5-5
// Constructor
Bullet::Bullet(float x, float y, BulletType bullet_type, bool powered, int owner)
: sprite_(std::make_unique<AnimatedSprite>(Resource::get()->getTexture("bullet.png"), Resource::get()->getAnimation("bullet.ani"))),
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;
std::string powered_type = powered ? "powered_" : "normal_";
switch (bullet_type)
{
case BulletType::UP:
sprite_->setCurrentAnimation(powered_type + "up");
break;
case BulletType::LEFT:
sprite_->setCurrentAnimation(powered_type + "left");
break;
case BulletType::RIGHT:
sprite_->setCurrentAnimation(powered_type + "right");
break;
default:
break;
}
collider_.r = BULLET_WIDTH_ / 2;
shiftColliders();
}
// Implementación de render (llama al render del sprite_)
void Bullet::render()
{
if (bullet_type_ != BulletType::NONE)
sprite_->render();
}
// Actualiza el estado del objeto
BulletMoveStatus Bullet::update()
{
sprite_->update();
return move();
}
// 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_);
}