47 lines
1.3 KiB
C#
47 lines
1.3 KiB
C#
using System.Threading;
|
|
using Cysharp.Threading.Tasks;
|
|
using RebootKit.Engine.Foundation;
|
|
using UnityEngine.Assertions;
|
|
|
|
namespace RebootKit.Engine.Services.Game {
|
|
public class GameService : IService {
|
|
static readonly Logger s_logger = new(nameof(GameService));
|
|
|
|
[Inject] DIContext m_DIContext;
|
|
GameMode m_GameMode;
|
|
|
|
GameModeAsset m_GameModeAsset;
|
|
bool m_isRunning;
|
|
|
|
public void Dispose() {
|
|
m_isRunning = false;
|
|
m_GameMode.Dispose();
|
|
}
|
|
|
|
public async UniTask Start(GameModeAsset asset, CancellationToken cancellationToken) {
|
|
Assert.IsNotNull(asset);
|
|
|
|
m_GameMode = asset.Create(m_DIContext);
|
|
await m_GameMode.Start(cancellationToken);
|
|
|
|
Run(cancellationToken).Forget();
|
|
}
|
|
|
|
async UniTask Run(CancellationToken cancellationToken) {
|
|
if (m_GameMode == null) {
|
|
s_logger.Error("Trying to run game without game mode");
|
|
return;
|
|
}
|
|
|
|
m_isRunning = true;
|
|
while (m_isRunning) {
|
|
await UniTask.Yield(PlayerLoopTiming.Update, cancellationToken);
|
|
m_GameMode.Tick();
|
|
|
|
if (cancellationToken.IsCancellationRequested) {
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |