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

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

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 54594dc099644958a5f437811cc47a5e
timeCreated: 1779205047

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

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 873c14eb9ab94868b0572ae86759b2a7
timeCreated: 1779205549