Files
RebootKit/Runtime/Engine/Code/Services/Game/GameService.cs
2025-03-15 12:33:50 +01:00

43 lines
1.3 KiB
C#

using System.Threading;
using Cysharp.Threading.Tasks;
using RebootKit.Engine.Foundation;
using RebootKit.Engine.Services.Console;
namespace RebootKit.Engine.Services.Game {
public class GameService : IService {
private static readonly Logger Logger = new(nameof(GameService));
private GameModeAsset _gameModeAsset;
private GameMode _gameMode;
private bool _running;
public async UniTask OnInit(CancellationToken cancellationToken) {
await UniTask.Yield(cancellationToken);
}
public void Dispose() {
_running = false;
_gameMode.Dispose();
}
public async UniTask Start(GameModeAsset asset, CancellationToken cancellationToken) {
_gameMode = asset.Create();
await _gameMode.Start(cancellationToken);
Run(cancellationToken).Forget();
}
private async UniTask Run(CancellationToken cancellationToken) {
if (_gameMode == null) {
Logger.Error("Trying to run game without game mode");
return;
}
_running = true;
while (_running) {
await UniTask.Yield(PlayerLoopTiming.Update, cancellationToken);
_gameMode.Tick();
}
}
}
}