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.

214 lines
6.3 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 one round has finished
  64. /// </summary>
  65. /// <param name="allPlayers">all players in game</param>
  66. protected virtual void OnRoundEnd(PlayerData[] allPlayers) { }
  67. /// <summary>
  68. /// Called once at end of game
  69. /// </summary>
  70. /// <param name="allPlayers">All players in game</param>
  71. protected virtual void OnGameOver(PlayerData[] allPlayers) { }
  72. /// <summary>
  73. /// Called to check if victory condition has been met
  74. /// </summary>
  75. /// <param name="allPlayers">All players in game</param>
  76. /// <returns>returns true if victory condition has been met</returns>
  77. public abstract bool isGameOver(PlayerData[] allPlayers);
  78. public float GetRoundTime()
  79. {
  80. float retVal = gameTimes[0];
  81. if (gameTimes.Count > 1)
  82. gameTimes.RemoveAt(0);
  83. return retVal;
  84. }
  85. #region Public functions
  86. public void PreGameStart()
  87. {
  88. OnPreGameStart();
  89. }
  90. public void GameStart(PlayerData[] allPlayers)
  91. {
  92. gameTimes = new List<float>(RoundTimes);
  93. if (!String.IsNullOrEmpty(GameModeScene))
  94. UnityEngine.SceneManagement.SceneManager.LoadScene(GameModeScene, UnityEngine.SceneManagement.LoadSceneMode.Additive);
  95. for (int i = 0; i < allPlayers.Length; i++)
  96. {
  97. allPlayers[i].client.Lives = 3;
  98. }
  99. OnGameStart(allPlayers);
  100. GameStartEvent?.Invoke();
  101. }
  102. public void InputStart(PlayerData[] AllPlayers)
  103. {
  104. OnInputStart(AllPlayers);
  105. InputStartEvent?.Invoke();
  106. }
  107. public void RecievedPlayerInput(PlayerData player)
  108. {
  109. OnRecievedPlayerInput(player);
  110. }
  111. public void InputEnd(PlayerData[] AllPlayers)
  112. {
  113. }
  114. public void RoundStart(PlayerData[] allPlayers)
  115. {
  116. OnRoundStart(allPlayers);
  117. RoundStartEvent?.Invoke();
  118. }
  119. public void PlayerMoved(PlayerData player)
  120. {
  121. OnPlayerMoved(player.character, player.client, player.character.CurrentBlock);
  122. }
  123. public void PlayerKilled(PlayerData player)
  124. {
  125. OnPlayerKilled(player.character, player.client);
  126. }
  127. public void AllPlayersMoved(PlayerData[] allPlayers)
  128. {
  129. OnAllPlayersMoved(allPlayers);
  130. AllPlayersMovedEvent?.Invoke();
  131. }
  132. public void RoundEnd(PlayerData[] allPlayers)
  133. {
  134. OnRoundEnd(allPlayers);
  135. RoundEndEvent?.Invoke();
  136. }
  137. public void GameEnd(PlayerData[] allPlayers)
  138. {
  139. OnGameOver(allPlayers);
  140. OnGameOverEvent?.Invoke();
  141. }
  142. }
  143. #endregion Public Functions
  144. /* --- Calls we need ---
  145. *
  146. * PreGameSetup(); - //Called once before the game actually starts
  147. * GameStart(PlayerData[] AllPlayers); //Called once after players have Spawned
  148. *
  149. * OnInputStart(PlayerData[] AllPlayers) //Called when players enter input mode
  150. * OnRecievedPlayerInput(PlayerData Player) //Called when a specific player has sent their input
  151. * OnInputEnd(PlayerData[] AllPlayers) //Called when player input mode has finished
  152. *
  153. * OnRoundStart(PlayerData[] AllPlayers) //Just before Players start moving
  154. *
  155. * ------- Start Round Loop -------
  156. *
  157. * OnPlayerMoved(PlayerData player) //Called after each player moves
  158. * IsGameOver(PlayerData[] AllPlayers) //Called after each player moves
  159. *
  160. * AllPlayersMoved(PlayerData[] AllPlayers) //Called after all players have moved
  161. * EnvironmentTurn() //Also called after all players have moved but should be kept specialised to environment stuff
  162. * IsGameOver(PlayerData[] AllPlayers) //We need to check if the environment terminated the game
  163. *
  164. * --------- End Game Loop ---------
  165. *
  166. * OnRoundEnd(PlayerData[] AllPlayers) //Called after movements are made
  167. * IsGameOver(PlayerData[] AllPlayers) //This needs to be checked again
  168. *
  169. * GameEnd(PlayerData[] AllPlayers)
  170. *
  171. *
  172. *
  173. *
  174. *
  175. */