//! The `EnemyBehavior` trait and its on-death dispatch runner. The `Enemy` entity lives in
//! `crate::enemy`.

use crate::{
    behavior::{dyn_clone_trait, projectile::BehaviorPriority},
    context::{DeathContext, TickContext},
    enemy::Enemy,
};

pub trait EnemyBehavior: EnemyClone + Send + Sync {
    fn on_death(&mut self, _e: &mut Enemy, _c: &mut DeathContext) {}
    fn on_tick(&mut self, _e: &mut Enemy, _c: &mut TickContext) {}
    fn priority(&mut self) -> i32 {
        BehaviorPriority::DEFAULT
    }
}

dyn_clone_trait!(EnemyClone, EnemyBehavior);

pub fn dispatch_death(e: &mut Enemy, c: &mut DeathContext) {
    let mut behaviors = std::mem::take(&mut e.behaviors);
    for b in behaviors.iter_mut() {
        b.on_death(e, c);
    }
    e.behaviors = behaviors;
}

pub fn dispatch_enemy_tick(e: &mut Enemy, c: &mut TickContext) {
    let mut behaviors = std::mem::take(&mut e.behaviors);
    for b in behaviors.iter_mut() {
        b.on_tick(e, c);
    }
    e.behaviors = behaviors
}
