< Summary

Class:FixedWingThreat
Assembly:bamlab.micromissiles
File(s):/github/workspace/Assets/Scripts/Threats/FixedWingThreat.cs
Covered lines:27
Uncovered lines:38
Coverable lines:65
Total lines:118
Line coverage:41.5% (27 of 65)
Covered branches:0
Total branches:0
Covered methods:3
Total methods:9
Method coverage:33.3% (3 of 9)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
FixedWingThreat()0%110100%
Start()0%110100%
FixedUpdate()0%2100%
UpdateReady(...)0%2100%
UpdateBoost(...)0%2100%
UpdateMidCourse(...)0%20400%
UpdateWaypointAndPower()0%2100%
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]
 97  private float _navigationGain = 50f;
 8
 9  private Vector3 _accelerationInput;
 910  private double _elapsedTime = 0;
 11  private Rigidbody _rigidbody;
 12
 813  protected override void Start() {
 814    base.Start();
 815    _rigidbody = GetComponent<Rigidbody>();
 816  }
 17
 018  protected override void FixedUpdate() {
 019    base.FixedUpdate();
 020  }
 21
 022  protected override void UpdateReady(double deltaTime) {}
 23
 024  protected override void UpdateBoost(double deltaTime) {}
 25
 026  protected override void UpdateMidCourse(double deltaTime) {
 027    _elapsedTime += deltaTime;
 028    Vector3 accelerationInput = Vector3.zero;
 29
 030    if (ShouldEvade()) {
 031      accelerationInput = EvadeInterceptor(GetClosestInterceptor());
 032    } else if (HasAssignedTarget()) {
 33      // Update the waypoint and power setting.
 034      UpdateWaypointAndPower();
 35
 036      float sensorUpdatePeriod = 1f / agentConfig.DynamicConfig.SensorConfig.Frequency;
 037      if (_elapsedTime >= sensorUpdatePeriod) {
 38        // TODO: Implement guidance filter to estimate the state from sensor output.
 039        _elapsedTime = 0;
 040      }
 41
 42      // Calculate the normal acceleration input using proportional navigation.
 043      Vector3 normalAcceleration = CalculateAccelerationInput();
 44
 45      // Adjust the speed based on the power setting.
 046      Vector3 forwardAcceleration = CalculateForwardAcceleration();
 47
 48      // Combine the accelerations.
 049      accelerationInput = normalAcceleration + forwardAcceleration;
 050    }
 51
 52    // Calculate and set the total acceleration.
 053    Vector3 acceleration = CalculateAcceleration(accelerationInput);
 054    _rigidbody.AddForce(acceleration, ForceMode.Acceleration);
 055  }
 56
 057  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.
 060    (_currentWaypoint, _currentPower) =
 61        _attackBehavior.GetNextWaypoint(transform.position, _target.transform.position);
 062  }
 63
 264  private Vector3 CalculateAccelerationInput() {
 65    // Cache the transform and velocity.
 266    Transform threatTransform = transform;
 267    Vector3 right = threatTransform.right;
 268    Vector3 forward = threatTransform.forward;
 269    Vector3 position = threatTransform.position;
 270    Vector3 velocity = GetVelocity();
 271    float speed = velocity.magnitude;
 72
 273    IController controller = new PnController(this, _navigationGain);
 274    Vector3 accelerationInput = controller.PlanToWaypoint(_currentWaypoint);
 75
 76    // Counter gravity as much as possible.
 277    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.
 281    float maxNormalAcceleration = CalculateMaxNormalAcceleration();
 282    accelerationInput = Vector3.ClampMagnitude(accelerationInput, maxNormalAcceleration);
 83
 84    // Avoid the ground when close to the surface and too low on the glideslope.
 285    float altitude = position.y;
 86    // Sink rate is opposite to climb rate.
 287    float sinkRate = -velocity.y;
 288    float distanceToTarget = (_currentWaypoint - position).magnitude;
 289    float groundProximityThreshold = Mathf.Abs(sinkRate) * 5f;  // Adjust threshold as necessary.
 290    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
 2103    accelerationInput = Vector3.ClampMagnitude(accelerationInput, maxNormalAcceleration);
 2104    _accelerationInput = accelerationInput;
 2105    return accelerationInput;
 2106  }
 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}