mirror of
https://github.com/kuhyx/WUT_Computer_Science.git
synced 2026-07-04 17:23:12 +02:00
74 lines
1.7 KiB
C#
74 lines
1.7 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
[System.Serializable]
|
|
public class TTLList<T> where T : UnityEngine.Object
|
|
{
|
|
private Dictionary<T, int> dict = new Dictionary<T, int>();
|
|
public List<T> 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<T> keys = new List<T>(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<T> GetTList()
|
|
{
|
|
return new List<T>(dict.Keys);
|
|
}
|
|
|
|
// ---------- private methods
|
|
|
|
// DEBUG - Changes a view list to see soldiers active in squad in inspector
|
|
private void UpdateViewList()
|
|
{
|
|
viewList = new List<T>(dict.Keys);
|
|
}
|
|
}
|