ArmBow.cs 7.6 KB

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