use macroquad::math::Vec2;

use crate::{
    behavior::enemy::EnemyBehavior,
    enemy::Enemy,
    tower::tower_location,
    world::{CENTER_X, CENTER_Y},
};

#[derive(Clone)]
pub struct SeekCenter {}
#[derive(Clone)]
pub struct ZigZag {}

impl SeekCenter {
    pub fn create(_tier: u32) -> Box<dyn EnemyBehavior> {
        Box::new(Self {})
    }
}

impl ZigZag {
    pub fn create(_tier: u32) -> Box<dyn EnemyBehavior> {
        Box::new(Self {})
    }
}

impl EnemyBehavior for SeekCenter {
    fn on_tick(&mut self, e: &mut Enemy, c: &mut crate::context::TickContext) {
        if e.pos.distance_squared(tower_location()) <= e.radius {
            c.core.cmd.emit(crate::types::GameEvent::DespawnEnemy(e.id));
        }
        let center = Vec2::new(CENTER_X, CENTER_Y);
        let seek = (center - e.pos).normalize() * 60.0;
        e.vel = seek;
    }
}

impl EnemyBehavior for ZigZag {
    fn on_tick(&mut self, e: &mut Enemy, c: &mut crate::context::TickContext) {
        if e.pos.distance_squared(tower_location()) <= e.radius {
            c.core.cmd.emit(crate::types::GameEvent::DespawnEnemy(e.id));
        }
        let center = Vec2::new(CENTER_X, CENTER_Y);
        let seek = (center - e.pos).normalize() * 60.0;
        e.vel = seek;
    }
}
