polimorfise d'enemics
moving platforms
This commit is contained in:
96
source/game/entities/moving_platform.cpp
Normal file
96
source/game/entities/moving_platform.cpp
Normal file
@@ -0,0 +1,96 @@
|
||||
#include "game/entities/moving_platform.hpp"
|
||||
|
||||
#include <cstdlib> // Para rand
|
||||
|
||||
#include "core/rendering/sprite/animated_sprite.hpp" // Para AnimatedSprite
|
||||
#include "core/resources/resource_cache.hpp" // Para Resource
|
||||
|
||||
// Constructor
|
||||
MovingPlatform::MovingPlatform(const Data& data)
|
||||
: sprite_(std::make_shared<AnimatedSprite>(Resource::Cache::get()->getAnimationData(data.animation_path))),
|
||||
x1_(data.x1),
|
||||
x2_(data.x2),
|
||||
y1_(data.y1),
|
||||
y2_(data.y2) {
|
||||
sprite_->setPosX(data.x);
|
||||
sprite_->setPosY(data.y);
|
||||
sprite_->setVelX(data.vx);
|
||||
sprite_->setVelY(data.vy);
|
||||
|
||||
collider_ = getRect();
|
||||
|
||||
// Coloca un frame al azar o el designado
|
||||
sprite_->setCurrentAnimationFrame((data.frame == -1) ? (rand() % sprite_->getCurrentAnimationSize()) : data.frame);
|
||||
}
|
||||
|
||||
// Actualiza posición, calcula desplazamiento real del frame
|
||||
void MovingPlatform::update(float delta_time) {
|
||||
float old_x = sprite_->getPosX();
|
||||
float old_y = sprite_->getPosY();
|
||||
|
||||
sprite_->update(delta_time);
|
||||
checkPath();
|
||||
|
||||
last_dx_ = sprite_->getPosX() - old_x;
|
||||
last_dy_ = sprite_->getPosY() - old_y;
|
||||
|
||||
collider_ = getRect();
|
||||
}
|
||||
|
||||
// Pinta la plataforma en pantalla
|
||||
void MovingPlatform::render() {
|
||||
sprite_->render();
|
||||
}
|
||||
|
||||
#ifdef _DEBUG
|
||||
// Solo actualiza la animación sin mover la plataforma
|
||||
void MovingPlatform::updateAnimation(float delta_time) {
|
||||
sprite_->animate(delta_time);
|
||||
}
|
||||
|
||||
// Resetea la plataforma a su posición inicial (para editor)
|
||||
void MovingPlatform::resetToInitialPosition(const Data& data) {
|
||||
sprite_->setPosX(data.x);
|
||||
sprite_->setPosY(data.y);
|
||||
sprite_->setVelX(data.vx);
|
||||
sprite_->setVelY(data.vy);
|
||||
|
||||
x1_ = data.x1;
|
||||
x2_ = data.x2;
|
||||
y1_ = data.y1;
|
||||
y2_ = data.y2;
|
||||
|
||||
collider_ = getRect();
|
||||
}
|
||||
#endif
|
||||
|
||||
// Devuelve el rectangulo que contiene a la plataforma
|
||||
auto MovingPlatform::getRect() -> SDL_FRect {
|
||||
return sprite_->getRect();
|
||||
}
|
||||
|
||||
// Obtiene el rectangulo de colisión
|
||||
auto MovingPlatform::getCollider() -> SDL_FRect& {
|
||||
return collider_;
|
||||
}
|
||||
|
||||
// Comprueba los límites del recorrido para invertir dirección
|
||||
void MovingPlatform::checkPath() { // NOLINT(readability-make-member-function-const)
|
||||
if (sprite_->getPosX() > x2_ || sprite_->getPosX() < x1_) {
|
||||
if (sprite_->getPosX() > x2_) {
|
||||
sprite_->setPosX(x2_);
|
||||
} else {
|
||||
sprite_->setPosX(x1_);
|
||||
}
|
||||
sprite_->setVelX(sprite_->getVelX() * (-1));
|
||||
}
|
||||
|
||||
if (sprite_->getPosY() > y2_ || sprite_->getPosY() < y1_) {
|
||||
if (sprite_->getPosY() > y2_) {
|
||||
sprite_->setPosY(y2_);
|
||||
} else {
|
||||
sprite_->setPosY(y1_);
|
||||
}
|
||||
sprite_->setVelY(sprite_->getVelY() * (-1));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user