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.

231 lines
6.7 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. using UnityEngine.Networking;
  6. using Networking.Server;
  7. using Networking;
  8. using UnityEngine.SceneManagement;
  9. public class GameManager : MonoBehaviour
  10. {
  11. #region Inspector Field
  12. [Header("Settings")]
  13. [SerializeField]
  14. private float AnimationTime;
  15. [SerializeField]
  16. private GameModeReference CurrentGameMode;
  17. [Header("References")]
  18. [SerializeField]
  19. [Tooltip("Prefab of character for players to play")]
  20. private Character characterPrefab;
  21. [SerializeField]
  22. private ServerObject server;
  23. [SerializeField]
  24. private ClientList ClientList;
  25. #endregion Inspector Field
  26. #region Private Variables
  27. private Dictionary<int, PlayerData> playerData;
  28. private GameMode gamemode;
  29. #endregion Private Variables
  30. #region Read Only
  31. private IEnumerable<PlayerData> playerArray { get { return playerData.Values; } }
  32. #endregion Read Only
  33. public GameObject levelInfo;
  34. public blockSpawn bspawn;
  35. public void Awake()
  36. {
  37. gamemode = CurrentGameMode.Value;
  38. RegisterHandlers();
  39. SpawnCharacters();
  40. ClientList.ForEach(p => p.ChangeScene("ClientScene"));
  41. }
  42. private void Start()
  43. {
  44. StartCoroutine(displayforSeconds(levelInfo, 5.0f));
  45. gamemode.GameStart(playerArray.ToArray());
  46. StartRound();
  47. }
  48. private void Update()
  49. {
  50. server.ServerUpdate();
  51. }
  52. IEnumerator displayforSeconds(GameObject display, float time)
  53. {
  54. display.SetActive (true);
  55. yield return new WaitForSeconds(time);
  56. display.SetActive (false);
  57. }
  58. private void RecieveLogicList(NetworkMessage msg)
  59. {
  60. LogicProtocols.LogicMsg logicMsg;
  61. if (!msg.TryRead(out logicMsg))
  62. return;
  63. Debug.Log("Recieved function from " + msg.conn.connectionId);
  64. playerData[msg.conn.connectionId].blockReader.LogicChain = new List<LogicBlock>(logicMsg.elements);
  65. playerData[msg.conn.connectionId].recievedList = true;
  66. if (playerData.All(p => p.Value.recievedList))
  67. DoRoundRoutine();
  68. }
  69. private void DoRoundRoutine()
  70. {
  71. Debug.Log("Starting Round");
  72. StartCoroutine(RoundRoutine());
  73. }
  74. private void StartRound()
  75. {
  76. gamemode.RoundStart(playerArray.ToArray());
  77. LogicProtocols.FloatMsg RoundTime = new LogicProtocols.FloatMsg( gamemode.GetRoundTime());
  78. bspawn.getPlayerScores();
  79. bspawn.getPlayerLocations();
  80. playerArray.ForEach(p => p.client.conn.Send(LogicProtocols.SendRoundTime, RoundTime));
  81. }
  82. private IEnumerator RoundRoutine()
  83. {
  84. playerArray.ForEach(p => p.recievedList = false);
  85. //while (playerArray.Any(p => !p.blockReader.Finished))
  86. //{
  87. // foreach (PlayerData player in playerArray)
  88. // {
  89. // if (!player.waiting)
  90. // {
  91. // if (player.blockReader.CurrentBlock != null && !player.blockReader.CurrentBlock.hasBeenRemoved)
  92. // {
  93. // //Debug.Log("used Block: " + player.blockReader.CurrentBlock.name);
  94. // player.client.Inventory.Remove(player.blockReader.CurrentBlock);
  95. // player.blockReader.CurrentBlock.hasBeenRemoved = true;
  96. // }
  97. // player.waiting = player.blockReader.Read(player.character, AnimationTime);
  98. // }
  99. // }
  100. //
  101. // yield return new WaitForSeconds(AnimationTime);
  102. //
  103. //
  104. //
  105. // if (playerArray.All(p => p.waiting))
  106. // {
  107. // playerArray.ForEach(p => p.waiting = false);
  108. // Debug.Log("Finished one move");
  109. // }
  110. //}
  111. Debug.Log("Doing Round Routine");
  112. while (playerArray.Any(p => !p.blockReader.Finished))
  113. {
  114. Debug.Log("One Move");
  115. foreach (PlayerData player in playerArray)
  116. StartCoroutine(RunOnce(player));
  117. //wait until all players have finished
  118. yield return new WaitUntil(() => playerArray.All(p => p.waiting));
  119. gamemode.FinishedMove(playerArray.ToArray());
  120. }
  121. if (gamemode.isGameOver(playerArray.ToArray()))
  122. {
  123. Debug.Log("Game Over");
  124. SceneManager.LoadScene("ScoreBoards");
  125. }
  126. gamemode.RoundEnd(playerArray.ToArray());
  127. foreach (PlayerData player in playerArray)
  128. {
  129. player.blockReader.Reset();
  130. player.waiting = false;
  131. player.client.SendInventory();
  132. player.client.ChangeScene("ClientScene");
  133. }
  134. Debug.Log("Finished Moving");
  135. StartRound();
  136. }
  137. private void SpawnCharacters()
  138. {
  139. playerData = new Dictionary<int, PlayerData>();
  140. Block[] SpawnBlocks = FindObjectsOfType<Block>().Where(p => p.isSpawnable).ToArray();
  141. int spawnIndex = 0;
  142. foreach (ClientData client in ClientList)
  143. {
  144. Character newChar = Instantiate(characterPrefab);
  145. Block startingBlock = SpawnBlocks[(spawnIndex++ % ClientList.ConnectedClients.Count)];
  146. newChar.Initialise(startingBlock, client.Inventory, client.characterAnimal);
  147. newChar.transform.forward = startingBlock.SpawnDirection.ToVector();
  148. playerData.Add(client.ID, new PlayerData(newChar,client));
  149. newChar.ClientLink = client;
  150. client.playerCharacter = newChar;
  151. }
  152. }
  153. private void RegisterHandlers()
  154. {
  155. server.server.RegisterHandler(LogicProtocols.SendLogicList, RecieveLogicList);
  156. }
  157. private IEnumerator RunOnce(PlayerData data)
  158. {
  159. data.waiting = false;
  160. bool blockFinished = false;
  161. float waitTime;
  162. while (!blockFinished && !data.blockReader.Finished)
  163. {
  164. Debug.Log(data.client + "Moving once");
  165. if (data.blockReader.CurrentBlock != null && !data.blockReader.CurrentBlock.hasBeenRemoved)
  166. {
  167. data.client.Inventory.Remove(data.blockReader.CurrentBlock);
  168. data.blockReader.CurrentBlock.hasBeenRemoved = true;
  169. }
  170. blockFinished = data.blockReader.Read(data.character, AnimationTime,out waitTime);
  171. Debug.Log("Waiting: " + waitTime);
  172. yield return new WaitForSeconds(waitTime);
  173. }
  174. data.waiting = true;
  175. }
  176. }
  177. public class PlayerData
  178. {
  179. public Character character;
  180. public BlockReader blockReader;
  181. public ClientData client;
  182. public bool recievedList;
  183. public bool waiting;
  184. public PlayerData(Character character, ClientData client)
  185. {
  186. this.character = character;
  187. this.client = client;
  188. blockReader = new BlockReader();
  189. }
  190. }