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.

174 lines
4.5 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. public class GameManager : MonoBehaviour
  9. {
  10. #region Inspector Field
  11. [Header("Settings")]
  12. [SerializeField]
  13. private float AnimationTime;
  14. [SerializeField]
  15. private GameModeReference CurrentGameMode;
  16. [Header("References")]
  17. [SerializeField]
  18. [Tooltip("Prefab of character for players to play")]
  19. private Character characterPrefab;
  20. [SerializeField]
  21. private ServerObject server;
  22. [SerializeField]
  23. private ClientList ClientList;
  24. #endregion Inspector Field
  25. #region Private Variables
  26. private Dictionary<int, PlayerData> playerData;
  27. private GameMode gamemode;
  28. #endregion Private Variables
  29. #region Read Only
  30. private IEnumerable<PlayerData> playerArray { get { return playerData.Values; } }
  31. #endregion Read Only
  32. public void Awake()
  33. {
  34. gamemode = CurrentGameMode.Value;
  35. RegisterHandlers();
  36. SpawnCharacters();
  37. gamemode.GameStart(playerArray.ToArray());
  38. ClientList.ForEach(p => p.ChangeScene("ClientScene"));
  39. gamemode.GameStart(playerArray.ToArray());
  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. }
  94. }
  95. gamemode.RoundEnd(playerArray.ToArray());
  96. foreach (PlayerData player in playerArray)
  97. player.blockReader.Reset();
  98. ClientList.ForEach(p => p.SendInventory());
  99. ClientList.ForEach(p => p.ChangeScene("ClientScene"));
  100. Debug.Log("Finished Moving");
  101. StartRound();
  102. }
  103. private void SpawnCharacters()
  104. {
  105. playerData = new Dictionary<int, PlayerData>();
  106. Block[] SpawnBlocks = FindObjectsOfType<Block>().Where(p => p.isSpawnable).ToArray();
  107. int spawnIndex = 0;
  108. foreach (ClientData client in ClientList)
  109. {
  110. Character newChar = Instantiate(characterPrefab);
  111. Block startingBlock = SpawnBlocks[(spawnIndex++ % ClientList.ConnectedClients.Count)];
  112. newChar.Initialise(startingBlock, client.Inventory, client.characterAnimal);
  113. playerData.Add(client.ID, new PlayerData(newChar,client));
  114. }
  115. }
  116. private void RegisterHandlers()
  117. {
  118. server.server.RegisterHandler(LogicProtocols.SendLogicList, RecieveLogicList);
  119. }
  120. }
  121. public class PlayerData
  122. {
  123. public Character character;
  124. public BlockReader blockReader;
  125. public ClientData client;
  126. public bool recievedList;
  127. public bool waiting;
  128. public PlayerData(Character character, ClientData client)
  129. {
  130. this.character = character;
  131. this.client = client;
  132. blockReader = new BlockReader();
  133. }
  134. }