From f20a5b1e71e03e88b65910b42b9cabc3a3158a02 Mon Sep 17 00:00:00 2001 From: Maciekxdabu <40292375+Maciekxdabu@users.noreply.github.com> Date: Mon, 30 May 2022 14:12:04 +0200 Subject: [PATCH] merloj: TTLList --- .../Assets/Scripts/Entities/Communication.cs | 8 +++ .../Scripts/Entities/Communication.cs.meta | 11 +++ theProject/Assets/Scripts/Entities/Soldier.cs | 1 + theProject/Assets/Scripts/Squad.cs | 18 +++-- .../Assets/Scripts/TickSystem/TTLList.cs | 70 +++++++++++++++++++ .../Assets/Scripts/TickSystem/TTLList.cs.meta | 11 +++ 6 files changed, 115 insertions(+), 4 deletions(-) create mode 100644 theProject/Assets/Scripts/Entities/Communication.cs create mode 100644 theProject/Assets/Scripts/Entities/Communication.cs.meta create mode 100644 theProject/Assets/Scripts/TickSystem/TTLList.cs create mode 100644 theProject/Assets/Scripts/TickSystem/TTLList.cs.meta diff --git a/theProject/Assets/Scripts/Entities/Communication.cs b/theProject/Assets/Scripts/Entities/Communication.cs new file mode 100644 index 00000000..98f4c6e2 --- /dev/null +++ b/theProject/Assets/Scripts/Entities/Communication.cs @@ -0,0 +1,8 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Communication : MonoBehaviour +{ + +} diff --git a/theProject/Assets/Scripts/Entities/Communication.cs.meta b/theProject/Assets/Scripts/Entities/Communication.cs.meta new file mode 100644 index 00000000..d8a51e59 --- /dev/null +++ b/theProject/Assets/Scripts/Entities/Communication.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 531947a4f2c9d414880a0f04ecd2c970 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/theProject/Assets/Scripts/Entities/Soldier.cs b/theProject/Assets/Scripts/Entities/Soldier.cs index 03bb8114..c2189d81 100644 --- a/theProject/Assets/Scripts/Entities/Soldier.cs +++ b/theProject/Assets/Scripts/Entities/Soldier.cs @@ -4,6 +4,7 @@ using TMPro; using UnityEngine; using UnityEngine.Events; +[RequireComponent(typeof(Communication))] public class Soldier : Entity { private Queue actions = new Queue(); diff --git a/theProject/Assets/Scripts/Squad.cs b/theProject/Assets/Scripts/Squad.cs index c8ac6558..13363973 100644 --- a/theProject/Assets/Scripts/Squad.cs +++ b/theProject/Assets/Scripts/Squad.cs @@ -42,25 +42,35 @@ public class Squad : MonoBehaviour #endregion [SerializeField] private Formation formation; public Formation GetFormation() { return formation; } - [SerializeField] private List soldiers = new List(); // soldiers belonging to the squad - public List GetSoldiers() { return soldiers; } + //[SerializeField] private List soldiers = new List(); // soldiers belonging to the squad + public List GetSoldiers() { return soldiers.GetTList(); } private Queue orders = new Queue(); // orders given to the squad + [SerializeField] private TTLList soldiers = new TTLList(); // soldiers belonging to the squad + [SerializeField] private TTLList enemiesSpotted = new TTLList(); // 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(); } diff --git a/theProject/Assets/Scripts/TickSystem/TTLList.cs b/theProject/Assets/Scripts/TickSystem/TTLList.cs new file mode 100644 index 00000000..5dce16a7 --- /dev/null +++ b/theProject/Assets/Scripts/TickSystem/TTLList.cs @@ -0,0 +1,70 @@ +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); + 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 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); + } +} diff --git a/theProject/Assets/Scripts/TickSystem/TTLList.cs.meta b/theProject/Assets/Scripts/TickSystem/TTLList.cs.meta new file mode 100644 index 00000000..856dd3fe --- /dev/null +++ b/theProject/Assets/Scripts/TickSystem/TTLList.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: b59e65e041baac940a520c97d9c377a7 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: