ArmBow.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. // 筛选出一个稳定的发射角度
  112. Quaternion absolute_rotation = this.bowCamera.transform.rotation;
  113. Quaternion final_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;
  131. absolute_rotation = recordRotations[best_rotation_index];
  132. best_rotation_index -= this.shootBackTime;
  133. if (best_rotation_index < 0) best_rotation_index = 0;
  134. final_rotation = recordRotations[best_rotation_index];
  135. }
  136. }
  137. }
  138. }
  139. Quaternion oldCameraRotation = Camera.main.transform.rotation;
  140. Camera.main.transform.rotation = absolute_rotation;
  141. RaycastHit absoluteRay = CrossHair.ins.GetRaycastHit();
  142. Camera.main.transform.rotation = oldCameraRotation;
  143. Vector3 shootOutPosition = this.bowCamera.transform.position;
  144. GameObject arrowCopy = GameObject.Instantiate(this.arrow, shootOutPosition, absolute_rotation);
  145. Vector3 s1 = arrowCopy.transform.localScale;
  146. Vector3 s2 = bowCamera.transform.localScale;
  147. arrowCopy.transform.localScale = new Vector3(s1.x * s2.x, s1.y * s2.y, s1.z * s2.z);
  148. Arrow arrowComp = arrowCopy.AddComponent<Arrow>();
  149. arrowComp.armBow = this;
  150. arrowComp.shootOutPosition = shootOutPosition;
  151. arrowComp.absoluteRay = absoluteRay;
  152. arrowComp.offsetAngle = GameDebug.ins ?
  153. GameDebug.ins.GetOffsetAngle() :
  154. FormatAngle(final_rotation.eulerAngles.x) - FormatAngle(absolute_rotation.eulerAngles.x);
  155. if (ShootCheck.ins && !GameMgr.debugInEditor)
  156. {
  157. Arrow.speed = GameMgr.RealSizeToGameSize(ShootCheck.ins.shootSpeed);
  158. }
  159. arrowCopy.SetActive(true);
  160. arrow.SetActive(false);
  161. AudioMgr.ins.PlayShoot(AudioMgr.GetAudioSource(arrowCopy));
  162. // this.gameObject.SetActive(false);
  163. }
  164. float FormatAngle(float value)
  165. {
  166. if (value > 180) value = 360 - value;
  167. else value = -value;
  168. return value;
  169. }
  170. public void OnEnable()
  171. {
  172. AudioMgr.GetAudioSource(this.gameObject).clip = null;
  173. }
  174. public void Hide()
  175. {
  176. this.transform.localScale = Vector3.zero;
  177. }
  178. public void Show()
  179. {
  180. this.transform.localScale = new Vector3(1, 1, 1);
  181. }
  182. public bool IsCanShoot()
  183. {
  184. return canShoot;
  185. }
  186. //debug
  187. public void mouseDown()
  188. {
  189. if (!this.readying) return;
  190. if (this.pulling || this.canShoot) return;
  191. this.idleToADS();
  192. }
  193. public void mouseUp()
  194. {
  195. if (!this.readying) return;
  196. if (this.pulling) {
  197. this.idle();
  198. } else if (this.canShoot) {
  199. this.ADS_fire();
  200. }
  201. }
  202. }