using System.Threading; using Cysharp.Threading.Tasks; using RebootKit.Engine.Foundation; using UnityEngine.Assertions; namespace RebootKit.Engine.Services.Game { public class GameService : IService { private static readonly Logger Logger = new(nameof(GameService)); [Inject] private DIContext _diContext; private GameModeAsset _gameModeAsset; private GameMode _gameMode; private bool _running; public void Dispose() { _running = false; _gameMode.Dispose(); } public async UniTask Start(GameModeAsset asset, CancellationToken cancellationToken) { Assert.IsNotNull(asset); _gameMode = asset.Create(_diContext); 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(); } } } }