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.

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