broken
This commit is contained in:
@@ -4,14 +4,9 @@ using RebootKit.Engine.Foundation;
|
||||
using UnityEngine;
|
||||
|
||||
namespace RebootKit.Engine.Services.Game {
|
||||
[Serializable]
|
||||
public class GameModeConfig {
|
||||
public ControllerAsset[] Controllers;
|
||||
}
|
||||
|
||||
public abstract class GameModeAsset : FactoryAsset<GameMode> {
|
||||
[field: SerializeField]
|
||||
public GameModeConfig GameModeConfig { get; private set; }
|
||||
public GameMode.Config GameModeConfig { get; private set; }
|
||||
|
||||
public override GameMode Create(DIContext context) {
|
||||
GameMode gameMode = new(GameModeConfig);
|
||||
@@ -22,51 +17,62 @@ namespace RebootKit.Engine.Services.Game {
|
||||
|
||||
public abstract void ConfigureGameMode(GameMode gameMode);
|
||||
}
|
||||
|
||||
|
||||
public class GameMode : IDisposable {
|
||||
private readonly GameModeConfig _config;
|
||||
private readonly ControllersManager _controllersManager;
|
||||
[Serializable]
|
||||
public class Config {
|
||||
public ControllerAsset[] controllers;
|
||||
}
|
||||
|
||||
[Inject]
|
||||
private DIContext _diContext;
|
||||
readonly Config m_Config;
|
||||
readonly ControllersManager<IController> m_Controllers;
|
||||
|
||||
private CancellationTokenSource _destroyCancellationTokenSource;
|
||||
|
||||
private bool _isRunning;
|
||||
readonly CancellationTokenSource m_DestroyCancellationTokenSource;
|
||||
|
||||
public GameMode(GameModeConfig config) {
|
||||
_config = config;
|
||||
_isRunning = false;
|
||||
[Inject] DIContext m_DIContext;
|
||||
bool m_IsRunning;
|
||||
|
||||
_destroyCancellationTokenSource = new CancellationTokenSource();
|
||||
_controllersManager = new ControllersManager(_destroyCancellationTokenSource.Token);
|
||||
public GameMode(Config config) {
|
||||
m_Config = config;
|
||||
m_IsRunning = false;
|
||||
|
||||
m_DestroyCancellationTokenSource = new CancellationTokenSource();
|
||||
m_Controllers = new ControllersManager<IController>(m_DestroyCancellationTokenSource.Token);
|
||||
}
|
||||
|
||||
public void Dispose() {
|
||||
_destroyCancellationTokenSource.Cancel();
|
||||
_controllersManager.Dispose();
|
||||
m_DestroyCancellationTokenSource.Cancel();
|
||||
m_Controllers.Dispose();
|
||||
}
|
||||
|
||||
public async Awaitable<bool> Start(CancellationToken cancellationToken) {
|
||||
_controllersManager.Add(_config.Controllers, _diContext);
|
||||
await _controllersManager.Start(cancellationToken);
|
||||
m_Controllers.Add(m_Config.controllers, m_DIContext);
|
||||
await m_Controllers.Start(cancellationToken);
|
||||
|
||||
_isRunning = true;
|
||||
m_IsRunning = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Stop() {
|
||||
_isRunning = false;
|
||||
m_IsRunning = false;
|
||||
|
||||
_controllersManager.Stop();
|
||||
m_Controllers.Stop();
|
||||
}
|
||||
|
||||
public void Tick() {
|
||||
_controllersManager.Tick();
|
||||
m_Controllers.Tick();
|
||||
}
|
||||
|
||||
public void AddController(IController controller) {
|
||||
_controllersManager.Add(controller);
|
||||
m_Controllers.Add(controller);
|
||||
}
|
||||
|
||||
public T FindController<T>() where T : class, IController {
|
||||
if (m_Controllers.TryFind<T>(out T controller)) {
|
||||
return controller;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,39 +5,42 @@ using UnityEngine.Assertions;
|
||||
|
||||
namespace RebootKit.Engine.Services.Game {
|
||||
public class GameService : IService {
|
||||
private static readonly Logger Logger = new(nameof(GameService));
|
||||
static readonly Logger s_logger = new(nameof(GameService));
|
||||
|
||||
[Inject] DIContext m_DIContext;
|
||||
GameMode m_GameMode;
|
||||
|
||||
GameModeAsset m_GameModeAsset;
|
||||
bool m_isRunning;
|
||||
|
||||
[Inject]
|
||||
private DIContext _diContext;
|
||||
|
||||
private GameModeAsset _gameModeAsset;
|
||||
private GameMode _gameMode;
|
||||
private bool _running;
|
||||
|
||||
public void Dispose() {
|
||||
_running = false;
|
||||
_gameMode.Dispose();
|
||||
m_isRunning = false;
|
||||
m_GameMode.Dispose();
|
||||
}
|
||||
|
||||
public async UniTask Start(GameModeAsset asset, CancellationToken cancellationToken) {
|
||||
Assert.IsNotNull(asset);
|
||||
|
||||
_gameMode = asset.Create(_diContext);
|
||||
await _gameMode.Start(cancellationToken);
|
||||
|
||||
m_GameMode = asset.Create(m_DIContext);
|
||||
await m_GameMode.Start(cancellationToken);
|
||||
|
||||
Run(cancellationToken).Forget();
|
||||
}
|
||||
|
||||
private async UniTask Run(CancellationToken cancellationToken) {
|
||||
if (_gameMode == null) {
|
||||
Logger.Error("Trying to run game without game mode");
|
||||
async UniTask Run(CancellationToken cancellationToken) {
|
||||
if (m_GameMode == null) {
|
||||
s_logger.Error("Trying to run game without game mode");
|
||||
return;
|
||||
}
|
||||
|
||||
_running = true;
|
||||
while (_running) {
|
||||
|
||||
m_isRunning = true;
|
||||
while (m_isRunning) {
|
||||
await UniTask.Yield(PlayerLoopTiming.Update, cancellationToken);
|
||||
_gameMode.Tick();
|
||||
m_GameMode.Tick();
|
||||
|
||||
if (cancellationToken.IsCancellationRequested) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace RebootKit.Engine.Services.Game {
|
||||
[CreateAssetMenu(menuName = RConsts.ServiceAssetMenu + "Game")]
|
||||
[CreateAssetMenu(menuName = RConsts.k_ServiceAssetMenu + "Game")]
|
||||
public class GameServiceAsset : ServiceAsset<GameService> {
|
||||
public override GameService Create(DIContext context) {
|
||||
GameService service = context.Create<GameService>();
|
||||
|
||||
Reference in New Issue
Block a user