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.

214 lines
6.7 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Networking.Server;
  5. using TMPro;
  6. using UnityEngine.UI;
  7. [CreateAssetMenu(menuName = "Major Project/GameModes/Color Collide", order = 201)]
  8. public class ColorGameMode : GameMode
  9. {
  10. public int MaxRound = 5;
  11. public string nextScene = "ServerTestScene";
  12. List<ClientData> ConnectedClients;
  13. public Material OverlayMaterial;
  14. public float scrollSpeed = 0.0f; //The rate at which the level will scroll past
  15. public int RoundCount { get; private set; }
  16. private Dictionary<ClientData, List<Block>> BlocksOwned;
  17. /// <summary>
  18. /// Called once all players have finished their moves but before the Objective is checked
  19. /// </summary>
  20. protected override void OnRoundEnd(PlayerData[] allPlayers)
  21. {
  22. //Move the camera forward at a steady rate each round
  23. if (scrollSpeed > 0.0f)
  24. {
  25. Camera.main.transform.Translate(scrollSpeed, 0, 0, Space.World);
  26. Debug.Log("New camera position at x = " + Camera.main.transform.position.x);
  27. }
  28. else
  29. {
  30. Debug.Log("Not scrolling");
  31. }
  32. //At the end of each round, any stuck players are freed to resume moving next round
  33. foreach (PlayerData player in allPlayers)
  34. {
  35. player.character.stuck = false;
  36. if (player.character.inPit)
  37. {
  38. player.character.respawnCharacter();
  39. }
  40. }
  41. RoundCount++;
  42. }
  43. /// <summary>
  44. /// Checks if the Game is finished
  45. /// </summary>
  46. /// <returns>returns if game is finished</returns>
  47. public override bool isGameOver(PlayerData[] allPlayers)
  48. {
  49. return (RoundCount >= MaxRound -1);
  50. }
  51. /// <summary>
  52. /// Called once per player after they have moved onto a block
  53. /// </summary>
  54. /// <param name="character">Character which moved</param>
  55. /// <param name="client">Client of the character</param>
  56. /// <param name="currentBlock">Block moved onto</param>
  57. protected override void OnPlayerMoved(Character character, ClientData client, Block currentBlock)
  58. {
  59. handleFalling(character, client, currentBlock, character.justMoved);
  60. /*Debug.Log("Moved to square at " + currentBlock.transform.position.x + ", "
  61. + currentBlock.transform.position.y + ", "
  62. + currentBlock.transform.position.z);
  63. //If a character has fallen in the water or into a pit, we mark that fact, and they lose the rest of their turn
  64. character.inWater = currentBlock.isWater;
  65. character.inPit = currentBlock.isPit;
  66. if (character.inWater == true || character.inPit == true)
  67. {
  68. character.stuck = true;
  69. }
  70. Debug.Log("inWater = " + character.inWater + ", inPit = " + character.inPit + ", stuck = " + character.stuck);*/
  71. //Commented out because we don't do this in the racetrack mode
  72. /*ClientData OwnedClient;
  73. Material overlay = null;
  74. if (isOwned(currentBlock, out OwnedClient))
  75. {
  76. if (OwnedClient == client)
  77. return;
  78. BlocksOwned[OwnedClient].Remove(currentBlock);
  79. foreach (Material mat in currentBlock.GetComponent<Renderer>().materials)
  80. {
  81. if (mat.name == OverlayMaterial.name + " (Instance)")
  82. overlay = mat;
  83. }
  84. }
  85. if (overlay == null)
  86. {
  87. overlay = new Material(OverlayMaterial);
  88. List<Material> mats = new List<Material>(currentBlock.GetComponent<Renderer>().materials);
  89. mats.Add(overlay);
  90. currentBlock.GetComponent<Renderer>().materials = mats.ToArray();
  91. }
  92. overlay.SetColor("_NewColor", client.Color);
  93. if (!BlocksOwned.ContainsKey(client))
  94. BlocksOwned.Add(client, new List<Block>());
  95. BlocksOwned[client].Add(currentBlock);
  96. if (overlay != null)
  97. currentBlock.StartCoroutine(AnimateBlock(overlay, 0.25f));*/
  98. }
  99. protected override void OnRoundStart(PlayerData[] allPlayers)
  100. {
  101. }
  102. protected override void OnAllPlayersMoved(PlayerData[] allPlayers)
  103. {
  104. foreach (PlayerData player in allPlayers)
  105. {
  106. /* The justMoved variable is used to determine whether a player taking their turn in the water should become stuck
  107. * (because they moved into/in the water), or not (because they're turning while remaining in the same square)
  108. * It's not needed from move to move, so we clear it
  109. */
  110. player.character.justMoved = false;
  111. }
  112. }
  113. protected override void OnGameOver(PlayerData[] allPlayers)
  114. {
  115. throw new System.NotImplementedException();
  116. }
  117. private bool isOwned(Block block, out ClientData client)
  118. {
  119. client = null;
  120. foreach (KeyValuePair<ClientData, List<Block>> ownedList in BlocksOwned)
  121. {
  122. if (ownedList.Value.Contains(block))
  123. {
  124. client = ownedList.Key;
  125. return true;
  126. }
  127. }
  128. return false;
  129. }
  130. private void handleFalling(Character character, ClientData client, Block currentBlock, bool didMove)
  131. {
  132. //If a character has fallen in the water or into a pit, we mark that fact, and they lose the rest of their turn
  133. character.inWater = currentBlock.isWater;
  134. character.inPit = currentBlock.isPit;
  135. if (didMove && (character.inWater || character.inPit))
  136. {
  137. character.stuck = true;
  138. }
  139. Debug.Log("inWater = " + character.inWater + ", inPit = " + character.inPit + ", stuck = " + character.stuck);
  140. }
  141. protected override void OnPlayerKilled(Character character, ClientData client)
  142. {
  143. if (character.inPit)
  144. {
  145. character.lives -= 1;
  146. character.ClientLink.Lives = character.lives;
  147. }
  148. }
  149. private IEnumerator AnimateBlock(Material mat, float time)
  150. {
  151. float timeElasped = 0;
  152. while (timeElasped < time)
  153. {
  154. mat.SetFloat("_Multiplier", (timeElasped / time));
  155. yield return new WaitForEndOfFrame();
  156. timeElasped += Time.deltaTime;
  157. }
  158. mat.SetFloat("_Multiplier", 1);
  159. }
  160. protected override void OnGameStart(PlayerData[] allPlayers)
  161. {
  162. BlocksOwned = new Dictionary<ClientData, List<Block>>();
  163. RoundCount = 0;
  164. for(int i = 0; i < allPlayers.Length; i++)
  165. {
  166. OnPlayerMoved(allPlayers[i].character, allPlayers[i].client, allPlayers[i].character.CurrentBlock);
  167. }
  168. }
  169. public override PlayerData[] getPlayerOrder(PlayerData[] AllPlayers)
  170. {
  171. throw new System.NotImplementedException();
  172. }
  173. }