| | 1 | | using System.Collections; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using UnityEngine; |
| | 4 | |
|
| | 5 | | public class Interceptor : Agent { |
| | 6 | | [SerializeField] |
| 6 | 7 | | private float _navigationGain = 3f; // Typically 3-5 |
| | 8 | |
|
| | 9 | | [SerializeField] |
| 6 | 10 | | protected bool _showDebugVectors = true; |
| | 11 | |
|
| | 12 | | private GameObject _missileTrailEffect; |
| 6 | 13 | | private bool _missileTrailEffectAttached = false; |
| | 14 | |
|
| | 15 | | private Coroutine _returnParticleToManagerCoroutine; |
| | 16 | |
|
| | 17 | | private Vector3 _accelerationInput; |
| | 18 | |
|
| 6 | 19 | | private double _elapsedTime = 0; |
| | 20 | |
|
| 0 | 21 | | protected override void Awake() { |
| 0 | 22 | | base.Awake(); |
| 0 | 23 | | SetFlightPhase(FlightPhase.INITIALIZED); |
| 0 | 24 | | } |
| | 25 | |
|
| | 26 | | // Return whether a target can be assigned to the interceptor. |
| 6 | 27 | | public override bool IsAssignable() { |
| 6 | 28 | | bool assignable = !HasAssignedTarget(); |
| 6 | 29 | | return assignable; |
| 6 | 30 | | } |
| | 31 | |
|
| 0 | 32 | | protected override void UpdateReady(double deltaTime) { |
| 0 | 33 | | Vector3 accelerationInput = Vector3.zero; |
| 0 | 34 | | Vector3 acceleration = CalculateAcceleration(accelerationInput); |
| 0 | 35 | | GetComponent<Rigidbody>().AddForce(acceleration, ForceMode.Acceleration); |
| 0 | 36 | | } |
| | 37 | |
|
| 0 | 38 | | protected override void FixedUpdate() { |
| 0 | 39 | | base.FixedUpdate(); |
| 0 | 40 | | if (_showDebugVectors) { |
| 0 | 41 | | DrawDebugVectors(); |
| 0 | 42 | | } |
| 0 | 43 | | } |
| | 44 | |
|
| 0 | 45 | | protected override void UpdateBoost(double deltaTime) { |
| 0 | 46 | | if (_missileTrailEffect == null) { |
| 0 | 47 | | AttachMissileTrailEffect(); |
| 0 | 48 | | } |
| 0 | 49 | | UpdateMissileTrailEffect(); |
| | 50 | |
|
| | 51 | | // Calculate boost acceleration |
| 0 | 52 | | float boostAcceleration = |
| | 53 | | (float)(staticAgentConfig.boostConfig.boostAcceleration * Constants.kGravity); |
| 0 | 54 | | Vector3 boostAccelerationVector = boostAcceleration * transform.forward; |
| | 55 | |
|
| | 56 | | // Add PN acceleration to boost acceleration |
| 0 | 57 | | Vector3 controllerAcceleration = CalcualteAccelerationInput(deltaTime); |
| 0 | 58 | | Vector3 accelerationInput = boostAccelerationVector + controllerAcceleration; |
| | 59 | |
|
| | 60 | | // Calculate the total acceleration |
| 0 | 61 | | Vector3 acceleration = CalculateAcceleration(accelerationInput); |
| | 62 | |
|
| | 63 | | // Apply the acceleration force |
| 0 | 64 | | GetComponent<Rigidbody>().AddForce(acceleration, ForceMode.Acceleration); |
| 0 | 65 | | } |
| | 66 | |
|
| 0 | 67 | | protected override void UpdateMidCourse(double deltaTime) { |
| 0 | 68 | | UpdateMissileTrailEffect(); |
| | 69 | |
|
| 0 | 70 | | _elapsedTime += deltaTime; |
| 0 | 71 | | Vector3 accelerationInput = CalcualteAccelerationInput(deltaTime); |
| | 72 | |
|
| | 73 | | // Calculate and set the total acceleration |
| 0 | 74 | | Vector3 acceleration = CalculateAcceleration(accelerationInput); |
| 0 | 75 | | GetComponent<Rigidbody>().AddForce(acceleration, ForceMode.Acceleration); |
| 0 | 76 | | } |
| | 77 | |
|
| 0 | 78 | | private Vector3 CalcualteAccelerationInput(double deltaTime) { |
| 0 | 79 | | if (!HasAssignedTarget()) { |
| 0 | 80 | | return Vector3.zero; |
| | 81 | | } |
| | 82 | |
|
| 0 | 83 | | UpdateTargetModel(deltaTime); |
| | 84 | |
|
| | 85 | | // Check whether the threat should be considered a miss |
| 0 | 86 | | SensorOutput sensorOutput = GetComponent<Sensor>().Sense(_target); |
| | 87 | | // DL: This causes trouble with Fateh110B (high-speed threats) |
| | 88 | | // if (sensorOutput.velocity.range > 1000f) { |
| | 89 | | // this.HandleInterceptMiss(); |
| | 90 | | // return Vector3.zero; |
| | 91 | | // } |
| | 92 | |
|
| | 93 | | IController controller; |
| 0 | 94 | | if (dynamicAgentConfig.dynamic_config.flight_config.augmentedPnEnabled) { |
| 0 | 95 | | controller = new ApnController(this, _navigationGain); |
| 0 | 96 | | } else { |
| 0 | 97 | | controller = new PnController(this, _navigationGain); |
| 0 | 98 | | } |
| 0 | 99 | | Vector3 accelerationInput = controller.Plan(); |
| | 100 | |
|
| | 101 | | // Clamp the normal acceleration input to the maximum normal acceleration |
| 0 | 102 | | float maxNormalAcceleration = CalculateMaxNormalAcceleration(); |
| 0 | 103 | | accelerationInput = Vector3.ClampMagnitude(accelerationInput, maxNormalAcceleration); |
| 0 | 104 | | _accelerationInput = accelerationInput; |
| 0 | 105 | | return accelerationInput; |
| 0 | 106 | | } |
| | 107 | |
|
| 0 | 108 | | private void UpdateTargetModel(double deltaTime) { |
| 0 | 109 | | _elapsedTime += deltaTime; |
| 0 | 110 | | float sensorUpdatePeriod = 1f / dynamicAgentConfig.dynamic_config.sensor_config.frequency; |
| 0 | 111 | | if (_elapsedTime >= sensorUpdatePeriod) { |
| | 112 | | // TODO: Implement guidance filter to estimate state from sensor output |
| | 113 | | // For now, we'll use the threat's actual state |
| 0 | 114 | | _targetModel.SetPosition(_target.GetPosition()); |
| 0 | 115 | | _targetModel.SetVelocity(_target.GetVelocity()); |
| 0 | 116 | | _targetModel.SetAcceleration(_target.GetAcceleration()); |
| 0 | 117 | | _elapsedTime = 0; |
| 0 | 118 | | } |
| 0 | 119 | | } |
| | 120 | |
|
| 0 | 121 | | private void OnTriggerEnter(Collider other) { |
| 0 | 122 | | if (other.gameObject.name == "Floor") { |
| 0 | 123 | | this.HandleInterceptMiss(); |
| 0 | 124 | | } |
| | 125 | | // Check if the collision is with another Agent |
| 0 | 126 | | Agent otherAgent = other.gameObject.GetComponentInParent<Agent>(); |
| 0 | 127 | | if (otherAgent != null && otherAgent.GetComponent<Threat>() != null) { |
| | 128 | | // Check kill probability before marking as hit |
| 0 | 129 | | float killProbability = otherAgent.staticAgentConfig.hitConfig.killProbability; |
| 0 | 130 | | GameObject markerObject = Instantiate(Resources.Load<GameObject>("Prefabs/HitMarkerPrefab"), |
| | 131 | | transform.position, Quaternion.identity); |
| 0 | 132 | | if (Random.value <= killProbability) { |
| 0 | 133 | | markerObject.GetComponent<UIHitMarker>().SetHit(); |
| | 134 | | // Mark both this agent and the other agent as hit |
| 0 | 135 | | this.HandleInterceptHit(otherAgent); |
| 0 | 136 | | otherAgent.HandleInterceptHit(otherAgent); |
| | 137 | |
|
| 0 | 138 | | } else { |
| 0 | 139 | | markerObject.GetComponent<UIHitMarker>().SetMiss(); |
| 0 | 140 | | this.HandleInterceptMiss(); |
| | 141 | | // otherAgent.MarkAsMiss(); |
| 0 | 142 | | } |
| 0 | 143 | | } |
| 0 | 144 | | } |
| | 145 | |
|
| 0 | 146 | | public override void TerminateAgent() { |
| 0 | 147 | | DetatchMissileTrail(); |
| 0 | 148 | | base.TerminateAgent(); |
| 0 | 149 | | } |
| | 150 | |
|
| 0 | 151 | | public void OnDestroy() { |
| 0 | 152 | | if (_returnParticleToManagerCoroutine != null) { |
| 0 | 153 | | StopCoroutine(_returnParticleToManagerCoroutine); |
| 0 | 154 | | } |
| 0 | 155 | | if (_missileTrailEffect != null && ParticleManager.Instance != null) { |
| 0 | 156 | | ParticleManager.Instance.ReturnMissileTrailParticle(_missileTrailEffect); |
| 0 | 157 | | _missileTrailEffect = null; |
| 0 | 158 | | } |
| 0 | 159 | | } |
| | 160 | |
|
| 0 | 161 | | private void AttachMissileTrailEffect() { |
| 0 | 162 | | if (_missileTrailEffect == null) { |
| 0 | 163 | | _missileTrailEffect = ParticleManager.Instance.RequestMissileTrailParticle(); |
| 0 | 164 | | if (_missileTrailEffect != null) { |
| 0 | 165 | | _missileTrailEffect.transform.parent = transform; |
| 0 | 166 | | _missileTrailEffect.transform.localPosition = Vector3.zero; |
| 0 | 167 | | _missileTrailEffectAttached = true; |
| 0 | 168 | | ParticleSystem particleSystem = _missileTrailEffect.GetComponent<ParticleSystem>(); |
| 0 | 169 | | float duration = particleSystem.main.duration; |
| | 170 | |
|
| | 171 | | // Extend the duration of the missile trail effect to be the same as the boost time |
| 0 | 172 | | if (duration < staticAgentConfig.boostConfig.boostTime) { |
| 0 | 173 | | ParticleSystem.MainModule mainModule = particleSystem.main; |
| 0 | 174 | | mainModule.duration = staticAgentConfig.boostConfig.boostTime; |
| 0 | 175 | | } |
| | 176 | |
|
| 0 | 177 | | _returnParticleToManagerCoroutine = StartCoroutine(ReturnParticleToManager(duration * 2f)); |
| 0 | 178 | | particleSystem.Play(); |
| 0 | 179 | | } |
| 0 | 180 | | } |
| 0 | 181 | | } |
| | 182 | |
|
| 0 | 183 | | private IEnumerator ReturnParticleToManager(float delay) { |
| 0 | 184 | | yield return new WaitForSeconds(delay); |
| 0 | 185 | | if (_missileTrailEffect != null) { |
| 0 | 186 | | ParticleManager.Instance.ReturnMissileTrailParticle(_missileTrailEffect); |
| 0 | 187 | | _missileTrailEffect = null; |
| 0 | 188 | | _missileTrailEffectAttached = false; |
| 0 | 189 | | } |
| 0 | 190 | | } |
| | 191 | |
|
| 0 | 192 | | private void UpdateMissileTrailEffect() { |
| 0 | 193 | | if (_missileTrailEffect == null || !_missileTrailEffectAttached) { |
| 0 | 194 | | return; |
| | 195 | | } |
| | 196 | |
|
| | 197 | | // Get the particle effect duration time |
| 0 | 198 | | float duration = _missileTrailEffect.GetComponent<ParticleSystem>().main.duration; |
| 0 | 199 | | if (_timeSinceBoost > duration) { |
| 0 | 200 | | DetatchMissileTrail(); |
| 0 | 201 | | } |
| 0 | 202 | | } |
| | 203 | |
|
| 0 | 204 | | private void DetatchMissileTrail() { |
| 0 | 205 | | if (_missileTrailEffect != null && _missileTrailEffectAttached) { |
| 0 | 206 | | Vector3 currentPosition = _missileTrailEffect.transform.position; |
| 0 | 207 | | _missileTrailEffect.transform.SetParent(null); |
| 0 | 208 | | _missileTrailEffect.transform.position = currentPosition; |
| 0 | 209 | | _missileTrailEffectAttached = false; |
| | 210 | | // Stop emitting particles |
| 0 | 211 | | ParticleSystem particleSystem = _missileTrailEffect.GetComponent<ParticleSystem>(); |
| 0 | 212 | | particleSystem.Stop(); |
| 0 | 213 | | } |
| 0 | 214 | | } |
| | 215 | |
|
| 0 | 216 | | protected virtual void DrawDebugVectors() { |
| 0 | 217 | | if (_target != null) { |
| | 218 | | // Line of sight |
| 0 | 219 | | Debug.DrawLine(transform.position, _target.transform.position, new Color(1, 1, 1, 0.15f)); |
| | 220 | |
|
| | 221 | | // Velocity vector |
| 0 | 222 | | Debug.DrawRay(transform.position, GetVelocity() * 0.01f, new Color(0, 0, 1, 0.15f)); |
| | 223 | |
|
| | 224 | | // Current forward direction |
| 0 | 225 | | Debug.DrawRay(transform.position, transform.forward * 5f, Color.yellow); |
| | 226 | |
|
| | 227 | | // Pitch axis (right) |
| 0 | 228 | | Debug.DrawRay(transform.position, transform.right * 5f, Color.red); |
| | 229 | |
|
| | 230 | | // Yaw axis (up) |
| 0 | 231 | | Debug.DrawRay(transform.position, transform.up * 5f, Color.magenta); |
| | 232 | |
|
| 0 | 233 | | if (_accelerationInput != null) { |
| 0 | 234 | | Debug.DrawRay(transform.position, _accelerationInput * 1f, Color.green); |
| 0 | 235 | | } |
| 0 | 236 | | } |
| 0 | 237 | | } |
| | 238 | | } |