using Unity.Mathematics;
namespace ProjectM.Simulation
{
///
/// Deterministic spawn-position math for co-op. Pure and side-effect-free — no RNG, no wall-clock —
/// so the same NetworkId always yields the same planar offset. The server places each connecting
/// player at a stable ring slot keyed by its NetworkId, so players never stack. Unit-tested in
/// EditMode (no netcode world required).
///
public static class PlayerSpawnMath
{
///
/// Planar (XZ) offset from the base spawn point for a connection's NetworkId. NetworkIds start at
/// 1, so id 1 maps to slot 0. Slots are evenly spaced around a ring of ;
/// once a ring's slots are full, further players spill onto an outer ring (radius * (ring + 1)),
/// keeping every position distinct. Returns zero when <= 0 (degenerate
/// or unbaked spawner) so behaviour falls back to the single shared spawn point.
///
public static float3 SpawnOffset(int networkId, float radius, int slots)
{
if (radius <= 0f)
return float3.zero;
if (slots < 1)
slots = 1;
int idx = networkId - 1;
if (idx < 0)
idx = 0;
int ring = idx / slots;
int slotInRing = idx % slots;
float angle = (2f * math.PI * slotInRing) / slots;
float r = radius * (ring + 1);
math.sincos(angle, out float s, out float c);
return new float3(c * r, 0f, s * r);
}
}
}