Added basic lobby
This commit is contained in:
@@ -45,4 +45,4 @@ namespace PashaBibko.PenguinChase.Network
|
||||
networkObject.SpawnAsPlayerObject(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +1,54 @@
|
||||
using System.Collections.Generic;
|
||||
using Unity.Netcode;
|
||||
using System.Linq;
|
||||
using Unity.Collections;
|
||||
using UnityEngine;
|
||||
|
||||
namespace PashaBibko.PenguinChase.Network
|
||||
{
|
||||
public class GameNetworkClient : NetworkBehaviour
|
||||
{
|
||||
private static Dictionary<ulong, GameNetworkClient> sConnectedClients = new();
|
||||
public static int LocalClientCount { get; set; }
|
||||
public static string LocalName { get; set; }
|
||||
|
||||
private static readonly Dictionary<ulong, GameNetworkClient> sConnectedClients = new();
|
||||
private static bool sDoneInitialSearch = false;
|
||||
|
||||
public static GameNetworkClient LocalClient { get; private set; }
|
||||
|
||||
public static GameNetworkClient[] ConnectedClients => sConnectedClients.Values.ToArray();
|
||||
public static int ConnectedClientCount => sConnectedClients.Count;
|
||||
|
||||
private readonly NetworkVariable<FixedString32Bytes> mPlayerName = new
|
||||
(
|
||||
default,
|
||||
NetworkVariableReadPermission.Everyone,
|
||||
NetworkVariableWritePermission.Owner
|
||||
);
|
||||
|
||||
private readonly NetworkVariable<int> mLocalPlayerCount = new
|
||||
(
|
||||
0,
|
||||
NetworkVariableReadPermission.Everyone,
|
||||
NetworkVariableWritePermission.Owner
|
||||
);
|
||||
|
||||
public int LocalPlayerCount => mLocalPlayerCount.Value;
|
||||
public string PlayerName => mPlayerName.Value.ToString();
|
||||
|
||||
private void Start()
|
||||
{
|
||||
name = $"NetworkClient-{OwnerClientId}";
|
||||
|
||||
// If it is the local instance registers it as such
|
||||
if (IsOwner)
|
||||
{
|
||||
mLocalPlayerCount.Value = LocalClientCount;
|
||||
mPlayerName.Value = LocalName;
|
||||
|
||||
LocalClient = this;
|
||||
|
||||
Debug.Log($"[Game Network Client] has had start called [{LocalName} | {mPlayerName.Value}]");
|
||||
}
|
||||
|
||||
// Searches for existing clients if it is the first one to be locally loaded
|
||||
if (!sDoneInitialSearch)
|
||||
{
|
||||
@@ -32,5 +68,7 @@ namespace PashaBibko.PenguinChase.Network
|
||||
sConnectedClients.Add(OwnerClientId, this);
|
||||
}
|
||||
}
|
||||
|
||||
public override void OnDestroy() => sConnectedClients.Remove(OwnerClientId);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user