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