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.

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