76 lines
1.8 KiB
C++
76 lines
1.8 KiB
C++
#include "game/entities/path_enemy.hpp"
|
|
|
|
#include "core/rendering/sprite/animated_sprite.hpp" // Para AnimatedSprite
|
|
|
|
// Constructor: delega al base y almacena boundaries
|
|
PathEnemy::PathEnemy(const Data& data)
|
|
: Enemy(data),
|
|
x1_(data.x1),
|
|
x2_(data.x2),
|
|
y1_(data.y1),
|
|
y2_(data.y2) {
|
|
}
|
|
|
|
// Actualiza posición y animación del enemigo
|
|
void PathEnemy::update(float delta_time) {
|
|
sprite_->update(delta_time);
|
|
checkPath();
|
|
collider_ = getRect();
|
|
}
|
|
|
|
#ifdef _DEBUG
|
|
// Resetea el enemigo a su posición inicial (para editor)
|
|
void PathEnemy::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;
|
|
|
|
applyFlipMirror(data.vx);
|
|
|
|
collider_ = getRect();
|
|
}
|
|
#endif
|
|
|
|
// Comprueba si ha llegado al limite del recorrido para darse media vuelta
|
|
void PathEnemy::checkPath() { // NOLINT(readability-make-member-function-const)
|
|
if (sprite_->getPosX() > x2_ || sprite_->getPosX() < x1_) {
|
|
// Recoloca
|
|
if (sprite_->getPosX() > x2_) {
|
|
sprite_->setPosX(x2_);
|
|
} else {
|
|
sprite_->setPosX(x1_);
|
|
}
|
|
|
|
// Cambia el sentido
|
|
sprite_->setVelX(sprite_->getVelX() * (-1));
|
|
|
|
// Invierte el sprite
|
|
if (should_flip_) {
|
|
sprite_->flip();
|
|
}
|
|
}
|
|
|
|
if (sprite_->getPosY() > y2_ || sprite_->getPosY() < y1_) {
|
|
// Recoloca
|
|
if (sprite_->getPosY() > y2_) {
|
|
sprite_->setPosY(y2_);
|
|
} else {
|
|
sprite_->setPosY(y1_);
|
|
}
|
|
|
|
// Cambia el sentido
|
|
sprite_->setVelY(sprite_->getVelY() * (-1));
|
|
|
|
// Invierte el sprite
|
|
if (should_flip_) {
|
|
sprite_->flip();
|
|
}
|
|
}
|
|
}
|