multiplayer stuff

This commit is contained in:
2025-07-01 18:57:31 +02:00
parent e5616474f1
commit dca9f8883a
13 changed files with 351 additions and 31 deletions

View File

@@ -0,0 +1,6 @@
using Unity.Netcode;
namespace RebootKit.Engine.Main {
public abstract class NetworkWorldController : NetworkBehaviour {
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: edd521a69a6f4e43b97ec258adf499a6
timeCreated: 1751377120

View File

@@ -4,10 +4,12 @@ using System.Threading;
using Cysharp.Threading.Tasks;
using R3;
using RebootKit.Engine.Foundation;
using RebootKit.Engine.Input;
using RebootKit.Engine.Services.Console;
using RebootKit.Engine.Services.Input;
using RebootKit.Engine.Services.Simulation;
using RebootKit.Engine.Simulation;
using RebootKit.Engine.Steam;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.AddressableAssets;
@@ -24,7 +26,7 @@ namespace RebootKit.Engine.Main {
[ConfigVar("con.write_log", 1, "Enables writing game log to console output")]
static ConfigVar s_writeLogToConsole;
[ConfigVar("sv.tick_rate", 60, "Server tick rate in Hz")]
public static ConfigVar TickRate;
@@ -41,14 +43,14 @@ namespace RebootKit.Engine.Main {
public static Camera MainCamera { get; internal set; }
internal static Game GameInstance;
public static Game GameInstance { get; internal set; }
public static ulong TickCount { get; private set; }
public static event Action<ulong> ServerTick = delegate { };
public static event Action ClientTick = delegate { };
// Lifecycle API
// @NOTE: This method is called at the very start of the game, when boot scene loaded.
internal static async UniTask InitAsync(EngineConfigAsset configAsset, CancellationToken cancellationToken) {
Assert.IsNotNull(configAsset, "Config asset is required");
@@ -66,11 +68,7 @@ namespace RebootKit.Engine.Main {
await InitializeAssetsAsync(cancellationToken);
// await SteamManager.InitializeAsync(cancellationToken);
// if (SteamManager.IsInitialized) {
// s_networkTransport = SteamManager.NetworkTransport;
// }
await SteamManager.InitializeAsync(cancellationToken);
}
// @NOTE: This method is called after the main scene is loaded.
@@ -107,14 +105,13 @@ namespace RebootKit.Engine.Main {
GameInstance = null;
}
if (NetworkManager.Singleton is not null) {
NetworkManager.Singleton.OnConnectionEvent -= OnConnectionEvent;
NetworkManager.Singleton.OnServerStarted -= OnServerStarted;
NetworkManager.Singleton.OnServerStopped -= OnServerStopped;
}
// SteamManager.Shutdown();
SteamManager.Shutdown();
s_servicesBag.Dispose();
s_disposableBag.Dispose();
@@ -167,7 +164,7 @@ namespace RebootKit.Engine.Main {
if (!s_mainMenuSceneHandle.IsValid()) {
return;
}
Addressables.UnloadSceneAsync(s_mainMenuSceneHandle);
}
@@ -176,7 +173,7 @@ namespace RebootKit.Engine.Main {
s_Logger.Error("Cannot set server world. Not a server instance.");
return;
}
if (GameInstance is null) {
s_Logger.Error("Game is not initialized. Cannot set server world.");
return;
@@ -189,7 +186,7 @@ namespace RebootKit.Engine.Main {
s_Logger.Error($"World '{worldID}' not found");
return;
}
GameInstance.SetCurrentWorldServerRpc(worldID);
}
@@ -272,7 +269,7 @@ namespace RebootKit.Engine.Main {
public static bool IsClient() {
return NetworkManager.Singleton.IsClient;
}
public static void StartHost() {
if (NetworkManager.Singleton.IsHost) {
s_Logger.Error("Already hosting a server");
@@ -282,9 +279,8 @@ namespace RebootKit.Engine.Main {
s_Logger.Info("Starting host");
NetworkManager.Singleton.StartHost();
}
public static void StopServer() {
}
public static void StopServer() { }
public static void Connect() {
if (NetworkManager.Singleton.IsClient) {
@@ -296,9 +292,23 @@ namespace RebootKit.Engine.Main {
NetworkManager.Singleton.StartClient();
}
public static void Disconnect() {
public static void ConnectWithSteamID(ulong steamId) {
if (NetworkManager.Singleton.IsClient) {
s_Logger.Error("Already connected to a server");
return;
}
s_Logger.Info($"Connecting to server with Steam ID: {steamId}");
if (NetworkManager.Singleton.NetworkConfig.NetworkTransport is FacepunchTransport facepunchTransport) {
facepunchTransport.targetSteamId = steamId;
NetworkManager.Singleton.StartClient();
} else {
s_Logger.Error("Network transport is not FacepunchTransport. Cannot connect with Steam ID.");
}
}
public static void Disconnect() { }
public static void SendChatMessage(string message) {
if (!IsClient()) {
s_Logger.Error("Cannot send chat message. Not connected to a server.");
@@ -313,16 +323,16 @@ namespace RebootKit.Engine.Main {
}
static float s_tickTimer;
static void Tick() {
float deltaTime = Time.deltaTime;
float minTickTime = 1.0f / TickRate.IndexValue;
s_tickTimer += deltaTime;
while (s_tickTimer >= minTickTime) {
s_tickTimer -= minTickTime;
if (IsServer()) {
ServerTick?.Invoke(TickCount);
}
@@ -333,14 +343,14 @@ namespace RebootKit.Engine.Main {
TickCount++;
}
World.Tick(deltaTime);
}
static void OnConnectionEvent(NetworkManager network, ConnectionEventData data) {
s_Logger.Info("Connection event: " + data.EventType);
}
static void OnServerStarted() {
s_Logger.Info("Server started");