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.

175 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. player.blockReader.Reset();
  99. ClientList.ForEach(p => p.SendInventory());
  100. ClientList.ForEach(p => p.ChangeScene("ClientScene"));
  101. Debug.Log("Finished Moving");
  102. StartRound();
  103. }
  104. private void SpawnCharacters()
  105. {
  106. playerData = new Dictionary<int, PlayerData>();
  107. Block[] SpawnBlocks = FindObjectsOfType<Block>().Where(p => p.isSpawnable).ToArray();
  108. int spawnIndex = 0;
  109. foreach (ClientData client in ClientList)
  110. {
  111. Character newChar = Instantiate(characterPrefab);
  112. Block startingBlock = SpawnBlocks[(spawnIndex++ % ClientList.ConnectedClients.Count)];
  113. newChar.Initialise(startingBlock, client.Inventory, client.characterAnimal);
  114. newChar.transform.forward = startingBlock.SpawnDirection.ToVector();
  115. playerData.Add(client.ID, new PlayerData(newChar,client));
  116. }
  117. }
  118. private void RegisterHandlers()
  119. {
  120. server.server.RegisterHandler(LogicProtocols.SendLogicList, RecieveLogicList);
  121. }
  122. }
  123. public class PlayerData
  124. {
  125. public Character character;
  126. public BlockReader blockReader;
  127. public ClientData client;
  128. public bool recievedList;
  129. public bool waiting;
  130. public PlayerData(Character character, ClientData client)
  131. {
  132. this.character = character;
  133. this.client = client;
  134. blockReader = new BlockReader();
  135. }
  136. }