merloj: TTLList

This commit is contained in:
Maciekxdabu 2022-05-30 14:12:04 +02:00
parent 635c7d2b0a
commit f20a5b1e71
6 changed files with 115 additions and 4 deletions

View File

@ -0,0 +1,8 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Communication : MonoBehaviour
{
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 531947a4f2c9d414880a0f04ecd2c970
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -4,6 +4,7 @@ using TMPro;
using UnityEngine;
using UnityEngine.Events;
[RequireComponent(typeof(Communication))]
public class Soldier : Entity
{
private Queue<Action> actions = new Queue<Action>();

View File

@ -42,25 +42,35 @@ public class Squad : MonoBehaviour
#endregion
[SerializeField] private Formation formation;
public Formation GetFormation() { return formation; }
[SerializeField] private List<Entity> soldiers = new List<Entity>(); // soldiers belonging to the squad
public List<Entity> GetSoldiers() { return soldiers; }
//[SerializeField] private List<Entity> soldiers = new List<Entity>(); // soldiers belonging to the squad
public List<Entity> GetSoldiers() { return soldiers.GetTList(); }
private Queue<Order> orders = new Queue<Order>(); // orders given to the squad
[SerializeField] private TTLList<Entity> soldiers = new TTLList<Entity>(); // soldiers belonging to the squad
[SerializeField] private TTLList<Entity> enemiesSpotted = new TTLList<Entity>(); // enemies currently viewed by squad
[SerializeField] private int basicSoldierTTL = 5;
[SerializeField] private int basicEnemyTTL = 5;
public void AddSoldierToSquad(Entity soldier)
{
soldiers.Add(soldier);
soldiers.AddToList(soldier);
soldier.OnDeath.AddListener(RemoveSoldierFromSquad);
}
public void RemoveSoldierFromSquad(Entity soldier)
{
soldiers.Remove(soldier);
soldiers.RemoveFromList(soldier);
soldier.OnDeath.RemoveListener(RemoveSoldierFromSquad);
}
private void Awake()
{
TickSystem.OnTick += HandleTick;
soldiers.Initialize(true, basicSoldierTTL);
TickSystem.OnTick += soldiers.OnTick;
enemiesSpotted.Initialize(false, basicEnemyTTL);
TickSystem.OnTick += enemiesSpotted.OnTick;
formation = GetComponent<Formation>();
}

View File

@ -0,0 +1,70 @@
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);
dict.Add(obj, basicTTL);
Debug.Log("Added object to 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);
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b59e65e041baac940a520c97d9c377a7
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: