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