Added basic lobby

This commit is contained in:
2026-05-20 21:03:54 +01:00
parent 48f8ae6c08
commit 6055fba959
15 changed files with 1019 additions and 6 deletions

View File

@@ -1,4 +1,5 @@
using UnityEngine;
using UnityEngine.SceneManagement;
namespace PashaBibko.PenguinChase.Core
{
@@ -7,6 +8,10 @@ namespace PashaBibko.PenguinChase.Core
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
private static void LoadBoostrapScene()
{
// Makes sure the game onto the first scene
SceneManager.LoadScene(0);
// Loads the bootstrap object
GameObject go = Resources.Load<GameObject>("Bootstrap");
GameObject instance = Object.Instantiate(go);

View File

@@ -12,4 +12,4 @@ namespace PashaBibko.PenguinChase.Extensions
}
}
}
}
}

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 24cb1941713e23d4dbc9f68bb85d6435
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,19 @@
using PashaBibko.PenguinChase.Network;
using UnityEngine.UI;
using UnityEngine;
namespace PashaBibko.PenguinChase.Lobby
{
public class LobbyClientDisplay : MonoBehaviour
{
[Header("References")]
[SerializeField] private Text PlayerCountDisplay;
[SerializeField] private Text PlayerNameDisplay;
public void SetText(GameNetworkClient client)
{
PlayerCountDisplay.text = $"[{client.LocalPlayerCount}]";
PlayerNameDisplay.text = client.PlayerName;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 9f49201e34d24522b93e78c7c24206ee
timeCreated: 1779304359

View File

@@ -0,0 +1,36 @@
using PashaBibko.PenguinChase.Network;
using UnityEngine;
namespace PashaBibko.PenguinChase.Lobby
{
public class LobbyClientDisplayManager : MonoBehaviour
{
[Header("References")]
[SerializeField] private GameObject ClientDisplayPrefab;
[SerializeField] private GameObject ClientDisplayParent;
private int mCachedClientCount = -1; // Forces a refresh on initial load
private void Update()
{
// Checks if the client count has changed since last update
if (mCachedClientCount != GameNetworkClient.ConnectedClientCount)
{
// Clears all old client displays
foreach (Transform child in ClientDisplayParent.transform)
{
Destroy(child.gameObject);
}
// Creates new objects for each client
GameNetworkClient[] clients = GameNetworkClient.ConnectedClients;
foreach (GameNetworkClient client in clients)
{
GameObject go = Instantiate(ClientDisplayPrefab, ClientDisplayParent.transform);
LobbyClientDisplay display = go.GetComponent<LobbyClientDisplay>();
display.SetText(client);
}
}
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 3dbc4cc72e209e84aa134a24a54c818f

View File

@@ -10,6 +10,8 @@ public class MainMenuController : MonoBehaviour
[SerializeField] private Button JoinButton;
[SerializeField] private InputField HostCode;
[SerializeField] private Dropdown HostNetworkType;
[SerializeField] private InputField PlayerName;
[SerializeField] private Slider PlayerCount;
private void Start()
{
@@ -35,5 +37,15 @@ public class MainMenuController : MonoBehaviour
Network.Join(code);
});
PlayerCount.onValueChanged.AddListener(value =>
{
GameNetworkClient.LocalClientCount = Mathf.RoundToInt(value);
});
PlayerName.onEndEdit.AddListener(value =>
{
GameNetworkClient.LocalName = value;
});
}
}

View File

@@ -45,4 +45,4 @@ namespace PashaBibko.PenguinChase.Network
networkObject.SpawnAsPlayerObject(id);
}
}
}
}

View File

@@ -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);
}
}