196 lines
5.2 KiB
C++
196 lines
5.2 KiB
C++
#include "game/entities/item.h"
|
|
|
|
#include <algorithm> // for max
|
|
#include <cstdlib> // for rand
|
|
|
|
#include "core/rendering/animatedsprite.h" // for AnimatedSprite
|
|
#include "game/defaults.hpp" // for PLAY_AREA_LEFT, PLAY_AREA_RIGHT, PLAY_AR...
|
|
class Texture;
|
|
|
|
// Constructor
|
|
Item::Item(Id id, float x, float y, Texture *texture, const std::vector<std::string> *animation, SDL_Renderer *renderer) {
|
|
sprite_ = new AnimatedSprite(texture, renderer, "", animation);
|
|
|
|
this->id_ = id;
|
|
enabled_ = true;
|
|
time_to_live_ = 600;
|
|
time_to_live_s_ = TIME_TO_LIVE_S;
|
|
accel_x_s_ = 0.0F;
|
|
floor_collision_ = false;
|
|
|
|
if (id == Item::Id::COFFEE_MACHINE) {
|
|
width_ = 23;
|
|
height_ = 29;
|
|
pos_x_ = (((int)x + (PLAY_AREA_WIDTH / 2)) % (PLAY_AREA_WIDTH - width_ - 5)) + 2;
|
|
pos_y_ = PLAY_AREA_TOP - height_;
|
|
vel_x_s_ = 0.0F;
|
|
vel_y_s_ = COFFEE_VEL_Y_PX_PER_S;
|
|
accel_y_s_ = COFFEE_ACCEL_Y_PX_PER_S2;
|
|
collider_.r = 10;
|
|
} else {
|
|
width_ = 16;
|
|
height_ = 16;
|
|
pos_x_ = x;
|
|
pos_y_ = y;
|
|
// Distribució original: -1.0, -0.5, 0.0, 0.5, 1.0 px/frame ⇒ -60..60 px/s en passos de 30.
|
|
const int RAND_STEP = rand() % 5;
|
|
vel_x_s_ = (-2.0F + static_cast<float>(RAND_STEP)) * ITEM_VEL_X_STEP_PX_PER_S;
|
|
vel_y_s_ = ITEM_VEL_Y_PX_PER_S;
|
|
accel_y_s_ = ITEM_ACCEL_Y_PX_PER_S2;
|
|
collider_.r = width_ / 2;
|
|
}
|
|
|
|
sprite_->setPosX(pos_x_);
|
|
sprite_->setPosY(pos_y_);
|
|
shiftColliders();
|
|
}
|
|
|
|
// Destructor
|
|
Item::~Item() {
|
|
delete sprite_;
|
|
}
|
|
|
|
// Centra el objeto en la posición X
|
|
void Item::allignTo(int x) {
|
|
pos_x_ = float(x - (width_ / 2));
|
|
|
|
if (pos_x_ < PLAY_AREA_LEFT) {
|
|
pos_x_ = PLAY_AREA_LEFT + 1;
|
|
} else if ((pos_x_ + width_) > PLAY_AREA_RIGHT) {
|
|
pos_x_ = float(PLAY_AREA_RIGHT - width_ - 1);
|
|
}
|
|
|
|
// Posición X,Y del sprite
|
|
sprite_->setPosX(int(pos_x_));
|
|
sprite_->setPosY(int(pos_y_));
|
|
|
|
// Alinea el circulo de colisión con el objeto
|
|
shiftColliders();
|
|
}
|
|
|
|
// Pinta el objeto en la pantalla
|
|
void Item::render() {
|
|
// Mentre quede temps de sobra (>200) es renderitza sempre; quan està a
|
|
// punt d'expirar, parpalleja alternant 10 frames visibles i 10 invisibles.
|
|
if (enabled_ && (time_to_live_ > 200 || time_to_live_ % 20 > 10)) {
|
|
sprite_->render();
|
|
}
|
|
}
|
|
|
|
// Actualiza la posición y estados del objeto
|
|
void Item::move(float dt_s) {
|
|
floor_collision_ = false;
|
|
|
|
// Posició
|
|
pos_x_ += vel_x_s_ * dt_s;
|
|
pos_y_ += vel_y_s_ * dt_s;
|
|
|
|
// Acceleració
|
|
vel_x_s_ += accel_x_s_ * dt_s;
|
|
vel_y_s_ += accel_y_s_ * dt_s;
|
|
|
|
// Si surt per laterals, corregeix i inverteix
|
|
if ((pos_x_ < PLAY_AREA_LEFT) || (pos_x_ + width_ > PLAY_AREA_RIGHT)) {
|
|
pos_x_ -= vel_x_s_ * dt_s;
|
|
vel_x_s_ = -vel_x_s_;
|
|
}
|
|
|
|
// Rebot per dalt (excepte la màquina de cafè)
|
|
if ((pos_y_ < PLAY_AREA_TOP) && !(id_ == Item::Id::COFFEE_MACHINE)) {
|
|
pos_y_ -= vel_y_s_ * dt_s;
|
|
vel_y_s_ = -vel_y_s_;
|
|
}
|
|
|
|
// Topa amb el terra
|
|
if (pos_y_ + height_ > PLAY_AREA_BOTTOM) {
|
|
vel_y_s_ = 0;
|
|
vel_x_s_ = 0;
|
|
accel_x_s_ = 0;
|
|
accel_y_s_ = 0;
|
|
pos_y_ = PLAY_AREA_BOTTOM - height_;
|
|
if (id_ == Item::Id::COFFEE_MACHINE) {
|
|
floor_collision_ = true;
|
|
}
|
|
}
|
|
|
|
sprite_->setPosX(int(pos_x_));
|
|
sprite_->setPosY(int(pos_y_));
|
|
shiftColliders();
|
|
}
|
|
|
|
// Pone a cero todos los valores del objeto
|
|
void Item::disable() {
|
|
enabled_ = false;
|
|
}
|
|
|
|
// Actualiza el objeto a su posicion, animación y controla los contadores
|
|
void Item::update(float dt_s) {
|
|
move(dt_s);
|
|
sprite_->animate(dt_s);
|
|
updateTimeToLive(dt_s);
|
|
checkTimeToLive();
|
|
}
|
|
|
|
// Actualiza el contador. Manté time_to_live_ (frames) sincronitzat amb el
|
|
// segons per a que render() segueixi funcionant amb la mateixa condició de
|
|
// parpelleig.
|
|
void Item::updateTimeToLive(float dt_s) {
|
|
if (time_to_live_s_ > 0.0F) {
|
|
time_to_live_s_ = std::max(0.0F, time_to_live_s_ - dt_s);
|
|
}
|
|
constexpr float FRAMES_PER_S = 60.0F;
|
|
time_to_live_ = static_cast<Uint16>(time_to_live_s_ * FRAMES_PER_S);
|
|
}
|
|
|
|
// Comprueba si el objeto sigue vivo
|
|
void Item::checkTimeToLive() {
|
|
if (time_to_live_ == 0) {
|
|
disable();
|
|
}
|
|
}
|
|
|
|
// Obtiene del valor de la variable
|
|
auto Item::getPosX() const -> float {
|
|
return pos_x_;
|
|
}
|
|
|
|
// Obtiene del valor de la variable
|
|
auto Item::getPosY() const -> float {
|
|
return pos_y_;
|
|
}
|
|
|
|
// Obtiene del valor de la variable
|
|
auto Item::getWidth() const -> int {
|
|
return width_;
|
|
}
|
|
|
|
// Obtiene del valor de la variable
|
|
auto Item::getHeight() const -> int {
|
|
return height_;
|
|
}
|
|
|
|
// Obtiene del valor de la variable
|
|
auto Item::getId() const -> Id {
|
|
return id_;
|
|
}
|
|
|
|
// Obtiene el valor de la variable
|
|
auto Item::isEnabled() const -> bool {
|
|
return enabled_;
|
|
}
|
|
|
|
// Obtiene el circulo de colisión
|
|
auto Item::getCollider() -> Circle & {
|
|
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));
|
|
}
|
|
|
|
// Informa si el objeto ha colisionado con el suelo
|
|
auto Item::isOnFloor() const -> bool {
|
|
return floor_collision_;
|
|
} |