PunTeams.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="PunTeams.cs" company="Exit Games GmbH">
  3. // Part of: Photon Unity Utilities,
  4. // </copyright>
  5. // <summary>
  6. // Implements teams in a room/game with help of player properties. Access them by Player.GetTeam extension.
  7. // </summary>
  8. // <remarks>
  9. // Teams are defined by enum Team. Change this to get more / different teams.
  10. // There are no rules when / if you can join a team. You could add this in JoinTeam or something.
  11. // </remarks>
  12. // <author>developer@exitgames.com</author>
  13. // --------------------------------------------------------------------------------------------------------------------
  14. using System;
  15. using System.Collections.Generic;
  16. using UnityEngine;
  17. using Photon.Pun;
  18. using Photon.Realtime;
  19. using ExitGames.Client.Photon;
  20. using Hashtable = ExitGames.Client.Photon.Hashtable;
  21. namespace Photon.Pun.UtilityScripts
  22. {
  23. /// <summary>
  24. /// Implements teams in a room/game with help of player properties. Access them by Player.GetTeam extension.
  25. /// </summary>
  26. /// <remarks>
  27. /// Teams are defined by enum Team. Change this to get more / different teams.
  28. /// There are no rules when / if you can join a team. You could add this in JoinTeam or something.
  29. /// </remarks>
  30. [Obsolete("do not use this or add it to the scene. use PhotonTeamsManager instead")]
  31. public class PunTeams : MonoBehaviourPunCallbacks
  32. {
  33. /// <summary>Enum defining the teams available. First team should be neutral (it's the default value any field of this enum gets).</summary>
  34. [Obsolete("use custom PhotonTeam instead")]
  35. public enum Team : byte { none, red, blue };
  36. /// <summary>The main list of teams with their player-lists. Automatically kept up to date.</summary>
  37. /// <remarks>Note that this is static. Can be accessed by PunTeam.PlayersPerTeam. You should not modify this.</remarks>
  38. [Obsolete("use PhotonTeamsManager.Instance.TryGetTeamMembers instead")]
  39. public static Dictionary<Team, List<Player>> PlayersPerTeam;
  40. /// <summary>Defines the player custom property name to use for team affinity of "this" player.</summary>
  41. [Obsolete("do not use this. PhotonTeamsManager.TeamPlayerProp is used internally instead.")]
  42. public const string TeamPlayerProp = "team";
  43. #region Events by Unity and Photon
  44. public void Start()
  45. {
  46. PlayersPerTeam = new Dictionary<Team, List<Player>>();
  47. Array enumVals = Enum.GetValues(typeof(Team));
  48. foreach (var enumVal in enumVals)
  49. {
  50. PlayersPerTeam[(Team)enumVal] = new List<Player>();
  51. }
  52. }
  53. public override void OnDisable()
  54. {
  55. base.OnDisable();
  56. this.Start();
  57. }
  58. /// <summary>Needed to update the team lists when joining a room.</summary>
  59. /// <remarks>Called by PUN. See enum MonoBehaviourPunCallbacks for an explanation.</remarks>
  60. public override void OnJoinedRoom()
  61. {
  62. this.UpdateTeams();
  63. }
  64. public override void OnLeftRoom()
  65. {
  66. Start();
  67. }
  68. /// <summary>Refreshes the team lists. It could be a non-team related property change, too.</summary>
  69. /// <remarks>Called by PUN. See enum MonoBehaviourPunCallbacks for an explanation.</remarks>
  70. public override void OnPlayerPropertiesUpdate(Player targetPlayer, Hashtable changedProps)
  71. {
  72. this.UpdateTeams();
  73. }
  74. public override void OnPlayerLeftRoom(Player otherPlayer)
  75. {
  76. this.UpdateTeams();
  77. }
  78. public override void OnPlayerEnteredRoom(Player newPlayer)
  79. {
  80. this.UpdateTeams();
  81. }
  82. #endregion
  83. [Obsolete("do not call this.")]
  84. public void UpdateTeams()
  85. {
  86. Array enumVals = Enum.GetValues(typeof(Team));
  87. foreach (var enumVal in enumVals)
  88. {
  89. PlayersPerTeam[(Team)enumVal].Clear();
  90. }
  91. for (int i = 0; i < PhotonNetwork.PlayerList.Length; i++)
  92. {
  93. Player player = PhotonNetwork.PlayerList[i];
  94. Team playerTeam = player.GetTeam();
  95. PlayersPerTeam[playerTeam].Add(player);
  96. }
  97. }
  98. }
  99. /// <summary>Extension used for PunTeams and Player class. Wraps access to the player's custom property.</summary>
  100. public static class TeamExtensions
  101. {
  102. /// <summary>Extension for Player class to wrap up access to the player's custom property.</summary>
  103. /// <returns>PunTeam.Team.none if no team was found (yet).</returns>
  104. [Obsolete("Use player.GetPhotonTeam")]
  105. public static PunTeams.Team GetTeam(this Player player)
  106. {
  107. object teamId;
  108. if (player.CustomProperties.TryGetValue(PunTeams.TeamPlayerProp, out teamId))
  109. {
  110. return (PunTeams.Team)teamId;
  111. }
  112. return PunTeams.Team.none;
  113. }
  114. /// <summary>Switch that player's team to the one you assign.</summary>
  115. /// <remarks>Internally checks if this player is in that team already or not. Only team switches are actually sent.</remarks>
  116. /// <param name="player"></param>
  117. /// <param name="team"></param>
  118. [Obsolete("Use player.JoinTeam")]
  119. public static void SetTeam(this Player player, PunTeams.Team team)
  120. {
  121. if (!PhotonNetwork.IsConnectedAndReady)
  122. {
  123. Debug.LogWarning("JoinTeam was called in state: " + PhotonNetwork.NetworkClientState + ". Not IsConnectedAndReady.");
  124. return;
  125. }
  126. PunTeams.Team currentTeam = player.GetTeam();
  127. if (currentTeam != team)
  128. {
  129. player.SetCustomProperties(new Hashtable() { { PunTeams.TeamPlayerProp, (byte)team } });
  130. }
  131. }
  132. }
  133. }