working on multiplayer

This commit is contained in:
2025-06-24 14:45:45 +02:00
parent b1050f627b
commit 5a813f212c
67 changed files with 499 additions and 127 deletions

View File

@@ -0,0 +1,62 @@
using System.Threading;
using Cysharp.Threading.Tasks;
using R3;
using RebootKit.Engine.Foundation;
using RebootKit.Engine.Main;
using RebootKit.Engine.Services.Simulation;
namespace RebootKit.Engine.Services.GameMode {
public class GameModesService : IService {
static readonly Logger s_logger = new Logger(nameof(GameModesService));
[Inject] DIContext m_DIContext;
GameModeAsset m_GameModeAsset;
readonly CancellationTokenSource m_DestroyCancellationTokenSource = new CancellationTokenSource();
DisposableBag m_ActiveGameModeDisposableBag;
readonly ReactiveProperty<IGameMode> m_ActiveGameMode = new ReactiveProperty<IGameMode>(null);
public ReadOnlyReactiveProperty<IGameMode> ActiveGameMode => m_ActiveGameMode;
public void Dispose() {
m_DestroyCancellationTokenSource.Cancel();
m_DestroyCancellationTokenSource.Dispose();
m_ActiveGameModeDisposableBag.Dispose();
}
public void Start(GameModeAsset asset, WorldConfig worldConfig) {
if (m_ActiveGameMode.Value != null) {
s_logger.Warning("Game is already running");
return;
}
Stop();
m_ActiveGameModeDisposableBag = new DisposableBag();
m_ActiveGameMode.Value = asset.Create();
m_ActiveGameModeDisposableBag.Add(m_ActiveGameMode);
InitializeGameModeAsync(worldConfig, CancellationToken.None).Forget();
}
async UniTask InitializeGameModeAsync(WorldConfig worldConfig, CancellationToken cancellationToken) {
await m_ActiveGameMode.Value.OnInit(m_DestroyCancellationTokenSource.Token);
await RR.World.LoadAsync(worldConfig, cancellationToken);
m_ActiveGameMode.Value.OnStart();
Observable.EveryUpdate().Subscribe(_ => { m_ActiveGameMode.Value?.OnTick(); }).AddTo(ref m_ActiveGameModeDisposableBag);
}
public void Stop() {
if (m_ActiveGameMode.Value == null) {
return;
}
m_ActiveGameMode.Value.OnStop();
m_ActiveGameMode.Value = null;
m_ActiveGameModeDisposableBag.Dispose();
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 82082a4d082be274bbec52347d25b836

View File

@@ -0,0 +1,18 @@
using System;
using System.Threading;
using Cysharp.Threading.Tasks;
using RebootKit.Engine.Foundation;
namespace RebootKit.Engine.Services.GameMode {
public interface IGameMode : IDisposable {
UniTask OnInit(CancellationToken cancellationToken);
void OnStart();
void OnStop();
void OnTick();
}
public abstract class GameModeAsset : FactoryAsset<IGameMode> {
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 562c4ff92afe4949b468003a0e997522
timeCreated: 1743456239