// -------------------------------------------------------------------------------------------------------------------- // // Part of: Photon Unity Utilities, // // // Implements teams in a room/game with help of player properties. Access them by Player.GetTeam extension. // // // Teams are defined by enum Team. Change this to get more / different teams. // There are no rules when / if you can join a team. You could add this in JoinTeam or something. // // developer@exitgames.com // -------------------------------------------------------------------------------------------------------------------- using System; using System.Collections.Generic; using UnityEngine; using Photon.Pun; using Photon.Realtime; using ExitGames.Client.Photon; using Hashtable = ExitGames.Client.Photon.Hashtable; namespace Photon.Pun.UtilityScripts { /// /// Implements teams in a room/game with help of player properties. Access them by Player.GetTeam extension. /// /// /// Teams are defined by enum Team. Change this to get more / different teams. /// There are no rules when / if you can join a team. You could add this in JoinTeam or something. /// [Obsolete("do not use this or add it to the scene. use PhotonTeamsManager instead")] public class PunTeams : MonoBehaviourPunCallbacks { /// Enum defining the teams available. First team should be neutral (it's the default value any field of this enum gets). [Obsolete("use custom PhotonTeam instead")] public enum Team : byte { none, red, blue }; /// The main list of teams with their player-lists. Automatically kept up to date. /// Note that this is static. Can be accessed by PunTeam.PlayersPerTeam. You should not modify this. [Obsolete("use PhotonTeamsManager.Instance.TryGetTeamMembers instead")] public static Dictionary> PlayersPerTeam; /// Defines the player custom property name to use for team affinity of "this" player. [Obsolete("do not use this. PhotonTeamsManager.TeamPlayerProp is used internally instead.")] public const string TeamPlayerProp = "team"; #region Events by Unity and Photon public void Start() { PlayersPerTeam = new Dictionary>(); Array enumVals = Enum.GetValues(typeof(Team)); foreach (var enumVal in enumVals) { PlayersPerTeam[(Team)enumVal] = new List(); } } public override void OnDisable() { base.OnDisable(); this.Start(); } /// Needed to update the team lists when joining a room. /// Called by PUN. See enum MonoBehaviourPunCallbacks for an explanation. public override void OnJoinedRoom() { this.UpdateTeams(); } public override void OnLeftRoom() { Start(); } /// Refreshes the team lists. It could be a non-team related property change, too. /// Called by PUN. See enum MonoBehaviourPunCallbacks for an explanation. public override void OnPlayerPropertiesUpdate(Player targetPlayer, Hashtable changedProps) { this.UpdateTeams(); } public override void OnPlayerLeftRoom(Player otherPlayer) { this.UpdateTeams(); } public override void OnPlayerEnteredRoom(Player newPlayer) { this.UpdateTeams(); } #endregion [Obsolete("do not call this.")] public void UpdateTeams() { Array enumVals = Enum.GetValues(typeof(Team)); foreach (var enumVal in enumVals) { PlayersPerTeam[(Team)enumVal].Clear(); } for (int i = 0; i < PhotonNetwork.PlayerList.Length; i++) { Player player = PhotonNetwork.PlayerList[i]; Team playerTeam = player.GetTeam(); PlayersPerTeam[playerTeam].Add(player); } } } /// Extension used for PunTeams and Player class. Wraps access to the player's custom property. public static class TeamExtensions { /// Extension for Player class to wrap up access to the player's custom property. /// PunTeam.Team.none if no team was found (yet). [Obsolete("Use player.GetPhotonTeam")] public static PunTeams.Team GetTeam(this Player player) { object teamId; if (player.CustomProperties.TryGetValue(PunTeams.TeamPlayerProp, out teamId)) { return (PunTeams.Team)teamId; } return PunTeams.Team.none; } /// Switch that player's team to the one you assign. /// Internally checks if this player is in that team already or not. Only team switches are actually sent. /// /// [Obsolete("Use player.JoinTeam")] public static void SetTeam(this Player player, PunTeams.Team team) { if (!PhotonNetwork.IsConnectedAndReady) { Debug.LogWarning("JoinTeam was called in state: " + PhotonNetwork.NetworkClientState + ". Not IsConnectedAndReady."); return; } PunTeams.Team currentTeam = player.GetTeam(); if (currentTeam != team) { player.SetCustomProperties(new Hashtable() { { PunTeams.TeamPlayerProp, (byte)team } }); } } } }