Els items reboten al tocar el piso
This commit is contained in:
152
source/item.cpp
152
source/item.cpp
@@ -1,10 +1,10 @@
|
||||
#include "item.h"
|
||||
#include <algorithm> // para std::clamp
|
||||
#include <stdlib.h> // para rand
|
||||
#include "animated_sprite.h" // para SpriteAnimated
|
||||
#include "param.h" // para param
|
||||
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)
|
||||
: sprite_(std::make_unique<AnimatedSprite>(texture, animation)),
|
||||
type_(type),
|
||||
@@ -38,34 +38,26 @@ Item::Item(ItemType type, float x, float y, SDL_Rect &play_area, std::shared_ptr
|
||||
}
|
||||
}
|
||||
|
||||
sprite_->setPosX(pos_x_);
|
||||
sprite_->setPosY(pos_y_);
|
||||
// Actualiza el sprite
|
||||
shiftSprite();
|
||||
shiftColliders();
|
||||
}
|
||||
|
||||
// Centra el objeto en la posición X
|
||||
void Item::alignTo(int x)
|
||||
{
|
||||
pos_x_ = static_cast<float>(x - (width_ / 2));
|
||||
const float min_x = param.game.play_area.rect.x + 1;
|
||||
const float max_x = play_area_.w - width_ - 1;
|
||||
|
||||
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)
|
||||
{
|
||||
pos_x_ = static_cast<float>(play_area_.w - width_ - 1);
|
||||
}
|
||||
pos_x_ = x - (width_ / 2);
|
||||
|
||||
// Posición X,Y del sprite
|
||||
sprite_->setPosX(pos_x_);
|
||||
sprite_->setPosY(pos_y_);
|
||||
// Ajusta para que no quede fuera de la zona de juego
|
||||
pos_x_ = std::clamp(pos_x_, min_x, max_x);
|
||||
|
||||
// Alinea el circulo de colisión con el objeto
|
||||
// Actualiza el sprite
|
||||
shiftSprite();
|
||||
shiftColliders();
|
||||
}
|
||||
|
||||
// Pinta el objeto en la pantalla
|
||||
void Item::render()
|
||||
{
|
||||
if (enabled_)
|
||||
@@ -81,7 +73,8 @@ void Item::render()
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza la posición y estados del objeto
|
||||
#include <algorithm> // Necesario para std::clamp
|
||||
|
||||
void Item::move()
|
||||
{
|
||||
floor_collision_ = false;
|
||||
@@ -94,129 +87,90 @@ void Item::move()
|
||||
vel_x_ += accel_x_;
|
||||
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))
|
||||
{
|
||||
// Corregir posición
|
||||
pos_x_ -= vel_x_;
|
||||
// Comprueba los laterales de la zona de juego
|
||||
const float min_x = param.game.play_area.rect.x;
|
||||
const float max_x = play_area_.w - width_;
|
||||
pos_x_ = std::clamp(pos_x_, min_x, max_x);
|
||||
|
||||
// Invertir sentido
|
||||
// Si toca el borde lateral, invierte la velocidad horizontal
|
||||
if (pos_x_ == min_x || pos_x_ == max_x)
|
||||
{
|
||||
vel_x_ = -vel_x_;
|
||||
}
|
||||
|
||||
// Si se sale por arriba rebota (excepto la maquina de café)
|
||||
// Si colisiona por arriba, rebota (excepto la máquina de café)
|
||||
if ((pos_y_ < param.game.play_area.rect.y) && !(type_ == ItemType::COFFEE_MACHINE))
|
||||
{
|
||||
// Corrige
|
||||
pos_y_ = param.game.play_area.rect.y;
|
||||
|
||||
// Invierte el sentido
|
||||
// Invierte la velocidad
|
||||
vel_y_ = -vel_y_;
|
||||
}
|
||||
|
||||
// Si el objeto se sale por la parte inferior
|
||||
if (pos_y_ + height_ > play_area_.h)
|
||||
// Si colisiona con la parte inferior
|
||||
if (pos_y_ > play_area_.h - height_)
|
||||
{
|
||||
// Detiene el objeto
|
||||
vel_y_ = 0;
|
||||
vel_x_ = 0;
|
||||
accel_x_ = 0;
|
||||
accel_y_ = 0;
|
||||
// Corrige la posición
|
||||
pos_y_ = play_area_.h - height_;
|
||||
|
||||
if (type_ == ItemType::COFFEE_MACHINE)
|
||||
{
|
||||
// Si es una máquina de café, detiene el objeto
|
||||
vel_y_ = vel_x_ = accel_x_ = accel_y_ = 0;
|
||||
floor_collision_ = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Si no es una máquina de café
|
||||
if (vel_y_ < 1.0f)
|
||||
{
|
||||
// Si la velocidad vertical es baja, detiene el objeto
|
||||
vel_y_ = vel_x_ = accel_x_ = accel_y_ = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Si la velocidad vertical es alta, el objeto rebota y pierde velocidad
|
||||
vel_y_ *= -0.5f;
|
||||
vel_x_ *= 0.75f;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza la posición del sprite
|
||||
sprite_->setPosX(int(pos_x_));
|
||||
sprite_->setPosY(int(pos_y_));
|
||||
shiftSprite();
|
||||
shiftColliders();
|
||||
}
|
||||
|
||||
// Pone a cero todos los valores del objeto
|
||||
void Item::disable()
|
||||
{
|
||||
enabled_ = false;
|
||||
}
|
||||
void Item::disable() { enabled_ = false; }
|
||||
|
||||
// Actualiza el objeto a su posicion, animación y controla los contadores
|
||||
void Item::update()
|
||||
{
|
||||
move();
|
||||
sprite_->update();
|
||||
updateTimeToLive();
|
||||
checkTimeToLive();
|
||||
}
|
||||
|
||||
// Actualiza el contador
|
||||
void Item::updateTimeToLive()
|
||||
{
|
||||
if (time_to_live_ > 0)
|
||||
{
|
||||
time_to_live_--;
|
||||
}
|
||||
}
|
||||
|
||||
// Comprueba si el objeto sigue vivo
|
||||
void Item::checkTimeToLive()
|
||||
{
|
||||
if (time_to_live_ == 0)
|
||||
else
|
||||
{
|
||||
disable();
|
||||
}
|
||||
}
|
||||
|
||||
// Obtiene del valor de la variable
|
||||
float Item::getPosX()
|
||||
{
|
||||
return pos_x_;
|
||||
}
|
||||
|
||||
// Obtiene del valor de la variable
|
||||
float Item::getPosY()
|
||||
{
|
||||
return pos_y_;
|
||||
}
|
||||
|
||||
// Obtiene del valor de la variable
|
||||
int Item::getWidth()
|
||||
{
|
||||
return width_;
|
||||
}
|
||||
|
||||
// Obtiene del valor de la variable
|
||||
int Item::getHeight()
|
||||
{
|
||||
return height_;
|
||||
}
|
||||
|
||||
// Obtiene del valor de la variable
|
||||
ItemType Item::getType()
|
||||
{
|
||||
return type_;
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
bool Item::isEnabled()
|
||||
{
|
||||
return enabled_;
|
||||
}
|
||||
|
||||
// Obtiene el circulo de colisión
|
||||
Circle &Item::getCollider()
|
||||
{
|
||||
return collider_;
|
||||
}
|
||||
|
||||
// Alinea el circulo de colisión con la posición del objeto
|
||||
void Item::shiftColliders()
|
||||
{
|
||||
collider_.x = int(pos_x_ + (width_ / 2));
|
||||
collider_.y = int(pos_y_ + (height_ / 2));
|
||||
collider_.x = static_cast<int>(pos_x_ + (width_ / 2));
|
||||
collider_.y = static_cast<int>(pos_y_ + (height_ / 2));
|
||||
}
|
||||
|
||||
// Informa si el objeto ha colisionado con el suelo
|
||||
bool Item::isOnFloor()
|
||||
void Item::shiftSprite()
|
||||
{
|
||||
return floor_collision_;
|
||||
}
|
||||
sprite_->setPosX(pos_x_);
|
||||
sprite_->setPosY(pos_y_);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user