< Summary

Class:FixedWingThreat
Assembly:bamlab.micromissiles
File(s):/github/workspace/Assets/Scripts/Threats/FixedWingThreat.cs
Covered lines:26
Uncovered lines:38
Coverable lines:64
Total lines:117
Line coverage:40.6% (26 of 64)
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.073080%
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
 13  // Start is called before the first frame update
 814  protected override void Start() {
 815    base.Start();
 816    _rigidbody = GetComponent<Rigidbody>();
 817  }
 18
 19  // Update is called once per frame
 020  protected override void FixedUpdate() {
 021    base.FixedUpdate();
 022  }
 23
 024  protected override void UpdateReady(double deltaTime) {}
 25
 026  protected override void UpdateBoost(double deltaTime) {}
 27
 028  protected override void UpdateMidCourse(double deltaTime) {
 029    _elapsedTime += deltaTime;
 030    Vector3 accelerationInput = Vector3.zero;
 31
 032    if (ShouldEvade()) {
 033      accelerationInput = EvadeInterceptor(GetClosestInterceptor());
 034    } else if (HasAssignedTarget()) {
 35      // Update waypoint and power setting
 036      UpdateWaypointAndPower();
 37
 038      float sensorUpdatePeriod = 1f / dynamicAgentConfig.dynamic_config.sensor_config.frequency;
 039      if (_elapsedTime >= sensorUpdatePeriod) {
 40        // TODO: Implement guidance filter to estimate state from sensor output
 41        // For now, we'll use the threat's actual state
 042        _elapsedTime = 0;
 043      }
 44
 45      // Calculate the normal acceleration input using PN
 046      Vector3 normalAcceleration = CalculateAccelerationInput();
 47
 48      // Adjust the speed based on power setting
 049      Vector3 forwardAcceleration = CalculateForwardAcceleration();
 50
 51      // Combine the accelerations
 052      accelerationInput = normalAcceleration + forwardAcceleration;
 053    }
 54
 55    // Calculate and set the total acceleration
 056    Vector3 acceleration = CalculateAcceleration(accelerationInput);
 057    _rigidbody.AddForce(acceleration, ForceMode.Acceleration);
 058  }
 59
 060  private void UpdateWaypointAndPower() {
 61    // Get the next waypoint and power setting from the attack behavior
 62    // TODO: Implement support for SENSORS to update the track on the target position
 063    (_currentWaypoint, _currentPowerSetting) =
 64        _attackBehavior.GetNextWaypoint(transform.position, _target.transform.position);
 065  }
 66
 267  private Vector3 CalculateAccelerationInput() {
 68    // Cache the transform and velocity.
 269    Transform threatTransform = transform;
 270    Vector3 right = threatTransform.right;
 271    Vector3 forward = threatTransform.forward;
 272    Vector3 position = threatTransform.position;
 273    Vector3 velocity = GetVelocity();
 274    float speed = velocity.magnitude;
 75
 276    IController controller = new PnController(this, _navigationGain);
 277    Vector3 accelerationInput = controller.PlanToWaypoint(_currentWaypoint);
 78
 79    // Clamp the normal acceleration input to the maximum normal acceleration.
 280    float maxNormalAcceleration = CalculateMaxNormalAcceleration();
 281    accelerationInput = Vector3.ClampMagnitude(accelerationInput, maxNormalAcceleration);
 82
 83    // Avoid the ground when close to the surface and too low on the glideslope.
 284    float altitude = position.y;
 85    // Sink rate is opposite to climb rate.
 286    float sinkRate = -velocity.y;
 287    float distanceToTarget = (_currentWaypoint - position).magnitude;
 288    float groundProximityThreshold = Mathf.Abs(sinkRate) * 5f;  // Adjust threshold as necessary
 289    if (sinkRate > 0 && altitude / sinkRate < distanceToTarget / speed) {
 90      // Evade upward normal to the velocity.
 091      Vector3 upwardsDirection = Vector3.Cross(forward, right);
 92
 93      // Blend between the calculated acceleration input and the upward acceleration.
 094      float blendFactor = 1 - (altitude / groundProximityThreshold);
 095      accelerationInput.y =
 96          Vector3
 97              .Lerp(accelerationInput, upwardsDirection * CalculateMaxNormalAcceleration(),
 98                    blendFactor)
 99              .y;
 0100    }
 101
 2102    accelerationInput = Vector3.ClampMagnitude(accelerationInput, maxNormalAcceleration);
 2103    _accelerationInput = accelerationInput;
 2104    return accelerationInput;
 2105  }
 106
 107  // Optional: Add this method to visualize debug information
 0108  protected virtual void OnDrawGizmos() {
 0109    if (Application.isPlaying) {
 0110      Gizmos.color = Color.yellow;
 0111      Gizmos.DrawLine(transform.position, _currentWaypoint);
 112
 0113      Gizmos.color = Color.green;
 0114      Gizmos.DrawRay(transform.position, _accelerationInput);
 0115    }
 0116  }
 117}