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.

159 lines
4.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. RoundCount++;
  33. }
  34. /// <summary>
  35. /// Checks if the Game is finished
  36. /// </summary>
  37. /// <returns>returns if game is finished</returns>
  38. public override bool isGameOver(PlayerData[] allPlayers)
  39. {
  40. return (RoundCount >= MaxRound -1);
  41. }
  42. /// <summary>
  43. /// Called once per player after they have moved onto a block
  44. /// </summary>
  45. /// <param name="character">Character which moved</param>
  46. /// <param name="client">Client of the character</param>
  47. /// <param name="currentBlock">Block moved onto</param>
  48. protected override void OnPlayerFinishedMove(Character character, ClientData client, Block currentBlock)
  49. {
  50. //Commented out because we don't do this in the racetrack mode, but I don't know what would break if I just deleted this
  51. /*ClientData OwnedClient;
  52. Material overlay = null;
  53. if (isOwned(currentBlock, out OwnedClient))
  54. {
  55. if (OwnedClient == client)
  56. return;
  57. BlocksOwned[OwnedClient].Remove(currentBlock);
  58. foreach (Material mat in currentBlock.GetComponent<Renderer>().materials)
  59. {
  60. if (mat.name == OverlayMaterial.name + " (Instance)")
  61. overlay = mat;
  62. }
  63. }
  64. if (overlay == null)
  65. {
  66. overlay = new Material(OverlayMaterial);
  67. List<Material> mats = new List<Material>(currentBlock.GetComponent<Renderer>().materials);
  68. mats.Add(overlay);
  69. currentBlock.GetComponent<Renderer>().materials = mats.ToArray();
  70. }
  71. overlay.SetColor("_NewColor", client.Color);
  72. if (!BlocksOwned.ContainsKey(client))
  73. BlocksOwned.Add(client, new List<Block>());
  74. BlocksOwned[client].Add(currentBlock);
  75. if (overlay != null)
  76. currentBlock.StartCoroutine(AnimateBlock(overlay, 0.25f));*/
  77. }
  78. protected override void OnRoundStart(PlayerData[] allPlayers)
  79. {
  80. }
  81. protected override void OnAllPlayersFinishedMove(PlayerData[] allPlayers)
  82. {
  83. foreach (PlayerData player in allPlayers)
  84. {
  85. if (BlocksOwned.ContainsKey(player.client))
  86. player.client.SceneScore = BlocksOwned[player.client].Count;
  87. else
  88. player.client.SceneScore = 0;
  89. }
  90. }
  91. protected override void OnGameOver(PlayerData[] allPlayers)
  92. {
  93. throw new System.NotImplementedException();
  94. }
  95. private bool isOwned(Block block, out ClientData client)
  96. {
  97. client = null;
  98. foreach (KeyValuePair<ClientData, List<Block>> ownedList in BlocksOwned)
  99. {
  100. if (ownedList.Value.Contains(block))
  101. {
  102. client = ownedList.Key;
  103. return true;
  104. }
  105. }
  106. return false;
  107. }
  108. private IEnumerator AnimateBlock(Material mat, float time)
  109. {
  110. float timeElasped = 0;
  111. while (timeElasped < time)
  112. {
  113. mat.SetFloat("_Multiplier", (timeElasped / time));
  114. yield return new WaitForEndOfFrame();
  115. timeElasped += Time.deltaTime;
  116. }
  117. mat.SetFloat("_Multiplier", 1);
  118. }
  119. protected override void OnGameStart(PlayerData[] allPlayers)
  120. {
  121. BlocksOwned = new Dictionary<ClientData, List<Block>>();
  122. RoundCount = 0;
  123. for(int i = 0; i < allPlayers.Length; i++)
  124. {
  125. OnPlayerFinishedMove(allPlayers[i].character, allPlayers[i].client, allPlayers[i].character.CurrentBlock);
  126. }
  127. }
  128. }