first
This commit is contained in:
68
Runtime/Engine/Code/Services/Game/GameMode.cs
Normal file
68
Runtime/Engine/Code/Services/Game/GameMode.cs
Normal file
@@ -0,0 +1,68 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using RebootKit.Engine.Foundation;
|
||||
using UnityEngine;
|
||||
|
||||
namespace RebootKit.Engine.Services.Game {
|
||||
[Serializable]
|
||||
public class GameModeConfig {
|
||||
public ControllerAsset[] Controllers;
|
||||
}
|
||||
|
||||
public abstract class GameModeAsset : ScriptableObject {
|
||||
[field: SerializeField]
|
||||
public GameModeConfig GameModeConfig { get; private set; }
|
||||
|
||||
public GameMode Create() {
|
||||
GameMode gameMode = new GameMode(GameModeConfig);
|
||||
ConfigureGameMode(gameMode);
|
||||
return gameMode;
|
||||
}
|
||||
|
||||
public abstract void ConfigureGameMode(GameMode gameMode);
|
||||
}
|
||||
|
||||
public class GameMode : IDisposable {
|
||||
private readonly GameModeConfig _config;
|
||||
private readonly ControllersManager _controllersManager;
|
||||
|
||||
private CancellationTokenSource _destroyCancellationTokenSource;
|
||||
|
||||
private bool _isRunning;
|
||||
|
||||
public GameMode(GameModeConfig config) {
|
||||
_config = config;
|
||||
_isRunning = false;
|
||||
|
||||
_destroyCancellationTokenSource = new CancellationTokenSource();
|
||||
_controllersManager = new ControllersManager(_destroyCancellationTokenSource.Token);
|
||||
}
|
||||
|
||||
public void Dispose() {
|
||||
_destroyCancellationTokenSource.Cancel();
|
||||
_controllersManager.Dispose();
|
||||
}
|
||||
|
||||
public async Awaitable<bool> Start(CancellationToken cancellationToken) {
|
||||
_controllersManager.Add(_config.Controllers);
|
||||
await _controllersManager.Start(cancellationToken);
|
||||
|
||||
_isRunning = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Stop() {
|
||||
_isRunning = false;
|
||||
|
||||
_controllersManager.Stop();
|
||||
}
|
||||
|
||||
public void Tick() {
|
||||
_controllersManager.Tick();
|
||||
}
|
||||
|
||||
public void AddController(IController controller) {
|
||||
_controllersManager.Add(controller);
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Runtime/Engine/Code/Services/Game/GameMode.cs.meta
Normal file
2
Runtime/Engine/Code/Services/Game/GameMode.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 73f15dc489209cc4a9ff6963e0fbd2c6
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Runtime/Engine/Code/Services/Game/GameService.cs.meta
Normal file
2
Runtime/Engine/Code/Services/Game/GameService.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 82082a4d082be274bbec52347d25b836
|
||||
11
Runtime/Engine/Code/Services/Game/GameServiceAsset.cs
Normal file
11
Runtime/Engine/Code/Services/Game/GameServiceAsset.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using RebootKit.Engine.Foundation;
|
||||
using UnityEngine;
|
||||
|
||||
namespace RebootKit.Engine.Services.Game {
|
||||
[CreateAssetMenu(menuName = RConsts.ServiceAssetMenu + "Game")]
|
||||
public class GameServiceAsset : ServiceAsset<GameService> {
|
||||
public override GameService Create() {
|
||||
return new GameService();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 51f134b6c2abcd34fa8e376b3afc757d
|
||||
Reference in New Issue
Block a user