Files
jelito/Assets/jelycho/Code/Main/JelychoGame.cs

112 lines
4.0 KiB
C#
Executable File

using System;
using System.Linq;
using System.Threading;
using Cysharp.Threading.Tasks;
using R3;
using RebootKit.Engine.Main;
using RebootKit.Engine.Services.Console;
using RebootKit.Engine.Services.Crosshair;
using RebootKit.Engine.Services.GameMode;
using RebootKit.Engine.Services.Simulation;
namespace RebootReality.jelycho.Main {
public static class JelychoConsoleCommands {
[RCCMD("start", "Starts game mode with given name. Usage: start <game_mode_name> <world_name>")]
public static void StartGameMode(string[] args) {
if (args.Length != 3) {
RR.Console.WriteToOutput($"Usage: {args[0]} <game_mode_name> <world_name>");
return;
}
string worldName = args[2];
WorldConfigAsset worldConfig = RR.WorldConfigsAssets.FirstOrDefault(t => t.Config.name.Equals(worldName));
if (worldConfig is null) {
RR.Console.WriteToOutput($"World '{worldName}' not found");
return;
}
foreach (GameModeAsset gameModeAsset in RR.GameModesAssets) {
if (gameModeAsset.name == args[1]) {
RR.Console.WriteToOutput($"Starting game mode '{gameModeAsset.name}'");
RR.StartGameMode(gameModeAsset, worldConfig.Config);
return;
}
}
RR.Console.WriteToOutput($"Game mode '{args[1]}' not found");
}
[RCCMD("gamemodes", "Lists all game modes")]
public static void GameModes(string[] args) {
if (args.Length != 1) {
RR.Console.WriteToOutput($"Usage: {args[0]}");
return;
}
RR.Console.WriteToOutput("Game modes:");
foreach (GameModeAsset gameModeAsset in RR.GameModesAssets) {
RR.Console.WriteToOutput($" {gameModeAsset.name}");
}
}
[RCCMD("worlds", "Lists all worlds")]
public static void Worlds(string[] args) {
if (args.Length != 1) {
RR.Console.WriteToOutput($"Usage: {args[0]}");
return;
}
RR.Console.WriteToOutput("Worlds:");
foreach (WorldConfigAsset worldConfigAsset in RR.WorldConfigsAssets) {
RR.Console.WriteToOutput($" {worldConfigAsset.Config.name}");
}
}
}
public class JelychoGame : IGame {
[Serializable]
public class Config {
public string mainMenuWorld = "main_menu";
public string mainGameplayWorld = "dev";
public string standardGameMode = "gm_standard";
}
readonly Config m_Config;
DisposableBag m_DisposableBag;
public JelychoGame(Config config) {
m_Config = config;
m_DisposableBag = new DisposableBag();
}
public async UniTask InitAsync(CancellationToken cancellationToken) {
RR.CreateService<CrosshairService>();
await UniTask.Yield();
}
public void Run() {
RR.GameModes.ActiveGameMode.Subscribe(gameMode => {
if (gameMode == null) {
RR.Console.WriteToOutput("Game mode stopped, loading main menu world");
WorldConfigAsset mainMenuWorldConfigAsset = RR.GetWorldConfigAsset(m_Config.mainMenuWorld);
RR.World.LoadAsync(mainMenuWorldConfigAsset.Config, CancellationToken.None).Forget();
}
})
.AddTo(ref m_DisposableBag);
}
public void Dispose() {
m_DisposableBag.Dispose();
}
public void NewGame() {
GameModeAsset gameModeAsset = RR.GetGameMode(m_Config.standardGameMode);
WorldConfigAsset worldConfigAsset = RR.GetWorldConfigAsset(m_Config.mainGameplayWorld);
RR.StartGameMode(gameModeAsset, worldConfigAsset.Config);
}
}
}