| | 1 | | using NUnit.Framework; |
| | 2 | | using UnityEngine; |
| | 3 | | using System.Collections.Generic; |
| | 4 | |
|
| | 5 | | public class BehaviorTests : TestBase { |
| | 6 | | [Test] |
| 0 | 7 | | public void TestDirectAttackBehaviorFactoryUsesOverride() { |
| 0 | 8 | | var config = new Configs.AttackBehaviorConfig() { |
| | 9 | | Name = "Sample Attack", Type = Configs.AttackType.DirectAttack, |
| | 10 | | FlightPlan = |
| | 11 | | new Configs.AttackBehaviorConfig.Types.FlightPlan() { |
| | 12 | | Type = Configs.AttackBehaviorConfig.Types.FlightPlanType.DistanceToTarget, |
| | 13 | | Waypoints = { new List<Configs.AttackBehaviorConfig.Types.FlightPlan.Types.Waypoint>() { |
| | 14 | | new Configs.AttackBehaviorConfig.Types.FlightPlan.Types.Waypoint() { |
| | 15 | | Distance = 1000, |
| | 16 | | Altitude = 100, |
| | 17 | | Power = Configs.Power.Cruise, |
| | 18 | | } |
| | 19 | | } } |
| | 20 | | } |
| | 21 | | }; |
| | 22 | |
|
| 0 | 23 | | AttackBehavior attackBehavior = AttackBehaviorFactory.Create(config); |
| 0 | 24 | | Assert.IsNotNull(attackBehavior); |
| | 25 | |
|
| 0 | 26 | | Vector3 currentPosition = new Vector3(-100, 0, 0); |
| 0 | 27 | | Vector3 targetPosition = new Vector3(0, 0, 0); |
| 0 | 28 | | var result = attackBehavior.GetNextWaypoint(currentPosition, targetPosition); |
| | 29 | |
|
| 0 | 30 | | Assert.AreEqual(Configs.Power.Cruise, result.power); |
| 0 | 31 | | } |
| | 32 | |
|
| | 33 | | [Test] |
| 0 | 34 | | public void TestDirectAttackBehaviorWaypoints() { |
| | 35 | | // Create a sample direct attack behavior. |
| 0 | 36 | | DirectAttackBehavior attackBehavior = |
| | 37 | | new DirectAttackBehavior( |
| | 38 | | new Configs.AttackBehaviorConfig() { |
| | 39 | | Name = "Sample Attack", Type = Configs.AttackType.DirectAttack, |
| | 40 | | FlightPlan = |
| | 41 | | new Configs.AttackBehaviorConfig.Types.FlightPlan() { |
| | 42 | | Type = Configs.AttackBehaviorConfig.Types.FlightPlanType.DistanceToTarget, |
| | 43 | | Waypoints = |
| | 44 | | { new List<Configs.AttackBehaviorConfig.Types.FlightPlan.Types.Waypoint>() { |
| | 45 | | new Configs.AttackBehaviorConfig.Types.FlightPlan.Types.Waypoint() { |
| | 46 | | Distance = 1000, |
| | 47 | | Altitude = 100, |
| | 48 | | Power = Configs.Power.Cruise, |
| | 49 | | }, |
| | 50 | | new Configs.AttackBehaviorConfig.Types.FlightPlan.Types.Waypoint() { |
| | 51 | | Distance = 500, |
| | 52 | | Altitude = 50, |
| | 53 | | Power = Configs.Power.Mil, |
| | 54 | | }, |
| | 55 | | new Configs.AttackBehaviorConfig.Types.FlightPlan.Types.Waypoint() { |
| | 56 | | Distance = 100, |
| | 57 | | Altitude = 25, |
| | 58 | | Power = Configs.Power.Max, |
| | 59 | | } |
| | 60 | | } } |
| | 61 | | } |
| | 62 | | }); |
| | 63 | |
|
| 0 | 64 | | Vector3 targetPosition = new Vector3(1000, 0, 0); |
| | 65 | | const float epsilon = 0.001f; |
| | 66 | |
|
| | 67 | | // Test the waypoint selection based on distance. |
| 0 | 68 | | Vector3 currentPosition = new Vector3(-100, 0, 0); |
| 0 | 69 | | var result = attackBehavior.GetNextWaypoint(currentPosition, targetPosition); |
| 0 | 70 | | Assert.AreEqual(0, result.waypointPosition.x, epsilon); |
| 0 | 71 | | Assert.AreEqual(100, result.waypointPosition.y, epsilon); |
| 0 | 72 | | Assert.AreEqual(0, result.waypointPosition.z, epsilon); |
| 0 | 73 | | Assert.AreEqual(Configs.Power.Cruise, result.power); |
| | 74 | |
|
| 0 | 75 | | currentPosition = new Vector3(0, 0, 0); |
| 0 | 76 | | result = attackBehavior.GetNextWaypoint(currentPosition, targetPosition); |
| 0 | 77 | | Assert.AreEqual(500, result.waypointPosition.x, epsilon); |
| 0 | 78 | | Assert.AreEqual(50, result.waypointPosition.y, epsilon); |
| 0 | 79 | | Assert.AreEqual(0, result.waypointPosition.z, epsilon); |
| 0 | 80 | | Assert.AreEqual(Configs.Power.Mil, result.power); |
| | 81 | |
|
| 0 | 82 | | currentPosition = new Vector3(600, 0, 0); |
| 0 | 83 | | result = attackBehavior.GetNextWaypoint(currentPosition, targetPosition); |
| 0 | 84 | | Assert.AreEqual(900, result.waypointPosition.x, epsilon); |
| 0 | 85 | | Assert.AreEqual(25, result.waypointPosition.y, epsilon); |
| 0 | 86 | | Assert.AreEqual(0, result.waypointPosition.z, epsilon); |
| 0 | 87 | | Assert.AreEqual(Configs.Power.Max, result.power); |
| | 88 | |
|
| | 89 | | // Test the attack behavior within the final distance. |
| 0 | 90 | | currentPosition = new Vector3(920, 0, 0); |
| 0 | 91 | | result = attackBehavior.GetNextWaypoint(currentPosition, targetPosition); |
| 0 | 92 | | Assert.AreEqual(1000, result.waypointPosition.x, epsilon); |
| 0 | 93 | | Assert.AreEqual(0, result.waypointPosition.y, epsilon); |
| 0 | 94 | | Assert.AreEqual(0, result.waypointPosition.z, epsilon); |
| 0 | 95 | | Assert.AreEqual(Configs.Power.Max, result.power); |
| | 96 | |
|
| | 97 | | // Test with non-zero z-coordinate. |
| 0 | 98 | | targetPosition = new Vector3(800, 0, 600); |
| 0 | 99 | | currentPosition = new Vector3(0, 0, 0); |
| 0 | 100 | | result = attackBehavior.GetNextWaypoint(currentPosition, targetPosition); |
| 0 | 101 | | Assert.AreEqual(400, result.waypointPosition.x, epsilon); |
| 0 | 102 | | Assert.AreEqual(50, result.waypointPosition.y, epsilon); |
| 0 | 103 | | Assert.AreEqual(300, result.waypointPosition.z, epsilon); |
| 0 | 104 | | Assert.AreEqual(Configs.Power.Mil, result.power); |
| | 105 | |
|
| 0 | 106 | | currentPosition = new Vector3(400, 0, 300); |
| 0 | 107 | | result = attackBehavior.GetNextWaypoint(currentPosition, targetPosition); |
| 0 | 108 | | Assert.AreEqual(720, result.waypointPosition.x, epsilon); |
| 0 | 109 | | Assert.AreEqual(25, result.waypointPosition.y, epsilon); |
| 0 | 110 | | Assert.AreEqual(540, result.waypointPosition.z, epsilon); |
| 0 | 111 | | Assert.AreEqual(Configs.Power.Max, result.power); |
| 0 | 112 | | } |
| | 113 | | } |