Co-Op Layer

This commit is contained in:
Luis Gonzalez
2026-06-01 10:48:18 -07:00
parent 1f647dd5e1
commit e851d5f8e9
29 changed files with 667 additions and 20 deletions
@@ -0,0 +1,41 @@
using Unity.Mathematics;
namespace ProjectM.Simulation
{
/// <summary>
/// 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).
/// </summary>
public static class PlayerSpawnMath
{
/// <summary>
/// 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 <paramref name="radius"/>;
/// once a ring's slots are full, further players spill onto an outer ring (radius * (ring + 1)),
/// keeping every position distinct. Returns zero when <paramref name="radius"/> &lt;= 0 (degenerate
/// or unbaked spawner) so behaviour falls back to the single shared spawn point.
/// </summary>
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);
}
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: cabd6efb0aaf14e3a95c47c95bc4160c
@@ -11,5 +11,10 @@ namespace ProjectM.Simulation
{
public Entity PlayerPrefab;
public float3 SpawnPoint;
// M4 co-op: deterministic per-NetworkId ring spread so players don't stack on connect.
// Radius of the spawn ring (metres); RingSlots = evenly-spaced positions before rings expand.
public float SpawnRingRadius;
public int RingSlots;
}
}