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,35 @@
using Unity.Collections;
using Unity.Entities;
namespace ProjectM.Simulation
{
/// <summary>
/// Connection intent for the M4 LAN host/join flow. Lives in Simulation so both the server and
/// client worlds can read it. A UI (<c>ConnectionUI</c>) — or, in the editor, the auto-host system —
/// writes the desired <see cref="Mode"/> + endpoint and sets <see cref="Requested"/> true; the
/// per-world ConnectionControlSystem turns it into a netcode <c>NetworkStreamRequestListen</c> /
/// <c>NetworkStreamRequestConnect</c> and clears <see cref="Requested"/>. Direct IP/LAN only for now —
/// Unity Relay is deferred to a later pass. Created per-world as a singleton.
/// </summary>
public enum ConnectionMode : byte
{
None,
Host,
Join,
}
public struct ConnectionConfig : IComponentData
{
/// <summary>What to do with this world's network stream.</summary>
public ConnectionMode Mode;
/// <summary>Dotted IPv4 to connect to (Join). Ignored for Host (binds AnyIpv4).</summary>
public FixedString64Bytes Address;
/// <summary>Listen/connect port.</summary>
public ushort Port;
/// <summary>Set true to request the control system act on Mode this frame; it clears the flag.</summary>
public bool Requested;
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 85356726c8f524aebb6cc93336fa09dd
@@ -0,0 +1,35 @@
#if UNITY_EDITOR
using Unity.Entities;
using Unity.NetCode;
namespace ProjectM.Simulation
{
/// <summary>
/// EDITOR-ONLY dev convenience for the M4 LAN flow. With auto-connect disabled in <c>GameBootstrap</c>,
/// this seeds a <see cref="ConnectionConfig"/> in each world on its first update so entering Play "just
/// works" multi-client: the server world hosts on loopback and every client / thin-client world joins
/// it — reproducing the old single-key playflow but for N players. Runs once per world then disables
/// itself, and skips seeding if a <see cref="ConnectionConfig"/> already exists (e.g. set by
/// ConnectionUI). Does not exist in player builds.
/// </summary>
[WorldSystemFilter(WorldSystemFilterFlags.ServerSimulation | WorldSystemFilterFlags.ClientSimulation | WorldSystemFilterFlags.ThinClientSimulation)]
public partial struct EditorAutoHostSystem : ISystem
{
public void OnUpdate(ref SystemState state)
{
state.Enabled = false; // run exactly once per world
if (SystemAPI.HasSingleton<ConnectionConfig>())
return;
const ushort port = 7979;
var cfg = state.WorldUnmanaged.IsServer()
? new ConnectionConfig { Mode = ConnectionMode.Host, Address = "127.0.0.1", Port = port, Requested = true }
: new ConnectionConfig { Mode = ConnectionMode.Join, Address = "127.0.0.1", Port = port, Requested = true };
var e = state.EntityManager.CreateEntity();
state.EntityManager.AddComponentData(e, cfg);
}
}
}
#endif
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 642fc5c4d71654becba10814a2d08e92
@@ -5,21 +5,24 @@ using UnityEngine.Scripting;
namespace ProjectM.Simulation
{
/// <summary>
/// Custom Netcode for Entities bootstrap. Subclassing <see cref="ClientServerBootstrap"/>
/// gives an explicit hook to customize world creation, tick rate, and auto-connect.
/// For now it reproduces the default behavior: create separate client and server worlds
/// based on the Multiplayer PlayMode Tools settings, without auto-connecting.
/// Custom Netcode for Entities bootstrap. Subclassing <see cref="ClientServerBootstrap"/> gives an
/// explicit hook to customize world creation, tick rate, and connection.
/// <para>
/// M4 (LAN co-op): auto-connect is DISABLED (<see cref="ClientServerBootstrap.AutoConnectPort"/> = 0).
/// Listening/connecting is driven explicitly via the <see cref="ConnectionConfig"/> singleton and the
/// per-world ConnectionControlSystems — from <c>ConnectionUI</c> (Host / Join + IP) in player builds,
/// or from the editor-only <c>EditorAutoHostSystem</c>, which auto-hosts on loopback and connects the
/// in-proc client plus any Multiplayer-PlayMode-Tools thin clients. Direct IP/LAN only for now; Unity
/// Relay is deferred to a later pass.
/// </para>
/// </summary>
[Preserve]
public class GameBootstrap : ClientServerBootstrap
{
public override bool Initialize(string defaultWorldName)
{
// Auto-connect in-editor: the server listens and the in-process client connects (over
// IPC) on the default BinaryWorlds host mode — one process hosts both worlds (M1 listen
// server). M3 replaces this with an explicit Unity Relay host/join flow. Set to 0 to
// disable auto-connect.
AutoConnectPort = 7979;
// No auto-connect: ConnectionConfig + the ConnectionControlSystems own listen/connect (M4).
AutoConnectPort = 0;
CreateDefaultClientServerWorlds();
return true;
}
@@ -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;
}
}