This commit is contained in:
2024-10-23 18:29:52 +02:00
parent 95478134dd
commit 6e2f80d8ce
26 changed files with 232 additions and 290 deletions

View File

@@ -5,27 +5,26 @@
class Texture;
// Constructor
Item::Item(ItemType type, float x, float y, SDL_Rect *play_area, std::shared_ptr<Texture> texture, const std::vector<std::string> &animation)
Item::Item(ItemType type, float x, float y, SDL_Rect &play_area, std::shared_ptr<Texture> texture, const std::vector<std::string> &animation)
: sprite_(std::make_unique<AnimatedSprite>(texture, animation)),
accel_x_(0.0f),
floor_collision_(false),
type_(type),
enabled_(true),
play_area_(play_area),
time_to_live_(600)
play_area_(play_area)
{
if (type == ItemType::COFFEE_MACHINE)
switch (type)
{
case ItemType::COFFEE_MACHINE:
{
width_ = 28;
height_ = 37;
pos_x_ = (((int)x + (play_area->w / 2)) % (play_area->w - width_ - 5)) + 2;
pos_x_ = ((static_cast<int>(x) + (play_area.w / 2)) % (play_area.w - width_ - 5)) + 2;
pos_y_ = -height_;
vel_x_ = 0.0f;
vel_y_ = -0.1f;
accel_y_ = 0.1f;
collider_.r = 10;
break;
}
else
default:
{
width_ = 20;
height_ = 20;
@@ -35,6 +34,8 @@ Item::Item(ItemType type, float x, float y, SDL_Rect *play_area, std::shared_ptr
vel_y_ = -4.0f;
accel_y_ = 0.2f;
collider_.r = width_ / 2;
break;
}
}
sprite_->setPosX(pos_x_);
@@ -45,20 +46,20 @@ Item::Item(ItemType type, float x, float y, SDL_Rect *play_area, std::shared_ptr
// Centra el objeto en la posición X
void Item::allignTo(int x)
{
pos_x_ = float(x - (width_ / 2));
pos_x_ = static_cast<float>(x - (width_ / 2));
if (pos_x_ < param.game.play_area.rect.x)
{
pos_x_ = param.game.play_area.rect.x + 1;
}
else if ((pos_x_ + width_) > play_area_->w)
else if (pos_x_ + width_ > play_area_.w)
{
pos_x_ = float(play_area_->w - width_ - 1);
pos_x_ = static_cast<float>(play_area_.w - width_ - 1);
}
// Posición X,Y del sprite
sprite_->setPosX(int(pos_x_));
sprite_->setPosY(int(pos_y_));
sprite_->setPosX(pos_x_);
sprite_->setPosY(pos_y_);
// Alinea el circulo de colisión con el objeto
shiftColliders();
@@ -94,7 +95,7 @@ void Item::move()
vel_y_ += accel_y_;
// Si queda fuera de pantalla, corregimos su posición y cambiamos su sentido
if ((pos_x_ < param.game.play_area.rect.x) || (pos_x_ + width_ > play_area_->w))
if ((pos_x_ < param.game.play_area.rect.x) || (pos_x_ + width_ > play_area_.w))
{
// Corregir posición
pos_x_ -= vel_x_;
@@ -114,14 +115,14 @@ void Item::move()
}
// Si el objeto se sale por la parte inferior
if (pos_y_ + height_ > play_area_->h)
if (pos_y_ + height_ > play_area_.h)
{
// Detiene el objeto
vel_y_ = 0;
vel_x_ = 0;
accel_x_ = 0;
accel_y_ = 0;
pos_y_ = play_area_->h - height_;
pos_y_ = play_area_.h - height_;
if (type_ == ItemType::COFFEE_MACHINE)
{
floor_collision_ = true;