< Summary

Class:SwarmStatusDialog
Assembly:bamlab.micromissiles
File(s):/github/workspace/Assets/Scripts/UI/Dialogs/SwarmStatusDialog.cs
Covered lines:4
Uncovered lines:129
Coverable lines:133
Total lines:167
Line coverage:3% (4 of 133)
Covered branches:0
Total branches:0
Covered methods:1
Total methods:16
Method coverage:6.2% (1 of 16)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
SwarmStatusDialog()0%110100%
Awake()0%2100%
Start()0%2100%
InitDialog()0%30500%
RedrawFullDialog()0%2100%
CreateParentEntries()0%2100%
OnClickSwarmEntry(...)0%6200%
CreateInterceptorSwarmEntries(...)0%12300%
CreateSubmunitionsSwarmEntries(...)0%12300%
CreateThreatSwarmEntries(...)0%12300%
RegisterInterceptorSwarmChanged(...)0%6200%
RegisterSubmunitionsSwarmChanged(...)0%6200%
RegisterThreatSwarmChanged(...)0%6200%
RegisterSimulationEnded()0%6200%
OnEnable()0%6200%
Update()0%2100%

File(s)

/github/workspace/Assets/Scripts/UI/Dialogs/SwarmStatusDialog.cs

#LineLine coverage
 1using System.Collections;
 2using System.Collections.Generic;
 3using UnityEngine;
 4using System.Linq;
 5using UnityEngine.AI;
 6public class SwarmStatusDialog : UIDialog {
 7  // Start is called before the first frame update
 8
 19  bool wasStarted = false;
 10
 111  private List<List<(Agent, bool)>> interceptorSwarms = new List<List<(Agent, bool)>>();
 112  private List<List<(Agent, bool)>> submunitionsSwarms = new List<List<(Agent, bool)>>();
 113  private List<List<(Agent, bool)>> threatsSwarms = new List<List<(Agent, bool)>>();
 014  protected void Awake() {}
 15
 016  public override void Start() {
 017    base.Start();
 18
 019    InitDialog();
 020    wasStarted = true;
 021  }
 22
 023  public void InitDialog() {
 024    SimManager.Instance.OnInterceptorSwarmChanged += RegisterInterceptorSwarmChanged;
 025    SimManager.Instance.OnSubmunitionsSwarmChanged += RegisterSubmunitionsSwarmChanged;
 026    SimManager.Instance.OnThreatSwarmChanged += RegisterThreatSwarmChanged;
 027    SimManager.Instance.OnSimulationEnded += RegisterSimulationEnded;
 028    RedrawFullDialog();
 29
 030    AddDialogTab("All", () => {});
 031    AddDialogTab("Interceptors", () => {});
 032    AddDialogTab("Submunitions", () => {});
 033    AddDialogTab("Threats", () => {});
 034  }
 35
 036  private void RedrawFullDialog() {
 037    ClearDialogEntries();
 038    List<UISelectableEntry> parents = CreateParentEntries();
 039    List<UISelectableEntry> interceptorChildren = CreateInterceptorSwarmEntries(interceptorSwarms);
 040    List<UISelectableEntry> submunitionsChildren =
 41        CreateSubmunitionsSwarmEntries(submunitionsSwarms);
 042    List<UISelectableEntry> threatsChildren = CreateThreatSwarmEntries(threatsSwarms);
 043    parents[1].SetChildEntries(interceptorChildren);
 044    parents[2].SetChildEntries(submunitionsChildren);
 045    parents[3].SetChildEntries(threatsChildren);
 046    SetDialogEntries(parents);
 047  }
 48
 049  private List<UISelectableEntry> CreateParentEntries() {
 050    List<UISelectableEntry> children = new List<UISelectableEntry>();
 51
 052    UISelectableEntry masterTitleBar = CreateSelectableEntry();
 053    masterTitleBar.SetTextContent(new List<string>(new string[] { "Swarm Name", "Active Agents" }));
 054    masterTitleBar.SetIsSelectable(false);
 55
 056    UISelectableEntry interceptorParentEntry = CreateSelectableEntry();
 057    interceptorParentEntry.SetTextContent(new List<string>(new string[] { "Interceptor Swarms" }));
 058    interceptorParentEntry.SetIsSelectable(false);
 59
 060    UISelectableEntry submunitionsParentEntry = CreateSelectableEntry();
 061    submunitionsParentEntry.SetTextContent(
 62        new List<string>(new string[] { "Submunitions Swarms" }));
 063    submunitionsParentEntry.SetIsSelectable(false);
 64
 065    UISelectableEntry threatsParentEntry = CreateSelectableEntry();
 066    threatsParentEntry.SetTextContent(new List<string>(new string[] { "Threat Swarms" }));
 067    threatsParentEntry.SetIsSelectable(false);
 68
 069    children.Add(masterTitleBar);
 070    children.Add(interceptorParentEntry);
 071    children.Add(submunitionsParentEntry);
 072    children.Add(threatsParentEntry);
 073    return children;
 074  }
 75
 076  private void OnClickSwarmEntry(object swarm) {
 077    List<(Agent, bool)> swarmTuple = (List<(Agent, bool)>)swarm;
 078    List<Agent> swarmAgents = swarmTuple.ConvertAll(agent => agent.Item1);
 079    CameraController.Instance.SnapToSwarm(swarmAgents, false);
 080  }
 81
 082  private List<UISelectableEntry> CreateInterceptorSwarmEntries(List<List<(Agent, bool)>> swarms) {
 083    List<UISelectableEntry> children = new List<UISelectableEntry>();
 084    foreach (var swarm in swarms) {
 085      string swarmTitle = SimManager.Instance.GenerateInterceptorSwarmTitle(swarm);
 086      int activeCount = swarm.Count(agent => agent.Item2);
 087      UISelectableEntry entry = CreateSelectableEntry();
 088      entry.SetTextContent(new List<string>(new string[] { swarmTitle, activeCount.ToString() }));
 089      entry.SetParent(this);
 090      entry.SetClickCallback(OnClickSwarmEntry, swarm);
 091      children.Add(entry);
 092    }
 093    return children;
 094  }
 95
 096  private List<UISelectableEntry> CreateSubmunitionsSwarmEntries(List<List<(Agent, bool)>> swarms) {
 097    List<UISelectableEntry> children = new List<UISelectableEntry>();
 098    foreach (var swarm in swarms) {
 099      int interceptorSwarmIndex =
 100          SimManager.Instance.LookupSubmunitionSwarnIndexInInterceptorSwarm(swarm);
 0101      string swarmTitle = SimManager.Instance.GenerateSubmunitionsSwarmTitle(swarm);
 0102      int activeCount = swarm.Count(agent => agent.Item2);
 0103      UISelectableEntry entry = CreateSelectableEntry();
 0104      entry.SetTextContent(new List<string>(new string[] { swarmTitle, activeCount.ToString() }));
 0105      entry.SetParent(this);
 0106      entry.SetClickCallback(OnClickSwarmEntry, swarm);
 0107      children.Add(entry);
 0108    }
 0109    return children;
 0110  }
 111
 0112  private List<UISelectableEntry> CreateThreatSwarmEntries(List<List<(Agent, bool)>> swarms) {
 0113    List<UISelectableEntry> children = new List<UISelectableEntry>();
 0114    foreach (var swarm in swarms) {
 0115      string swarmTitle = SimManager.Instance.GenerateThreatSwarmTitle(swarm);
 0116      int activeCount = swarm.Count(agent => agent.Item2);
 0117      UISelectableEntry entry = CreateSelectableEntry();
 0118      entry.SetTextContent(new List<string>(new string[] { swarmTitle, activeCount.ToString() }));
 0119      entry.SetParent(this);
 0120      entry.SetClickCallback(OnClickSwarmEntry, swarm);
 0121      children.Add(entry);
 0122    }
 0123    return children;
 0124  }
 125
 0126  private void RegisterInterceptorSwarmChanged(List<List<(Agent, bool)>> swarms) {
 0127    if (isActiveAndEnabled) {
 0128      interceptorSwarms = swarms;
 0129      RedrawFullDialog();
 0130    }
 0131  }
 132
 0133  private void RegisterSubmunitionsSwarmChanged(List<List<(Agent, bool)>> swarms) {
 0134    if (isActiveAndEnabled) {
 0135      submunitionsSwarms = swarms;
 0136      RedrawFullDialog();
 0137    }
 0138  }
 139
 0140  private void RegisterThreatSwarmChanged(List<List<(Agent, bool)>> swarms) {
 0141    if (isActiveAndEnabled) {
 0142      threatsSwarms = swarms;
 0143      RedrawFullDialog();
 0144    }
 0145  }
 146
 0147  private void RegisterSimulationEnded() {
 0148    if (isActiveAndEnabled) {
 0149      ClearDialogEntries();
 0150    }
 0151  }
 152
 0153  protected override void OnEnable() {
 0154    base.OnEnable();
 0155    if (!wasStarted) {
 0156      base.Start();
 0157      InitDialog();
 0158      wasStarted = true;
 0159    }
 0160    interceptorSwarms = SimManager.Instance.GetInterceptorSwarms();
 0161    submunitionsSwarms = SimManager.Instance.GetSubmunitionsSwarms();
 0162    threatsSwarms = SimManager.Instance.GetThreatSwarms();
 0163    RedrawFullDialog();
 0164  }
 165  // Update is called once per frame
 0166  void Update() {}
 167}