mirror of
https://github.com/kuhyx/WUT_Computer_Science.git
synced 2026-11-20 00:54:27 +01:00
merloj: TTLList
This commit is contained in:
parent
635c7d2b0a
commit
f20a5b1e71
8
theProject/Assets/Scripts/Entities/Communication.cs
Normal file
8
theProject/Assets/Scripts/Entities/Communication.cs
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
public class Communication : MonoBehaviour
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
11
theProject/Assets/Scripts/Entities/Communication.cs.meta
Normal file
11
theProject/Assets/Scripts/Entities/Communication.cs.meta
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 531947a4f2c9d414880a0f04ecd2c970
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
@ -4,6 +4,7 @@ using TMPro;
|
|||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using UnityEngine.Events;
|
using UnityEngine.Events;
|
||||||
|
|
||||||
|
[RequireComponent(typeof(Communication))]
|
||||||
public class Soldier : Entity
|
public class Soldier : Entity
|
||||||
{
|
{
|
||||||
private Queue<Action> actions = new Queue<Action>();
|
private Queue<Action> actions = new Queue<Action>();
|
||||||
|
|||||||
@ -42,25 +42,35 @@ public class Squad : MonoBehaviour
|
|||||||
#endregion
|
#endregion
|
||||||
[SerializeField] private Formation formation;
|
[SerializeField] private Formation formation;
|
||||||
public Formation GetFormation() { return formation; }
|
public Formation GetFormation() { return formation; }
|
||||||
[SerializeField] private List<Entity> soldiers = new List<Entity>(); // soldiers belonging to the squad
|
//[SerializeField] private List<Entity> soldiers = new List<Entity>(); // soldiers belonging to the squad
|
||||||
public List<Entity> GetSoldiers() { return soldiers; }
|
public List<Entity> GetSoldiers() { return soldiers.GetTList(); }
|
||||||
private Queue<Order> orders = new Queue<Order>(); // orders given to the squad
|
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)
|
public void AddSoldierToSquad(Entity soldier)
|
||||||
{
|
{
|
||||||
soldiers.Add(soldier);
|
soldiers.AddToList(soldier);
|
||||||
soldier.OnDeath.AddListener(RemoveSoldierFromSquad);
|
soldier.OnDeath.AddListener(RemoveSoldierFromSquad);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RemoveSoldierFromSquad(Entity soldier)
|
public void RemoveSoldierFromSquad(Entity soldier)
|
||||||
{
|
{
|
||||||
soldiers.Remove(soldier);
|
soldiers.RemoveFromList(soldier);
|
||||||
soldier.OnDeath.RemoveListener(RemoveSoldierFromSquad);
|
soldier.OnDeath.RemoveListener(RemoveSoldierFromSquad);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Awake()
|
private void Awake()
|
||||||
{
|
{
|
||||||
TickSystem.OnTick += HandleTick;
|
TickSystem.OnTick += HandleTick;
|
||||||
|
soldiers.Initialize(true, basicSoldierTTL);
|
||||||
|
TickSystem.OnTick += soldiers.OnTick;
|
||||||
|
enemiesSpotted.Initialize(false, basicEnemyTTL);
|
||||||
|
TickSystem.OnTick += enemiesSpotted.OnTick;
|
||||||
|
|
||||||
formation = GetComponent<Formation>();
|
formation = GetComponent<Formation>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
70
theProject/Assets/Scripts/TickSystem/TTLList.cs
Normal file
70
theProject/Assets/Scripts/TickSystem/TTLList.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
11
theProject/Assets/Scripts/TickSystem/TTLList.cs.meta
Normal file
11
theProject/Assets/Scripts/TickSystem/TTLList.cs.meta
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: b59e65e041baac940a520c97d9c377a7
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
Loading…
Reference in New Issue
Block a user