| | 1 | | using System.Collections; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using UnityEngine; |
| | 4 | |
|
| | 5 | | public class RotaryWingThreat : Threat { |
| | 6 | | private Vector3 _accelerationInput; |
| | 7 | |
|
| | 8 | | // Start is called before the first frame update |
| 7 | 9 | | protected override void Start() { |
| 7 | 10 | | base.Start(); |
| 7 | 11 | | } |
| | 12 | |
|
| | 13 | | // Update is called once per frame |
| 0 | 14 | | protected override void FixedUpdate() { |
| 0 | 15 | | base.FixedUpdate(); |
| 0 | 16 | | } |
| | 17 | |
|
| 0 | 18 | | protected override void UpdateReady(double deltaTime) {} |
| | 19 | |
|
| 0 | 20 | | protected override void UpdateBoost(double deltaTime) {} |
| | 21 | |
|
| 0 | 22 | | protected override void UpdateMidCourse(double deltaTime) { |
| 0 | 23 | | Vector3 accelerationInput = Vector3.zero; |
| | 24 | |
|
| 0 | 25 | | if (ShouldEvade()) { |
| 0 | 26 | | accelerationInput = EvadeInterceptor(GetClosestInterceptor()); |
| 0 | 27 | | } else if (HasAssignedTarget()) { |
| | 28 | | // Update waypoint and power setting |
| 0 | 29 | | UpdateWaypointAndPower(); |
| | 30 | |
|
| | 31 | | // Calculate and apply acceleration |
| 0 | 32 | | accelerationInput = CalculateAccelerationToWaypoint(); |
| 0 | 33 | | } |
| | 34 | |
|
| | 35 | | // For RotaryWingThreat, we don't need to compensate for gravity or consider drag |
| 0 | 36 | | GetComponent<Rigidbody>().AddForce(accelerationInput, ForceMode.Acceleration); |
| 0 | 37 | | } |
| | 38 | |
|
| 0 | 39 | | private void UpdateWaypointAndPower() { |
| 0 | 40 | | (_currentWaypoint, _currentPowerSetting) = |
| | 41 | | _attackBehavior.GetNextWaypoint(transform.position, _target.transform.position); |
| 0 | 42 | | } |
| | 43 | |
|
| 3 | 44 | | private Vector3 CalculateAccelerationToWaypoint() { |
| 3 | 45 | | float desiredSpeed = PowerTableLookup(_currentPowerSetting); |
| | 46 | |
|
| 3 | 47 | | IController controller = new WaypointController(this, desiredSpeed); |
| 3 | 48 | | Vector3 accelerationInput = controller.PlanToWaypoint(_currentWaypoint); |
| | 49 | |
|
| 3 | 50 | | Vector3 forwardAccelerationInput = Vector3.Project(accelerationInput, transform.forward); |
| 3 | 51 | | Vector3 normalAccelerationInput = accelerationInput - forwardAccelerationInput; |
| | 52 | |
|
| | 53 | | // Limit the acceleration magnitude. |
| 3 | 54 | | float maxForwardAcceleration = CalculateMaxForwardAcceleration(); |
| 3 | 55 | | forwardAccelerationInput = |
| | 56 | | Vector3.ClampMagnitude(forwardAccelerationInput, maxForwardAcceleration); |
| 3 | 57 | | float maxNormalAcceleration = CalculateMaxNormalAcceleration(); |
| 3 | 58 | | normalAccelerationInput = |
| | 59 | | Vector3.ClampMagnitude(normalAccelerationInput, maxNormalAcceleration); |
| 3 | 60 | | accelerationInput = forwardAccelerationInput + normalAccelerationInput; |
| | 61 | |
|
| 3 | 62 | | _accelerationInput = accelerationInput; |
| 3 | 63 | | return accelerationInput; |
| 3 | 64 | | } |
| | 65 | |
|
| | 66 | | // Optional: Add this method to visualize debug information |
| 0 | 67 | | protected virtual void OnDrawGizmos() { |
| 0 | 68 | | if (Application.isPlaying) { |
| 0 | 69 | | Gizmos.color = Color.yellow; |
| 0 | 70 | | Gizmos.DrawLine(transform.position, _currentWaypoint); |
| | 71 | |
|
| 0 | 72 | | Gizmos.color = Color.green; |
| 0 | 73 | | Gizmos.DrawRay(transform.position, _accelerationInput); |
| 0 | 74 | | } |
| 0 | 75 | | } |
| | 76 | | } |