ArmBow.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class ArmBow : MonoBehaviour
  5. {
  6. [SerializeField] AnimationPlayer AP_arm;
  7. [SerializeField] AnimationPlayer AP_bow;
  8. [SerializeField] GameObject arrow;
  9. [SerializeField] BowCamera bowCamera;
  10. public HashSet<TargetBody> validTargets = new HashSet<TargetBody>();
  11. private bool canShoot = false;
  12. private bool pulling = false;
  13. private bool readying = false;
  14. public static ArmBow ins;
  15. public Quaternion[] recordRotations = new Quaternion[100];
  16. public float[] recordRotationVars = new float[99];
  17. public int recordCount = 0;
  18. void Start()
  19. {
  20. ins = this;
  21. this.readyShoot();
  22. }
  23. void OnDestroy()
  24. {
  25. ins = null;
  26. }
  27. void FixedUpdate()
  28. {
  29. if (this.canShoot)
  30. {
  31. if (DebugBowPower.ins) DebugBowPower.ins.DoUpdate();
  32. }
  33. }
  34. void Update()
  35. {
  36. if (Input.GetKeyDown(KeyCode.Q))
  37. {
  38. this.ADS_fire();
  39. }
  40. if (this.pulling) {
  41. this.bowCamera.updateFollowPullBow();
  42. }
  43. else if (!this.canShoot)
  44. {
  45. this.bowCamera.updateGiveUpPullBow();
  46. }
  47. CrossHair.ins.gameObject.SetActive(this.canShoot);
  48. }
  49. void onComplete(AnimationPlayerCompleteResult res) {
  50. if (res.index == 0) {
  51. this.readying = true;
  52. this.idle();
  53. this.Invoke("idleToADS", 0.1f);
  54. }
  55. else if (res.index == 2) {
  56. this.ADS_idle();
  57. }
  58. }
  59. void ready() {
  60. this.arrow.SetActive(true);
  61. AP_arm.play(0, WrapMode.Once);
  62. AP_bow.play(0, WrapMode.Once);
  63. AP_arm.completeCallback = onComplete;
  64. this.pulling = false;
  65. this.canShoot = false;
  66. }
  67. void idle() {
  68. AP_arm.play(1, WrapMode.Loop);
  69. AP_bow.play(1, WrapMode.Loop);
  70. AP_arm.completeCallback = null;
  71. this.pulling = false;
  72. this.canShoot = false;
  73. if (DebugBowPower.ins) DebugBowPower.ins.Init();
  74. }
  75. public void idleToADS() {
  76. AP_arm.play(2, WrapMode.Once);
  77. AP_bow.play(2, WrapMode.Once);
  78. AP_arm.completeCallback = onComplete;
  79. this.pulling = true;
  80. this.canShoot = false;
  81. }
  82. void ADS_idle() {
  83. this.canShoot = true;
  84. this.pulling = false;
  85. if (DebugBowPower.ins) DebugBowPower.ins.PullFinish();
  86. }
  87. public void ADS_fire() {
  88. if (!canShoot) return;
  89. this.pulling = false;
  90. this.canShoot = false;
  91. this.readying = false;
  92. AP_arm.play(3, WrapMode.Once);
  93. AP_bow.play(3, WrapMode.Once);
  94. AP_arm.completeCallback = null;
  95. this.arrow.SetActive(false);
  96. this.Invoke("shoot", 0.1f);
  97. }
  98. public void readyShoot() {
  99. this.bowCamera.setFieldOfView(60, false);
  100. this.gameObject.SetActive(true);
  101. this.ready();
  102. }
  103. public void shoot() {
  104. // Vector3 rayHitPoint = CrossHair.ins.getRayHitPoint();
  105. // 筛选出一个稳定的发射角度---start
  106. Quaternion best_rotation = this.bowCamera.transform.rotation;
  107. if (recordCount >= recordRotations.Length) {
  108. int single_check_count = 6;
  109. float min_wave = float.MaxValue;
  110. for (int i = 0; i < recordRotationVars.Length; i++)
  111. {
  112. recordRotationVars[i] = Quaternion.Angle(recordRotations[i], recordRotations[i + 1]);
  113. if (i >= single_check_count - 1)
  114. {
  115. float wave = 0;
  116. for (int j = i; j > i - single_check_count; j--)
  117. {
  118. wave += recordRotationVars[j];
  119. }
  120. if (wave < min_wave)
  121. {
  122. min_wave = wave;
  123. best_rotation = recordRotations[i - single_check_count + 1];
  124. }
  125. }
  126. }
  127. }
  128. // 筛选出一个稳定的发射角度---end
  129. GameObject arrowCopy = GameObject.Instantiate(this.arrow, this.bowCamera.transform.position, best_rotation);
  130. Vector3 s1 = arrowCopy.transform.localScale;
  131. Vector3 s2 = bowCamera.transform.localScale;
  132. arrowCopy.transform.localScale = new Vector3(s1.x * s2.x, s1.y * s2.y, s1.z * s2.z);
  133. // arrowCopy.transform.LookAt(rayHitPoint);
  134. Arrow arrowComp = arrowCopy.AddComponent<Arrow>();
  135. arrowComp.armBow = this;
  136. // Arrow.speed = DebugBowPower.ins.getPowerPercent() * 100f;
  137. // Arrow.speed = BaseSpeedSlider.ins.getValue();
  138. // Arrow.speed = Mathf.Pow(ShootCheck.ins.shootSpeed, ShootCheck.ins.shootSpeed < 13 ? 2f: (ShootCheck.ins.shootSpeed < 15 ? 2.5f : 3.0f));
  139. if (ShootCheck.ins) {
  140. Arrow.speed = ShootCheck.ins.shootSpeed / 16 * 80;
  141. }
  142. arrowCopy.SetActive(true);
  143. arrow.SetActive(false);
  144. AudioMgr.ins.PlayShoot(AudioMgr.GetAudioSource(arrowCopy));
  145. this.gameObject.SetActive(false);
  146. }
  147. public void OnEnable()
  148. {
  149. AudioMgr.GetAudioSource(this.gameObject).clip = null;
  150. }
  151. //debug
  152. public void mouseDown()
  153. {
  154. if (!this.readying) return;
  155. if (this.pulling || this.canShoot) return;
  156. this.idleToADS();
  157. }
  158. public void mouseUp()
  159. {
  160. if (!this.readying) return;
  161. if (this.pulling) {
  162. this.idle();
  163. } else if (this.canShoot) {
  164. this.ADS_fire();
  165. }
  166. }
  167. }