multiplayer refactor
This commit is contained in:
@@ -7,6 +7,7 @@ using R3;
|
||||
using RebootKit.Engine.Console;
|
||||
using RebootKit.Engine.Foundation;
|
||||
using RebootKit.Engine.Input;
|
||||
using RebootKit.Engine.Network;
|
||||
using RebootKit.Engine.Simulation;
|
||||
using RebootKit.Engine.Steam;
|
||||
using Unity.Netcode;
|
||||
@@ -26,19 +27,16 @@ namespace RebootKit.Engine.Main {
|
||||
static readonly Logger s_Logger = new Logger("RR");
|
||||
|
||||
[ConfigVar("con.write_log", 1, "Enables writing game log to console output")]
|
||||
static ConfigVar s_writeLogToConsole;
|
||||
|
||||
[ConfigVar("sv.tick_rate", 24, "Server tick rate in Hz")]
|
||||
public static ConfigVar TickRate;
|
||||
|
||||
static ConfigVar s_WriteLogToConsole;
|
||||
|
||||
internal static EngineConfigAsset EngineConfig;
|
||||
|
||||
static DisposableBag s_disposableBag;
|
||||
static DisposableBag s_servicesBag;
|
||||
static DisposableBag s_DisposableBag;
|
||||
static DisposableBag s_ServicesBag;
|
||||
|
||||
static AsyncOperationHandle<SceneInstance> s_mainMenuSceneHandle;
|
||||
static AsyncOperationHandle<SceneInstance> s_MainMenuSceneHandle;
|
||||
|
||||
static NetworkSystem s_networkSystemPrefab;
|
||||
static NetworkSystem s_NetworkSystemPrefab;
|
||||
internal static NetworkSystem NetworkSystemInstance;
|
||||
|
||||
internal static ConsoleService Console { get; private set; }
|
||||
@@ -49,10 +47,6 @@ namespace RebootKit.Engine.Main {
|
||||
|
||||
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.
|
||||
@@ -62,13 +56,13 @@ namespace RebootKit.Engine.Main {
|
||||
EngineConfig = configAsset;
|
||||
|
||||
s_Logger.Info("Initializing");
|
||||
s_servicesBag = new DisposableBag();
|
||||
s_disposableBag = new DisposableBag();
|
||||
s_ServicesBag = new DisposableBag();
|
||||
s_DisposableBag = new DisposableBag();
|
||||
|
||||
s_Logger.Info("Registering core services");
|
||||
Console = CreateService<ConsoleService>();
|
||||
Input = new InputService(EngineConfig.inputConfig);
|
||||
s_servicesBag.Add(Input);
|
||||
s_ServicesBag.Add(Input);
|
||||
World = CreateService<WorldService>();
|
||||
|
||||
await InitializeAssetsAsync(cancellationToken);
|
||||
@@ -80,7 +74,7 @@ namespace RebootKit.Engine.Main {
|
||||
|
||||
// @NOTE: This method is called after the main scene is loaded.
|
||||
internal static async UniTask RunAsync(CancellationToken cancellationToken) {
|
||||
s_networkSystemPrefab =
|
||||
s_NetworkSystemPrefab =
|
||||
Resources.Load<NetworkSystem>(RConsts.k_CoreNetworkGameSystemsResourcesPath);
|
||||
|
||||
NetworkManager.Singleton.OnConnectionEvent += OnConnectionEvent;
|
||||
@@ -97,7 +91,7 @@ namespace RebootKit.Engine.Main {
|
||||
|
||||
Observable.EveryUpdate()
|
||||
.Subscribe(_ => Tick())
|
||||
.AddTo(ref s_disposableBag);
|
||||
.AddTo(ref s_DisposableBag);
|
||||
|
||||
await OpenMainMenuAsync(cancellationToken);
|
||||
|
||||
@@ -133,8 +127,8 @@ namespace RebootKit.Engine.Main {
|
||||
SteamManager.Shutdown();
|
||||
#endif
|
||||
|
||||
s_servicesBag.Dispose();
|
||||
s_disposableBag.Dispose();
|
||||
s_ServicesBag.Dispose();
|
||||
s_DisposableBag.Dispose();
|
||||
}
|
||||
|
||||
// Assets API
|
||||
@@ -176,16 +170,16 @@ namespace RebootKit.Engine.Main {
|
||||
return;
|
||||
}
|
||||
|
||||
s_mainMenuSceneHandle = Addressables.LoadSceneAsync(EngineConfig.mainMenuScene, LoadSceneMode.Additive);
|
||||
await s_mainMenuSceneHandle;
|
||||
s_MainMenuSceneHandle = Addressables.LoadSceneAsync(EngineConfig.mainMenuScene, LoadSceneMode.Additive);
|
||||
await s_MainMenuSceneHandle;
|
||||
}
|
||||
|
||||
internal static void CloseMainMenu() {
|
||||
if (!s_mainMenuSceneHandle.IsValid()) {
|
||||
if (!s_MainMenuSceneHandle.IsValid()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Addressables.UnloadSceneAsync(s_mainMenuSceneHandle);
|
||||
Addressables.UnloadSceneAsync(s_MainMenuSceneHandle);
|
||||
}
|
||||
|
||||
public static void SetServerWorld(string worldID) {
|
||||
@@ -253,13 +247,13 @@ namespace RebootKit.Engine.Main {
|
||||
}
|
||||
|
||||
TService service = asset.Create();
|
||||
s_servicesBag.Add(service);
|
||||
s_ServicesBag.Add(service);
|
||||
return service;
|
||||
}
|
||||
|
||||
public static TService CreateService<TService>() where TService : class, IService {
|
||||
TService service = Activator.CreateInstance<TService>();
|
||||
s_servicesBag.Add(service);
|
||||
s_ServicesBag.Add(service);
|
||||
return service;
|
||||
}
|
||||
|
||||
@@ -382,28 +376,8 @@ namespace RebootKit.Engine.Main {
|
||||
|
||||
GameInstance.SendChatMessageRpc(message);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
if (IsClient()) {
|
||||
ClientTick?.Invoke();
|
||||
}
|
||||
|
||||
TickCount++;
|
||||
}
|
||||
}
|
||||
|
||||
static void OnConnectionEvent(NetworkManager network, ConnectionEventData data) {
|
||||
@@ -416,7 +390,7 @@ namespace RebootKit.Engine.Main {
|
||||
GameInstance = Object.Instantiate(EngineConfig.gamePrefab);
|
||||
GameInstance.NetworkObject.Spawn();
|
||||
|
||||
NetworkSystemInstance = Object.Instantiate(s_networkSystemPrefab);
|
||||
NetworkSystemInstance = Object.Instantiate(s_NetworkSystemPrefab);
|
||||
NetworkSystemInstance.NetworkObject.Spawn();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user