Started adding multiplayer

This commit is contained in:
2026-05-19 19:14:03 +01:00
parent f826689bdd
commit e8e6c710df
36 changed files with 892 additions and 17 deletions

View File

@@ -0,0 +1,46 @@
using PashaBibko.PenguinChase.Extensions;
using Unity.Netcode;
using UnityEngine;
namespace PashaBibko.PenguinChase.Core.Network
{
public class ConnectionManager : MonoBehaviour
{
private static ConnectionManager sInstance;
[SerializeField] private GameObject PrefabForEachClient;
public static GameObject ClientPrefab => sInstance?.PrefabForEachClient;
private void Start()
{
// Stops overlapping instances
if (sInstance is not null)
{
Debug.LogError($"Multiple of [{nameof(ConnectionManager)}] cannot exist.");
Destroy(gameObject);
return;
}
sInstance = this;
}
public static void CreateNetworkConnectionManager()
{
NetworkManager.Singleton.OnClientConnectedCallback += OnClientJoin;
OnClientJoin(0); // Has to be manually called for local client
}
public static void DestroyNetworkConnectionManager()
{
NetworkManager.Singleton.OnClientConnectedCallback -= OnClientJoin;
sInstance.DestroyAllChildren();
}
private static void OnClientJoin(ulong id)
{
GameObject client = Instantiate(ClientPrefab);
NetworkObject networkObject = client.GetComponent<NetworkObject>();
networkObject.SpawnAsPlayerObject(id);
}
}
}