| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.IO; |
| | | 4 | | using TMPro; |
| | | 5 | | using UnityEngine; |
| | | 6 | | using UnityEngine.UI; |
| | | 7 | | |
| | | 8 | | public class UIManager : MonoBehaviour { |
| | 103 | 9 | | public static UIManager Instance { get; private set; } |
| | | 10 | | |
| | | 11 | | [SerializeField] |
| | | 12 | | [Tooltip("The UI panel that renders the camera view for the THREE_DIMENSIONAL mode")] |
| | 1 | 13 | | private GameObject _cameraPanel = null!; |
| | | 14 | | |
| | | 15 | | [SerializeField] |
| | | 16 | | [Tooltip("The UI panel that renders the tactical view for the TACTICAL mode")] |
| | 1 | 17 | | private GameObject _tacticalPanel = null!; |
| | | 18 | | |
| | | 19 | | [SerializeField] |
| | 1 | 20 | | private GameObject _configSelectorPanel = null!; |
| | | 21 | | private TMP_Dropdown _configDropdown; |
| | | 22 | | public TextMeshProUGUI simTimeText; |
| | | 23 | | public TextMeshProUGUI interceptorCostText; |
| | | 24 | | public TextMeshProUGUI threatCostText; |
| | | 25 | | public TextMeshProUGUI netCostText; |
| | | 26 | | |
| | | 27 | | public TextMeshProUGUI interceptorHitTextHandle; |
| | | 28 | | public TextMeshProUGUI interceptorMissTextHandle; |
| | | 29 | | public TextMeshProUGUI interceptorRemainingTextHandle; |
| | | 30 | | public TextMeshProUGUI threatRemainingTextHandle; |
| | | 31 | | |
| | | 32 | | public TextMeshProUGUI actionMessageTextHandle; |
| | | 33 | | public TextMeshProUGUI pActionMessageTextHandle; |
| | | 34 | | public TextMeshProUGUI ppActionMessageTextHandle; |
| | | 35 | | public TextMeshProUGUI ppppActionMessageTextHandle; |
| | | 36 | | public TextMeshProUGUI pppppActionMessageTextHandle; |
| | | 37 | | |
| | | 38 | | public TMP_FontAsset GlobalFont; |
| | 1 | 39 | | private int _numInterceptorHits = 0; |
| | 1 | 40 | | private int _numInterceptorMisses = 0; |
| | 1 | 41 | | private int _numInterceptorsRemaining = 0; |
| | 1 | 42 | | private int _numThreatsRemaining = 0; |
| | | 43 | | |
| | 1 | 44 | | private UIMode _uiMode = UIMode.THREE_DIMENSIONAL; |
| | | 45 | | |
| | | 46 | | public UIMode UIMode { |
| | 66 | 47 | | get => _uiMode; |
| | 1 | 48 | | set { |
| | 1 | 49 | | _uiMode = value; |
| | 1 | 50 | | _cameraPanel.SetActive(_uiMode == UIMode.THREE_DIMENSIONAL); |
| | 1 | 51 | | _tacticalPanel.SetActive(_uiMode == UIMode.TACTICAL); |
| | 1 | 52 | | } |
| | | 53 | | } |
| | | 54 | | |
| | 0 | 55 | | public void ToggleUIMode() { |
| | 0 | 56 | | Array uiModeValues = Enum.GetValues(typeof(UIMode)); |
| | 0 | 57 | | int currentIndex = Array.IndexOf(uiModeValues, UIMode); |
| | 0 | 58 | | int nextIndex = (currentIndex + 1) % uiModeValues.Length; |
| | 0 | 59 | | UIMode = (UIMode)uiModeValues.GetValue(nextIndex); |
| | 0 | 60 | | } |
| | | 61 | | |
| | 0 | 62 | | public void ToggleConfigSelectorPanel() { |
| | 0 | 63 | | _configSelectorPanel.SetActive(!_configSelectorPanel.activeSelf); |
| | 0 | 64 | | } |
| | | 65 | | |
| | 35 | 66 | | public void LogAction(string message, Color color) { |
| | | 67 | | // Shift existing messages to older slots with faded colors. |
| | 35 | 68 | | pppppActionMessageTextHandle.text = ppppActionMessageTextHandle.text; |
| | 35 | 69 | | pppppActionMessageTextHandle.color = |
| | | 70 | | ppppActionMessageTextHandle.color * 0.8f; // Fade color by 20%. |
| | | 71 | | |
| | 35 | 72 | | ppppActionMessageTextHandle.text = ppActionMessageTextHandle.text; |
| | 35 | 73 | | ppppActionMessageTextHandle.color = ppActionMessageTextHandle.color * 0.85f; |
| | | 74 | | |
| | 35 | 75 | | ppActionMessageTextHandle.text = pActionMessageTextHandle.text; |
| | 35 | 76 | | ppActionMessageTextHandle.color = pActionMessageTextHandle.color * 0.85f; |
| | | 77 | | |
| | 35 | 78 | | pActionMessageTextHandle.text = actionMessageTextHandle.text; |
| | 35 | 79 | | pActionMessageTextHandle.color = actionMessageTextHandle.color * 0.9f; |
| | | 80 | | |
| | | 81 | | // Set new message. |
| | 35 | 82 | | actionMessageTextHandle.text = message; |
| | 35 | 83 | | actionMessageTextHandle.color = color; |
| | 35 | 84 | | } |
| | | 85 | | |
| | 35 | 86 | | public void LogActionMessage(string message) { |
| | 35 | 87 | | LogAction(message, Color.white); |
| | 35 | 88 | | } |
| | | 89 | | |
| | 0 | 90 | | public void LogActionWarning(string message) { |
| | 0 | 91 | | LogAction(message, Color.yellow); |
| | 0 | 92 | | } |
| | | 93 | | |
| | 0 | 94 | | public void LogActionError(string message) { |
| | 0 | 95 | | LogAction(message, Color.red); |
| | 0 | 96 | | } |
| | | 97 | | |
| | 1 | 98 | | private void Awake() { |
| | 1 | 99 | | if (Instance != null && Instance != this) { |
| | 0 | 100 | | Destroy(gameObject); |
| | 1 | 101 | | } else { |
| | 1 | 102 | | Instance = this; |
| | 1 | 103 | | } |
| | 1 | 104 | | } |
| | | 105 | | |
| | 1 | 106 | | private void Start() { |
| | 1 | 107 | | UIMode = UIMode.THREE_DIMENSIONAL; |
| | 1 | 108 | | _configSelectorPanel.SetActive(false); |
| | 1 | 109 | | SetupConfigSelectorPanel(); |
| | 1 | 110 | | SimManager.Instance.OnNewInterceptor += RegisterNewInterceptor; |
| | 1 | 111 | | SimManager.Instance.OnNewThreat += RegisterNewThreat; |
| | 1 | 112 | | SimManager.Instance.OnSimulationEnded += RegisterSimulationEnded; |
| | 1 | 113 | | actionMessageTextHandle.text = ""; |
| | 1 | 114 | | pActionMessageTextHandle.text = ""; |
| | 1 | 115 | | ppActionMessageTextHandle.text = ""; |
| | 1 | 116 | | ppppActionMessageTextHandle.text = ""; |
| | 1 | 117 | | pppppActionMessageTextHandle.text = ""; |
| | 1 | 118 | | } |
| | | 119 | | |
| | 33 | 120 | | private void Update() { |
| | 33 | 121 | | UpdateSimTimeText(); |
| | 33 | 122 | | UpdateTotalCostText(); |
| | 33 | 123 | | } |
| | | 124 | | |
| | 1 | 125 | | private void SetupConfigSelectorPanel() { |
| | 1 | 126 | | _configSelectorPanel.GetComponentInChildren<Button>().onClick.AddListener( |
| | 0 | 127 | | delegate { LoadSelectedConfig(); }); |
| | 1 | 128 | | _configDropdown = _configSelectorPanel.GetComponentInChildren<TMP_Dropdown>(); |
| | 1 | 129 | | PopulateConfigDropdown(); |
| | 1 | 130 | | } |
| | | 131 | | |
| | 1 | 132 | | private void PopulateConfigDropdown() { |
| | 1 | 133 | | _configDropdown.ClearOptions(); |
| | 1 | 134 | | string configPath = ConfigLoader.GetStreamingAssetsFilePath("Configs/Simulations"); |
| | 1 | 135 | | string[] configFiles = Directory.GetFiles(configPath, "*.pbtxt"); |
| | | 136 | | |
| | 1 | 137 | | List<string> configFileNames = new List<string>(); |
| | 39 | 138 | | foreach (string configFile in configFiles) { |
| | 12 | 139 | | configFileNames.Add(Path.GetFileName(configFile)); |
| | 12 | 140 | | } |
| | 1 | 141 | | _configDropdown.AddOptions(configFileNames); |
| | 1 | 142 | | } |
| | | 143 | | |
| | 0 | 144 | | private void LoadSelectedConfig() { |
| | 0 | 145 | | string selectedConfig = _configDropdown.options[_configDropdown.value].text; |
| | 0 | 146 | | SimManager.Instance.LoadNewSimulationConfig(selectedConfig); |
| | 0 | 147 | | _configSelectorPanel.SetActive(false); |
| | 0 | 148 | | } |
| | | 149 | | |
| | 33 | 150 | | private void UpdateSimTimeText() { |
| | 33 | 151 | | simTimeText.text = "Elapsed Time: " + SimManager.Instance.ElapsedTime.ToString("F2"); |
| | 33 | 152 | | float expectedSimTimeAdvance = Time.unscaledDeltaTime * Time.timeScale; |
| | 33 | 153 | | float actualSimTimeAdvance = Time.deltaTime; |
| | | 154 | | |
| | | 155 | | // Allow a small epsilon to account for floating-point precision errors. |
| | 55 | 156 | | if (actualSimTimeAdvance < expectedSimTimeAdvance - 0.001f) { |
| | 22 | 157 | | simTimeText.text += "\nThrottling time to meet physics rate"; |
| | 22 | 158 | | } |
| | 33 | 159 | | } |
| | | 160 | | |
| | 33 | 161 | | private void UpdateTotalCostText() { |
| | 33 | 162 | | double interceptorCost = SimManager.Instance.CostLaunchedInterceptors; |
| | 33 | 163 | | double threatCost = SimManager.Instance.CostDestroyedThreats; |
| | 33 | 164 | | double netCost = interceptorCost - threatCost; |
| | | 165 | | |
| | 33 | 166 | | interceptorCostText.text = $"Interceptors\n(launched)\n${FormatCost(interceptorCost)}"; |
| | 33 | 167 | | threatCostText.text = $"Threats\n(destroyed)\n${FormatCost(threatCost)}"; |
| | 33 | 168 | | netCostText.text = $"Cost\ndifference\n${FormatCost(netCost)}"; |
| | 33 | 169 | | if (netCost < 0) { |
| | 0 | 170 | | netCostText.color = Color.green; |
| | 33 | 171 | | } else { |
| | 33 | 172 | | netCostText.color = Color.red; |
| | 33 | 173 | | } |
| | 33 | 174 | | } |
| | | 175 | | |
| | 99 | 176 | | private string FormatCost(double cost) { |
| | 99 | 177 | | double absCost = Math.Abs(cost); |
| | 99 | 178 | | if (absCost >= 1e9) |
| | 0 | 179 | | return $"{cost / 1e9:F2}B"; |
| | 99 | 180 | | if (absCost >= 1e6) |
| | 0 | 181 | | return $"{cost / 1e6:F2}M"; |
| | 99 | 182 | | if (absCost >= 1e3) |
| | 0 | 183 | | return $"{cost / 1e3:F2}k"; |
| | 99 | 184 | | return $"{cost:F2}"; |
| | 99 | 185 | | } |
| | | 186 | | |
| | 980 | 187 | | private void UpdateSummaryText() { |
| | 980 | 188 | | interceptorRemainingTextHandle.text = _numInterceptorsRemaining.ToString(); |
| | 980 | 189 | | threatRemainingTextHandle.text = _numThreatsRemaining.ToString(); |
| | 980 | 190 | | interceptorHitTextHandle.text = _numInterceptorHits.ToString(); |
| | 980 | 191 | | interceptorMissTextHandle.text = _numInterceptorMisses.ToString(); |
| | 980 | 192 | | } |
| | | 193 | | |
| | 14 | 194 | | private void RegisterNewInterceptor(IInterceptor interceptor) { |
| | 14 | 195 | | ++_numInterceptorsRemaining; |
| | 14 | 196 | | interceptor.OnHit += RegisterInterceptorHit; |
| | 14 | 197 | | interceptor.OnMiss += RegisterInterceptorMiss; |
| | 14 | 198 | | interceptor.OnTerminated += RegisterAgentTerminated; |
| | 14 | 199 | | UpdateSummaryText(); |
| | 14 | 200 | | } |
| | | 201 | | |
| | 955 | 202 | | private void RegisterNewThreat(IThreat threat) { |
| | 955 | 203 | | ++_numThreatsRemaining; |
| | 955 | 204 | | threat.OnTerminated += RegisterAgentTerminated; |
| | 955 | 205 | | UpdateSummaryText(); |
| | 955 | 206 | | } |
| | | 207 | | |
| | 0 | 208 | | private void RegisterInterceptorHit(IInterceptor interceptor) { |
| | 0 | 209 | | ++_numInterceptorHits; |
| | 0 | 210 | | UpdateSummaryText(); |
| | 0 | 211 | | } |
| | | 212 | | |
| | 0 | 213 | | private void RegisterInterceptorMiss(IInterceptor interceptor) { |
| | 0 | 214 | | ++_numInterceptorMisses; |
| | 0 | 215 | | UpdateSummaryText(); |
| | 0 | 216 | | } |
| | | 217 | | |
| | 0 | 218 | | private void RegisterAgentTerminated(IAgent agent) { |
| | 0 | 219 | | if (agent is IInterceptor) { |
| | 0 | 220 | | --_numInterceptorsRemaining; |
| | 0 | 221 | | } else if (agent is IThreat) { |
| | 0 | 222 | | --_numThreatsRemaining; |
| | 0 | 223 | | } |
| | 0 | 224 | | UpdateSummaryText(); |
| | 0 | 225 | | } |
| | | 226 | | |
| | 11 | 227 | | private void RegisterSimulationEnded() { |
| | 11 | 228 | | _numInterceptorsRemaining = 0; |
| | 11 | 229 | | _numThreatsRemaining = 0; |
| | 11 | 230 | | _numInterceptorHits = 0; |
| | 11 | 231 | | _numInterceptorMisses = 0; |
| | 11 | 232 | | UpdateSummaryText(); |
| | 11 | 233 | | } |
| | | 234 | | } |