ArmBow.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. public int shootBackTime = 0;
  12. private bool canShoot = false;
  13. private bool pulling = false;
  14. private bool readying = false;
  15. //禁止准备的外部接口
  16. public bool banReady = false;
  17. //禁止射击的外部接口
  18. public bool banShoot = false;
  19. public static ArmBow ins;
  20. public Quaternion[] recordRotations = new Quaternion[100];
  21. public float[] recordRotationVars = new float[99];
  22. public int recordCount = 0;
  23. void Start()
  24. {
  25. ins = this;
  26. this.readyShoot();
  27. }
  28. void FixedUpdate()
  29. {
  30. if (this.canShoot)
  31. {
  32. if (DebugBowPower.ins) DebugBowPower.ins.DoUpdate();
  33. }
  34. }
  35. void Update()
  36. {
  37. if (Input.GetKeyDown(KeyCode.Q))
  38. {
  39. this.ADS_fire();
  40. }
  41. if (this.pulling) {
  42. this.bowCamera.updateFollowPullBow();
  43. }
  44. else if (!this.canShoot)
  45. {
  46. this.bowCamera.updateGiveUpPullBow();
  47. }
  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.PauseTimeCounting(this);
  62. GameMgr.ins.gameMode.onBowReady();
  63. this.arrow.SetActive(true);
  64. AP_arm.play(0, WrapMode.Once);
  65. AP_bow.play(0, WrapMode.Once);
  66. AP_arm.completeCallback = onComplete;
  67. this.pulling = false;
  68. this.canShoot = false;
  69. }
  70. void idle() {
  71. AP_arm.play(1, WrapMode.Loop);
  72. AP_bow.play(1, WrapMode.Loop);
  73. AP_arm.completeCallback = null;
  74. this.pulling = false;
  75. this.canShoot = false;
  76. if (DebugBowPower.ins) DebugBowPower.ins.Init();
  77. }
  78. public void idleToADS() {
  79. AP_arm.play(2, WrapMode.Once);
  80. AP_bow.play(2, WrapMode.Once);
  81. AP_arm.completeCallback = onComplete;
  82. this.pulling = true;
  83. this.canShoot = false;
  84. }
  85. void ADS_idle() {
  86. GameMgr.ins.gameMode.ResumeTimeCounting(this);
  87. this.canShoot = true;
  88. this.pulling = false;
  89. if (DebugBowPower.ins) DebugBowPower.ins.PullFinish();
  90. }
  91. public void ADS_fire() {
  92. if (!canShoot || banShoot || GameMgr.ins.gamePause) return;
  93. GameMgr.ins.gameMode.onBowShoot();
  94. this.pulling = false;
  95. this.canShoot = false;
  96. this.readying = false;
  97. AP_bow.play(0, WrapMode.Once);
  98. this.arrow.SetActive(false);
  99. shoot();
  100. }
  101. public void readyShoot() {
  102. this.bowCamera.setFieldOfView(60, false);
  103. // this.gameObject.SetActive(true);
  104. this.ready();
  105. }
  106. void shoot() {
  107. // Vector3 rayHitPoint = CrossHair.ins.getRayHitPoint();
  108. // 筛选出一个稳定的发射角度---start
  109. Quaternion best_rotation = this.bowCamera.transform.rotation;
  110. if (recordCount >= recordRotations.Length) {
  111. int single_check_count = 6;
  112. float min_wave = float.MaxValue;
  113. for (int i = 0; i < recordRotationVars.Length; i++)
  114. {
  115. recordRotationVars[i] = Quaternion.Angle(recordRotations[i], recordRotations[i + 1]);
  116. if (i >= single_check_count - 1)
  117. {
  118. float wave = 0;
  119. for (int j = i; j > i - single_check_count; j--)
  120. {
  121. wave += recordRotationVars[j];
  122. }
  123. if (wave < min_wave)
  124. {
  125. min_wave = wave;
  126. int best_rotation_index = i - single_check_count + 1 - this.shootBackTime;
  127. if (best_rotation_index < 0) best_rotation_index = 0;
  128. best_rotation = recordRotations[best_rotation_index];
  129. //显示射出角度
  130. Quaternion first_rotation = recordRotations[i - single_check_count + 1];
  131. ShootCheck.ins.shootAngleText.text =
  132. "最佳角度:" + first_rotation.eulerAngles + "\n" +
  133. "延迟角度:" + best_rotation.eulerAngles + "\n" +
  134. "角度偏差:" + Quaternion.Angle(first_rotation, best_rotation);
  135. }
  136. }
  137. }
  138. }
  139. // if (recordCount >= recordRotations.Length) {
  140. // int single_check_count = 6;
  141. // float min_wave = float.MaxValue;
  142. // for (int i = 0; i < recordRotationVars.Length; i++)
  143. // {
  144. // recordRotationVars[i] = Quaternion.Angle(recordRotations[i], recordRotations[i + 1]);
  145. // if (i >= single_check_count - 1)
  146. // {
  147. // float wave = 0;
  148. // for (int j = i; j > i - single_check_count; j--)
  149. // {
  150. // wave += recordRotationVars[j];
  151. // }
  152. // if (wave < min_wave)
  153. // {
  154. // min_wave = wave;
  155. // best_rotation = recordRotations[i - single_check_count + 1];
  156. // }
  157. // }
  158. // }
  159. // }
  160. // 筛选出一个稳定的发射角度---end
  161. Vector3 shootOutPosition = this.bowCamera.transform.position;
  162. //如果弓指向太过朝下,在弓口发射可能会射穿地面,因此需要角度适当时,才从弓口射出。
  163. if (BowCamera.ins.transform.localEulerAngles.x < 30) shootOutPosition += this.bowCamera.transform.forward * 1.5f;
  164. GameObject arrowCopy = GameObject.Instantiate(this.arrow, shootOutPosition, best_rotation);
  165. Vector3 s1 = arrowCopy.transform.localScale;
  166. Vector3 s2 = bowCamera.transform.localScale;
  167. arrowCopy.transform.localScale = new Vector3(s1.x * s2.x, s1.y * s2.y, s1.z * s2.z);
  168. // arrowCopy.transform.LookAt(rayHitPoint);
  169. Arrow arrowComp = arrowCopy.AddComponent<Arrow>();
  170. arrowComp.armBow = this;
  171. arrowComp.shootOutPosition = shootOutPosition;
  172. // Arrow.speed = DebugBowPower.ins.getPowerPercent() * 100f;
  173. // Arrow.speed = BaseSpeedSlider.ins.getValue();
  174. // Arrow.speed = Mathf.Pow(ShootCheck.ins.shootSpeed, ShootCheck.ins.shootSpeed < 13 ? 2f: (ShootCheck.ins.shootSpeed < 15 ? 2.5f : 3.0f));
  175. if (ShootCheck.ins && !GameMgr.debugInEditor)
  176. {
  177. Arrow.speed = GameMgr.RealSizeToGameSize(ShootCheck.ins.shootSpeed);
  178. }
  179. arrowCopy.SetActive(true);
  180. arrow.SetActive(false);
  181. AudioMgr.ins.PlayShoot(AudioMgr.GetAudioSource(arrowCopy));
  182. // this.gameObject.SetActive(false);
  183. }
  184. public void OnEnable()
  185. {
  186. AudioMgr.GetAudioSource(this.gameObject).clip = null;
  187. }
  188. public void Hide()
  189. {
  190. this.transform.localScale = Vector3.zero;
  191. }
  192. public void Show()
  193. {
  194. this.transform.localScale = new Vector3(1, 1, 1);
  195. }
  196. public bool NeedCrossHair()
  197. {
  198. return canShoot;
  199. }
  200. //debug
  201. public void mouseDown()
  202. {
  203. if (!this.readying) return;
  204. if (this.pulling || this.canShoot) return;
  205. this.idleToADS();
  206. }
  207. public void mouseUp()
  208. {
  209. if (!this.readying) return;
  210. if (this.pulling) {
  211. this.idle();
  212. } else if (this.canShoot) {
  213. this.ADS_fire();
  214. }
  215. }
  216. }