< Summary

Class:DirectAttackBehavior
Assembly:bamlab.micromissiles
File(s):/github/workspace/Assets/Scripts/Behavior/DirectAttackBehavior.cs
Covered lines:26
Uncovered lines:6
Coverable lines:32
Total lines:74
Line coverage:81.2% (26 of 32)
Covered branches:0
Total branches:0
Covered methods:2
Total methods:2
Method coverage:100% (2 of 2)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
GetNextWaypoint(...)0%8.467068.97%
FromJson(...)0%440100%

File(s)

/github/workspace/Assets/Scripts/Behavior/DirectAttackBehavior.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using UnityEngine;
 4using Newtonsoft.Json;
 5using Newtonsoft.Json.Converters;
 6
 7public 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)
 35713      GetNextWaypoint(Vector3 currentPosition, Vector3 targetPosition) {
 35714    if (flightPlan.waypoints == null || flightPlan.waypoints.Count == 0) {
 15      // If no waypoints are defined, directly target the target position
 016      return (targetPosition, PowerSetting.MAX);
 17    }
 18
 35719    Vector3 directionToTarget = targetPosition - currentPosition;
 35720    float distanceToTarget = directionToTarget.magnitude;
 21
 22    // Find the current waypoint based on the distance to target
 35723    int currentWaypointIndex = 0;
 107124    for (int i = 0; i < flightPlan.waypoints.Count; i++) {
 71425      if (distanceToTarget > flightPlan.waypoints[i].distance) {
 35726        break;
 27      }
 028      currentWaypointIndex = i;
 029    }
 30
 31    Vector3 waypointPosition;
 32    PowerSetting power;
 33
 35734    if (currentWaypointIndex == flightPlan.waypoints.Count - 1 &&
 035        distanceToTarget < flightPlan.waypoints[currentWaypointIndex].distance) {
 36      // This is the last waypoint, so target the final position
 037      waypointPosition = targetPosition;
 038      power = flightPlan.waypoints[0].power;
 35739    } else {
 40      // There is a next waypoint
 35741      DTTWaypoint nextWaypoint = flightPlan.waypoints[currentWaypointIndex + 1];
 35742      waypointPosition = targetPosition + directionToTarget.normalized * nextWaypoint.distance;
 35743      waypointPosition.y = nextWaypoint.altitude;
 35744      power = nextWaypoint.power;
 35745    }
 46
 35747    return (waypointPosition, power);
 35748  }
 49
 750  public new static DirectAttackBehavior FromJson(string json) {
 751    string resolvedPath = ResolveBehaviorPath(json);
 752  string fileContent = ConfigLoader.LoadFromStreamingAssets(resolvedPath);
 753  DirectAttackBehavior behavior = JsonConvert.DeserializeObject<DirectAttackBehavior>(
 54      fileContent, new JsonSerializerSettings { Converters = { new StringEnumConverter() } });
 55
 56  // Sort waypoints in ascending order based on distance
 1457  if (behavior.flightPlan != null && behavior.flightPlan.waypoints != null) {
 2858    behavior.flightPlan.waypoints.Sort((a, b) => a.distance.CompareTo(b.distance));
 759  }
 60
 761  return behavior;
 762}
 63}
 64
 65[System.Serializable]
 66public class DTTWaypoint : Waypoint {
 67  public float distance;
 68  public float altitude;
 69  public PowerSetting power;
 70}
 71
 72public class DTTFlightPlan : FlightPlan {
 73  public List<DTTWaypoint> waypoints;
 74}