73 lines
3.1 KiB
C#
73 lines
3.1 KiB
C#
using Unity.Mathematics;
|
|
|
|
namespace ProjectM.Simulation
|
|
{
|
|
/// <summary>
|
|
/// Pure, deterministic Husk AI math — no RNG, no wall-clock — so server simulation stays reproducible and
|
|
/// the helpers are EditMode-unit-testable without an ECS world (mirrors <see cref="PlayerSpawnMath"/> /
|
|
/// <c>StatMath</c>).
|
|
/// </summary>
|
|
public static class EnemyAIMath
|
|
{
|
|
/// <summary>
|
|
/// Planar (XZ) seek velocity from <paramref name="from"/> toward <paramref name="to"/> at
|
|
/// <paramref name="speed"/>. Y is forced to 0 (top-down plane). Returns zero once within
|
|
/// <paramref name="stopDistance"/> (so the Husk halts at strike range instead of jittering through the
|
|
/// target) or when the two points coincide.
|
|
/// </summary>
|
|
public static float3 SeekVelocity(float3 from, float3 to, float speed, float stopDistance)
|
|
{
|
|
float3 d = to - from;
|
|
d.y = 0f;
|
|
float distSq = math.lengthsq(d);
|
|
if (distSq <= stopDistance * stopDistance || distSq < 1e-8f)
|
|
return float3.zero;
|
|
return math.normalize(d) * speed;
|
|
}
|
|
|
|
/// <summary>
|
|
/// True when <paramref name="to"/> is within <paramref name="range"/> of <paramref name="from"/> on the
|
|
/// XZ plane.
|
|
/// </summary>
|
|
public static bool InAttackRange(float3 from, float3 to, float range)
|
|
{
|
|
float3 d = to - from;
|
|
d.y = 0f;
|
|
return math.lengthsq(d) <= range * range;
|
|
}
|
|
/// <summary>
|
|
/// Projects a planar movement <paramref name="vel"/> onto a wall plane defined by <paramref name="surfaceNormal"/>
|
|
/// (collide-and-slide): removes the component of <paramref name="vel"/> that pushes into the surface so the
|
|
/// mover glances along the wall instead of stopping dead. Both inputs are flattened to the XZ plane (top-down).
|
|
/// Returns <paramref name="vel"/> unchanged when the normal is degenerate.
|
|
/// </summary>
|
|
public static float3 SlideVelocity(float3 vel, float3 surfaceNormal)
|
|
{
|
|
surfaceNormal.y = 0f;
|
|
float len = math.length(surfaceNormal);
|
|
if (len < 1e-6f)
|
|
return vel;
|
|
float3 n = surfaceNormal / len;
|
|
float3 slid = vel - math.dot(vel, n) * n;
|
|
slid.y = 0f;
|
|
return slid;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Deterministic planar ring position around <paramref name="center"/> for spawn
|
|
/// <paramref name="index"/>: evenly spaced over <paramref name="slots"/> angles at
|
|
/// <paramref name="radius"/>. Stable per index so a replayed spawn lands identically.
|
|
/// </summary>
|
|
public static float3 RingPosition(float3 center, int index, int slots, float radius)
|
|
{
|
|
if (slots < 1)
|
|
slots = 1;
|
|
int slot = ((index % slots) + slots) % slots;
|
|
float angle = (2f * math.PI * slot) / slots;
|
|
math.sincos(angle, out float s, out float c);
|
|
return center + new float3(c * radius, 0f, s * radius);
|
|
}
|
|
}
|
|
}
|