first
This commit is contained in:
42
Runtime/Engine/Code/Services/Game/GameService.cs
Normal file
42
Runtime/Engine/Code/Services/Game/GameService.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
using System.Threading;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using RebootKit.Engine.Foundation;
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user