Acabat de renamar, encara que he descobert cosetes i tindré que fer altra pasaeta

Actualitzat stb_image.h a la última versió
This commit is contained in:
2024-10-12 22:25:43 +02:00
parent cce14dba4d
commit 33ea8d90ca
16 changed files with 528 additions and 431 deletions

View File

@@ -5,61 +5,61 @@
class Texture;
// Constructor
Item::Item(int kind, float x, float y, SDL_Rect *playArea, std::shared_ptr<Texture> texture, std::vector<std::string> *animation)
: kind(kind), playArea(playArea)
Item::Item(int kind, float x, float y, SDL_Rect *play_area, std::shared_ptr<Texture> texture, std::vector<std::string> *animation)
: kind_(kind), play_area_(play_area)
{
sprite = std::make_unique<AnimatedSprite>(texture, "", animation);
sprite_ = std::make_unique<AnimatedSprite>(texture, "", animation);
enabled = true;
timeToLive = 600;
accelX = 0.0f;
floorCollision = false;
enabled_ = true;
time_to_live_ = 600;
accel_x_ = 0.0f;
floor_collision_ = false;
if (kind == ITEM_COFFEE_MACHINE)
{
width = 28;
height = 37;
posX = (((int)x + (playArea->w / 2)) % (playArea->w - width - 5)) + 2;
posY = -height;
velX = 0.0f;
velY = -0.1f;
accelY = 0.1f;
collider.r = 10;
width_ = 28;
height_ = 37;
pos_x_ = (((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;
}
else
{
width = 20;
height = 20;
posX = x;
posY = y;
velX = -1.0f + ((rand() % 5) * 0.5f);
velY = -4.0f;
accelY = 0.2f;
collider.r = width / 2;
width_ = 20;
height_ = 20;
pos_x_ = x;
pos_y_ = y;
vel_x_ = -1.0f + ((rand() % 5) * 0.5f);
vel_y_ = -4.0f;
accel_y_ = 0.2f;
collider_.r = width_ / 2;
}
sprite->setPosX(posX);
sprite->setPosY(posY);
sprite_->setPosX(pos_x_);
sprite_->setPosY(pos_y_);
shiftColliders();
}
// Centra el objeto en la posición X
void Item::allignTo(int x)
{
posX = float(x - (width / 2));
pos_x_ = float(x - (width_ / 2));
if (posX < param.game.play_area.rect.x)
if (pos_x_ < param.game.play_area.rect.x)
{
posX = param.game.play_area.rect.x + 1;
pos_x_ = param.game.play_area.rect.x + 1;
}
else if ((posX + width) > playArea->w)
else if ((pos_x_ + width_) > play_area_->w)
{
posX = float(playArea->w - width - 1);
pos_x_ = float(play_area_->w - width_ - 1);
}
// Posición X,Y del sprite
sprite->setPosX(int(posX));
sprite->setPosY(int(posY));
sprite_->setPosX(int(pos_x_));
sprite_->setPosY(int(pos_y_));
// Alinea el circulo de colisión con el objeto
shiftColliders();
@@ -68,15 +68,15 @@ void Item::allignTo(int x)
// Pinta el objeto en la pantalla
void Item::render()
{
if (enabled)
if (enabled_)
{
if (timeToLive > 200)
if (time_to_live_ > 200)
{
sprite->render();
sprite_->render();
}
else if (timeToLive % 20 > 10)
else if (time_to_live_ % 20 > 10)
{
sprite->render();
sprite_->render();
}
}
}
@@ -84,68 +84,68 @@ void Item::render()
// Actualiza la posición y estados del objeto
void Item::move()
{
floorCollision = false;
floor_collision_ = false;
// Calcula la nueva posición
posX += velX;
posY += velY;
pos_x_ += vel_x_;
pos_y_ += vel_y_;
// Aplica las aceleraciones a la velocidad
velX += accelX;
velY += accelY;
vel_x_ += accel_x_;
vel_y_ += accel_y_;
// Si queda fuera de pantalla, corregimos su posición y cambiamos su sentido
if ((posX < param.game.play_area.rect.x) || (posX + width > playArea->w))
if ((pos_x_ < param.game.play_area.rect.x) || (pos_x_ + width_ > play_area_->w))
{
// Corregir posición
posX -= velX;
pos_x_ -= vel_x_;
// Invertir sentido
velX = -velX;
vel_x_ = -vel_x_;
}
// Si se sale por arriba rebota (excepto la maquina de café)
if ((posY < param.game.play_area.rect.y) && !(kind == ITEM_COFFEE_MACHINE))
if ((pos_y_ < param.game.play_area.rect.y) && !(kind_ == ITEM_COFFEE_MACHINE))
{
// Corrige
posY = param.game.play_area.rect.y;
pos_y_ = param.game.play_area.rect.y;
// Invierte el sentido
velY = -velY;
vel_y_ = -vel_y_;
}
// Si el objeto se sale por la parte inferior
if (posY + height > playArea->h)
if (pos_y_ + height_ > play_area_->h)
{
// Detiene el objeto
velY = 0;
velX = 0;
accelX = 0;
accelY = 0;
posY = playArea->h - height;
if (kind == ITEM_COFFEE_MACHINE)
vel_y_ = 0;
vel_x_ = 0;
accel_x_ = 0;
accel_y_ = 0;
pos_y_ = play_area_->h - height_;
if (kind_ == ITEM_COFFEE_MACHINE)
{
floorCollision = true;
floor_collision_ = true;
}
}
// Actualiza la posición del sprite
sprite->setPosX(int(posX));
sprite->setPosY(int(posY));
sprite_->setPosX(int(pos_x_));
sprite_->setPosY(int(pos_y_));
shiftColliders();
}
// Pone a cero todos los valores del objeto
void Item::disable()
{
enabled = false;
enabled_ = false;
}
// Actualiza el objeto a su posicion, animación y controla los contadores
void Item::update()
{
move();
sprite->animate();
sprite_->animate();
updateTimeToLive();
checkTimeToLive();
}
@@ -153,70 +153,70 @@ void Item::update()
// Actualiza el contador
void Item::updateTimeToLive()
{
if (timeToLive > 0)
if (time_to_live_ > 0)
{
timeToLive--;
time_to_live_--;
}
}
// Comprueba si el objeto sigue vivo
void Item::checkTimeToLive()
{
if (timeToLive == 0)
if (time_to_live_ == 0)
disable();
}
// Obtiene del valor de la variable
float Item::getPosX()
{
return posX;
return pos_x_;
}
// Obtiene del valor de la variable
float Item::getPosY()
{
return posY;
return pos_y_;
}
// Obtiene del valor de la variable
int Item::getWidth()
{
return width;
return width_;
}
// Obtiene del valor de la variable
int Item::getHeight()
{
return height;
return height_;
}
// Obtiene del valor de la variable
int Item::getClass()
{
return kind;
return kind_;
}
// Obtiene el valor de la variable
bool Item::isEnabled()
{
return enabled;
return enabled_;
}
// Obtiene el circulo de colisión
Circle &Item::getCollider()
{
return collider;
return collider_;
}
// Alinea el circulo de colisión con la posición del objeto
void Item::shiftColliders()
{
collider.x = int(posX + (width / 2));
collider.y = int(posY + (height / 2));
collider_.x = int(pos_x_ + (width_ / 2));
collider_.y = int(pos_y_ + (height_ / 2));
}
// Informa si el objeto ha colisionado con el suelo
bool Item::isOnFloor()
{
return floorCollision;
return floor_collision_;
}