< Summary

Class:FixedWingThreat
Assembly:bamlab.micromissiles
File(s):/github/workspace/Assets/Scripts/Threats/FixedWingThreat.cs
Covered lines:51
Uncovered lines:14
Coverable lines:65
Total lines:118
Line coverage:78.4% (51 of 65)
Covered branches:0
Total branches:0
Covered methods:7
Total methods:9
Method coverage:77.7% (7 of 9)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
FixedWingThreat()0%110100%
Start()0%110100%
FixedUpdate()0%110100%
UpdateReady(...)0%2100%
UpdateBoost(...)0%110100%
UpdateMidCourse(...)0%4.044086.36%
UpdateWaypointAndPower()0%110100%
CalculateAccelerationInput()0%3.063080.77%
OnDrawGizmos()0%6200%

File(s)

/github/workspace/Assets/Scripts/Threats/FixedWingThreat.cs

#LineLine coverage
 1using System.Collections;
 2using System.Collections.Generic;
 3using UnityEngine;
 4
 5public class FixedWingThreat : Threat {
 6  [SerializeField]
 7007  private float _navigationGain = 50f;
 8
 9  private Vector3 _accelerationInput;
 70010  private double _elapsedTime = 0;
 11  private Rigidbody _rigidbody;
 12
 69913  protected override void Start() {
 69914    base.Start();
 69915    _rigidbody = GetComponent<Rigidbody>();
 69916  }
 17
 554718  protected override void FixedUpdate() {
 554719    base.FixedUpdate();
 554720  }
 21
 022  protected override void UpdateReady(double deltaTime) {}
 23
 139824  protected override void UpdateBoost(double deltaTime) {}
 25
 484826  protected override void UpdateMidCourse(double deltaTime) {
 484827    _elapsedTime += deltaTime;
 484828    Vector3 accelerationInput = Vector3.zero;
 29
 484830    if (ShouldEvade()) {
 031      accelerationInput = EvadeInterceptor(GetClosestInterceptor());
 969632    } else if (HasAssignedTarget()) {
 33      // Update the waypoint and power setting.
 484834      UpdateWaypointAndPower();
 35
 484836      float sensorUpdatePeriod = 1f / agentConfig.DynamicConfig.SensorConfig.Frequency;
 538437      if (_elapsedTime >= sensorUpdatePeriod) {
 38        // TODO: Implement guidance filter to estimate the state from sensor output.
 53639        _elapsedTime = 0;
 53640      }
 41
 42      // Calculate the normal acceleration input using proportional navigation.
 484843      Vector3 normalAcceleration = CalculateAccelerationInput();
 44
 45      // Adjust the speed based on the power setting.
 484846      Vector3 forwardAcceleration = CalculateForwardAcceleration();
 47
 48      // Combine the accelerations.
 484849      accelerationInput = normalAcceleration + forwardAcceleration;
 484850    }
 51
 52    // Calculate and set the total acceleration.
 484853    Vector3 acceleration = CalculateAcceleration(accelerationInput);
 484854    _rigidbody.AddForce(acceleration, ForceMode.Acceleration);
 484855  }
 56
 484857  private void UpdateWaypointAndPower() {
 58    // Get the next waypoint and power setting from the attack behavior.
 59    // TODO: Implement support for sensors to update the track on the target position.
 484860    (_currentWaypoint, _currentPower) =
 61        _attackBehavior.GetNextWaypoint(transform.position, _target.transform.position);
 484862  }
 63
 484864  private Vector3 CalculateAccelerationInput() {
 65    // Cache the transform and velocity.
 484866    Transform threatTransform = transform;
 484867    Vector3 right = threatTransform.right;
 484868    Vector3 forward = threatTransform.forward;
 484869    Vector3 position = threatTransform.position;
 484870    Vector3 velocity = GetVelocity();
 484871    float speed = velocity.magnitude;
 72
 484873    IController controller = new PnController(this, _navigationGain);
 484874    Vector3 accelerationInput = controller.PlanToWaypoint(_currentWaypoint);
 75
 76    // Counter gravity as much as possible.
 484877    accelerationInput +=
 78        (float)Constants.kGravity / Vector3.Dot(transform.up, Vector3.up) * transform.up;
 79
 80    // Clamp the normal acceleration input to the maximum normal acceleration.
 484881    float maxNormalAcceleration = CalculateMaxNormalAcceleration();
 484882    accelerationInput = Vector3.ClampMagnitude(accelerationInput, maxNormalAcceleration);
 83
 84    // Avoid the ground when close to the surface and too low on the glideslope.
 484885    float altitude = position.y;
 86    // Sink rate is opposite to climb rate.
 484887    float sinkRate = -velocity.y;
 484888    float distanceToTarget = (_currentWaypoint - position).magnitude;
 484889    float groundProximityThreshold = Mathf.Abs(sinkRate) * 5f;  // Adjust threshold as necessary.
 484890    if (sinkRate > 0 && altitude / sinkRate < distanceToTarget / speed) {
 91      // Evade upward normal to the velocity.
 092      Vector3 upwardsDirection = Vector3.Cross(forward, right);
 93
 94      // Blend between the calculated acceleration input and the upward acceleration.
 095      float blendFactor = 1 - (altitude / groundProximityThreshold);
 096      accelerationInput.y =
 97          Vector3
 98              .Lerp(accelerationInput, upwardsDirection * CalculateMaxNormalAcceleration(),
 99                    blendFactor)
 100              .y;
 0101    }
 102
 4848103    accelerationInput = Vector3.ClampMagnitude(accelerationInput, maxNormalAcceleration);
 4848104    _accelerationInput = accelerationInput;
 4848105    return accelerationInput;
 4848106  }
 107
 108  // Optional: Add this method to visualize debug information.
 0109  protected virtual void OnDrawGizmos() {
 0110    if (Application.isPlaying) {
 0111      Gizmos.color = Color.yellow;
 0112      Gizmos.DrawLine(transform.position, _currentWaypoint);
 113
 0114      Gizmos.color = Color.green;
 0115      Gizmos.DrawRay(transform.position, _accelerationInput);
 0116    }
 0117  }
 118}