ArmBow.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. // 筛选出一个稳定的发射角度---start
  112. Quaternion best_best_rotation = this.bowCamera.transform.rotation; //最终的结果不包含shootBackTime处理
  113. Quaternion best_rotation = this.bowCamera.transform.rotation; //最终的结果包含shootBackTime处理
  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;
  131. best_best_rotation = recordRotations[best_rotation_index];
  132. best_rotation_index -= this.shootBackTime;
  133. if (best_rotation_index < 0) best_rotation_index = 0;
  134. best_rotation = recordRotations[best_rotation_index];
  135. }
  136. }
  137. }
  138. }
  139. Quaternion oldCameraRotation = Camera.main.transform.rotation;
  140. Camera.main.transform.rotation = best_best_rotation;
  141. RaycastHit raycastHit = CrossHair.ins.GetRaycastHit();
  142. Camera.main.transform.rotation = best_rotation;
  143. RaycastHit raycastHit1 = CrossHair.ins.GetRaycastHit();
  144. Camera.main.transform.rotation = oldCameraRotation;
  145. // if (recordCount >= recordRotations.Length) {
  146. // int single_check_count = 6;
  147. // float min_wave = float.MaxValue;
  148. // for (int i = 0; i < recordRotationVars.Length; i++)
  149. // {
  150. // recordRotationVars[i] = Quaternion.Angle(recordRotations[i], recordRotations[i + 1]);
  151. // if (i >= single_check_count - 1)
  152. // {
  153. // float wave = 0;
  154. // for (int j = i; j > i - single_check_count; j--)
  155. // {
  156. // wave += recordRotationVars[j];
  157. // }
  158. // if (wave < min_wave)
  159. // {
  160. // min_wave = wave;
  161. // best_rotation = recordRotations[i - single_check_count + 1];
  162. // }
  163. // }
  164. // }
  165. // }
  166. // 筛选出一个稳定的发射角度---end
  167. Vector3 shootOutPosition = this.bowCamera.transform.position;
  168. GameObject arrowCopy = GameObject.Instantiate(this.arrow, shootOutPosition, best_rotation);
  169. Vector3 s1 = arrowCopy.transform.localScale;
  170. Vector3 s2 = bowCamera.transform.localScale;
  171. arrowCopy.transform.localScale = new Vector3(s1.x * s2.x, s1.y * s2.y, s1.z * s2.z);
  172. // arrowCopy.transform.LookAt(rayHitPoint);
  173. Arrow arrowComp = arrowCopy.AddComponent<Arrow>();
  174. arrowComp.armBow = this;
  175. arrowComp.shootOutPosition = shootOutPosition;
  176. arrowComp.raycastHit = raycastHit;
  177. arrowComp.raycastHit1 = raycastHit1;
  178. // Arrow.speed = DebugBowPower.ins.getPowerPercent() * 100f;
  179. // Arrow.speed = BaseSpeedSlider.ins.getValue();
  180. // Arrow.speed = Mathf.Pow(ShootCheck.ins.shootSpeed, ShootCheck.ins.shootSpeed < 13 ? 2f: (ShootCheck.ins.shootSpeed < 15 ? 2.5f : 3.0f));
  181. if (ShootCheck.ins && !GameMgr.debugInEditor)
  182. {
  183. Arrow.speed = GameMgr.RealSizeToGameSize(ShootCheck.ins.shootSpeed);
  184. }
  185. arrowCopy.SetActive(true);
  186. arrow.SetActive(false);
  187. AudioMgr.ins.PlayShoot(AudioMgr.GetAudioSource(arrowCopy));
  188. // this.gameObject.SetActive(false);
  189. }
  190. public void OnEnable()
  191. {
  192. AudioMgr.GetAudioSource(this.gameObject).clip = null;
  193. }
  194. public void Hide()
  195. {
  196. this.transform.localScale = Vector3.zero;
  197. }
  198. public void Show()
  199. {
  200. this.transform.localScale = new Vector3(1, 1, 1);
  201. }
  202. public bool NeedCrossHair()
  203. {
  204. return canShoot;
  205. }
  206. //debug
  207. public void mouseDown()
  208. {
  209. if (!this.readying) return;
  210. if (this.pulling || this.canShoot) return;
  211. this.idleToADS();
  212. }
  213. public void mouseUp()
  214. {
  215. if (!this.readying) return;
  216. if (this.pulling) {
  217. this.idle();
  218. } else if (this.canShoot) {
  219. this.ADS_fire();
  220. }
  221. }
  222. }