| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using UnityEngine; |
| | 4 | | using Newtonsoft.Json; |
| | 5 | | using Newtonsoft.Json.Converters; |
| | 6 | |
|
| | 7 | | public class DirectAttackBehavior : AttackBehavior { |
| | 8 | | public new DTTFlightPlan flightPlan; |
| | 9 | |
|
| | 10 | | // Returns the next waypoint for the threat to navigate to |
| | 11 | | // In addition, return the power setting to use toward the waypoint |
| | 12 | | public override (Vector3 waypointPosition, PowerSetting power) |
| 0 | 13 | | GetNextWaypoint(Vector3 currentPosition, Vector3 targetPosition) { |
| 0 | 14 | | if (flightPlan.waypoints == null || flightPlan.waypoints.Count == 0) { |
| | 15 | | // If no waypoints are defined, directly target the target position |
| 0 | 16 | | return (targetPosition, PowerSetting.MAX); |
| | 17 | | } |
| | 18 | |
|
| 0 | 19 | | Vector3 directionToTarget = targetPosition - currentPosition; |
| 0 | 20 | | float distanceToTarget = directionToTarget.magnitude; |
| | 21 | |
|
| | 22 | | // Find the current waypoint based on the distance to target |
| 0 | 23 | | int currentWaypointIndex = 0; |
| 0 | 24 | | for (int i = 0; i < flightPlan.waypoints.Count; i++) { |
| 0 | 25 | | if (distanceToTarget > flightPlan.waypoints[i].distance) { |
| 0 | 26 | | break; |
| | 27 | | } |
| 0 | 28 | | currentWaypointIndex = i; |
| 0 | 29 | | } |
| | 30 | |
|
| | 31 | | Vector3 waypointPosition; |
| | 32 | | PowerSetting power; |
| | 33 | |
|
| 0 | 34 | | if (currentWaypointIndex == flightPlan.waypoints.Count - 1 && |
| 0 | 35 | | distanceToTarget < flightPlan.waypoints[currentWaypointIndex].distance) { |
| | 36 | | // This is the last waypoint, so target the final position |
| 0 | 37 | | waypointPosition = targetPosition; |
| 0 | 38 | | power = flightPlan.waypoints[0].power; |
| 0 | 39 | | } else { |
| | 40 | | // There is a next waypoint |
| 0 | 41 | | DTTWaypoint nextWaypoint = flightPlan.waypoints[currentWaypointIndex + 1]; |
| 0 | 42 | | waypointPosition = targetPosition + directionToTarget.normalized * nextWaypoint.distance; |
| 0 | 43 | | waypointPosition.y = nextWaypoint.altitude; |
| 0 | 44 | | power = nextWaypoint.power; |
| 0 | 45 | | } |
| | 46 | |
|
| 0 | 47 | | return (waypointPosition, power); |
| 0 | 48 | | } |
| | 49 | |
|
| 15 | 50 | | public new static DirectAttackBehavior FromJson(string json) { |
| 15 | 51 | | string resolvedPath = ResolveBehaviorPath(json); |
| 15 | 52 | | string fileContent = ConfigLoader.LoadFromStreamingAssets(resolvedPath); |
| 15 | 53 | | DirectAttackBehavior behavior = JsonConvert.DeserializeObject<DirectAttackBehavior>( |
| | 54 | | fileContent, new JsonSerializerSettings { Converters = { new StringEnumConverter() } }); |
| | 55 | |
|
| | 56 | | // Sort waypoints in ascending order based on distance |
| 30 | 57 | | if (behavior.flightPlan != null && behavior.flightPlan.waypoints != null) { |
| 30 | 58 | | behavior.flightPlan.waypoints.Sort((a, b) => a.distance.CompareTo(b.distance)); |
| 15 | 59 | | } |
| | 60 | |
|
| 15 | 61 | | return behavior; |
| 15 | 62 | | } |
| | 63 | | } |
| | 64 | |
|
| | 65 | | [System.Serializable] |
| | 66 | | public class DTTWaypoint : Waypoint { |
| | 67 | | public float distance; |
| | 68 | | public float altitude; |
| | 69 | | public PowerSetting power; |
| | 70 | | } |
| | 71 | |
|
| | 72 | | public class DTTFlightPlan : FlightPlan { |
| | 73 | | public List<DTTWaypoint> waypoints; |
| | 74 | | } |