| | 1 | | using UnityEngine; |
| | 2 | |
|
| | 3 | | public class DirectAttackBehavior : AttackBehavior { |
| 51 | 4 | | public DirectAttackBehavior(in Configs.AttackBehaviorConfig config) : base(config) {} |
| | 5 | |
|
| | 6 | | // Return the next waypoint for the threat to navigate to and the power setting to use towards the |
| | 7 | | // waypoint. |
| | 8 | | public override (Vector3 waypointPosition, Configs.Power power) |
| 7 | 9 | | GetNextWaypoint(Vector3 currentPosition, Vector3 targetPosition) { |
| 7 | 10 | | if (FlightPlan.Waypoints.Count == 0) { |
| | 11 | | // If no waypoints are defined, directly target the target position. |
| 0 | 12 | | return (targetPosition, Configs.Power.Max); |
| | 13 | | } |
| | 14 | |
|
| 7 | 15 | | Vector3 directionToTarget = targetPosition - currentPosition; |
| 7 | 16 | | float distanceToTarget = directionToTarget.magnitude; |
| | 17 | |
|
| | 18 | | // Find the index of the first waypoint whose position is closer to the target than the current |
| | 19 | | // position. |
| 7 | 20 | | int waypointIndex = 0; |
| 49 | 21 | | for (waypointIndex = 0; waypointIndex < FlightPlan.Waypoints.Count; ++waypointIndex) { |
| 20 | 22 | | if (distanceToTarget > FlightPlan.Waypoints[waypointIndex].Distance) { |
| 5 | 23 | | break; |
| | 24 | | } |
| 10 | 25 | | } |
| | 26 | |
|
| 7 | 27 | | Vector3 waypointPosition = targetPosition; |
| 7 | 28 | | Configs.Power power = Configs.Power.Idle; |
| 9 | 29 | | if (waypointIndex == FlightPlan.Waypoints.Count) { |
| | 30 | | // This is the last waypoint, so target the final position with the last waypoint's power |
| | 31 | | // setting. |
| 2 | 32 | | waypointPosition = targetPosition; |
| 2 | 33 | | power = FlightPlan.Waypoints[FlightPlan.Waypoints.Count - 1].Power; |
| 2 | 34 | | return (waypointPosition, power); |
| | 35 | | } |
| | 36 | | // There is a next waypoint. |
| 5 | 37 | | var waypoint = FlightPlan.Waypoints[waypointIndex]; |
| 5 | 38 | | waypointPosition = targetPosition - directionToTarget.normalized * waypoint.Distance; |
| 5 | 39 | | waypointPosition.y = waypoint.Altitude; |
| 5 | 40 | | power = waypoint.Power; |
| 5 | 41 | | return (waypointPosition, power); |
| 7 | 42 | | } |
| | 43 | | } |