|
- using System;
- using System.Collections.Generic;
- using System.Threading.Tasks;
- using Svelto.Tasks;
- using TechbloxModdingAPI.Client.Game;
- using TechbloxModdingAPI.Common.Engines;
- using TechbloxModdingAPI.Utility;
- using UnityEngine;
-
- namespace TechbloxModdingAPI.Client.App;
-
- /// <summary>
- /// Contains information about the game client's current state.
- /// </summary>
- public static class GameClient
- {
- private static readonly ClientEngine _engine = new();
-
- public static GameState CurrentState
- {
- get => _currentState;
- internal set
- {
- _currentState = value;
- var old = _currentState;
- _stateChanged.Invoke(null, new GameStateChangedArgs { OldState = old, NewState = value });
- }
- }
-
- private static GameState _currentState;
-
- public static bool IsBuildMode =>
- CurrentState is GameState.InMachineEditor or GameState.InWorldEditor;
-
- public static bool IsSimulationMode =>
- CurrentState is GameState.InTestMode or GameState.InWorldTestMode or GameState.InOnlineMatch;
-
- /// <summary>
- /// An event that fires whenever the game's state changes
- /// </summary>
- public static event EventHandler<GameStateChangedArgs> StateChanged
- {
- add => _stateChanged += value;
- remove => _stateChanged -= value;
- }
-
- private static WrappedHandler<GameStateChangedArgs> _stateChanged;
-
- /// <summary>
- /// Techblox build version string.
- /// Usually this is in the form YYYY.mm.DD.HH.MM.SS
- /// </summary>
- /// <value>The version.</value>
- public static string Version => Application.version;
-
- /// <summary>
- /// Unity version string.
- /// </summary>
- /// <value>The unity version.</value>
- public static string UnityVersion => Application.unityVersion;
-
- /// <summary>
- /// Environments (maps) currently visible in the menu.
- /// These take a second to completely populate after the EnterMenu event fires.
- /// </summary>
- /// <value>Available environments.</value>
- public static ClientEnvironment[] Environments { get; }
-
- public static ClientMachine[] Machines { get; }
-
- public static void EnterBuildMode(ClientEnvironment environment, ClientMachine machine)
- {
- if (CurrentState == GameState.InMenu)
- throw new InvalidOperationException($"Can only enter test mode from build mode! Current mode: {CurrentState}");
- var env = new ClientEnvironment("GAMEID_Road_Track"); // TODO: The options are hardcoded
- _engine.EnterBuildMode(env, machine);
- }
-
- public static IEnumerator<TaskContract> EnterTestMode()
- {
- if (!IsBuildMode)
- throw new InvalidOperationException($"Can only enter test mode from build mode! Current mode: {CurrentState}");
- // TODO
- //return Task.CompletedTask;
- yield break;
- }
-
- public static IEnumerator<TaskContract> ExitSimulationMode()
- { // TODO: Separate these based on the current game state?
- if (!IsSimulationMode)
- throw new InvalidOperationException($"Can only exit test mode when in it! Current mode: {CurrentState}");
- // TODO
- //return Task.CompletedTask;
- yield break;
- }
-
- public static IEnumerator<TaskContract> ExitBuildMode()
- {
- if (!IsBuildMode)
- throw new InvalidOperationException($"Can only exit test mode when in it! Current mode: {CurrentState}");
- // TODO
- //return Task.CompletedTask;
- yield break;
- }
-
- public static void Init()
- {
- EngineManager.AddEngine(_engine, ApiEngineType.Menu);
- }
-
- public struct GameStateChangedArgs
- {
- public GameState OldState { get; set; }
- public GameState NewState { get; set; }
- }
- }
|