Files
2026-04-08 14:09:28 +02:00

63 lines
2.0 KiB
C++

#include "game/entities/enemy.hpp"
#include <SDL3/SDL.h>
#include <cstdlib> // Para rand
#include "core/rendering/sprite/animated_sprite.hpp" // Para SAnimatedSprite
#include "core/resources/resource_cache.hpp" // Para Resource
#include "game/entities/path_enemy.hpp" // Para PathEnemy
// Constructor base: configura sprite, collider, flip/mirror
Enemy::Enemy(const Data& enemy)
: sprite_(std::make_shared<AnimatedSprite>(Resource::Cache::get()->getAnimationData(enemy.animation_path))),
should_flip_(enemy.flip),
should_mirror_(enemy.mirror) {
sprite_->setPosX(enemy.x);
sprite_->setPosY(enemy.y);
sprite_->setVelX(enemy.vx);
sprite_->setVelY(enemy.vy);
applyFlipMirror(enemy.vx);
collider_ = getRect();
// Coloca un frame al azar o el designado
sprite_->setCurrentAnimationFrame((enemy.frame == -1) ? (rand() % sprite_->getCurrentAnimationSize()) : enemy.frame);
}
// Pinta el enemigo en pantalla
void Enemy::render() {
sprite_->render();
}
#ifdef _DEBUG
// Solo actualiza la animación sin mover al enemigo
void Enemy::updateAnimation(float delta_time) {
sprite_->animate(delta_time);
}
#endif
// Aplica flip horizontal y/o mirror vertical al sprite
void Enemy::applyFlipMirror(float vx) {
const int FLIP = (should_flip_ && vx < 0.0F) ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE;
const int MIRROR = should_mirror_ ? SDL_FLIP_VERTICAL : SDL_FLIP_NONE;
sprite_->setFlip(static_cast<SDL_FlipMode>(FLIP | MIRROR)); // NOLINT(clang-analyzer-optin.core.EnumCastOutOfRange) SDL flags are designed for bitwise OR
}
// Devuelve el rectangulo que contiene al enemigo
auto Enemy::getRect() -> SDL_FRect {
return sprite_->getRect();
}
// Obtiene el rectangulo de colision del enemigo
auto Enemy::getCollider() -> SDL_FRect& {
return collider_;
}
// Factory: crea el subtipo de enemigo correcto según data.type
auto Enemy::create(const Data& data) -> std::shared_ptr<Enemy> {
// Por ahora solo existe PathEnemy; futuros tipos se añadirán aquí
return std::make_shared<PathEnemy>(data);
}