refactor
This commit is contained in:
@@ -1,78 +0,0 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using RebootKit.Engine.Foundation;
|
||||
using UnityEngine;
|
||||
|
||||
namespace RebootKit.Engine.Services.Game {
|
||||
public abstract class GameModeAsset : FactoryAsset<GameMode> {
|
||||
[field: SerializeField]
|
||||
public GameMode.Config GameModeConfig { get; private set; }
|
||||
|
||||
public override GameMode Create(DIContext context) {
|
||||
GameMode gameMode = new(GameModeConfig);
|
||||
context.Inject(gameMode);
|
||||
ConfigureGameMode(gameMode);
|
||||
return gameMode;
|
||||
}
|
||||
|
||||
public abstract void ConfigureGameMode(GameMode gameMode);
|
||||
}
|
||||
|
||||
public class GameMode : IDisposable {
|
||||
[Serializable]
|
||||
public class Config {
|
||||
public ControllerAsset[] controllers;
|
||||
}
|
||||
|
||||
readonly Config m_Config;
|
||||
readonly ControllersManager<IController> m_Controllers;
|
||||
|
||||
readonly CancellationTokenSource m_DestroyCancellationTokenSource;
|
||||
|
||||
[Inject] DIContext m_DIContext;
|
||||
bool m_IsRunning;
|
||||
|
||||
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() {
|
||||
m_DestroyCancellationTokenSource.Cancel();
|
||||
m_Controllers.Dispose();
|
||||
}
|
||||
|
||||
public async Awaitable<bool> Start(CancellationToken cancellationToken) {
|
||||
m_Controllers.Add(m_Config.controllers, m_DIContext);
|
||||
await m_Controllers.Start(cancellationToken);
|
||||
|
||||
m_IsRunning = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Stop() {
|
||||
m_IsRunning = false;
|
||||
|
||||
m_Controllers.Stop();
|
||||
}
|
||||
|
||||
public void Tick() {
|
||||
m_Controllers.Tick();
|
||||
}
|
||||
|
||||
public void AddController(IController 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 73f15dc489209cc4a9ff6963e0fbd2c6
|
||||
@@ -1,47 +1,57 @@
|
||||
using System.Threading;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using R3;
|
||||
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;
|
||||
|
||||
CancellationTokenSource m_DestroyCancellationTokenSource = new();
|
||||
DisposableBag m_ActiveGameModeDisposableBag;
|
||||
IGameMode m_ActiveGameMode;
|
||||
|
||||
public void Dispose() {
|
||||
m_isRunning = false;
|
||||
m_GameMode.Dispose();
|
||||
m_DestroyCancellationTokenSource.Cancel();
|
||||
m_DestroyCancellationTokenSource.Dispose();
|
||||
m_ActiveGameModeDisposableBag.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");
|
||||
public void Start(GameModeAsset asset) {
|
||||
if (m_ActiveGameMode != null) {
|
||||
s_logger.Warning("Game is already running");
|
||||
return;
|
||||
}
|
||||
|
||||
m_isRunning = true;
|
||||
while (m_isRunning) {
|
||||
await UniTask.Yield(PlayerLoopTiming.Update, cancellationToken);
|
||||
m_GameMode.Tick();
|
||||
Stop();
|
||||
|
||||
if (cancellationToken.IsCancellationRequested) {
|
||||
return;
|
||||
}
|
||||
m_ActiveGameModeDisposableBag = new DisposableBag();
|
||||
m_ActiveGameMode = asset.Create(m_DIContext);
|
||||
m_ActiveGameModeDisposableBag.Add(m_ActiveGameMode);
|
||||
|
||||
InitializeGameModeAsync().Forget();
|
||||
}
|
||||
|
||||
async UniTask InitializeGameModeAsync() {
|
||||
await m_ActiveGameMode.OnInit(m_DestroyCancellationTokenSource.Token);
|
||||
|
||||
m_ActiveGameMode.OnStart();
|
||||
Observable.EveryUpdate().Subscribe(_ => { m_ActiveGameMode?.OnTick(); }).AddTo(ref m_ActiveGameModeDisposableBag);
|
||||
}
|
||||
|
||||
public void Stop() {
|
||||
if (m_ActiveGameMode == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_ActiveGameMode.OnStop();
|
||||
m_ActiveGameMode = null;
|
||||
|
||||
m_ActiveGameModeDisposableBag.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
18
Runtime/Engine/Code/Services/Game/IGameMode.cs
Normal file
18
Runtime/Engine/Code/Services/Game/IGameMode.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using RebootKit.Engine.Foundation;
|
||||
|
||||
namespace RebootKit.Engine.Services.Game {
|
||||
public interface IGameMode : IDisposable {
|
||||
UniTask OnInit(CancellationToken cancellationToken);
|
||||
|
||||
void OnStart();
|
||||
void OnStop();
|
||||
|
||||
void OnTick();
|
||||
}
|
||||
|
||||
public abstract class GameModeAsset : FactoryAsset<IGameMode> {
|
||||
}
|
||||
}
|
||||
3
Runtime/Engine/Code/Services/Game/IGameMode.cs.meta
Normal file
3
Runtime/Engine/Code/Services/Game/IGameMode.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 562c4ff92afe4949b468003a0e997522
|
||||
timeCreated: 1743456239
|
||||
5
Runtime/Engine/Code/Services/Game/Player.cs
Normal file
5
Runtime/Engine/Code/Services/Game/Player.cs
Normal file
@@ -0,0 +1,5 @@
|
||||
namespace RebootKit.Engine.Services.Game {
|
||||
public class Player {
|
||||
|
||||
}
|
||||
}
|
||||
3
Runtime/Engine/Code/Services/Game/Player.cs.meta
Normal file
3
Runtime/Engine/Code/Services/Game/Player.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: be94d631484b4bd2a2c9825290b74b36
|
||||
timeCreated: 1744413325
|
||||
Reference in New Issue
Block a user