using System.Collections; using System.Collections.Generic; using UnityEngine; /* void Usage() { //Access the 5th unit created Debug.Log(Unit.All[5].transform.position); foreach(Unit unit in Unit.Teams[Team.Red]) { //loop over each unit on Red team Debug.Log(unit.transform.position); } //How many Units does Blue have left? Debug.Log(Unit.Teams[Team.Blue].Count); } */ public class Unit : MonoBehaviour { /// /// Rudimentary team definition /// public enum Team { Neutral, Red, Blue } /// /// List of all active units /// public static List All; /// /// List of all units by team /// public static Dictionary> Teams; /// /// Static Constructor initializes all the lists and dictionaries /// static Unit() { All = new List(); Teams = new Dictionary>(); //register each Team by initializing a new list for each one var teamTypes = System.Enum.GetValues(typeof(Team)); foreach (var t in teamTypes) Teams[(Team)t] = new List(); } /// /// The unit's team /// public Team team; /// /// A useful position property /// public Vector3 Position => transform.position; public virtual void Start() { //Add to the All list All.Add(this); //Add to the appropriate Team list Teams[team].Add(this); } public virtual void OnDestroy() { //Remove from the All list All.Remove(this); //Remove from the apporpriate Team list Teams[team].Remove(this); } }