| | | 1 | | using UnityEngine; |
| | | 2 | | |
| | | 3 | | // Speed escape detector. |
| | | 4 | | // |
| | | 5 | | // The speed escape detector checks whether the agent has a speed greater than the threat's when it |
| | | 6 | | // has navigated to the threat's current position. |
| | | 7 | | public class SpeedEscapeDetector : EscapeDetectorBase { |
| | 12 | 8 | | public SpeedEscapeDetector(IAgent agent) : base(agent) {} |
| | | 9 | | |
| | 4 | 10 | | public override bool IsEscaping(IHierarchical target) { |
| | 5 | 11 | | if (target == null) { |
| | 1 | 12 | | return false; |
| | | 13 | | } |
| | | 14 | | |
| | 3 | 15 | | float predictedAgentSpeed = CalculatePredictedAgentSpeed(target.Position); |
| | 3 | 16 | | return predictedAgentSpeed <= target.Speed; |
| | 4 | 17 | | } |
| | | 18 | | |
| | | 19 | | // Calculate the predicted agent speed when it has reached the target's current position. |
| | 3 | 20 | | private float CalculatePredictedAgentSpeed(in Vector3 targetPosition) { |
| | 3 | 21 | | float fractionalSpeed = FractionalSpeed.Calculate(Agent, targetPosition); |
| | 3 | 22 | | return fractionalSpeed * Agent.Speed; |
| | 3 | 23 | | } |
| | | 24 | | } |