< Summary

Class:FixedWingThreat
Assembly:bamlab.micromissiles
File(s):/github/workspace/Assets/Scripts/Threats/FixedWingThreat.cs
Covered lines:0
Uncovered lines:64
Coverable lines:64
Total lines:117
Line coverage:0% (0 of 64)
Covered branches:0
Total branches:0
Covered methods:0
Total methods:9
Method coverage:0% (0 of 9)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
FixedWingThreat()0%2100%
Start()0%2100%
FixedUpdate()0%2100%
UpdateReady(...)0%2100%
UpdateBoost(...)0%2100%
UpdateMidCourse(...)0%20400%
UpdateWaypointAndPower()0%2100%
CalculateAccelerationInput()0%12300%
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]
 07  private float _navigationGain = 50f;
 8
 9  private Vector3 _accelerationInput;
 010  private double _elapsedTime = 0;
 11  private Rigidbody _rigidbody;
 12
 13  // Start is called before the first frame update
 014  protected override void Start() {
 015    base.Start();
 016    _rigidbody = GetComponent<Rigidbody>();
 017  }
 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
 067  private Vector3 CalculateAccelerationInput() {
 68    // Cache the transform and velocity.
 069    Transform threatTransform = transform;
 070    Vector3 right = threatTransform.right;
 071    Vector3 forward = threatTransform.forward;
 072    Vector3 position = threatTransform.position;
 073    Vector3 velocity = GetVelocity();
 074    float speed = velocity.magnitude;
 75
 076    IController controller = new PnController(this, _navigationGain);
 077    Vector3 accelerationInput = controller.PlanToWaypoint(_currentWaypoint);
 78
 79    // Clamp the normal acceleration input to the maximum normal acceleration.
 080    float maxNormalAcceleration = CalculateMaxNormalAcceleration();
 081    accelerationInput = Vector3.ClampMagnitude(accelerationInput, maxNormalAcceleration);
 82
 83    // Avoid the ground when close to the surface and too low on the glideslope.
 084    float altitude = position.y;
 85    // Sink rate is opposite to climb rate.
 086    float sinkRate = -velocity.y;
 087    float distanceToTarget = (_currentWaypoint - position).magnitude;
 088    float groundProximityThreshold = Mathf.Abs(sinkRate) * 5f;  // Adjust threshold as necessary
 089    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
 0102    accelerationInput = Vector3.ClampMagnitude(accelerationInput, maxNormalAcceleration);
 0103    _accelerationInput = accelerationInput;
 0104    return accelerationInput;
 0105  }
 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}