| | 1 | | using System.Collections; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using UnityEngine; |
| | 4 | |
|
| | 5 | | public abstract class Threat : Agent { |
| | 6 | | protected AttackBehavior _attackBehavior; |
| | 7 | | [SerializeField] |
| | 8 | | protected Vector3 _currentWaypoint; |
| | 9 | | [SerializeField] |
| | 10 | | protected Configs.Power _currentPower; |
| | 11 | |
|
| | 12 | | protected SensorOutput _sensorOutput; |
| | 13 | | protected Sensor _sensor; |
| | 14 | |
|
| 0 | 15 | | protected override void Awake() { |
| 0 | 16 | | base.Awake(); |
| 0 | 17 | | SetFlightPhase(FlightPhase.INITIALIZED); |
| 0 | 18 | | } |
| | 19 | |
|
| 15 | 20 | | public void SetAttackBehavior(AttackBehavior attackBehavior) { |
| 15 | 21 | | _attackBehavior = attackBehavior; |
| | 22 | | // TODO(titan): Set the threat's initial target properly. |
| 15 | 23 | | _target = SimManager.Instance.CreateDummyAgent(Vector3.zero, Vector3.zero); |
| 15 | 24 | | } |
| | 25 | |
|
| 4 | 26 | | protected float LookupPowerTable(Configs.Power power) { |
| 38 | 27 | | foreach (var entry in staticConfig.PowerTable) { |
| 14 | 28 | | if (entry.Power == power) { |
| 4 | 29 | | return entry.Speed; |
| | 30 | | } |
| 6 | 31 | | } |
| 0 | 32 | | Debug.LogError($"Invalid power setting: {power}."); |
| 0 | 33 | | return 0f; |
| 4 | 34 | | } |
| | 35 | |
|
| 2 | 36 | | public override bool IsAssignable() { |
| 2 | 37 | | return false; |
| 2 | 38 | | } |
| | 39 | |
|
| 15 | 40 | | protected override void Start() { |
| 15 | 41 | | base.Start(); |
| 15 | 42 | | _sensor = GetComponent<Sensor>(); |
| 15 | 43 | | } |
| | 44 | |
|
| 0 | 45 | | protected override void FixedUpdate() { |
| 0 | 46 | | base.FixedUpdate(); |
| 0 | 47 | | } |
| | 48 | |
|
| 0 | 49 | | protected Vector3 CalculateForwardAcceleration() { |
| 0 | 50 | | Vector3 transformVelocity = GetVelocity(); |
| | 51 | |
|
| | 52 | | // Get the target speed for the current power setting. |
| 0 | 53 | | float targetSpeed = LookupPowerTable(_currentPower); |
| | 54 | |
|
| | 55 | | // Calculate the current speed. |
| 0 | 56 | | float currentSpeed = transformVelocity.magnitude; |
| | 57 | |
|
| | 58 | | // Speed error. |
| 0 | 59 | | float speedError = targetSpeed - currentSpeed; |
| | 60 | |
|
| | 61 | | // Proportional gain for speed control. |
| 0 | 62 | | float speedControlGain = 10.0f; // Adjust this gain as necessary. |
| | 63 | |
|
| | 64 | | // Desired acceleration to adjust speed. |
| 0 | 65 | | float desiredAccelerationMagnitude = speedControlGain * speedError; |
| | 66 | |
|
| | 67 | | // Limit the desired acceleration. |
| 0 | 68 | | float maxForwardAcceleration = CalculateMaxForwardAcceleration(); |
| 0 | 69 | | desiredAccelerationMagnitude = |
| | 70 | | Mathf.Clamp(desiredAccelerationMagnitude, -maxForwardAcceleration, maxForwardAcceleration); |
| | 71 | |
|
| | 72 | | // Acceleration direction (along current velocity direction). |
| 0 | 73 | | Vector3 accelerationDirection = transformVelocity.normalized; |
| | 74 | |
|
| | 75 | | // Handle the zero velocity case. |
| 0 | 76 | | if (accelerationDirection.magnitude < 0.1f) { |
| 0 | 77 | | accelerationDirection = transform.forward; // Default direction. |
| 0 | 78 | | } |
| | 79 | |
|
| | 80 | | // Calculate the acceleration vector. |
| 0 | 81 | | Vector3 speedAdjustmentAcceleration = accelerationDirection * desiredAccelerationMagnitude; |
| 0 | 82 | | return speedAdjustmentAcceleration; |
| 0 | 83 | | } |
| | 84 | |
|
| 0 | 85 | | protected Agent GetClosestInterceptor() { |
| 0 | 86 | | if (_interceptors.Count == 0) { |
| 0 | 87 | | return null; |
| | 88 | | } |
| | 89 | |
|
| 0 | 90 | | Agent closestInterceptor = null; |
| 0 | 91 | | float minDistance = float.MaxValue; |
| 0 | 92 | | foreach (var interceptor in _interceptors) { |
| 0 | 93 | | if (!interceptor.IsTerminated()) { |
| 0 | 94 | | SensorOutput sensorOutput = _sensor.Sense(interceptor); |
| 0 | 95 | | if (sensorOutput.position.range < minDistance) { |
| 0 | 96 | | closestInterceptor = interceptor; |
| 0 | 97 | | minDistance = sensorOutput.position.range; |
| 0 | 98 | | } |
| 0 | 99 | | } |
| 0 | 100 | | } |
| 0 | 101 | | return closestInterceptor; |
| 0 | 102 | | } |
| | 103 | |
|
| 0 | 104 | | protected bool ShouldEvade() { |
| 0 | 105 | | if (!agentConfig.DynamicConfig.FlightConfig.EvasionConfig.Enabled) { |
| 0 | 106 | | return false; |
| | 107 | | } |
| | 108 | |
|
| 0 | 109 | | Agent closestInterceptor = GetClosestInterceptor(); |
| 0 | 110 | | if (closestInterceptor == null) { |
| 0 | 111 | | return false; |
| | 112 | | } |
| | 113 | |
|
| 0 | 114 | | float evasionRangeThreshold = |
| | 115 | | agentConfig.DynamicConfig.FlightConfig.EvasionConfig.RangeThreshold; |
| 0 | 116 | | SensorOutput sensorOutput = _sensor.Sense(closestInterceptor); |
| 0 | 117 | | return sensorOutput.position.range <= evasionRangeThreshold && sensorOutput.velocity.range < 0; |
| 0 | 118 | | } |
| | 119 | |
|
| 0 | 120 | | protected Vector3 EvadeInterceptor(Agent interceptor) { |
| 0 | 121 | | Vector3 transformVelocity = GetVelocity(); |
| 0 | 122 | | Vector3 interceptorVelocity = interceptor.GetVelocity(); |
| 0 | 123 | | Vector3 transformPosition = GetPosition(); |
| 0 | 124 | | Vector3 interceptorPosition = interceptor.GetPosition(); |
| | 125 | |
|
| | 126 | | // Set power to maximum. |
| 0 | 127 | | _currentPower = Configs.Power.Max; |
| | 128 | |
|
| | 129 | | // Evade the interceptor by changing the velocity to be normal to the interceptor's velocity. |
| 0 | 130 | | Vector3 normalVelocity = Vector3.ProjectOnPlane(transformVelocity, interceptorVelocity); |
| 0 | 131 | | Vector3 normalAccelerationDirection = |
| | 132 | | Vector3.ProjectOnPlane(normalVelocity, transformVelocity).normalized; |
| | 133 | |
|
| | 134 | | // Turn away from the interceptor. |
| 0 | 135 | | Vector3 relativePosition = interceptorPosition - transformPosition; |
| 0 | 136 | | if (Vector3.Dot(relativePosition, normalAccelerationDirection) > 0) { |
| 0 | 137 | | normalAccelerationDirection *= -1; |
| 0 | 138 | | } |
| | 139 | |
|
| | 140 | | // Avoid evading straight down when near the ground. |
| 0 | 141 | | float altitude = transformPosition.y; |
| 0 | 142 | | float groundProximityThreshold = |
| | 143 | | Mathf.Abs(transformVelocity.y) * 5f; // Adjust threshold as necessary. |
| 0 | 144 | | if (transformVelocity.y < 0 && altitude < groundProximityThreshold) { |
| | 145 | | // Determine evasion direction based on angle to interceptor. |
| 0 | 146 | | Vector3 toInterceptor = interceptorPosition - transformPosition; |
| 0 | 147 | | Vector3 rightDirection = Vector3.Cross(Vector3.up, transform.forward); |
| 0 | 148 | | float angle = Vector3.SignedAngle(transform.forward, toInterceptor, Vector3.up); |
| | 149 | |
|
| | 150 | | // Choose the direction that leads away from the interceptor. |
| 0 | 151 | | Vector3 bestHorizontalDirection = angle > 0 ? -rightDirection : rightDirection; |
| | 152 | |
|
| | 153 | | // Blend between horizontal evasion and slight upward movement. |
| 0 | 154 | | float blendFactor = 1 - (altitude / groundProximityThreshold); |
| 0 | 155 | | normalAccelerationDirection = |
| | 156 | | Vector3 |
| | 157 | | .Lerp(normalAccelerationDirection, bestHorizontalDirection + transform.up * 5f, |
| | 158 | | blendFactor) |
| | 159 | | .normalized; |
| 0 | 160 | | } |
| 0 | 161 | | Vector3 normalAcceleration = normalAccelerationDirection * CalculateMaxNormalAcceleration(); |
| | 162 | |
|
| | 163 | | // Adjust the forward speed. |
| 0 | 164 | | Vector3 forwardAcceleration = CalculateForwardAcceleration(); |
| | 165 | |
|
| 0 | 166 | | return normalAcceleration + forwardAcceleration; |
| 0 | 167 | | } |
| | 168 | |
|
| 0 | 169 | | private void OnTriggerEnter(Collider other) { |
| | 170 | | // Check if the threat hit the floor with a negative vertical speed. |
| 0 | 171 | | if (other.gameObject.name == "Floor" && Vector3.Dot(GetVelocity(), Vector3.up) < 0) { |
| 0 | 172 | | HandleHitGround(); |
| 0 | 173 | | } |
| | 174 | |
|
| | 175 | | // Check if the collision is with the asset. |
| 0 | 176 | | DummyAgent otherAgent = other.gameObject.GetComponentInParent<DummyAgent>(); |
| 0 | 177 | | if (otherAgent != null && _target == otherAgent) { |
| 0 | 178 | | HandleThreatHit(); |
| 0 | 179 | | } |
| 0 | 180 | | } |
| | 181 | | } |