Added game state and network client

This commit is contained in:
2026-05-20 19:56:25 +01:00
parent 8d3998945a
commit 48f8ae6c08
34 changed files with 713 additions and 16 deletions

View File

@@ -1,3 +0,0 @@
fileFormatVersion: 2
guid: 17aa567cc7b54d92a034c458aac68a8e
timeCreated: 1779196107

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 2644414c902145f4b252bf43944a2194
timeCreated: 1779273131

View File

@@ -0,0 +1,31 @@
using Unity.Netcode;
using UnityEngine;
namespace PashaBibko.PenguinChase.GameState
{
public enum GameState
{
Lobby,
Playing,
}
public class GameStateController : NetworkBehaviour
{
private static GameStateController sInstance;
[SerializeField] private NetworkVariable<GameState> GlobalState = new();
private void Awake()
{
// Stops overlapping instances
if (sInstance is not null)
{
Debug.LogError($"Multiple of [{nameof(GameStateController)}] cannot exist.");
Destroy(gameObject);
return;
}
sInstance = this;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 1ad1829714634ac1ab8f480e54fe27e9
timeCreated: 1779273141

View File

@@ -0,0 +1,33 @@
using Unity.Netcode;
using UnityEngine;
namespace PashaBibko.PenguinChase.GameState
{
public class GameStateSpawner : MonoBehaviour
{
private static GameStateSpawner sInstance;
[Header("References")]
[SerializeField] private GameObject GameStateControllerPrefab;
private void Awake()
{
// Stops overlapping instances
if (sInstance is not null)
{
Debug.LogError($"Multiple of [{nameof(GameStateSpawner)}] cannot exist.");
Destroy(gameObject);
return;
}
sInstance = this;
}
public static void CreateNetworkGameStateController()
{
GameObject go = Instantiate(sInstance.GameStateControllerPrefab);
go.GetComponent<NetworkObject>().Spawn();
DontDestroyOnLoad(go);
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 354f068d231a4ca0a1ccd0560f1836d5
timeCreated: 1779273552

View File

@@ -1,4 +1,4 @@
using PashaBibko.PenguinChase.Core.Network;
using PashaBibko.PenguinChase.Network;
using WebSocketSharp;
using UnityEngine.UI;
using UnityEngine;

View File

@@ -4,7 +4,7 @@ using Unity.Services.Core;
using System.Collections;
using UnityEngine;
namespace PashaBibko.PenguinChase.Core.Network
namespace PashaBibko.PenguinChase.Network
{
public static class Authenticator
{

View File

@@ -2,7 +2,7 @@
using Unity.Netcode;
using UnityEngine;
namespace PashaBibko.PenguinChase.Core.Network
namespace PashaBibko.PenguinChase.Network
{
public class ConnectionManager : MonoBehaviour
{

View File

@@ -6,7 +6,7 @@ using Unity.Netcode;
using UnityEngine;
using System;
namespace PashaBibko.PenguinChase.Core.Network
namespace PashaBibko.PenguinChase.Network
{
public enum TransportType
{
@@ -91,7 +91,7 @@ namespace PashaBibko.PenguinChase.Core.Network
// Then actually hosts the lobby
yield return sConnectionManager.Host(() =>
{
NetworkManager.Singleton.SceneManager.LoadScene("GameScene", LoadSceneMode.Single);
NetworkManager.Singleton.SceneManager.LoadScene("LobbyScene", LoadSceneMode.Single);
Debug.Log("Hosted");
});
}

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

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 4a2fdb2b188edc64aaa5ecbc8568bae0

View File

@@ -1,8 +1,9 @@
using System.Collections;
using PashaBibko.PenguinChase.GameState;
using System.Collections;
using Unity.Netcode;
using System;
namespace PashaBibko.PenguinChase.Core.Network
namespace PashaBibko.PenguinChase.Network
{
public class LocalhostTransport : INetworkTransport
{
@@ -18,6 +19,8 @@ namespace PashaBibko.PenguinChase.Core.Network
public IEnumerator Host(Action callback)
{
NetworkManager.Singleton.StartHost();
GameStateSpawner.CreateNetworkGameStateController();
ConnectionManager.CreateNetworkConnectionManager();
callback.Invoke();

View File

@@ -1,4 +1,5 @@
using PashaBibko.PenguinChase.Extensions;
using PashaBibko.PenguinChase.GameState;
using PashaBibko.PenguinChase.Extensions;
using Unity.Services.Relay.Models;
using Unity.Services.Relay;
using System.Collections;
@@ -6,7 +7,7 @@ using Unity.Netcode;
using UnityEngine;
using System;
namespace PashaBibko.PenguinChase.Core.Network
namespace PashaBibko.PenguinChase.Network
{
public class UnityRelayTransport : INetworkTransport
{
@@ -59,7 +60,9 @@ namespace PashaBibko.PenguinChase.Core.Network
Network.CurrentTransportComponent.SetHostRelayData(allocation);
NetworkManager.Singleton.StartHost();
GameStateSpawner.CreateNetworkGameStateController();
ConnectionManager.CreateNetworkConnectionManager();
Debug.Log($"Started server with code: [{joinCode}]");
callback.Invoke();