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.

186 lines
5.1 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 void Awake()
  35. {
  36. gamemode = CurrentGameMode.Value;
  37. RegisterHandlers();
  38. SpawnCharacters();
  39. ClientList.ForEach(p => p.ChangeScene("ClientScene"));
  40. }
  41. private void Start()
  42. {
  43. StartCoroutine(displayforSeconds(levelInfo, 5.0f));
  44. gamemode.GameStart(playerArray.ToArray());
  45. StartRound();
  46. }
  47. private void Update()
  48. {
  49. server.ServerUpdate();
  50. }
  51. IEnumerator displayforSeconds(GameObject display, float time)
  52. {
  53. display.SetActive (true);
  54. yield return new WaitForSeconds(time);
  55. display.SetActive (false);
  56. }
  57. private void RecieveLogicList(NetworkMessage msg)
  58. {
  59. LogicProtocols.LogicMsg logicMsg;
  60. if (!msg.TryRead(out logicMsg))
  61. return;
  62. Debug.Log("Recieved function from " + msg.conn.connectionId);
  63. playerData[msg.conn.connectionId].blockReader.LogicChain = new List<LogicBlock>(logicMsg.elements);
  64. playerData[msg.conn.connectionId].recievedList = true;
  65. if (playerData.All(p => p.Value.recievedList))
  66. DoRoundRoutine();
  67. }
  68. private void DoRoundRoutine()
  69. {
  70. Debug.Log("Starting Round");
  71. StartCoroutine(RoundRoutine());
  72. }
  73. private void StartRound()
  74. {
  75. gamemode.RoundStart(playerArray.ToArray());
  76. }
  77. private IEnumerator RoundRoutine()
  78. {
  79. playerArray.ForEach(p => p.recievedList = false);
  80. while (playerArray.Any(p => !p.blockReader.Finished))
  81. {
  82. foreach (PlayerData player in playerArray)
  83. {
  84. if (!player.waiting)
  85. {
  86. if (player.blockReader.CurrentBlock != null && !player.blockReader.CurrentBlock.hasBeenRemoved)
  87. {
  88. Debug.Log("used Block: " + player.blockReader.CurrentBlock.name);
  89. player.client.Inventory.Remove(player.blockReader.CurrentBlock);
  90. player.blockReader.CurrentBlock.hasBeenRemoved = true;
  91. }
  92. player.waiting = player.blockReader.Read(player.character, AnimationTime);
  93. }
  94. }
  95. yield return new WaitForSeconds(AnimationTime);
  96. gamemode.FinishedMove(playerArray.ToArray());
  97. if (playerArray.All(p => p.waiting))
  98. {
  99. playerArray.ForEach(p => p.waiting = false);
  100. Debug.Log("Finished one move");
  101. }
  102. }
  103. if (gamemode.isGameOver(playerArray.ToArray()))
  104. {
  105. Debug.Log("Game Over");
  106. SceneManager.LoadScene("ScoreBoards");
  107. }
  108. gamemode.RoundEnd(playerArray.ToArray());
  109. foreach (PlayerData player in playerArray)
  110. {
  111. player.blockReader.Reset();
  112. player.waiting = false;
  113. player.client.SendInventory();
  114. player.client.ChangeScene("ClientScene");
  115. }
  116. Debug.Log("Finished Moving");
  117. StartRound();
  118. }
  119. private void SpawnCharacters()
  120. {
  121. playerData = new Dictionary<int, PlayerData>();
  122. Block[] SpawnBlocks = FindObjectsOfType<Block>().Where(p => p.isSpawnable).ToArray();
  123. int spawnIndex = 0;
  124. foreach (ClientData client in ClientList)
  125. {
  126. Character newChar = Instantiate(characterPrefab);
  127. Block startingBlock = SpawnBlocks[(spawnIndex++ % ClientList.ConnectedClients.Count)];
  128. newChar.Initialise(startingBlock, client.Inventory, client.characterAnimal);
  129. newChar.transform.forward = startingBlock.SpawnDirection.ToVector();
  130. playerData.Add(client.ID, new PlayerData(newChar,client));
  131. }
  132. }
  133. private void RegisterHandlers()
  134. {
  135. server.server.RegisterHandler(LogicProtocols.SendLogicList, RecieveLogicList);
  136. }
  137. }
  138. public class PlayerData
  139. {
  140. public Character character;
  141. public BlockReader blockReader;
  142. public ClientData client;
  143. public bool recievedList;
  144. public bool waiting;
  145. public PlayerData(Character character, ClientData client)
  146. {
  147. this.character = character;
  148. this.client = client;
  149. blockReader = new BlockReader();
  150. }
  151. }