Finished basic lobby code

This commit is contained in:
2026-05-19 22:43:27 +01:00
parent e8e6c710df
commit 32a731d228
21 changed files with 1016 additions and 45 deletions

View File

@@ -39,6 +39,8 @@ namespace PashaBibko.PenguinChase.Core.Network
private static void OnClientJoin(ulong id)
{
GameObject client = Instantiate(ClientPrefab);
DontDestroyOnLoad(client);
NetworkObject networkObject = client.GetComponent<NetworkObject>();
networkObject.SpawnAsPlayerObject(id);
}

View File

@@ -1,11 +1,10 @@
using PashaBibko.Pacore.Attributes;
using Unity.Netcode.Transports.UTP;
using UnityEngine.SceneManagement;
using System.Collections;
using Unity.Netcode;
using UnityEngine;
using System;
using JetBrains.Annotations;
using UnityEngine.SceneManagement;
namespace PashaBibko.PenguinChase.Core.Network
{
@@ -19,17 +18,16 @@ namespace PashaBibko.PenguinChase.Core.Network
public interface INetworkTransport
{
public IEnumerator Join(string code);
public IEnumerator Host();
public IEnumerator Join(string code, Action callback);
public IEnumerator Host(Action callback);
}
public class Network : MonoBehaviour
{
private static Network sInstance;
[Header("Transports")] [SerializeField]
private GameObject LocalHostTransport;
[Header("Transports")]
[SerializeField] private GameObject LocalHostTransport;
[SerializeField] private GameObject UnityRelayTransport;
[Header("View only")] [SerializeField, InspectorReadOnly]
@@ -47,7 +45,9 @@ namespace PashaBibko.PenguinChase.Core.Network
#endif // UNITY_EDITOR
private static INetworkTransport sConnectionManager;
private GameObject mCurrentChildTransport;
private bool mSafeToConnect = true;
public static UnityTransport CurrentTransportComponent
{
@@ -72,29 +72,83 @@ namespace PashaBibko.PenguinChase.Core.Network
sInstance = this;
}
[UsedImplicitly, InspectorCallable("Choose Local Host Transport")]
private void ChooseLocalHostTransport() => CurrentTransport = TransportType.Localhost;
[UsedImplicitly, InspectorCallable("Host")]
public void Host()
private IEnumerator HostInternal()
{
if (sConnectionManager is null)
// Waits for it to be safe to connect, quits after 5 seconds
float timeout = 0f;
while (!mSafeToConnect)
{
throw new InvalidOperationException("No connection manager has been set.");
}
if (timeout > 5f)
{
Debug.LogError("Host timed out");
yield break;
}
StartCoroutine(sConnectionManager.Host());
timeout += Time.deltaTime;
yield return null;
}
// Then actually hosts the lobby
yield return sConnectionManager.Host(() =>
{
SceneManager.UnloadSceneAsync("MainMenu");
NetworkManager.Singleton.SceneManager.LoadScene("GameScene", LoadSceneMode.Additive);
Debug.Log("Hosted");
});
}
[UsedImplicitly, InspectorCallable("Join")]
public void Join()
public static void Host()
{
if (sConnectionManager is null)
{
throw new InvalidOperationException("No connection manager has been set.");
}
StartCoroutine(sConnectionManager.Join(GameJoinCode));
if (sInstance is null)
{
throw new InvalidOperationException("No network manager has been set.");
}
sInstance.StartCoroutine(sInstance.HostInternal());
}
private IEnumerator JoinInternal(string code)
{
// Waits for it to be safe to connect, quits after 5 seconds
float timeout = 0f;
while (!mSafeToConnect)
{
if (timeout > 5f)
{
Debug.LogError("Host timed out");
yield break;
}
timeout += Time.deltaTime;
yield return null;
}
// Then connects to the lobby
yield return sConnectionManager.Join(code, () =>
{
Debug.Log($"Joined {code}");
});
}
public static void Join(string code)
{
if (sConnectionManager is null)
{
throw new InvalidOperationException("No connection manager has been set.");
}
if (sInstance is null)
{
throw new InvalidOperationException("No network manager has been set.");
}
sInstance.StartCoroutine(sInstance.JoinInternal(code));
}
private IEnumerator ChangeTransport(TransportType transport)
@@ -105,6 +159,7 @@ namespace PashaBibko.PenguinChase.Core.Network
yield break;
}
mSafeToConnect = false;
InternalCurrentTransport = transport;
// Shutdown existing network manager (if there is one) to stop multiple at once
@@ -138,9 +193,12 @@ namespace PashaBibko.PenguinChase.Core.Network
yield break;
default:
mSafeToConnect = true;
throw new ArgumentOutOfRangeException();
}
DontDestroyOnLoad(mCurrentChildTransport);
mSafeToConnect = true;
}
}
}

View File

@@ -1,22 +1,26 @@
using System.Collections;
using Unity.Netcode;
using System;
namespace PashaBibko.PenguinChase.Core.Network
{
public class LocalhostTransport : INetworkTransport
{
// TODO: Allow connection to different devices on local network
public IEnumerator Join(string _)
public IEnumerator Join(string _, Action callback)
{
NetworkManager.Singleton.StartClient();
callback.Invoke();
yield break;
}
public IEnumerator Host()
public IEnumerator Host(Action callback)
{
NetworkManager.Singleton.StartHost();
ConnectionManager.CreateNetworkConnectionManager();
callback.Invoke();
yield break;
}
}

View File

@@ -4,6 +4,7 @@ using Unity.Services.Relay;
using System.Collections;
using Unity.Netcode;
using UnityEngine;
using System;
namespace PashaBibko.PenguinChase.Core.Network
{
@@ -11,7 +12,7 @@ namespace PashaBibko.PenguinChase.Core.Network
{
private const uint MAX_CONNECTIONS = 7;
public IEnumerator Join(string code)
public IEnumerator Join(string code, Action callback)
{
yield return Authenticator.Authenticate();
JoinAllocation allocation;
@@ -26,9 +27,11 @@ namespace PashaBibko.PenguinChase.Core.Network
Network.CurrentTransportComponent.SetRelayServerData(allocation);
NetworkManager.Singleton.StartClient();
callback.Invoke();
}
public IEnumerator Host()
public IEnumerator Host(Action callback)
{
yield return Authenticator.Authenticate();
Allocation allocation;
@@ -41,7 +44,7 @@ namespace PashaBibko.PenguinChase.Core.Network
allocation = result.Value;
}
string joinCode = null;
string joinCode;
{
Result<string> result = new();
yield return RelayService.Instance
@@ -56,6 +59,8 @@ namespace PashaBibko.PenguinChase.Core.Network
ConnectionManager.CreateNetworkConnectionManager();
Debug.Log($"Started server with code: [{joinCode}]");
callback.Invoke();
}
}
}

View File

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

View File

@@ -0,0 +1,25 @@
using PashaBibko.PenguinChase.Core.Network;
using UnityEngine.UI;
using UnityEngine;
public class MainMenuController : MonoBehaviour
{
[Header("References")]
[SerializeField] private Button HostButton;
[SerializeField] private Button JoinButton;
private void Start()
{
HostButton.onClick.AddListener(() =>
{
Network.CurrentTransport = TransportType.Localhost;
Network.Host();
});
JoinButton.onClick.AddListener(() =>
{
Network.CurrentTransport = TransportType.Localhost;
Network.Join("");
});
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 0e21d4c5d0ed58b48ab78ffe9eb72e6a