| | 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 |
| 357 | 14 | | protected override void FixedUpdate() { |
| 357 | 15 | | base.FixedUpdate(); |
| 357 | 16 | | } |
| | 17 | |
|
| 0 | 18 | | protected override void UpdateReady(double deltaTime) {} |
| | 19 | |
|
| 0 | 20 | | protected override void UpdateBoost(double deltaTime) {} |
| | 21 | |
|
| 357 | 22 | | protected override void UpdateMidCourse(double deltaTime) { |
| 357 | 23 | | Vector3 accelerationInput = Vector3.zero; |
| | 24 | |
|
| 357 | 25 | | if (ShouldEvade()) { |
| 0 | 26 | | accelerationInput = EvadeInterceptor(GetClosestInterceptor()); |
| 714 | 27 | | } else if (HasAssignedTarget()) { |
| | 28 | | // Update waypoint and power setting |
| 357 | 29 | | UpdateWaypointAndPower(); |
| | 30 | |
|
| | 31 | | // Calculate and apply acceleration |
| 357 | 32 | | accelerationInput = CalculateAccelerationToWaypoint(); |
| 357 | 33 | | } |
| | 34 | |
|
| | 35 | | // For RotaryWingThreat, we don't need to compensate for gravity or consider drag |
| 357 | 36 | | GetComponent<Rigidbody>().AddForce(accelerationInput, ForceMode.Acceleration); |
| 357 | 37 | | } |
| | 38 | |
|
| 357 | 39 | | private void UpdateWaypointAndPower() { |
| 357 | 40 | | (_currentWaypoint, _currentPowerSetting) = |
| | 41 | | _attackBehavior.GetNextWaypoint(transform.position, _target.transform.position); |
| 357 | 42 | | } |
| | 43 | |
|
| 357 | 44 | | private Vector3 CalculateAccelerationToWaypoint() { |
| 357 | 45 | | float desiredSpeed = PowerTableLookup(_currentPowerSetting); |
| | 46 | |
|
| 357 | 47 | | IController controller = new WaypointController(this, desiredSpeed); |
| 357 | 48 | | Vector3 accelerationInput = controller.PlanToWaypoint(_currentWaypoint); |
| | 49 | |
|
| 357 | 50 | | Vector3 forwardAccelerationInput = Vector3.Project(accelerationInput, transform.forward); |
| 357 | 51 | | Vector3 normalAccelerationInput = accelerationInput - forwardAccelerationInput; |
| | 52 | |
|
| | 53 | | // Limit the acceleration magnitude. |
| 357 | 54 | | float maxForwardAcceleration = CalculateMaxForwardAcceleration(); |
| 357 | 55 | | forwardAccelerationInput = |
| | 56 | | Vector3.ClampMagnitude(forwardAccelerationInput, maxForwardAcceleration); |
| 357 | 57 | | float maxNormalAcceleration = CalculateMaxNormalAcceleration(); |
| 357 | 58 | | normalAccelerationInput = |
| | 59 | | Vector3.ClampMagnitude(normalAccelerationInput, maxNormalAcceleration); |
| 357 | 60 | | accelerationInput = forwardAccelerationInput + normalAccelerationInput; |
| | 61 | |
|
| 357 | 62 | | _accelerationInput = accelerationInput; |
| 357 | 63 | | return accelerationInput; |
| 357 | 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 | | } |