Finished basic lobby code
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user