A stable modding interface between Techblox and mods https://mod.exmods.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
3.3KB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using Svelto.Tasks;
  5. using TechbloxModdingAPI.Client.Game;
  6. using TechbloxModdingAPI.Common.Engines;
  7. using TechbloxModdingAPI.Utility;
  8. using UnityEngine;
  9. namespace TechbloxModdingAPI.Client.App;
  10. /// <summary>
  11. /// Contains information about the game client's current state.
  12. /// </summary>
  13. public static class GameClient
  14. {
  15. private static readonly ClientEngine _engine = new();
  16. public static GameState CurrentState
  17. {
  18. get => _currentState;
  19. internal set
  20. {
  21. _currentState = value;
  22. var old = _currentState;
  23. _stateChanged.Invoke(null, new GameStateChangedArgs { OldState = old, NewState = value });
  24. }
  25. }
  26. private static GameState _currentState;
  27. public static bool IsBuildMode =>
  28. CurrentState is GameState.InMachineEditor or GameState.InWorldEditor;
  29. public static bool IsSimulationMode =>
  30. CurrentState is GameState.InTestMode or GameState.InWorldTestMode or GameState.InOnlineMatch;
  31. /// <summary>
  32. /// An event that fires whenever the game's state changes
  33. /// </summary>
  34. public static event EventHandler<GameStateChangedArgs> StateChanged
  35. {
  36. add => _stateChanged += value;
  37. remove => _stateChanged -= value;
  38. }
  39. private static WrappedHandler<GameStateChangedArgs> _stateChanged;
  40. /// <summary>
  41. /// Techblox build version string.
  42. /// Usually this is in the form YYYY.mm.DD.HH.MM.SS
  43. /// </summary>
  44. /// <value>The version.</value>
  45. public static string Version => Application.version;
  46. /// <summary>
  47. /// Unity version string.
  48. /// </summary>
  49. /// <value>The unity version.</value>
  50. public static string UnityVersion => Application.unityVersion;
  51. /// <summary>
  52. /// Environments (maps) currently visible in the menu.
  53. /// These take a second to completely populate after the EnterMenu event fires.
  54. /// </summary>
  55. /// <value>Available environments.</value>
  56. public static ClientEnvironment[] Environments { get; }
  57. public static ClientMachine[] Machines { get; }
  58. public static void EnterBuildMode(ClientEnvironment environment, ClientMachine machine)
  59. {
  60. if (CurrentState == GameState.InMenu)
  61. throw new InvalidOperationException($"Can only enter test mode from build mode! Current mode: {CurrentState}");
  62. var env = new ClientEnvironment("GAMEID_Road_Track"); // TODO: The options are hardcoded
  63. _engine.EnterBuildMode(env, machine);
  64. }
  65. public static IEnumerator<TaskContract> EnterTestMode()
  66. {
  67. if (!IsBuildMode)
  68. throw new InvalidOperationException($"Can only enter test mode from build mode! Current mode: {CurrentState}");
  69. // TODO
  70. //return Task.CompletedTask;
  71. yield break;
  72. }
  73. public static IEnumerator<TaskContract> ExitSimulationMode()
  74. { // TODO: Separate these based on the current game state?
  75. if (!IsSimulationMode)
  76. throw new InvalidOperationException($"Can only exit test mode when in it! Current mode: {CurrentState}");
  77. // TODO
  78. //return Task.CompletedTask;
  79. yield break;
  80. }
  81. public static IEnumerator<TaskContract> ExitBuildMode()
  82. {
  83. if (!IsBuildMode)
  84. throw new InvalidOperationException($"Can only exit test mode when in it! Current mode: {CurrentState}");
  85. // TODO
  86. //return Task.CompletedTask;
  87. yield break;
  88. }
  89. public static void Init()
  90. {
  91. EngineManager.AddEngine(_engine, ApiEngineType.Menu);
  92. }
  93. public struct GameStateChangedArgs
  94. {
  95. public GameState OldState { get; set; }
  96. public GameState NewState { get; set; }
  97. }
  98. }