using System.Collections; using System.Collections.Generic; using UnityEngine; [System.Serializable] public class TTLList where T : UnityEngine.Object { private Dictionary dict = new Dictionary(); public List viewList;//DO NOT EDIT (FOR DEBUG PURPOSES ONLY) bool isSquad = false; int basicTTL = 5; // ---------- methods public void Initialize(bool squad, int bTTL) { isSquad = squad; basicTTL = bTTL; } public void AddToList(T obj) { Debug.Log(obj); if (dict.ContainsKey(obj)) dict[obj] = basicTTL; else dict.Add(obj, basicTTL); Debug.Log("Added/Changed object in dictionary", obj); UpdateViewList(); } public void RemoveFromList(T obj) { dict.Remove(obj); UpdateViewList(); } public void OnTick(TickSystem.OnTickEventArgs eventArgs) { List keys = new List(dict.Keys); if (isSquad && keys.Count <= 1) return; foreach (T key in keys) { dict[key] -= 1; if (dict[key] <= 0 && !(isSquad && dict.Keys.Count <= 1)) { dict.Remove(key); Debug.Log("Lost view of soldier due to TTL:", key); UpdateViewList(); } else if (isSquad && dict.Keys.Count <= 1) { dict[key] = basicTTL; } } } public List GetTList() { return new List(dict.Keys); } // ---------- private methods // DEBUG - Changes a view list to see soldiers active in squad in inspector private void UpdateViewList() { viewList = new List(dict.Keys); } }