| | 1 | | using System.Collections; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using UnityEngine; |
| | 4 | |
|
| | 5 | | public class Interceptor : Agent { |
| | 6 | | [SerializeField] |
| 2 | 7 | | private float _navigationGain = 3f; // Typically 3-5 |
| | 8 | |
|
| | 9 | | [SerializeField] |
| 2 | 10 | | protected bool _showDebugVectors = true; |
| | 11 | |
|
| | 12 | | private GameObject _missileTrailEffect; |
| 2 | 13 | | private bool _missileTrailEffectAttached = false; |
| | 14 | |
|
| | 15 | | private Coroutine _returnParticleToManagerCoroutine; |
| | 16 | |
|
| | 17 | | private Vector3 _accelerationInput; |
| | 18 | |
|
| 2 | 19 | | private double _elapsedTime = 0; |
| | 20 | |
|
| 1 | 21 | | protected override void Awake() { |
| 1 | 22 | | base.Awake(); |
| 1 | 23 | | SetFlightPhase(FlightPhase.INITIALIZED); |
| 1 | 24 | | } |
| | 25 | |
|
| | 26 | | // Return whether a target can be assigned to the interceptor. |
| 0 | 27 | | public override bool IsAssignable() { |
| 0 | 28 | | bool assignable = !HasAssignedTarget(); |
| 0 | 29 | | return assignable; |
| 0 | 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 | |
|
| 51 | 38 | | protected override void FixedUpdate() { |
| 51 | 39 | | base.FixedUpdate(); |
| 100 | 40 | | if (_showDebugVectors) { |
| 50 | 41 | | DrawDebugVectors(); |
| 50 | 42 | | } |
| 50 | 43 | | } |
| | 44 | |
|
| 51 | 45 | | protected override void UpdateBoost(double deltaTime) { |
| 102 | 46 | | if (_missileTrailEffect == null) { |
| 51 | 47 | | AttachMissileTrailEffect(); |
| 51 | 48 | | } |
| 51 | 49 | | UpdateMissileTrailEffect(); |
| | 50 | |
|
| | 51 | | // Calculate boost acceleration |
| 51 | 52 | | float boostAcceleration = |
| | 53 | | (float)(staticAgentConfig.boostConfig.boostAcceleration * Constants.kGravity); |
| 51 | 54 | | Vector3 boostAccelerationVector = boostAcceleration * transform.forward; |
| | 55 | |
|
| | 56 | | // Add PN acceleration to boost acceleration |
| 51 | 57 | | Vector3 controllerAcceleration = CalcualteAccelerationInput(deltaTime); |
| 50 | 58 | | Vector3 accelerationInput = boostAccelerationVector + controllerAcceleration; |
| | 59 | |
|
| | 60 | | // Calculate the total acceleration |
| 50 | 61 | | Vector3 acceleration = CalculateAcceleration(accelerationInput); |
| | 62 | |
|
| | 63 | | // Apply the acceleration force |
| 50 | 64 | | GetComponent<Rigidbody>().AddForce(acceleration, ForceMode.Acceleration); |
| 50 | 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 | |
|
| 51 | 78 | | private Vector3 CalcualteAccelerationInput(double deltaTime) { |
| 51 | 79 | | if (!HasAssignedTarget()) { |
| 0 | 80 | | return Vector3.zero; |
| | 81 | | } |
| | 82 | |
|
| 51 | 83 | | UpdateTargetModel(deltaTime); |
| | 84 | |
|
| | 85 | | // Check whether the threat should be considered a miss |
| 51 | 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; |
| 50 | 94 | | if (dynamicAgentConfig.dynamic_config.flight_config.augmentedPnEnabled) { |
| 0 | 95 | | controller = new ApnController(this, _navigationGain); |
| 50 | 96 | | } else { |
| 50 | 97 | | controller = new PnController(this, _navigationGain); |
| 50 | 98 | | } |
| 50 | 99 | | Vector3 accelerationInput = controller.Plan(); |
| | 100 | |
|
| | 101 | | // Clamp the normal acceleration input to the maximum normal acceleration |
| 50 | 102 | | float maxNormalAcceleration = CalculateMaxNormalAcceleration(); |
| 50 | 103 | | accelerationInput = Vector3.ClampMagnitude(accelerationInput, maxNormalAcceleration); |
| 50 | 104 | | _accelerationInput = accelerationInput; |
| 50 | 105 | | return accelerationInput; |
| 50 | 106 | | } |
| | 107 | |
|
| 51 | 108 | | private void UpdateTargetModel(double deltaTime) { |
| 51 | 109 | | _elapsedTime += deltaTime; |
| 51 | 110 | | float sensorUpdatePeriod = 1f / dynamicAgentConfig.dynamic_config.sensor_config.frequency; |
| 61 | 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 |
| 10 | 114 | | _targetModel.SetPosition(_target.GetPosition()); |
| 10 | 115 | | _targetModel.SetVelocity(_target.GetVelocity()); |
| 10 | 116 | | _targetModel.SetAcceleration(_target.GetAcceleration()); |
| 10 | 117 | | _elapsedTime = 0; |
| 10 | 118 | | } |
| 51 | 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 | |
|
| 51 | 161 | | private void AttachMissileTrailEffect() { |
| 102 | 162 | | if (_missileTrailEffect == null) { |
| 51 | 163 | | _missileTrailEffect = ParticleManager.Instance.RequestMissileTrailParticle(); |
| 51 | 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 | | } |
| 51 | 180 | | } |
| 51 | 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 | |
|
| 51 | 192 | | private void UpdateMissileTrailEffect() { |
| 102 | 193 | | if (_missileTrailEffect == null || !_missileTrailEffectAttached) { |
| 51 | 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 | | } |
| 51 | 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 | |
|
| 50 | 216 | | protected virtual void DrawDebugVectors() { |
| 100 | 217 | | if (_target != null) { |
| | 218 | | // Line of sight |
| 50 | 219 | | Debug.DrawLine(transform.position, _target.transform.position, new Color(1, 1, 1, 0.15f)); |
| | 220 | |
|
| | 221 | | // Velocity vector |
| 50 | 222 | | Debug.DrawRay(transform.position, GetVelocity() * 0.01f, new Color(0, 0, 1, 0.15f)); |
| | 223 | |
|
| | 224 | | // Current forward direction |
| 50 | 225 | | Debug.DrawRay(transform.position, transform.forward * 5f, Color.yellow); |
| | 226 | |
|
| | 227 | | // Pitch axis (right) |
| 50 | 228 | | Debug.DrawRay(transform.position, transform.right * 5f, Color.red); |
| | 229 | |
|
| | 230 | | // Yaw axis (up) |
| 50 | 231 | | Debug.DrawRay(transform.position, transform.up * 5f, Color.magenta); |
| | 232 | |
|
| 100 | 233 | | if (_accelerationInput != null) { |
| 50 | 234 | | Debug.DrawRay(transform.position, _accelerationInput * 1f, Color.green); |
| 50 | 235 | | } |
| 50 | 236 | | } |
| 50 | 237 | | } |
| | 238 | | } |