ArmBow.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. }
  131. }
  132. }
  133. // if (recordCount >= recordRotations.Length) {
  134. // int single_check_count = 6;
  135. // float min_wave = float.MaxValue;
  136. // for (int i = 0; i < recordRotationVars.Length; i++)
  137. // {
  138. // recordRotationVars[i] = Quaternion.Angle(recordRotations[i], recordRotations[i + 1]);
  139. // if (i >= single_check_count - 1)
  140. // {
  141. // float wave = 0;
  142. // for (int j = i; j > i - single_check_count; j--)
  143. // {
  144. // wave += recordRotationVars[j];
  145. // }
  146. // if (wave < min_wave)
  147. // {
  148. // min_wave = wave;
  149. // best_rotation = recordRotations[i - single_check_count + 1];
  150. // }
  151. // }
  152. // }
  153. // }
  154. // 筛选出一个稳定的发射角度---end
  155. Vector3 shootOutPosition = this.bowCamera.transform.position;
  156. //如果弓指向太过朝下,在弓口发射可能会射穿地面,因此需要角度适当时,才从弓口射出。
  157. if (BowCamera.ins.transform.localEulerAngles.x < 30) shootOutPosition += this.bowCamera.transform.forward * 1.5f;
  158. GameObject arrowCopy = GameObject.Instantiate(this.arrow, shootOutPosition, best_rotation);
  159. Vector3 s1 = arrowCopy.transform.localScale;
  160. Vector3 s2 = bowCamera.transform.localScale;
  161. arrowCopy.transform.localScale = new Vector3(s1.x * s2.x, s1.y * s2.y, s1.z * s2.z);
  162. // arrowCopy.transform.LookAt(rayHitPoint);
  163. Arrow arrowComp = arrowCopy.AddComponent<Arrow>();
  164. arrowComp.armBow = this;
  165. arrowComp.shootOutPosition = shootOutPosition;
  166. // Arrow.speed = DebugBowPower.ins.getPowerPercent() * 100f;
  167. // Arrow.speed = BaseSpeedSlider.ins.getValue();
  168. // Arrow.speed = Mathf.Pow(ShootCheck.ins.shootSpeed, ShootCheck.ins.shootSpeed < 13 ? 2f: (ShootCheck.ins.shootSpeed < 15 ? 2.5f : 3.0f));
  169. if (ShootCheck.ins && !GameMgr.debugInEditor)
  170. {
  171. Arrow.speed = GameMgr.RealSizeToGameSize(ShootCheck.ins.shootSpeed);
  172. }
  173. arrowCopy.SetActive(true);
  174. arrow.SetActive(false);
  175. AudioMgr.ins.PlayShoot(AudioMgr.GetAudioSource(arrowCopy));
  176. // this.gameObject.SetActive(false);
  177. }
  178. public void OnEnable()
  179. {
  180. AudioMgr.GetAudioSource(this.gameObject).clip = null;
  181. }
  182. public void Hide()
  183. {
  184. this.transform.localScale = Vector3.zero;
  185. }
  186. public void Show()
  187. {
  188. this.transform.localScale = new Vector3(1, 1, 1);
  189. }
  190. public bool NeedCrossHair()
  191. {
  192. return canShoot;
  193. }
  194. //debug
  195. public void mouseDown()
  196. {
  197. if (!this.readying) return;
  198. if (this.pulling || this.canShoot) return;
  199. this.idleToADS();
  200. }
  201. public void mouseUp()
  202. {
  203. if (!this.readying) return;
  204. if (this.pulling) {
  205. this.idle();
  206. } else if (this.canShoot) {
  207. this.ADS_fire();
  208. }
  209. }
  210. }