| | | 1 | | using System.Collections; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using UnityEngine; |
| | | 4 | | |
| | | 5 | | public class TacticalPanel : MonoBehaviour { |
| | | 6 | | // Symbol position update period in seconds. |
| | | 7 | | private const float _symbolsUpdatePeriod = 0.1f; // 10 Hz |
| | | 8 | | |
| | | 9 | | // Keyboard pan speed. |
| | | 10 | | private const float _keyboardPanSpeed = 500f; |
| | | 11 | | |
| | | 12 | | // Zoom speed. |
| | | 13 | | private const float _zoomSpeed = 10f; |
| | | 14 | | |
| | | 15 | | [SerializeField] |
| | | 16 | | [Tooltip("The UI group that contains the radar symbology elements")] |
| | 1 | 17 | | private GameObject _radarUIGroup = null!; |
| | | 18 | | |
| | | 19 | | private RectTransform _radarUIGroupRectTransform; |
| | | 20 | | |
| | | 21 | | private TacticalPolarGridGraphic _polarGridGraphic; |
| | | 22 | | |
| | | 23 | | // Coroutine to update the symbols. |
| | | 24 | | private Coroutine _symbolsCoroutine; |
| | | 25 | | |
| | | 26 | | // Dictionary from each agent to its tactical symbol. |
| | 1 | 27 | | private Dictionary<IAgent, GameObject> _symbols = new Dictionary<IAgent, GameObject>(); |
| | | 28 | | |
| | | 29 | | // Origin symbol. |
| | | 30 | | // TODO(titan): This origin symbol is needed until an asset agent is implemented. |
| | | 31 | | private GameObject _origin; |
| | | 32 | | |
| | 2 | 33 | | public static TacticalPanel Instance { get; private set; } |
| | | 34 | | |
| | 0 | 35 | | public void Pan(in Vector2 delta) { |
| | 0 | 36 | | Vector3 currentPosition = _radarUIGroupRectTransform.localPosition; |
| | 0 | 37 | | _radarUIGroupRectTransform.localPosition = |
| | | 38 | | new Vector3(currentPosition.x + delta.x, currentPosition.y + delta.y, currentPosition.z); |
| | 0 | 39 | | } |
| | | 40 | | |
| | 0 | 41 | | public void PanWithKeyboard(in Vector2 direction) { |
| | 0 | 42 | | Vector2 delta = direction * _keyboardPanSpeed * Time.unscaledDeltaTime; |
| | 0 | 43 | | Pan(delta); |
| | 0 | 44 | | } |
| | | 45 | | |
| | 0 | 46 | | public void ZoomIn(float amount) { |
| | 0 | 47 | | AdjustRadarScale(amount * _zoomSpeed); |
| | 0 | 48 | | } |
| | | 49 | | |
| | 0 | 50 | | public void ZoomOut(float amount) { |
| | 0 | 51 | | AdjustRadarScale(-amount * _zoomSpeed); |
| | 0 | 52 | | } |
| | | 53 | | |
| | 0 | 54 | | public void CycleRangeUp() { |
| | 0 | 55 | | _polarGridGraphic.CycleRangeUp(); |
| | 0 | 56 | | } |
| | | 57 | | |
| | 0 | 58 | | public void CycleRangeDown() { |
| | 0 | 59 | | _polarGridGraphic.CycleRangeDown(); |
| | 0 | 60 | | } |
| | | 61 | | |
| | 1 | 62 | | private void Awake() { |
| | 1 | 63 | | if (Instance != null && Instance != this) { |
| | 0 | 64 | | Destroy(gameObject); |
| | 0 | 65 | | return; |
| | | 66 | | } |
| | 1 | 67 | | Instance = this; |
| | | 68 | | |
| | 1 | 69 | | _radarUIGroupRectTransform = _radarUIGroup.GetComponent<RectTransform>(); |
| | 1 | 70 | | _polarGridGraphic = _radarUIGroup.GetComponent<TacticalPolarGridGraphic>(); |
| | 1 | 71 | | } |
| | | 72 | | |
| | 0 | 73 | | private void Start() { |
| | 0 | 74 | | _radarUIGroupRectTransform.localScale = Vector3.one; |
| | 0 | 75 | | _radarUIGroupRectTransform.localPosition = Vector3.zero; |
| | | 76 | | |
| | 0 | 77 | | SimManager.Instance.OnSimulationEnded += RegisterSimulationEnded; |
| | 0 | 78 | | SimManager.Instance.OnNewInterceptor += RegisterNewAgent; |
| | 0 | 79 | | SimManager.Instance.OnNewThreat += RegisterNewAgent; |
| | | 80 | | |
| | 0 | 81 | | InitializeOrigin(); |
| | 0 | 82 | | } |
| | | 83 | | |
| | 0 | 84 | | private void OnDestroy() { |
| | 0 | 85 | | if (_symbolsCoroutine != null) { |
| | 0 | 86 | | StopCoroutine(_symbolsCoroutine); |
| | 0 | 87 | | _symbolsCoroutine = null; |
| | 0 | 88 | | } |
| | 0 | 89 | | } |
| | | 90 | | |
| | 1 | 91 | | private void OnEnable() { |
| | 1 | 92 | | if (SimManager.Instance != null) { |
| | 0 | 93 | | InitializeAgents(SimManager.Instance.Interceptors); |
| | 0 | 94 | | InitializeAgents(SimManager.Instance.Threats); |
| | 0 | 95 | | } |
| | 1 | 96 | | _symbolsCoroutine = StartCoroutine(SymbolsManager(_symbolsUpdatePeriod)); |
| | 1 | 97 | | } |
| | | 98 | | |
| | 1 | 99 | | private void OnDisable() { |
| | 2 | 100 | | if (_symbolsCoroutine != null) { |
| | 1 | 101 | | StopCoroutine(_symbolsCoroutine); |
| | 1 | 102 | | _symbolsCoroutine = null; |
| | 1 | 103 | | } |
| | 1 | 104 | | DestroyAllSymbols(); |
| | 1 | 105 | | } |
| | | 106 | | |
| | 0 | 107 | | private void RegisterSimulationEnded() { |
| | 0 | 108 | | DestroyAllSymbols(); |
| | 0 | 109 | | } |
| | | 110 | | |
| | 0 | 111 | | public void RegisterNewAgent(IAgent agent) { |
| | 0 | 112 | | agent.OnTerminated += DestroySymbol; |
| | 0 | 113 | | CreateSymbol(agent); |
| | 0 | 114 | | } |
| | | 115 | | |
| | 1 | 116 | | private IEnumerator SymbolsManager(float period) { |
| | 2 | 117 | | while (true) { |
| | 1 | 118 | | UpdateSymbols(); |
| | 1 | 119 | | yield return new WaitForSeconds(period); |
| | 0 | 120 | | } |
| | | 121 | | } |
| | | 122 | | |
| | 0 | 123 | | private void InitializeOrigin() { |
| | 0 | 124 | | GameObject origin = CreateSymbolPrefab(); |
| | 0 | 125 | | origin.GetComponent<TacticalSymbol>().SetSprite("friendly_carrier_present"); |
| | 0 | 126 | | origin.GetComponent<TacticalSymbol>().DisableDirectionArrow(); |
| | 0 | 127 | | _origin = origin; |
| | 0 | 128 | | } |
| | | 129 | | |
| | 0 | 130 | | private void InitializeAgents(IEnumerable<IAgent> agents) { |
| | 0 | 131 | | foreach (var agent in agents) { |
| | 0 | 132 | | agent.OnTerminated += DestroySymbol; |
| | 0 | 133 | | CreateSymbol(agent); |
| | 0 | 134 | | } |
| | 0 | 135 | | } |
| | | 136 | | |
| | 0 | 137 | | private GameObject CreateSymbolPrefab() { |
| | 0 | 138 | | return Instantiate(Resources.Load<GameObject>("Prefabs/Symbols/Symbol"), |
| | | 139 | | _radarUIGroupRectTransform); |
| | 0 | 140 | | } |
| | | 141 | | |
| | 0 | 142 | | private void CreateSymbol(IAgent agent) { |
| | 0 | 143 | | GameObject symbol = CreateSymbolPrefab(); |
| | 0 | 144 | | TacticalSymbol tacticalSymbol = symbol.GetComponent<TacticalSymbol>(); |
| | | 145 | | |
| | | 146 | | // Set common properties. |
| | 0 | 147 | | tacticalSymbol.SetSprite(agent.StaticConfig.VisualizationConfig?.SymbolPresent); |
| | 0 | 148 | | tacticalSymbol.SetDirectionArrowRotation(Mathf.Atan2(agent.Velocity.z, agent.Velocity.x) * |
| | | 149 | | Mathf.Rad2Deg); |
| | | 150 | | |
| | | 151 | | // Set type-specific properties. |
| | 0 | 152 | | switch (agent.StaticConfig.AgentType) { |
| | | 153 | | case Configs.AgentType.Vessel: |
| | 0 | 154 | | case Configs.AgentType.ShoreBattery: { |
| | 0 | 155 | | tacticalSymbol.SetType("Launcher"); |
| | 0 | 156 | | break; |
| | | 157 | | } |
| | 0 | 158 | | case Configs.AgentType.CarrierInterceptor: { |
| | 0 | 159 | | tacticalSymbol.SetType("Carrier"); |
| | 0 | 160 | | break; |
| | | 161 | | } |
| | 0 | 162 | | case Configs.AgentType.MissileInterceptor: { |
| | 0 | 163 | | tacticalSymbol.SetType("Interceptor"); |
| | 0 | 164 | | break; |
| | | 165 | | } |
| | 0 | 166 | | case Configs.AgentType.FixedWingThreat: { |
| | 0 | 167 | | tacticalSymbol.SetType("Fixed-Wing Threat"); |
| | 0 | 168 | | break; |
| | | 169 | | } |
| | 0 | 170 | | case Configs.AgentType.RotaryWingThreat: { |
| | 0 | 171 | | tacticalSymbol.SetType("Rotary-Wing Threat"); |
| | 0 | 172 | | break; |
| | | 173 | | } |
| | 0 | 174 | | default: { |
| | 0 | 175 | | Debug.LogError( |
| | | 176 | | $"Unknown agent type {agent.StaticConfig.AgentType} for agent {agent.gameObject.name}."); |
| | 0 | 177 | | tacticalSymbol.SetType("Unknown"); |
| | 0 | 178 | | break; |
| | | 179 | | } |
| | | 180 | | } |
| | 0 | 181 | | tacticalSymbol.SetUniqueDesignator(agent.gameObject.name); |
| | 0 | 182 | | UpdateAgentSymbol(symbol, agent); |
| | 0 | 183 | | _symbols.Add(agent, symbol); |
| | 0 | 184 | | } |
| | | 185 | | |
| | 0 | 186 | | private void DestroySymbol(IAgent agent) { |
| | 0 | 187 | | if (_symbols.TryGetValue(agent, out GameObject symbol)) { |
| | 0 | 188 | | Destroy(symbol); |
| | 0 | 189 | | _symbols.Remove(agent); |
| | 0 | 190 | | } |
| | 0 | 191 | | } |
| | | 192 | | |
| | 1 | 193 | | private void DestroyAllSymbols() { |
| | 3 | 194 | | foreach (var symbol in _symbols.Values) { |
| | 0 | 195 | | Destroy(symbol); |
| | 0 | 196 | | } |
| | 1 | 197 | | _symbols.Clear(); |
| | 1 | 198 | | } |
| | | 199 | | |
| | 1 | 200 | | private void UpdateSymbols() { |
| | 1 | 201 | | if (SimManager.Instance != null) { |
| | 0 | 202 | | UpdateAgentSymbols(SimManager.Instance.Interceptors); |
| | 0 | 203 | | UpdateAgentSymbols(SimManager.Instance.Threats); |
| | 0 | 204 | | } |
| | 1 | 205 | | } |
| | | 206 | | |
| | 0 | 207 | | private void UpdateAgentSymbols(IEnumerable<IAgent> agents) { |
| | 0 | 208 | | foreach (var agent in agents) { |
| | 0 | 209 | | if (_symbols.TryGetValue(agent, out GameObject symbol)) { |
| | 0 | 210 | | UpdateAgentSymbol(symbol, agent); |
| | 0 | 211 | | } |
| | 0 | 212 | | } |
| | 0 | 213 | | } |
| | | 214 | | |
| | 0 | 215 | | private void UpdateAgentSymbol(GameObject symbol, IAgent agent) { |
| | | 216 | | // Division factor for real-world scaling, e.g., 1000 meters = 1 unit on the grid. |
| | | 217 | | const float scaleDivisionFactor = 1000f; |
| | | 218 | | |
| | 0 | 219 | | if (_polarGridGraphic == null) { |
| | 0 | 220 | | Debug.LogError("TacticalPolarGridGraphic component is missing."); |
| | 0 | 221 | | return; |
| | | 222 | | } |
| | | 223 | | |
| | | 224 | | // Update the symbol position. |
| | 0 | 225 | | Vector3 position = agent.Position; |
| | 0 | 226 | | float scaleFactor = _polarGridGraphic.CurrentScaleFactor; |
| | 0 | 227 | | Vector3 scaledPosition = |
| | | 228 | | new Vector3(position.z / scaleDivisionFactor, position.x / scaleDivisionFactor, 0f); |
| | 0 | 229 | | symbol.transform.localPosition = scaledPosition * scaleFactor; |
| | | 230 | | |
| | | 231 | | // Update the symbol scale. |
| | 0 | 232 | | UpdateSymbolScale(symbol); |
| | | 233 | | |
| | | 234 | | // Update the symbol rotation. |
| | 0 | 235 | | Vector3 forward = agent.Forward; |
| | 0 | 236 | | symbol.GetComponent<TacticalSymbol>().SetDirectionArrowRotation( |
| | | 237 | | -1 * Mathf.Atan2(forward.z, forward.x) * Mathf.Rad2Deg); |
| | | 238 | | |
| | | 239 | | // Update the symbol's speed alternate text. |
| | 0 | 240 | | symbol.GetComponent<TacticalSymbol>().SetSpeedAlt( |
| | | 241 | | $"{Utilities.ConvertMpsToKnots(agent.Speed):F0} kts/" + |
| | | 242 | | $"{Utilities.ConvertMetersToFeet(agent.Position.y):F0} ft"); |
| | 0 | 243 | | } |
| | | 244 | | |
| | 0 | 245 | | private void UpdateSymbolScale(GameObject symbol) { |
| | | 246 | | // Calculate the inverse scale to maintain constant visual size. |
| | 0 | 247 | | float inverseScale = 2f / _radarUIGroupRectTransform.localScale.x; |
| | 0 | 248 | | symbol.transform.localScale = new Vector3(inverseScale, inverseScale, 1f); |
| | 0 | 249 | | } |
| | | 250 | | |
| | | 251 | | // Adjusts the radar scale by the specified amount. |
| | 0 | 252 | | private void AdjustRadarScale(float amount) { |
| | 0 | 253 | | Vector3 newScale = _radarUIGroupRectTransform.localScale + new Vector3(amount, amount, 0f); |
| | | 254 | | // Prevent negative or too small scaling. |
| | 0 | 255 | | if (newScale.x < 0.01f || newScale.y < 0.01f) { |
| | 0 | 256 | | return; |
| | | 257 | | } |
| | 0 | 258 | | _radarUIGroupRectTransform.localScale = newScale; |
| | | 259 | | |
| | | 260 | | // Update all existing symbols' scales. |
| | 0 | 261 | | foreach (var symbol in _symbols.Values) { |
| | 0 | 262 | | UpdateSymbolScale(symbol); |
| | 0 | 263 | | } |
| | 0 | 264 | | UpdateSymbolScale(_origin); |
| | 0 | 265 | | } |
| | | 266 | | } |