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.

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