ArmBow.cs 5.6 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. //禁止准备的外部接口
  15. public bool banReady = false;
  16. //禁止射击的外部接口
  17. public bool banShoot = false;
  18. public static ArmBow ins;
  19. public Quaternion[] recordRotations = new Quaternion[100];
  20. public float[] recordRotationVars = new float[99];
  21. public int recordCount = 0;
  22. void Start()
  23. {
  24. ins = this;
  25. this.readyShoot();
  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. public void ready() {
  60. if (banReady) return;
  61. GameMgr.ins.gameMode.onBowReady();
  62. this.arrow.SetActive(true);
  63. AP_arm.play(0, WrapMode.Once);
  64. AP_bow.play(0, WrapMode.Once);
  65. AP_arm.completeCallback = onComplete;
  66. this.pulling = false;
  67. this.canShoot = false;
  68. }
  69. void idle() {
  70. AP_arm.play(1, WrapMode.Loop);
  71. AP_bow.play(1, WrapMode.Loop);
  72. AP_arm.completeCallback = null;
  73. this.pulling = false;
  74. this.canShoot = false;
  75. if (DebugBowPower.ins) DebugBowPower.ins.Init();
  76. }
  77. public void idleToADS() {
  78. AP_arm.play(2, WrapMode.Once);
  79. AP_bow.play(2, WrapMode.Once);
  80. AP_arm.completeCallback = onComplete;
  81. this.pulling = true;
  82. this.canShoot = false;
  83. }
  84. void ADS_idle() {
  85. this.canShoot = true;
  86. this.pulling = false;
  87. if (DebugBowPower.ins) DebugBowPower.ins.PullFinish();
  88. }
  89. public void ADS_fire() {
  90. if (!canShoot || banShoot || GameMgr.ins.gamePause) return;
  91. GameMgr.ins.gameMode.onBowShoot();
  92. this.pulling = false;
  93. this.canShoot = false;
  94. this.readying = false;
  95. AP_bow.play(0, WrapMode.Once);
  96. this.arrow.SetActive(false);
  97. shoot();
  98. }
  99. public void readyShoot() {
  100. this.bowCamera.setFieldOfView(60, false);
  101. // this.gameObject.SetActive(true);
  102. this.ready();
  103. }
  104. public void shoot() {
  105. // Vector3 rayHitPoint = CrossHair.ins.getRayHitPoint();
  106. // 筛选出一个稳定的发射角度---start
  107. Quaternion best_rotation = this.bowCamera.transform.rotation;
  108. if (recordCount >= recordRotations.Length) {
  109. int single_check_count = 6;
  110. float min_wave = float.MaxValue;
  111. for (int i = 0; i < recordRotationVars.Length; i++)
  112. {
  113. recordRotationVars[i] = Quaternion.Angle(recordRotations[i], recordRotations[i + 1]);
  114. if (i >= single_check_count - 1)
  115. {
  116. float wave = 0;
  117. for (int j = i; j > i - single_check_count; j--)
  118. {
  119. wave += recordRotationVars[j];
  120. }
  121. if (wave < min_wave)
  122. {
  123. min_wave = wave;
  124. best_rotation = recordRotations[i - single_check_count + 1];
  125. }
  126. }
  127. }
  128. }
  129. // 筛选出一个稳定的发射角度---end
  130. GameObject arrowCopy = GameObject.Instantiate(this.arrow, this.bowCamera.transform.position, best_rotation);
  131. Vector3 s1 = arrowCopy.transform.localScale;
  132. Vector3 s2 = bowCamera.transform.localScale;
  133. arrowCopy.transform.localScale = new Vector3(s1.x * s2.x, s1.y * s2.y, s1.z * s2.z);
  134. // arrowCopy.transform.LookAt(rayHitPoint);
  135. Arrow arrowComp = arrowCopy.AddComponent<Arrow>();
  136. arrowComp.armBow = this;
  137. // Arrow.speed = DebugBowPower.ins.getPowerPercent() * 100f;
  138. // Arrow.speed = BaseSpeedSlider.ins.getValue();
  139. // Arrow.speed = Mathf.Pow(ShootCheck.ins.shootSpeed, ShootCheck.ins.shootSpeed < 13 ? 2f: (ShootCheck.ins.shootSpeed < 15 ? 2.5f : 3.0f));
  140. if (ShootCheck.ins && !GameMgr.debugInEditor) {
  141. Arrow.speed = ShootCheck.ins.shootSpeed / 16 * 80;
  142. }
  143. arrowCopy.SetActive(true);
  144. arrow.SetActive(false);
  145. AudioMgr.ins.PlayShoot(AudioMgr.GetAudioSource(arrowCopy));
  146. // this.gameObject.SetActive(false);
  147. }
  148. public void OnEnable()
  149. {
  150. AudioMgr.GetAudioSource(this.gameObject).clip = null;
  151. }
  152. //debug
  153. public void mouseDown()
  154. {
  155. if (!this.readying) return;
  156. if (this.pulling || this.canShoot) return;
  157. this.idleToADS();
  158. }
  159. public void mouseUp()
  160. {
  161. if (!this.readying) return;
  162. if (this.pulling) {
  163. this.idle();
  164. } else if (this.canShoot) {
  165. this.ADS_fire();
  166. }
  167. }
  168. }