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.

224 lines
6.7 KiB

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using Networking.Server;
  6. public abstract class GameMode : ScriptableObject
  7. {
  8. [SerializeField]
  9. [Tooltip("Scene to load which contains Gamemode UI")]
  10. private string GameModeScene;
  11. [SerializeField]
  12. [Tooltip("Round Times are acessed from the top down")]
  13. private List<float> RoundTimes;
  14. public Action GameStartEvent;
  15. public Action InputStartEvent;
  16. public Action RoundStartEvent;
  17. public Action AllPlayersMovedEvent;
  18. public Action RoundEndEvent;
  19. public Action OnGameOverEvent;
  20. private List<float> gameTimes;
  21. /// <summary>
  22. /// Called once before any players have spawned
  23. /// </summary>
  24. protected virtual void OnPreGameStart() { }
  25. /// <summary>
  26. /// Called once after players have spawned
  27. /// </summary>
  28. /// <param name="allPlayers">All players in game</param>
  29. protected virtual void OnGameStart(PlayerData[] allPlayers) { }
  30. /// <summary>
  31. /// Called every time players are moved to the input scene
  32. /// </summary>
  33. /// <param name="allPlayers">All players in game</param>
  34. protected virtual void OnInputStart(PlayerData[] allPlayers) { }
  35. /// <summary>
  36. /// Called for each player when we recieve their input
  37. /// </summary>
  38. /// <param name="player">Player who's input we recieved</param>
  39. protected virtual void OnRecievedPlayerInput(PlayerData player) { }
  40. /// <summary>
  41. /// Called after we have recieved all player input
  42. /// </summary>
  43. /// <param name="allPlayers">All players in game</param>
  44. protected virtual void OnInputEnd(PlayerData[] allPlayers) { }
  45. /// <summary>
  46. /// Called at the beginning of a new Round
  47. /// </summary>
  48. protected virtual void OnRoundStart(PlayerData[] allPlayers) { }
  49. /// <summary>
  50. /// Called for each player after they finished one move. Even if they didn't move!
  51. /// </summary>
  52. /// <param name="character">Character which moved</param>
  53. /// <param name="client">Client which controls character</param>
  54. /// <param name="currentBlock">Block which the character finished on</param>
  55. protected virtual void OnPlayerMoved(Character character, ClientData client, Block currentBlock) { }
  56. protected virtual void OnPlayerKilled(Character character, ClientData client) { }
  57. /// <summary>
  58. /// Called once after every player has finished one move
  59. /// </summary>
  60. /// <param name="allCharacters">List of all player Info</param>
  61. protected virtual void OnAllPlayersMoved(PlayerData[] allPlayers) { }
  62. /// <summary>
  63. /// Called after all environmentblocks have taken their turn
  64. /// </summary>
  65. /// <param name="allPlayers">All players in scene</param>
  66. protected virtual void OnEnvironmentTurn(PlayerData[] allPlayers) { }
  67. /// <summary>
  68. /// Called after one round has finished
  69. /// </summary>
  70. /// <param name="allPlayers">all players in game</param>
  71. protected virtual void OnRoundEnd(PlayerData[] allPlayers) { }
  72. /// <summary>
  73. /// Called once at end of game
  74. /// </summary>
  75. /// <param name="allPlayers">All players in game</param>
  76. protected virtual void OnGameOver(PlayerData[] allPlayers) { }
  77. /// <summary>
  78. /// Called to check if victory condition has been met
  79. /// </summary>
  80. /// <param name="allPlayers">All players in game</param>
  81. /// <returns>returns true if victory condition has been met</returns>
  82. public abstract bool isGameOver(PlayerData[] allPlayers);
  83. public float GetRoundTime()
  84. {
  85. float retVal = gameTimes[0];
  86. if (gameTimes.Count > 1)
  87. gameTimes.RemoveAt(0);
  88. return retVal;
  89. }
  90. #region Public functions
  91. public void PreGameStart()
  92. {
  93. OnPreGameStart();
  94. }
  95. public void GameStart(PlayerData[] allPlayers)
  96. {
  97. gameTimes = new List<float>(RoundTimes);
  98. if (!String.IsNullOrEmpty(GameModeScene))
  99. UnityEngine.SceneManagement.SceneManager.LoadScene(GameModeScene, UnityEngine.SceneManagement.LoadSceneMode.Additive);
  100. for (int i = 0; i < allPlayers.Length; i++)
  101. {
  102. allPlayers[i].client.Lives = 3;
  103. }
  104. OnGameStart(allPlayers);
  105. GameStartEvent?.Invoke();
  106. }
  107. public void InputStart(PlayerData[] AllPlayers)
  108. {
  109. OnInputStart(AllPlayers);
  110. InputStartEvent?.Invoke();
  111. }
  112. public void RecievedPlayerInput(PlayerData player)
  113. {
  114. OnRecievedPlayerInput(player);
  115. }
  116. public void InputEnd(PlayerData[] AllPlayers)
  117. {
  118. }
  119. public void RoundStart(PlayerData[] allPlayers)
  120. {
  121. OnRoundStart(allPlayers);
  122. RoundStartEvent?.Invoke();
  123. }
  124. public void PlayerMoved(PlayerData player)
  125. {
  126. OnPlayerMoved(player.character, player.client, player.character.CurrentBlock);
  127. }
  128. public void PlayerKilled(PlayerData player)
  129. {
  130. OnPlayerKilled(player.character, player.client);
  131. }
  132. public void AllPlayersMoved(PlayerData[] allPlayers)
  133. {
  134. OnAllPlayersMoved(allPlayers);
  135. AllPlayersMovedEvent?.Invoke();
  136. }
  137. public void EnvironmentTurn(PlayerData[] allPlayers)
  138. {
  139. OnEnvironmentTurn(allPlayers);
  140. }
  141. public void RoundEnd(PlayerData[] allPlayers)
  142. {
  143. OnRoundEnd(allPlayers);
  144. RoundEndEvent?.Invoke();
  145. }
  146. public void GameEnd(PlayerData[] allPlayers)
  147. {
  148. OnGameOver(allPlayers);
  149. OnGameOverEvent?.Invoke();
  150. }
  151. }
  152. #endregion Public Functions
  153. /* --- Calls we need ---
  154. *
  155. * PreGameSetup(); - //Called once before the game actually starts
  156. * GameStart(PlayerData[] AllPlayers); //Called once after players have Spawned
  157. *
  158. * OnInputStart(PlayerData[] AllPlayers) //Called when players enter input mode
  159. * OnRecievedPlayerInput(PlayerData Player) //Called when a specific player has sent their input
  160. * OnInputEnd(PlayerData[] AllPlayers) //Called when player input mode has finished
  161. *
  162. * OnRoundStart(PlayerData[] AllPlayers) //Just before Players start moving
  163. *
  164. * ------- Start Round Loop -------
  165. *
  166. * OnPlayerMoved(PlayerData player) //Called after each player moves
  167. * IsGameOver(PlayerData[] AllPlayers) //Called after each player moves
  168. *
  169. * AllPlayersMoved(PlayerData[] AllPlayers) //Called after all players have moved
  170. * EnvironmentTurn() //Also called after all players have moved but should be kept specialised to environment stuff
  171. * IsGameOver(PlayerData[] AllPlayers) //We need to check if the environment terminated the game
  172. *
  173. * --------- End Game Loop ---------
  174. *
  175. * OnRoundEnd(PlayerData[] AllPlayers) //Called after movements are made
  176. * IsGameOver(PlayerData[] AllPlayers) //This needs to be checked again
  177. *
  178. * GameEnd(PlayerData[] AllPlayers)
  179. *
  180. *
  181. *
  182. *
  183. *
  184. */