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.

145 lines
4.1 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 int RoundCount { get; private set; }
  15. private Dictionary<ClientData, List<Block>> BlocksOwned;
  16. /// <summary>
  17. /// Called once all players have finished their moves but before the Objective is checked
  18. /// </summary>
  19. protected override void OnRoundEnd(PlayerData[] allPlayers)
  20. {
  21. RoundCount++;
  22. }
  23. /// <summary>
  24. /// Checks if the Game is finished
  25. /// </summary>
  26. /// <returns>returns if game is finished</returns>
  27. public override bool isGameOver(PlayerData[] allPlayers)
  28. {
  29. return (RoundCount >= MaxRound -1);
  30. }
  31. /// <summary>
  32. /// Called once per player after they have moved onto a block
  33. /// </summary>
  34. /// <param name="character">Character which moved</param>
  35. /// <param name="client">Client of the character</param>
  36. /// <param name="currentBlock">Block moved onto</param>
  37. protected override void OnPlayerFinishedMove(Character character, ClientData client, Block currentBlock)
  38. {
  39. ClientData OwnedClient;
  40. Material overlay = null;
  41. if (isOwned(currentBlock, out OwnedClient))
  42. {
  43. if (OwnedClient == client)
  44. return;
  45. BlocksOwned[OwnedClient].Remove(currentBlock);
  46. foreach (Material mat in currentBlock.GetComponent<Renderer>().materials)
  47. {
  48. if (mat.name == OverlayMaterial.name + " (Instance)")
  49. overlay = mat;
  50. }
  51. }
  52. if (overlay == null)
  53. {
  54. overlay = new Material(OverlayMaterial);
  55. List<Material> mats = new List<Material>(currentBlock.GetComponent<Renderer>().materials);
  56. mats.Add(overlay);
  57. currentBlock.GetComponent<Renderer>().materials = mats.ToArray();
  58. }
  59. overlay.SetColor("_NewColor", client.Color);
  60. if (!BlocksOwned.ContainsKey(client))
  61. BlocksOwned.Add(client, new List<Block>());
  62. BlocksOwned[client].Add(currentBlock);
  63. if (overlay != null)
  64. currentBlock.StartCoroutine(AnimateBlock(overlay, 0.25f));
  65. }
  66. protected override void OnRoundStart(PlayerData[] allPlayers)
  67. {
  68. }
  69. protected override void OnAllPlayersFinishedMove(PlayerData[] allPlayers)
  70. {
  71. foreach (PlayerData player in allPlayers)
  72. {
  73. if (BlocksOwned.ContainsKey(player.client))
  74. player.client.SceneScore = BlocksOwned[player.client].Count;
  75. else
  76. player.client.SceneScore = 0;
  77. }
  78. }
  79. protected override void OnGameOver(PlayerData[] allPlayers)
  80. {
  81. throw new System.NotImplementedException();
  82. }
  83. private bool isOwned(Block block, out ClientData client)
  84. {
  85. client = null;
  86. foreach (KeyValuePair<ClientData, List<Block>> ownedList in BlocksOwned)
  87. {
  88. if (ownedList.Value.Contains(block))
  89. {
  90. client = ownedList.Key;
  91. return true;
  92. }
  93. }
  94. return false;
  95. }
  96. private IEnumerator AnimateBlock(Material mat, float time)
  97. {
  98. float timeElasped = 0;
  99. while (timeElasped < time)
  100. {
  101. mat.SetFloat("_Multiplier", (timeElasped / time));
  102. yield return new WaitForEndOfFrame();
  103. timeElasped += Time.deltaTime;
  104. }
  105. mat.SetFloat("_Multiplier", 1);
  106. }
  107. protected override void OnGameStart(PlayerData[] allPlayers)
  108. {
  109. BlocksOwned = new Dictionary<ClientData, List<Block>>();
  110. RoundCount = 0;
  111. for(int i = 0; i < allPlayers.Length; i++)
  112. {
  113. OnPlayerFinishedMove(allPlayers[i].character, allPlayers[i].client, allPlayers[i].character.CurrentBlock);
  114. }
  115. }
  116. }