//! Small geometry helpers shared by projectile behaviors (reflection, steering, surface
//! normals).

use macroquad::math::Vec2;

use crate::{context::HitContext, enemy::Enemy, world::Projectile};

/// Turn `v` toward `want` by a blend factor in [0,1] (caller passes turn_rate*dt),
/// preserving the original speed.
pub fn steer(v: Vec2, want: Vec2, amount: f32) -> Vec2 {
    let speed = (v.x * v.x + v.y * v.y).sqrt();
    let cur = v.normalize();
    let want = want.normalize();
    let t = amount.clamp(0.0, 1.0);
    let blended = Vec2 {
        x: cur.x + (want.x - cur.x) * t,
        y: cur.y + (want.y - cur.y) * t,
    };
    blended.normalize() * speed
}

pub fn reflect_dir(p: &Projectile, c: &HitContext) -> Vec2 {
    // v - 2*(v·n)*n — the reason `normal` lives on HitContext (on_hit_wall gets no enemy)
    let n = c.normal;
    let dot = p.vel.x * n.x + p.vel.y * n.y;
    p.vel - n * (2.0 * dot)
}

pub fn normal_off_enemy(p: &Projectile, e: &Enemy) -> Vec2 {
    (p.pos - e.pos).normalize()
}
