| | | 1 | | using System.Collections.Generic; |
| | | 2 | | using System.Linq; |
| | | 3 | | using UnityEngine; |
| | | 4 | | |
| | | 5 | | // Base implementation of a threat. |
| | | 6 | | public abstract class ThreatBase : AgentBase, IThreat { |
| | | 7 | | public event ThreatHitMissEventHandler OnHit; |
| | | 8 | | public event ThreatHitMissEventHandler OnMiss; |
| | | 9 | | |
| | | 10 | | // Speed difference threshold for applying forward acceleration. |
| | | 11 | | private const float _speedErrorThreshold = 1f; |
| | | 12 | | |
| | | 13 | | // Power table map from the power to the speed. |
| | | 14 | | private Dictionary<Configs.Power, float> _powerTable; |
| | | 15 | | |
| | | 16 | | // The attack behavior determines how the threat navigates towards the asset. |
| | 0 | 17 | | public IAttackBehavior AttackBehavior { get; set; } |
| | | 18 | | |
| | | 19 | | // The evasion handles how the threat behaves in the vicinity of a pursuing interceptor. |
| | 0 | 20 | | public IEvasion Evasion { get; set; } |
| | | 21 | | |
| | | 22 | | public IReadOnlyDictionary<Configs.Power, float> PowerTable { |
| | 0 | 23 | | get { |
| | 0 | 24 | | if (_powerTable == null) { |
| | 0 | 25 | | _powerTable = |
| | 0 | 26 | | StaticConfig.PowerTable.ToDictionary(entry => entry.Power, entry => entry.Speed); |
| | 0 | 27 | | } |
| | 0 | 28 | | return _powerTable; |
| | 0 | 29 | | } |
| | | 30 | | } |
| | | 31 | | |
| | 0 | 32 | | public float LookupPowerTable(Configs.Power power) { |
| | 0 | 33 | | PowerTable.TryGetValue(power, out float speed); |
| | 0 | 34 | | return speed; |
| | 0 | 35 | | } |
| | | 36 | | |
| | 0 | 37 | | public void HandleIntercept() { |
| | 0 | 38 | | OnMiss?.Invoke(this); |
| | 0 | 39 | | Terminate(); |
| | 0 | 40 | | } |
| | | 41 | | |
| | 0 | 42 | | protected override void Start() { |
| | 0 | 43 | | base.Start(); |
| | | 44 | | |
| | | 45 | | // The threat should target the nearest launcher. |
| | 0 | 46 | | IHierarchical target = null; |
| | 0 | 47 | | var launchers = IADS.Instance.Launchers; |
| | 0 | 48 | | if (launchers.Count == 0) { |
| | 0 | 49 | | target = new FixedHierarchical(position: Vector3.zero); |
| | 0 | 50 | | } else { |
| | 0 | 51 | | float minDistanceSqr = Mathf.Infinity; |
| | 0 | 52 | | foreach (var launcher in launchers) { |
| | 0 | 53 | | float distanceSqr = (launcher.Position - Position).sqrMagnitude; |
| | 0 | 54 | | if (distanceSqr < minDistanceSqr) { |
| | 0 | 55 | | minDistanceSqr = distanceSqr; |
| | 0 | 56 | | target = launcher; |
| | 0 | 57 | | } |
| | 0 | 58 | | } |
| | 0 | 59 | | } |
| | 0 | 60 | | HierarchicalAgent.Target = target; |
| | 0 | 61 | | } |
| | | 62 | | |
| | 0 | 63 | | protected override void FixedUpdate() { |
| | 0 | 64 | | base.FixedUpdate(); |
| | | 65 | | |
| | 0 | 66 | | float desiredSpeed = 0f; |
| | | 67 | | // Check whether the threat should evade any pursuer. |
| | 0 | 68 | | IAgent closestPursuer = FindClosestPursuer(); |
| | 0 | 69 | | if (Evasion != null && closestPursuer != null && Evasion.ShouldEvade(closestPursuer)) { |
| | 0 | 70 | | AccelerationInput = Evasion.Evade(closestPursuer); |
| | 0 | 71 | | desiredSpeed = LookupPowerTable(Configs.Power.Max); |
| | 0 | 72 | | } else { |
| | | 73 | | // Follow the attack behavior. |
| | 0 | 74 | | (Vector3 waypoint, Configs.Power waypointPower) = |
| | | 75 | | AttackBehavior.GetNextWaypoint(TargetModel.Position); |
| | 0 | 76 | | AccelerationInput = Controller?.Plan(waypoint) ?? Vector3.zero; |
| | 0 | 77 | | desiredSpeed = LookupPowerTable(waypointPower); |
| | 0 | 78 | | } |
| | | 79 | | |
| | | 80 | | // Limit the forward acceleration according to the desired speed. |
| | 0 | 81 | | float speedError = desiredSpeed - Speed; |
| | 0 | 82 | | Vector3 forwardAccelerationInput = Vector3.Project(AccelerationInput, Forward); |
| | 0 | 83 | | Vector3 normalAccelerationInput = Vector3.ProjectOnPlane(AccelerationInput, Forward); |
| | 0 | 84 | | if (Mathf.Abs(speedError) < _speedErrorThreshold) { |
| | 0 | 85 | | AccelerationInput = normalAccelerationInput; |
| | 0 | 86 | | } else { |
| | 0 | 87 | | float speedFactor = Mathf.Clamp01(Mathf.Abs(speedError) / _speedErrorThreshold); |
| | 0 | 88 | | AccelerationInput = |
| | | 89 | | normalAccelerationInput + forwardAccelerationInput * Mathf.Sign(speedError) * speedFactor; |
| | 0 | 90 | | } |
| | | 91 | | |
| | 0 | 92 | | Acceleration = Movement?.Act(AccelerationInput) ?? Vector3.zero; |
| | 0 | 93 | | _rigidbody.AddForce(Acceleration, ForceMode.Acceleration); |
| | 0 | 94 | | } |
| | | 95 | | |
| | 0 | 96 | | protected override void UpdateAgentConfig() { |
| | 0 | 97 | | base.UpdateAgentConfig(); |
| | | 98 | | |
| | | 99 | | // Set the attack behavior. |
| | 0 | 100 | | Configs.AttackBehaviorConfig attackBehaviorConfig = |
| | | 101 | | ConfigLoader.LoadAttackBehaviorConfig(AgentConfig.AttackBehaviorConfigFile ?? ""); |
| | 0 | 102 | | switch (attackBehaviorConfig?.Type) { |
| | 0 | 103 | | case Configs.AttackType.DirectAttack: { |
| | 0 | 104 | | AttackBehavior = new DirectAttackBehavior(this, attackBehaviorConfig); |
| | 0 | 105 | | break; |
| | | 106 | | } |
| | | 107 | | case Configs.AttackType.PreplannedAttack: |
| | 0 | 108 | | case Configs.AttackType.SlalomAttack: { |
| | 0 | 109 | | Debug.LogError($"Attack behavior type {attackBehaviorConfig?.Type} is unimplemented."); |
| | 0 | 110 | | break; |
| | | 111 | | } |
| | 0 | 112 | | default: { |
| | 0 | 113 | | Debug.LogError($"Attack behavior type {attackBehaviorConfig?.Type} not found."); |
| | 0 | 114 | | break; |
| | | 115 | | } |
| | | 116 | | } |
| | | 117 | | |
| | | 118 | | // Set the evasion. |
| | 0 | 119 | | Evasion = new OrthogonalEvasion(this); |
| | 0 | 120 | | } |
| | | 121 | | |
| | 0 | 122 | | private IAgent FindClosestPursuer() { |
| | 0 | 123 | | if (HierarchicalAgent == null || !HierarchicalAgent.ActivePursuers.Any() || Sensor == null) { |
| | 0 | 124 | | return null; |
| | | 125 | | } |
| | | 126 | | |
| | 0 | 127 | | HierarchicalAgent closestAgent = null; |
| | 0 | 128 | | float minDistance = float.MaxValue; |
| | 0 | 129 | | foreach (var pursuer in HierarchicalAgent.ActivePursuers) { |
| | 0 | 130 | | if (pursuer is HierarchicalAgent agent) { |
| | 0 | 131 | | SensorOutput sensorOutput = Sensor.Sense(agent); |
| | 0 | 132 | | if (sensorOutput.Position.Range < minDistance) { |
| | 0 | 133 | | closestAgent = agent; |
| | 0 | 134 | | minDistance = sensorOutput.Position.Range; |
| | 0 | 135 | | } |
| | 0 | 136 | | } |
| | 0 | 137 | | } |
| | 0 | 138 | | return closestAgent?.Agent; |
| | 0 | 139 | | } |
| | | 140 | | |
| | | 141 | | // If the threat collides with the ground or another agent, it will be terminated. It is possible |
| | | 142 | | // for a threat to collide with another threat or with a non-pursuing interceptor. Interceptors |
| | | 143 | | // will handle colliding with a threat. |
| | 0 | 144 | | private void OnTriggerEnter(Collider other) { |
| | 0 | 145 | | if (CheckFloorCollision(other)) { |
| | 0 | 146 | | OnMiss?.Invoke(this); |
| | 0 | 147 | | Terminate(); |
| | 0 | 148 | | } |
| | | 149 | | |
| | 0 | 150 | | IAgent otherAgent = other.gameObject.GetComponentInParent<IAgent>(); |
| | 0 | 151 | | if (ShouldIgnoreCollision(otherAgent)) { |
| | 0 | 152 | | return; |
| | | 153 | | } |
| | | 154 | | // Check if the collision is with another threat or with the intended target. |
| | 0 | 155 | | if (otherAgent is IThreat) { |
| | 0 | 156 | | OnMiss?.Invoke(this); |
| | 0 | 157 | | Terminate(); |
| | 0 | 158 | | } else if (HierarchicalAgent.Target is HierarchicalAgent targetAgent && |
| | 0 | 159 | | otherAgent == targetAgent.Agent) { |
| | 0 | 160 | | OnHit?.Invoke(this); |
| | 0 | 161 | | Terminate(); |
| | 0 | 162 | | } |
| | 0 | 163 | | } |
| | | 164 | | } |