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.

155 lines
3.8 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 GameMode 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. #endregion Private Variables
  28. public void Awake()
  29. {
  30. RegisterHandlers();
  31. SpawnCharacters();
  32. }
  33. private void Update()
  34. {
  35. server.ServerUpdate();
  36. }
  37. private void RecieveLogicList(NetworkMessage msg)
  38. {
  39. LogicProtocols.LogicMsg logicMsg;
  40. if (!msg.TryRead(out logicMsg))
  41. {
  42. Debug.Log("Recieved unknown message");
  43. return;
  44. }
  45. Debug.Log("Recieved function from " + msg.conn.connectionId);
  46. playerData[msg.conn.connectionId].blockReader.LogicChain = new List<LogicBlock>(logicMsg.elements);
  47. playerData[msg.conn.connectionId].recievedList = true;
  48. if (playerData.All(p => p.Value.recievedList))
  49. StartRound();
  50. }
  51. private void StartRound()
  52. {
  53. Debug.Log("Starting Round");
  54. StartCoroutine(RoundRoutine());
  55. }
  56. private IEnumerator RoundRoutine()
  57. {
  58. playerData.ForEach(p => p.Value.recievedList = false);
  59. while (playerData.Any(p => !p.Value.blockReader.Finished))
  60. {
  61. foreach (PlayerData player in playerData.Values)
  62. {
  63. if (!player.waiting)
  64. {
  65. player.waiting = player.blockReader.Read(player.character, AnimationTime);
  66. }
  67. }
  68. yield return new WaitForSeconds(AnimationTime);
  69. foreach(KeyValuePair<int,PlayerData> player in playerData)
  70. {
  71. ClientData client = ClientList.ConnectedClients.First(p => p.ID == player.Key);
  72. CurrentGameMode.OnPlayerFinishedMove(player.Value.character, client, player.Value.character.CurrentBlock);
  73. }
  74. if (playerData.All(p => p.Value.waiting))
  75. {
  76. playerData.ForEach(p => p.Value.waiting = false);
  77. Debug.Log("Finished one move");
  78. }
  79. }
  80. CurrentGameMode.OnRoundFinished();
  81. CurrentGameMode.isGameOver();
  82. Debug.Log("Finished Moving");
  83. }
  84. private void SpawnCharacters()
  85. {
  86. playerData = new Dictionary<int, PlayerData>();
  87. Block[] SpawnBlocks = FindObjectsOfType<Block>().Where(p => p.isSpawnable).ToArray();
  88. int spawnIndex = 0;
  89. foreach (ClientData client in ClientList)
  90. {
  91. Character newChar = Instantiate(characterPrefab);
  92. Block startingBlock = SpawnBlocks[(spawnIndex++ % ClientList.ConnectedClients.Count)];
  93. newChar.Initialise(startingBlock, client.Inventory, client.characterAnimal);
  94. playerData.Add(client.ID, new PlayerData(newChar,client));
  95. }
  96. }
  97. private void RegisterHandlers()
  98. {
  99. server.server.RegisterHandler(LogicProtocols.SendLogicList, RecieveLogicList);
  100. }
  101. }
  102. public class PlayerData
  103. {
  104. public Character character;
  105. public BlockReader blockReader;
  106. public ClientData client;
  107. public bool recievedList;
  108. public bool waiting;
  109. public PlayerData(Character character, ClientData client)
  110. {
  111. this.character = character;
  112. this.client = client;
  113. blockReader = new BlockReader();
  114. }
  115. }