| | 1 | | using System.Collections; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using UnityEngine; |
| | 4 | | using System.Linq; |
| | 5 | | using UnityEngine.AI; |
| | 6 | | public class SwarmStatusDialog : UIDialog { |
| | 7 | | // Start is called before the first frame update |
| | 8 | |
|
| 1 | 9 | | bool wasStarted = false; |
| | 10 | |
|
| 1 | 11 | | private List<List<(Agent, bool)>> interceptorSwarms = new List<List<(Agent, bool)>>(); |
| 1 | 12 | | private List<List<(Agent, bool)>> submunitionsSwarms = new List<List<(Agent, bool)>>(); |
| 1 | 13 | | private List<List<(Agent, bool)>> threatsSwarms = new List<List<(Agent, bool)>>(); |
| 0 | 14 | | protected void Awake() {} |
| | 15 | |
|
| 0 | 16 | | public override void Start() { |
| 0 | 17 | | base.Start(); |
| | 18 | |
|
| 0 | 19 | | InitDialog(); |
| 0 | 20 | | wasStarted = true; |
| 0 | 21 | | } |
| | 22 | |
|
| 0 | 23 | | public void InitDialog() { |
| 0 | 24 | | SimManager.Instance.OnInterceptorSwarmChanged += RegisterInterceptorSwarmChanged; |
| 0 | 25 | | SimManager.Instance.OnSubmunitionsSwarmChanged += RegisterSubmunitionsSwarmChanged; |
| 0 | 26 | | SimManager.Instance.OnThreatSwarmChanged += RegisterThreatSwarmChanged; |
| 0 | 27 | | SimManager.Instance.OnSimulationEnded += RegisterSimulationEnded; |
| 0 | 28 | | RedrawFullDialog(); |
| | 29 | |
|
| 0 | 30 | | AddDialogTab("All", () => {}); |
| 0 | 31 | | AddDialogTab("Interceptors", () => {}); |
| 0 | 32 | | AddDialogTab("Submunitions", () => {}); |
| 0 | 33 | | AddDialogTab("Threats", () => {}); |
| 0 | 34 | | } |
| | 35 | |
|
| 0 | 36 | | private void RedrawFullDialog() { |
| 0 | 37 | | ClearDialogEntries(); |
| 0 | 38 | | List<UISelectableEntry> parents = CreateParentEntries(); |
| 0 | 39 | | List<UISelectableEntry> interceptorChildren = CreateInterceptorSwarmEntries(interceptorSwarms); |
| 0 | 40 | | List<UISelectableEntry> submunitionsChildren = |
| | 41 | | CreateSubmunitionsSwarmEntries(submunitionsSwarms); |
| 0 | 42 | | List<UISelectableEntry> threatsChildren = CreateThreatSwarmEntries(threatsSwarms); |
| 0 | 43 | | parents[1].SetChildEntries(interceptorChildren); |
| 0 | 44 | | parents[2].SetChildEntries(submunitionsChildren); |
| 0 | 45 | | parents[3].SetChildEntries(threatsChildren); |
| 0 | 46 | | SetDialogEntries(parents); |
| 0 | 47 | | } |
| | 48 | |
|
| 0 | 49 | | private List<UISelectableEntry> CreateParentEntries() { |
| 0 | 50 | | List<UISelectableEntry> children = new List<UISelectableEntry>(); |
| | 51 | |
|
| 0 | 52 | | UISelectableEntry masterTitleBar = CreateSelectableEntry(); |
| 0 | 53 | | masterTitleBar.SetTextContent(new List<string>(new string[] { "Swarm Name", "Active Agents" })); |
| 0 | 54 | | masterTitleBar.SetIsSelectable(false); |
| | 55 | |
|
| 0 | 56 | | UISelectableEntry interceptorParentEntry = CreateSelectableEntry(); |
| 0 | 57 | | interceptorParentEntry.SetTextContent(new List<string>(new string[] { "Interceptor Swarms" })); |
| 0 | 58 | | interceptorParentEntry.SetIsSelectable(false); |
| | 59 | |
|
| 0 | 60 | | UISelectableEntry submunitionsParentEntry = CreateSelectableEntry(); |
| 0 | 61 | | submunitionsParentEntry.SetTextContent( |
| | 62 | | new List<string>(new string[] { "Submunitions Swarms" })); |
| 0 | 63 | | submunitionsParentEntry.SetIsSelectable(false); |
| | 64 | |
|
| 0 | 65 | | UISelectableEntry threatsParentEntry = CreateSelectableEntry(); |
| 0 | 66 | | threatsParentEntry.SetTextContent(new List<string>(new string[] { "Threat Swarms" })); |
| 0 | 67 | | threatsParentEntry.SetIsSelectable(false); |
| | 68 | |
|
| 0 | 69 | | children.Add(masterTitleBar); |
| 0 | 70 | | children.Add(interceptorParentEntry); |
| 0 | 71 | | children.Add(submunitionsParentEntry); |
| 0 | 72 | | children.Add(threatsParentEntry); |
| 0 | 73 | | return children; |
| 0 | 74 | | } |
| | 75 | |
|
| 0 | 76 | | private void OnClickSwarmEntry(object swarm) { |
| 0 | 77 | | List<(Agent, bool)> swarmTuple = (List<(Agent, bool)>)swarm; |
| 0 | 78 | | List<Agent> swarmAgents = swarmTuple.ConvertAll(agent => agent.Item1); |
| 0 | 79 | | CameraController.Instance.SnapToSwarm(swarmAgents, false); |
| 0 | 80 | | } |
| | 81 | |
|
| 0 | 82 | | private List<UISelectableEntry> CreateInterceptorSwarmEntries(List<List<(Agent, bool)>> swarms) { |
| 0 | 83 | | List<UISelectableEntry> children = new List<UISelectableEntry>(); |
| 0 | 84 | | foreach (var swarm in swarms) { |
| 0 | 85 | | string swarmTitle = SimManager.Instance.GenerateInterceptorSwarmTitle(swarm); |
| 0 | 86 | | int activeCount = swarm.Count(agent => agent.Item2); |
| 0 | 87 | | UISelectableEntry entry = CreateSelectableEntry(); |
| 0 | 88 | | entry.SetTextContent(new List<string>(new string[] { swarmTitle, activeCount.ToString() })); |
| 0 | 89 | | entry.SetParent(this); |
| 0 | 90 | | entry.SetClickCallback(OnClickSwarmEntry, swarm); |
| 0 | 91 | | children.Add(entry); |
| 0 | 92 | | } |
| 0 | 93 | | return children; |
| 0 | 94 | | } |
| | 95 | |
|
| 0 | 96 | | private List<UISelectableEntry> CreateSubmunitionsSwarmEntries(List<List<(Agent, bool)>> swarms) { |
| 0 | 97 | | List<UISelectableEntry> children = new List<UISelectableEntry>(); |
| 0 | 98 | | foreach (var swarm in swarms) { |
| 0 | 99 | | int interceptorSwarmIndex = |
| | 100 | | SimManager.Instance.LookupSubmunitionSwarnIndexInInterceptorSwarm(swarm); |
| 0 | 101 | | string swarmTitle = SimManager.Instance.GenerateSubmunitionsSwarmTitle(swarm); |
| 0 | 102 | | int activeCount = swarm.Count(agent => agent.Item2); |
| 0 | 103 | | UISelectableEntry entry = CreateSelectableEntry(); |
| 0 | 104 | | entry.SetTextContent(new List<string>(new string[] { swarmTitle, activeCount.ToString() })); |
| 0 | 105 | | entry.SetParent(this); |
| 0 | 106 | | entry.SetClickCallback(OnClickSwarmEntry, swarm); |
| 0 | 107 | | children.Add(entry); |
| 0 | 108 | | } |
| 0 | 109 | | return children; |
| 0 | 110 | | } |
| | 111 | |
|
| 0 | 112 | | private List<UISelectableEntry> CreateThreatSwarmEntries(List<List<(Agent, bool)>> swarms) { |
| 0 | 113 | | List<UISelectableEntry> children = new List<UISelectableEntry>(); |
| 0 | 114 | | foreach (var swarm in swarms) { |
| 0 | 115 | | string swarmTitle = SimManager.Instance.GenerateThreatSwarmTitle(swarm); |
| 0 | 116 | | int activeCount = swarm.Count(agent => agent.Item2); |
| 0 | 117 | | UISelectableEntry entry = CreateSelectableEntry(); |
| 0 | 118 | | entry.SetTextContent(new List<string>(new string[] { swarmTitle, activeCount.ToString() })); |
| 0 | 119 | | entry.SetParent(this); |
| 0 | 120 | | entry.SetClickCallback(OnClickSwarmEntry, swarm); |
| 0 | 121 | | children.Add(entry); |
| 0 | 122 | | } |
| 0 | 123 | | return children; |
| 0 | 124 | | } |
| | 125 | |
|
| 0 | 126 | | private void RegisterInterceptorSwarmChanged(List<List<(Agent, bool)>> swarms) { |
| 0 | 127 | | if (isActiveAndEnabled) { |
| 0 | 128 | | interceptorSwarms = swarms; |
| 0 | 129 | | RedrawFullDialog(); |
| 0 | 130 | | } |
| 0 | 131 | | } |
| | 132 | |
|
| 0 | 133 | | private void RegisterSubmunitionsSwarmChanged(List<List<(Agent, bool)>> swarms) { |
| 0 | 134 | | if (isActiveAndEnabled) { |
| 0 | 135 | | submunitionsSwarms = swarms; |
| 0 | 136 | | RedrawFullDialog(); |
| 0 | 137 | | } |
| 0 | 138 | | } |
| | 139 | |
|
| 0 | 140 | | private void RegisterThreatSwarmChanged(List<List<(Agent, bool)>> swarms) { |
| 0 | 141 | | if (isActiveAndEnabled) { |
| 0 | 142 | | threatsSwarms = swarms; |
| 0 | 143 | | RedrawFullDialog(); |
| 0 | 144 | | } |
| 0 | 145 | | } |
| | 146 | |
|
| 0 | 147 | | private void RegisterSimulationEnded() { |
| 0 | 148 | | if (isActiveAndEnabled) { |
| 0 | 149 | | ClearDialogEntries(); |
| 0 | 150 | | } |
| 0 | 151 | | } |
| | 152 | |
|
| 0 | 153 | | protected override void OnEnable() { |
| 0 | 154 | | base.OnEnable(); |
| 0 | 155 | | if (!wasStarted) { |
| 0 | 156 | | base.Start(); |
| 0 | 157 | | InitDialog(); |
| 0 | 158 | | wasStarted = true; |
| 0 | 159 | | } |
| 0 | 160 | | interceptorSwarms = SimManager.Instance.GetInterceptorSwarms(); |
| 0 | 161 | | submunitionsSwarms = SimManager.Instance.GetSubmunitionsSwarms(); |
| 0 | 162 | | threatsSwarms = SimManager.Instance.GetThreatSwarms(); |
| 0 | 163 | | RedrawFullDialog(); |
| 0 | 164 | | } |
| | 165 | | // Update is called once per frame |
| 0 | 166 | | void Update() {} |
| | 167 | | } |