< Summary

Class:DirectAttackBehavior
Assembly:bamlab.micromissiles
File(s):/github/workspace/Assets/Scripts/Behavior/DirectAttackBehavior.cs
Covered lines:9
Uncovered lines:23
Coverable lines:32
Total lines:74
Line coverage:28.1% (9 of 32)
Covered branches:0
Total branches:0
Covered methods:1
Total methods:2
Method coverage:50% (1 of 2)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
GetNextWaypoint(...)0%56700%
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)
 013      GetNextWaypoint(Vector3 currentPosition, Vector3 targetPosition) {
 014    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
 019    Vector3 directionToTarget = targetPosition - currentPosition;
 020    float distanceToTarget = directionToTarget.magnitude;
 21
 22    // Find the current waypoint based on the distance to target
 023    int currentWaypointIndex = 0;
 024    for (int i = 0; i < flightPlan.waypoints.Count; i++) {
 025      if (distanceToTarget > flightPlan.waypoints[i].distance) {
 026        break;
 27      }
 028      currentWaypointIndex = i;
 029    }
 30
 31    Vector3 waypointPosition;
 32    PowerSetting power;
 33
 034    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;
 039    } else {
 40      // There is a next waypoint
 041      DTTWaypoint nextWaypoint = flightPlan.waypoints[currentWaypointIndex + 1];
 042      waypointPosition = targetPosition + directionToTarget.normalized * nextWaypoint.distance;
 043      waypointPosition.y = nextWaypoint.altitude;
 044      power = nextWaypoint.power;
 045    }
 46
 047    return (waypointPosition, power);
 048  }
 49
 1550  public new static DirectAttackBehavior FromJson(string json) {
 1551    string resolvedPath = ResolveBehaviorPath(json);
 1552  string fileContent = ConfigLoader.LoadFromStreamingAssets(resolvedPath);
 1553  DirectAttackBehavior behavior = JsonConvert.DeserializeObject<DirectAttackBehavior>(
 54      fileContent, new JsonSerializerSettings { Converters = { new StringEnumConverter() } });
 55
 56  // Sort waypoints in ascending order based on distance
 3057  if (behavior.flightPlan != null && behavior.flightPlan.waypoints != null) {
 3058    behavior.flightPlan.waypoints.Sort((a, b) => a.distance.CompareTo(b.distance));
 1559  }
 60
 1561  return behavior;
 1562}
 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}