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.

189 lines
5.4 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. /*
  10. public class GameManagerRacetrack : MonoBehaviour
  11. {
  12. #region Inspector Field
  13. [Header("Settings")]
  14. [SerializeField]
  15. private float AnimationTime;
  16. [SerializeField]
  17. private GameModeReference CurrentGameMode;
  18. [Header("References")]
  19. [SerializeField]
  20. [Tooltip("Prefab of character for players to play")]
  21. private Character characterPrefab;
  22. [SerializeField]
  23. private ServerObject server;
  24. [SerializeField]
  25. private ClientList ClientList;
  26. #endregion Inspector Field
  27. #region Private Variables
  28. private Dictionary<int, PlayerData> playerData;
  29. private GameMode gamemode;
  30. #endregion Private Variables
  31. #region Read Only
  32. private IEnumerable<PlayerData> playerArray { get { return playerData.Values; } }
  33. #endregion Read Only
  34. public GameObject levelInfo;
  35. public blockSpawn bspawn;
  36. public void Awake()
  37. {
  38. gamemode = CurrentGameMode.Value;
  39. RegisterHandlers();
  40. SpawnCharacters();
  41. ClientList.ForEach(p => p.ChangeScene("ClientScene"));
  42. }
  43. private void Start()
  44. {
  45. StartCoroutine(displayforSeconds(levelInfo, 5.0f));
  46. gamemode.GameStart(playerArray.ToArray());
  47. StartRound();
  48. }
  49. private void Update()
  50. {
  51. server.ServerUpdate();
  52. }
  53. IEnumerator displayforSeconds(GameObject display, float time)
  54. {
  55. display.SetActive (true);
  56. yield return new WaitForSeconds(time);
  57. display.SetActive (false);
  58. }
  59. private void RecieveLogicList(NetworkMessage msg)
  60. {
  61. LogicProtocols.LogicMsg logicMsg;
  62. if (!msg.TryRead(out logicMsg))
  63. return;
  64. Debug.Log("Recieved function from " + msg.conn.connectionId);
  65. playerData[msg.conn.connectionId].blockReader.LogicChain = new List<LogicBlock>(logicMsg.elements);
  66. playerData[msg.conn.connectionId].recievedList = true;
  67. if (playerData.All(p => p.Value.recievedList))
  68. DoRoundRoutine();
  69. }
  70. private void DoRoundRoutine()
  71. {
  72. Debug.Log("Starting Round");
  73. StartCoroutine(RoundRoutine());
  74. }
  75. private void StartRound()
  76. {
  77. gamemode.RoundStart(playerArray.ToArray());
  78. LogicProtocols.FloatMsg RoundTime = new LogicProtocols.FloatMsg( gamemode.GetRoundTime());
  79. bspawn.Spawn();
  80. playerArray.ForEach(p => p.client.conn.Send(LogicProtocols.SendRoundTime, RoundTime));
  81. }
  82. private IEnumerator RoundRoutine()
  83. {
  84. playerArray.ForEach(p => p.recievedList = false);
  85. //Debug.Log("Doing Round Routine");
  86. while (playerArray.Any(p => !p.blockReader.Finished))
  87. {
  88. //Debug.Log("One Move");
  89. foreach (PlayerData player in playerArray)
  90. StartCoroutine(RunOnce(player));
  91. //wait until all players have finished
  92. yield return new WaitUntil(() => playerArray.All(p => p.waiting));
  93. gamemode.FinishedMove(playerArray.ToArray());
  94. playerArray.ForEach(p => p.client.SendScore());
  95. }
  96. if (gamemode.isGameOver(playerArray.ToArray()))
  97. {
  98. Debug.Log("Game Over");
  99. SceneManager.LoadScene("ScoreBoards");
  100. }
  101. gamemode.RoundEnd(playerArray.ToArray());
  102. foreach (PlayerData player in playerArray)
  103. {
  104. player.blockReader.Reset();
  105. player.waiting = false;
  106. player.client.SendInventory();
  107. player.client.ChangeScene("ClientScene");
  108. }
  109. //Debug.Log("Finished Moving");
  110. StartRound();
  111. }
  112. private void SpawnCharacters()
  113. {
  114. playerData = new Dictionary<int, PlayerData>();
  115. Block[] SpawnBlocks = FindObjectsOfType<Block>().Where(p => p.isSpawnable).ToArray();
  116. int spawnIndex = 0;
  117. foreach (ClientData client in ClientList)
  118. {
  119. Character newChar = Instantiate(characterPrefab);
  120. Block startingBlock = SpawnBlocks[(spawnIndex++ % ClientList.ConnectedClients.Count)];
  121. newChar.Initialise(startingBlock, client.Inventory, client.characterAnimal);
  122. newChar.transform.forward = startingBlock.SpawnDirection.ToVector();
  123. playerData.Add(client.ID, new PlayerData(newChar,client));
  124. newChar.ClientLink = client;
  125. client.playerCharacter = newChar;
  126. }
  127. }
  128. private void RegisterHandlers()
  129. {
  130. server.server.RegisterHandler(LogicProtocols.SendLogicList, RecieveLogicList);
  131. }
  132. private IEnumerator RunOnce(PlayerData data)
  133. {
  134. data.waiting = false;
  135. bool blockFinished = false;
  136. float waitTime;
  137. while (!blockFinished && !data.blockReader.Finished)
  138. {
  139. //Debug.Log(data.client + "Moving once");
  140. if (data.blockReader.CurrentBlock != null && !data.blockReader.CurrentBlock.hasBeenRemoved)
  141. {
  142. data.client.Inventory.Remove(data.blockReader.CurrentBlock);
  143. data.blockReader.CurrentBlock.hasBeenRemoved = true;
  144. }
  145. blockFinished = data.blockReader.Read(data.character, AnimationTime,out waitTime);
  146. //Debug.Log("Waiting: " + waitTime);
  147. yield return new WaitForSeconds(waitTime);
  148. gamemode.PlayerMoved(data);
  149. }
  150. data.waiting = true;
  151. }
  152. }*/