Added game state and network client
This commit is contained in:
46
Assets/Scripts/Network/Authenticator.cs
Normal file
46
Assets/Scripts/Network/Authenticator.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using PashaBibko.PenguinChase.Extensions;
|
||||
using Unity.Services.Authentication;
|
||||
using Unity.Services.Core;
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
|
||||
namespace PashaBibko.PenguinChase.Network
|
||||
{
|
||||
public static class Authenticator
|
||||
{
|
||||
public static bool IsAuthenticated { get; private set; }
|
||||
private static bool sIsAuthenticating;
|
||||
|
||||
public static IEnumerator Authenticate()
|
||||
{
|
||||
// Early return if already authenticated
|
||||
Debug.Log($"Authenticate called, [Authenticated: {IsAuthenticated}]");
|
||||
if (IsAuthenticated)
|
||||
{
|
||||
yield break;
|
||||
}
|
||||
|
||||
// Stops multiple authentication attempts at the same time
|
||||
if (sIsAuthenticating)
|
||||
{
|
||||
yield return new WaitUntil(() => IsAuthenticated || !sIsAuthenticating);
|
||||
yield break; // User should be logged in from other attempt
|
||||
}
|
||||
sIsAuthenticating = true;
|
||||
|
||||
Debug.Log("Initializing Unity Services");
|
||||
yield return UnityServices
|
||||
.InitializeAsync()
|
||||
.Await();
|
||||
|
||||
Debug.Log("Signing in anonymously");
|
||||
yield return AuthenticationService.Instance
|
||||
.SignInAnonymouslyAsync()
|
||||
.Await();
|
||||
|
||||
Debug.Log("User has been authenticated");
|
||||
sIsAuthenticating = false;
|
||||
IsAuthenticated = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Network/Authenticator.cs.meta
Normal file
3
Assets/Scripts/Network/Authenticator.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8ed9598a0f71474ea90df6a62ba55723
|
||||
timeCreated: 1779203851
|
||||
48
Assets/Scripts/Network/ConnectionManager.cs
Normal file
48
Assets/Scripts/Network/ConnectionManager.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
using PashaBibko.PenguinChase.Extensions;
|
||||
using Unity.Netcode;
|
||||
using UnityEngine;
|
||||
|
||||
namespace PashaBibko.PenguinChase.Network
|
||||
{
|
||||
public class ConnectionManager : MonoBehaviour
|
||||
{
|
||||
private static ConnectionManager sInstance;
|
||||
|
||||
[SerializeField] private GameObject PrefabForEachClient;
|
||||
public static GameObject ClientPrefab => sInstance?.PrefabForEachClient;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
// 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);
|
||||
DontDestroyOnLoad(client);
|
||||
|
||||
NetworkObject networkObject = client.GetComponent<NetworkObject>();
|
||||
networkObject.SpawnAsPlayerObject(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Network/ConnectionManager.cs.meta
Normal file
3
Assets/Scripts/Network/ConnectionManager.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ed9f8d66fd664fd68f1901f0120a0daf
|
||||
timeCreated: 1779208828
|
||||
202
Assets/Scripts/Network/Network.cs
Normal file
202
Assets/Scripts/Network/Network.cs
Normal file
@@ -0,0 +1,202 @@
|
||||
using PashaBibko.Pacore.Attributes;
|
||||
using Unity.Netcode.Transports.UTP;
|
||||
using UnityEngine.SceneManagement;
|
||||
using System.Collections;
|
||||
using Unity.Netcode;
|
||||
using UnityEngine;
|
||||
using System;
|
||||
|
||||
namespace PashaBibko.PenguinChase.Network
|
||||
{
|
||||
public enum TransportType
|
||||
{
|
||||
UnityRelay,
|
||||
Localhost,
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
public interface INetworkTransport
|
||||
{
|
||||
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;
|
||||
[SerializeField] private GameObject UnityRelayTransport;
|
||||
|
||||
[Header("View only")] [SerializeField, InspectorReadOnly]
|
||||
private TransportType InternalCurrentTransport = TransportType.None;
|
||||
|
||||
public static TransportType CurrentTransport
|
||||
{
|
||||
get => sInstance?.InternalCurrentTransport ?? TransportType.None;
|
||||
set => sInstance?.StartCoroutine(sInstance.ChangeTransport(value));
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
[Header("Editor Only")]
|
||||
[SerializeField] private string GameJoinCode;
|
||||
#endif // UNITY_EDITOR
|
||||
|
||||
private static INetworkTransport sConnectionManager;
|
||||
|
||||
private GameObject mCurrentChildTransport;
|
||||
private bool mSafeToConnect = true;
|
||||
|
||||
public static UnityTransport CurrentTransportComponent
|
||||
{
|
||||
get
|
||||
{
|
||||
UnityTransport component = null;
|
||||
sInstance?.mCurrentChildTransport?.TryGetComponent(out component);
|
||||
return component;
|
||||
}
|
||||
}
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
// Stops overlapping instances
|
||||
if (sInstance is not null)
|
||||
{
|
||||
Debug.LogError($"Multiple of [{nameof(Network)}] cannot exist.");
|
||||
Destroy(gameObject);
|
||||
return;
|
||||
}
|
||||
|
||||
sInstance = this;
|
||||
}
|
||||
|
||||
private IEnumerator HostInternal()
|
||||
{
|
||||
// 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 actually hosts the lobby
|
||||
yield return sConnectionManager.Host(() =>
|
||||
{
|
||||
NetworkManager.Singleton.SceneManager.LoadScene("LobbyScene", LoadSceneMode.Single);
|
||||
Debug.Log("Hosted");
|
||||
});
|
||||
}
|
||||
|
||||
public static void Host()
|
||||
{
|
||||
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.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)
|
||||
{
|
||||
// Stops network from being restarted when unneeded
|
||||
if (transport == InternalCurrentTransport)
|
||||
{
|
||||
yield break;
|
||||
}
|
||||
|
||||
mSafeToConnect = false;
|
||||
InternalCurrentTransport = transport;
|
||||
|
||||
// Shutdown existing network manager (if there is one) to stop multiple at once
|
||||
if (NetworkManager.Singleton is not null)
|
||||
{
|
||||
NetworkManager.Singleton.Shutdown();
|
||||
while (NetworkManager.Singleton.ShutdownInProgress)
|
||||
{
|
||||
yield return null; // Waits for next frame
|
||||
}
|
||||
|
||||
Destroy(mCurrentChildTransport);
|
||||
}
|
||||
|
||||
// Loads the new transport controller and lets it setup
|
||||
switch (InternalCurrentTransport)
|
||||
{
|
||||
case TransportType.UnityRelay:
|
||||
mCurrentChildTransport = Instantiate(UnityRelayTransport);
|
||||
sConnectionManager = new UnityRelayTransport();
|
||||
break;
|
||||
|
||||
case TransportType.Localhost:
|
||||
mCurrentChildTransport = Instantiate(LocalHostTransport);
|
||||
sConnectionManager = new LocalhostTransport();
|
||||
break;
|
||||
|
||||
case TransportType.None:
|
||||
// Nothing needs to be done here as there is no network controller
|
||||
mCurrentChildTransport = null;
|
||||
yield break;
|
||||
|
||||
default:
|
||||
mSafeToConnect = true;
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
|
||||
DontDestroyOnLoad(mCurrentChildTransport);
|
||||
mSafeToConnect = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Network/Network.cs.meta
Normal file
3
Assets/Scripts/Network/Network.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f490d6c8b73d447fbe47c95efede54d0
|
||||
timeCreated: 1779196408
|
||||
36
Assets/Scripts/Network/NetworkClient.cs
Normal file
36
Assets/Scripts/Network/NetworkClient.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using System.Collections.Generic;
|
||||
using Unity.Netcode;
|
||||
using UnityEngine;
|
||||
|
||||
namespace PashaBibko.PenguinChase.Network
|
||||
{
|
||||
public class GameNetworkClient : NetworkBehaviour
|
||||
{
|
||||
private static Dictionary<ulong, GameNetworkClient> sConnectedClients = new();
|
||||
private static bool sDoneInitialSearch = false;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
name = $"NetworkClient-{OwnerClientId}";
|
||||
|
||||
// Searches for existing clients if it is the first one to be locally loaded
|
||||
if (!sDoneInitialSearch)
|
||||
{
|
||||
GameNetworkClient[] clients = FindObjectsByType<GameNetworkClient>(FindObjectsSortMode.None);
|
||||
foreach (GameNetworkClient client in clients)
|
||||
{
|
||||
ulong clientId = client.OwnerClientId;
|
||||
sConnectedClients.Add(clientId, this);
|
||||
}
|
||||
|
||||
sDoneInitialSearch = true;
|
||||
}
|
||||
|
||||
// Else adds it to the global list of clients
|
||||
else
|
||||
{
|
||||
sConnectedClients.Add(OwnerClientId, this);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/Network/NetworkClient.cs.meta
Normal file
2
Assets/Scripts/Network/NetworkClient.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4a2fdb2b188edc64aaa5ecbc8568bae0
|
||||
3
Assets/Scripts/Network/Transports.meta
Normal file
3
Assets/Scripts/Network/Transports.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d478eb44fa9f45258a97c4a57e2e97e9
|
||||
timeCreated: 1779205011
|
||||
30
Assets/Scripts/Network/Transports/LocalhostTransport.cs
Normal file
30
Assets/Scripts/Network/Transports/LocalhostTransport.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using PashaBibko.PenguinChase.GameState;
|
||||
using System.Collections;
|
||||
using Unity.Netcode;
|
||||
using System;
|
||||
|
||||
namespace PashaBibko.PenguinChase.Network
|
||||
{
|
||||
public class LocalhostTransport : INetworkTransport
|
||||
{
|
||||
// TODO: Allow connection to different devices on local network
|
||||
public IEnumerator Join(string _, Action callback)
|
||||
{
|
||||
NetworkManager.Singleton.StartClient();
|
||||
callback.Invoke();
|
||||
|
||||
yield break;
|
||||
}
|
||||
|
||||
public IEnumerator Host(Action callback)
|
||||
{
|
||||
NetworkManager.Singleton.StartHost();
|
||||
|
||||
GameStateSpawner.CreateNetworkGameStateController();
|
||||
ConnectionManager.CreateNetworkConnectionManager();
|
||||
|
||||
callback.Invoke();
|
||||
yield break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 54594dc099644958a5f437811cc47a5e
|
||||
timeCreated: 1779205047
|
||||
71
Assets/Scripts/Network/Transports/UnityRelayTransport.cs
Normal file
71
Assets/Scripts/Network/Transports/UnityRelayTransport.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
using PashaBibko.PenguinChase.GameState;
|
||||
using PashaBibko.PenguinChase.Extensions;
|
||||
using Unity.Services.Relay.Models;
|
||||
using Unity.Services.Relay;
|
||||
using System.Collections;
|
||||
using Unity.Netcode;
|
||||
using UnityEngine;
|
||||
using System;
|
||||
|
||||
namespace PashaBibko.PenguinChase.Network
|
||||
{
|
||||
public class UnityRelayTransport : INetworkTransport
|
||||
{
|
||||
private const int MAX_CONNECTIONS = 7;
|
||||
|
||||
public IEnumerator Join(string code, Action callback)
|
||||
{
|
||||
yield return Authenticator.Authenticate();
|
||||
JoinAllocation allocation;
|
||||
{
|
||||
Result<JoinAllocation> result = new();
|
||||
yield return RelayService.Instance
|
||||
.JoinAllocationAsync(code)
|
||||
.Await(result);
|
||||
|
||||
allocation = result.Value;
|
||||
}
|
||||
|
||||
Network.CurrentTransportComponent.SetRelayServerData(allocation);
|
||||
NetworkManager.Singleton.StartClient();
|
||||
|
||||
callback.Invoke();
|
||||
}
|
||||
|
||||
public IEnumerator Host(Action callback)
|
||||
{
|
||||
yield return Authenticator.Authenticate();
|
||||
Debug.Log("Authenticated");
|
||||
|
||||
Allocation allocation;
|
||||
{
|
||||
Result<Allocation> result = new();
|
||||
yield return RelayService.Instance
|
||||
.CreateAllocationAsync(MAX_CONNECTIONS)
|
||||
.Await(result);
|
||||
|
||||
allocation = result.Value;
|
||||
}
|
||||
|
||||
string joinCode;
|
||||
{
|
||||
Result<string> result = new();
|
||||
yield return RelayService.Instance
|
||||
.GetJoinCodeAsync(allocation.AllocationId)
|
||||
.Await(result);
|
||||
|
||||
joinCode = result.Value;
|
||||
}
|
||||
|
||||
Network.CurrentTransportComponent.SetHostRelayData(allocation);
|
||||
NetworkManager.Singleton.StartHost();
|
||||
|
||||
GameStateSpawner.CreateNetworkGameStateController();
|
||||
ConnectionManager.CreateNetworkConnectionManager();
|
||||
|
||||
Debug.Log($"Started server with code: [{joinCode}]");
|
||||
|
||||
callback.Invoke();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 873c14eb9ab94868b0572ae86759b2a7
|
||||
timeCreated: 1779205549
|
||||
Reference in New Issue
Block a user