using Unity.Mathematics;
namespace ProjectM.Simulation
{
///
/// Pure, deterministic cone hit-test for the MC-4 melee cleave. Shares the EXACT range + bearing predicate with
/// (planar XZ distance gate + dot(bearing, facing) >= cos(halfAngle)) but as a
/// per-candidate boolean, so the cleave can collect ALL enemies in the cone — is a
/// single-winner reducer and cannot be reused for collect-all (MC-4 review REUSE-1). Burst-safe, allocation-free,
/// no wall-clock / no randomness; intended to run server-side inside the predicted MeleeComboSystem.
///
public static class MeleeConeMath
{
///
/// True iff lies within (planar XZ) of
/// AND its bearing from is within the cone half-angle of
/// ( = cos(halfAngle)). A coincident
/// (zero-distance) target is excluded (undefined bearing). must be
/// caller-normalized; a zero-length facing or a non-positive range returns false.
///
public static bool InCone(float3 from, float2 facingDir, float range, float cosHalfAngle, float3 targetPos)
{
if (range <= 0f || math.lengthsq(facingDir) < 1e-6f)
return false;
float3 offset = targetPos - from;
float2 planar = new float2(offset.x, offset.z);
float distSq = math.lengthsq(planar);
if (distSq < 1e-6f)
return false; // coincident -> undefined bearing
if (distSq > range * range)
return false; // out of range
float2 bearing = planar * math.rsqrt(distSq); // normalized planar bearing
return math.dot(bearing, facingDir) >= cosHalfAngle;
}
}
}