| | 1 | | using System.Collections; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.IO; |
| | 4 | | using UnityEngine; |
| | 5 | | using UnityEngine.UI; |
| | 6 | | using TMPro; |
| | 7 | | using System; |
| | 8 | |
|
| | 9 | | public class UIManager : MonoBehaviour { |
| 0 | 10 | | public static UIManager Instance { get; private set; } |
| | 11 | |
|
| | 12 | | [SerializeField] |
| | 13 | | private GameObject _agentStatusPanel; |
| | 14 | | [SerializeField] |
| | 15 | | private GameObject _configSelectorPanel; |
| | 16 | | private TMP_Dropdown _configDropdown; |
| | 17 | | public TextMeshProUGUI agentPanelText; |
| | 18 | | public TextMeshProUGUI simTimeText; |
| | 19 | | public TextMeshProUGUI interceptorCostText; |
| | 20 | | public TextMeshProUGUI threatCostText; |
| | 21 | | public TextMeshProUGUI netCostText; |
| | 22 | |
|
| | 23 | | public TextMeshProUGUI intrHitTextHandle; |
| | 24 | | public TextMeshProUGUI intrMissTextHandle; |
| | 25 | | public TextMeshProUGUI intrRemainTextHandle; |
| | 26 | | public TextMeshProUGUI thrtRemainTextHandle; |
| | 27 | |
|
| | 28 | | public TextMeshProUGUI actionMessageTextHandle; |
| | 29 | | public TextMeshProUGUI pActionMessageTextHandle; |
| | 30 | | public TextMeshProUGUI ppActionMessageTextHandle; |
| | 31 | |
|
| 0 | 32 | | private int _intrHitCount = 0; |
| 0 | 33 | | private int _intrMissCount = 0; |
| 0 | 34 | | private int _intrRemainCount = 0; |
| 0 | 35 | | private int _thrtRemainCount = 0; |
| | 36 | | public TMP_FontAsset Font; |
| | 37 | |
|
| 0 | 38 | | private UIMode curMode = UIMode.NONE; |
| | 39 | |
|
| | 40 | | // Start is called before the first frame update |
| 0 | 41 | | void Awake() { |
| | 42 | | // singleton |
| 0 | 43 | | if (Instance == null) |
| 0 | 44 | | Instance = this; |
| | 45 | | else |
| 0 | 46 | | Destroy(gameObject); |
| 0 | 47 | | } |
| | 48 | |
|
| 0 | 49 | | void Start() { |
| 0 | 50 | | _configSelectorPanel.SetActive(false); |
| 0 | 51 | | SetupConfigSelectorPanel(); |
| | 52 | | // inputManager = InputManager.Instance; |
| | 53 | | // worldManager = WorldManager.Instance; |
| 0 | 54 | | SimManager.Instance.OnNewInterceptor += RegisterNewInterceptor; |
| 0 | 55 | | SimManager.Instance.OnNewThreat += RegisterNewThreat; |
| 0 | 56 | | SimManager.Instance.OnSimulationEnded += RegisterSimulationEnded; |
| 0 | 57 | | actionMessageTextHandle.text = ""; |
| 0 | 58 | | pActionMessageTextHandle.text = ""; |
| 0 | 59 | | ppActionMessageTextHandle.text = ""; |
| 0 | 60 | | } |
| | 61 | |
|
| 0 | 62 | | public void LogAction(string message, Color color) { |
| | 63 | | // Shift existing messages to older slots with faded colors |
| 0 | 64 | | ppActionMessageTextHandle.text = pActionMessageTextHandle.text; |
| 0 | 65 | | ppActionMessageTextHandle.color = pActionMessageTextHandle.color * 0.5f; // Fade color by 50% |
| | 66 | |
|
| 0 | 67 | | pActionMessageTextHandle.text = actionMessageTextHandle.text; |
| 0 | 68 | | pActionMessageTextHandle.color = actionMessageTextHandle.color * 0.75f; // Fade color by 25% |
| | 69 | |
|
| | 70 | | // Set new message |
| 0 | 71 | | actionMessageTextHandle.text = message; |
| 0 | 72 | | actionMessageTextHandle.color = color; |
| 0 | 73 | | } |
| | 74 | |
|
| 0 | 75 | | public void LogActionMessage(string message) { |
| 0 | 76 | | LogAction(message, Color.white); |
| 0 | 77 | | } |
| | 78 | |
|
| 0 | 79 | | public void LogActionWarning(string message) { |
| 0 | 80 | | LogAction(message, Color.yellow); |
| 0 | 81 | | } |
| | 82 | |
|
| 0 | 83 | | public void LogActionError(string message) { |
| 0 | 84 | | LogAction(message, Color.red); |
| 0 | 85 | | } |
| | 86 | |
|
| 0 | 87 | | public void ToggleConfigSelectorPanel() { |
| 0 | 88 | | _configSelectorPanel.SetActive(!_configSelectorPanel.activeSelf); |
| 0 | 89 | | } |
| | 90 | |
|
| 0 | 91 | | private void SetupConfigSelectorPanel() { |
| 0 | 92 | | _configSelectorPanel.GetComponentInChildren<Button>().onClick.AddListener( |
| 0 | 93 | | delegate { LoadSelectedConfig(); }); |
| 0 | 94 | | _configDropdown = _configSelectorPanel.GetComponentInChildren<TMP_Dropdown>(); |
| 0 | 95 | | PopulateConfigDropdown(); |
| 0 | 96 | | } |
| | 97 | |
|
| 0 | 98 | | private void PopulateConfigDropdown() { |
| 0 | 99 | | _configDropdown.ClearOptions(); |
| 0 | 100 | | string configPath = Path.Combine(Application.streamingAssetsPath, "Configs"); |
| 0 | 101 | | string[] configFiles = Directory.GetFiles(configPath, "*.json"); |
| | 102 | |
|
| 0 | 103 | | List<string> configFileNames = new List<string>(); |
| 0 | 104 | | foreach (string configFile in configFiles) { |
| 0 | 105 | | configFileNames.Add(Path.GetFileName(configFile)); |
| 0 | 106 | | } |
| 0 | 107 | | _configDropdown.AddOptions(configFileNames); |
| 0 | 108 | | } |
| 0 | 109 | | private void LoadSelectedConfig() { |
| 0 | 110 | | string selectedConfig = _configDropdown.options[_configDropdown.value].text; |
| 0 | 111 | | SimManager.Instance.LoadNewConfig(selectedConfig); |
| 0 | 112 | | _configSelectorPanel.SetActive(false); |
| | 113 | | // if(!InputManager.Instance.mouseActive){ |
| | 114 | | // InputManager.Instance.mouseActive = true; |
| | 115 | | // } |
| 0 | 116 | | } |
| | 117 | |
|
| 0 | 118 | | public void SetUIMode(UIMode mode) { |
| 0 | 119 | | curMode = mode; |
| 0 | 120 | | } |
| | 121 | |
|
| 0 | 122 | | public UIMode GetUIMode() { |
| 0 | 123 | | return curMode; |
| 0 | 124 | | } |
| | 125 | |
|
| 0 | 126 | | public void SetAgentPanelText(string text) { |
| 0 | 127 | | agentPanelText.text = text; |
| 0 | 128 | | } |
| | 129 | |
|
| 0 | 130 | | public string GetSwarmPanelText() { |
| 0 | 131 | | return agentPanelText.text; |
| 0 | 132 | | } |
| | 133 | |
|
| 0 | 134 | | private void UpdateSwarmPanel() { |
| 0 | 135 | | string agentPanelText = ""; |
| 0 | 136 | | foreach (Agent agent in SimManager.Instance.GetActiveAgents()) { |
| 0 | 137 | | string jobText = agent.name + "| Phase: " + agent.GetFlightPhase().ToString(); |
| 0 | 138 | | agentPanelText += jobText + "\n"; |
| 0 | 139 | | } |
| 0 | 140 | | SetAgentPanelText(agentPanelText); |
| 0 | 141 | | } |
| | 142 | |
|
| 0 | 143 | | private void UpdateSimTimeText() { |
| 0 | 144 | | simTimeText.text = |
| | 145 | | "Elapsed Sim Time: " + SimManager.Instance.GetElapsedSimulationTime().ToString("F2"); |
| 0 | 146 | | float expectedSimTimeAdvance = Time.unscaledDeltaTime * Time.timeScale; |
| 0 | 147 | | float actualSimTimeAdvance = Time.deltaTime; |
| | 148 | |
|
| | 149 | | // Allow a small epsilon to account for floating-point precision errors |
| 0 | 150 | | if (actualSimTimeAdvance < expectedSimTimeAdvance - 0.001f) { |
| 0 | 151 | | simTimeText.text += "\nThrottling time to meet physics rate"; |
| 0 | 152 | | } |
| 0 | 153 | | } |
| | 154 | |
|
| 0 | 155 | | private void UpdateTotalCostText() { |
| 0 | 156 | | double interceptorCost = SimManager.Instance.GetCostLaunchedInterceptors(); |
| 0 | 157 | | double threatCost = SimManager.Instance.GetCostDestroyedThreats(); |
| 0 | 158 | | double netCost = interceptorCost - threatCost; |
| | 159 | |
|
| 0 | 160 | | interceptorCostText.text = $"Interceptors\n(launched)\n${FormatCost(interceptorCost)}"; |
| 0 | 161 | | threatCostText.text = $"Threats\n(destroyed)\n${FormatCost(threatCost)}"; |
| 0 | 162 | | netCostText.text = $"Cost\ndifference\n${FormatCost(netCost)}"; |
| 0 | 163 | | if (netCost < 0) { |
| 0 | 164 | | netCostText.color = Color.green; |
| 0 | 165 | | } else { |
| 0 | 166 | | netCostText.color = Color.red; |
| 0 | 167 | | } |
| 0 | 168 | | } |
| | 169 | |
|
| 0 | 170 | | private string FormatCost(double cost) { |
| 0 | 171 | | double absCost = Math.Abs(cost); |
| 0 | 172 | | if (absCost >= 1e9) |
| 0 | 173 | | return $"{cost / 1e9:F2}B"; |
| 0 | 174 | | if (absCost >= 1e6) |
| 0 | 175 | | return $"{cost / 1e6:F2}M"; |
| 0 | 176 | | if (absCost >= 1e3) |
| 0 | 177 | | return $"{cost / 1e3:F2}k"; |
| 0 | 178 | | return $"{cost:F2}"; |
| 0 | 179 | | } |
| | 180 | |
|
| 0 | 181 | | private void RegisterSimulationEnded() { |
| 0 | 182 | | _intrRemainCount = 0; |
| 0 | 183 | | _thrtRemainCount = 0; |
| 0 | 184 | | _intrHitCount = 0; |
| 0 | 185 | | _intrMissCount = 0; |
| 0 | 186 | | UpdateSummaryText(); |
| 0 | 187 | | } |
| | 188 | |
|
| 0 | 189 | | private void UpdateSummaryText() { |
| 0 | 190 | | intrRemainTextHandle.text = _intrRemainCount.ToString(); |
| 0 | 191 | | thrtRemainTextHandle.text = _thrtRemainCount.ToString(); |
| 0 | 192 | | intrHitTextHandle.text = _intrHitCount.ToString(); |
| 0 | 193 | | intrMissTextHandle.text = _intrMissCount.ToString(); |
| 0 | 194 | | } |
| | 195 | |
|
| 0 | 196 | | private void RegisterNewInterceptor(Interceptor interceptor) { |
| 0 | 197 | | _intrRemainCount++; |
| 0 | 198 | | interceptor.OnInterceptHit += RegisterInterceptorHit; |
| 0 | 199 | | interceptor.OnInterceptMiss += RegisterInterceptorMiss; |
| 0 | 200 | | interceptor.OnTerminated += RegisterAgentTerminated; |
| 0 | 201 | | UpdateSummaryText(); |
| 0 | 202 | | } |
| | 203 | |
|
| 0 | 204 | | private void RegisterNewThreat(Threat threat) { |
| 0 | 205 | | _thrtRemainCount++; |
| 0 | 206 | | threat.OnTerminated += RegisterAgentTerminated; |
| 0 | 207 | | UpdateSummaryText(); |
| 0 | 208 | | } |
| | 209 | |
|
| 0 | 210 | | private void RegisterAgentTerminated(Agent agent) { |
| 0 | 211 | | if (agent is Interceptor) { |
| 0 | 212 | | _intrRemainCount--; |
| 0 | 213 | | } else if (agent is Threat) { |
| 0 | 214 | | _thrtRemainCount--; |
| 0 | 215 | | } |
| 0 | 216 | | UpdateSummaryText(); |
| 0 | 217 | | } |
| | 218 | |
|
| 0 | 219 | | private void RegisterInterceptorHit(Interceptor interceptor, Threat threat) { |
| 0 | 220 | | _intrHitCount++; |
| 0 | 221 | | UpdateSummaryText(); |
| 0 | 222 | | } |
| | 223 | |
|
| 0 | 224 | | private void RegisterInterceptorMiss(Interceptor interceptor, Threat threat) { |
| 0 | 225 | | _intrMissCount++; |
| 0 | 226 | | UpdateSummaryText(); |
| 0 | 227 | | } |
| | 228 | |
|
| | 229 | | // Update is called once per frame |
| 0 | 230 | | void Update() { |
| | 231 | | // UpdateSwarmPanel(); |
| 0 | 232 | | UpdateSimTimeText(); |
| 0 | 233 | | UpdateTotalCostText(); |
| 0 | 234 | | } |
| | 235 | | } |
| | 236 | |
|
| | 237 | | public enum UIMode { NONE, BUILD, MINE } |