ArmBow.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /* 弓对象 */
  5. public class ArmBow : MonoBehaviour
  6. {
  7. [SerializeField] AnimationPlayer AP_arm;
  8. [SerializeField] AnimationPlayer AP_bow;
  9. [SerializeField] GameObject arrow;
  10. [SerializeField] BowCamera bowCamera;
  11. public HashSet<TargetBody> validTargets = new HashSet<TargetBody>();
  12. public int shootBackTime = 0;
  13. private bool canShoot = false;
  14. private bool pulling = false;
  15. private bool readying = false;
  16. //禁止准备的外部接口
  17. public bool banReady = false;
  18. //禁止射击的外部接口
  19. public bool banShoot = false;
  20. public static ArmBow ins;
  21. public Quaternion[] recordRotations = new Quaternion[100];
  22. public float[] recordRotationVars = new float[99];
  23. public int recordCount = 0;
  24. void Start()
  25. {
  26. ins = this;
  27. this.readyShoot();
  28. //initdata
  29. int currentShootLevel = LoginMgr.myUserInfo.shootLevel;
  30. int[] shootBackTimes = {0, 1, 5};
  31. shootBackTime = shootBackTimes[currentShootLevel];
  32. }
  33. void FixedUpdate()
  34. {
  35. if (this.canShoot)
  36. {
  37. if (DebugBowPower.ins) DebugBowPower.ins.DoUpdate();
  38. }
  39. }
  40. void Update()
  41. {
  42. if (Input.GetKeyDown(KeyCode.Q))
  43. {
  44. this.ADS_fire();
  45. }
  46. if (this.pulling) {
  47. this.bowCamera.updateFollowPullBow();
  48. }
  49. else if (!this.canShoot)
  50. {
  51. this.bowCamera.updateGiveUpPullBow();
  52. }
  53. }
  54. void onComplete(AnimationPlayerCompleteResult res) {
  55. if (res.index == 0) {
  56. this.readying = true;
  57. this.idle();
  58. this.Invoke("idleToADS", 0.1f);
  59. }
  60. else if (res.index == 2) {
  61. this.ADS_idle();
  62. }
  63. }
  64. public void ready() {
  65. if (banReady) return;
  66. GameMgr.ins.gameMode.PauseTimeCounting(this);
  67. GameMgr.ins.gameMode.onBowReady();
  68. this.arrow.SetActive(true);
  69. AP_arm.play(0, WrapMode.Once);
  70. AP_bow.play(0, WrapMode.Once);
  71. AP_arm.completeCallback = onComplete;
  72. this.pulling = false;
  73. this.canShoot = false;
  74. }
  75. void idle() {
  76. AP_arm.play(1, WrapMode.Loop);
  77. AP_bow.play(1, WrapMode.Loop);
  78. AP_arm.completeCallback = null;
  79. this.pulling = false;
  80. this.canShoot = false;
  81. if (DebugBowPower.ins) DebugBowPower.ins.Init();
  82. }
  83. public void idleToADS() {
  84. AP_arm.play(2, WrapMode.Once);
  85. AP_bow.play(2, WrapMode.Once);
  86. AP_arm.completeCallback = onComplete;
  87. this.pulling = true;
  88. this.canShoot = false;
  89. }
  90. void ADS_idle() {
  91. GameMgr.ins.gameMode.ResumeTimeCounting(this);
  92. this.canShoot = true;
  93. this.pulling = false;
  94. if (DebugBowPower.ins) DebugBowPower.ins.PullFinish();
  95. }
  96. public void ADS_fire() {
  97. if (!canShoot || banShoot || GameMgr.ins.gamePause) return;
  98. GameMgr.ins.gameMode.onBowShoot();
  99. this.pulling = false;
  100. this.canShoot = false;
  101. this.readying = false;
  102. AP_bow.play(0, WrapMode.Once);
  103. this.arrow.SetActive(false);
  104. shoot();
  105. }
  106. public void readyShoot() {
  107. this.bowCamera.setFieldOfView(60, false);
  108. // this.gameObject.SetActive(true);
  109. this.ready();
  110. }
  111. void shoot() {
  112. // 筛选出一个稳定的发射角度
  113. Quaternion absolute_rotation = this.bowCamera.transform.rotation;
  114. Quaternion final_rotation = this.bowCamera.transform.rotation;
  115. if (recordCount >= recordRotations.Length) {
  116. int single_check_count = 6;
  117. float min_wave = float.MaxValue;
  118. for (int i = 0; i < recordRotationVars.Length; i++)
  119. {
  120. recordRotationVars[i] = Quaternion.Angle(recordRotations[i], recordRotations[i + 1]);
  121. if (i >= single_check_count - 1)
  122. {
  123. float wave = 0;
  124. for (int j = i; j > i - single_check_count; j--)
  125. {
  126. wave += recordRotationVars[j];
  127. }
  128. if (wave < min_wave)
  129. {
  130. min_wave = wave;
  131. int best_rotation_index = i - single_check_count + 1;
  132. absolute_rotation = recordRotations[best_rotation_index];
  133. best_rotation_index -= this.shootBackTime;
  134. if (best_rotation_index < 0) best_rotation_index = 0;
  135. final_rotation = recordRotations[best_rotation_index];
  136. }
  137. }
  138. }
  139. }
  140. Quaternion oldCameraRotation = Camera.main.transform.rotation;
  141. Camera.main.transform.rotation = absolute_rotation;
  142. RaycastHit absoluteRay = CrossHair.ins.GetRaycastHit();
  143. Camera.main.transform.rotation = oldCameraRotation;
  144. Vector3 shootOutPosition = this.bowCamera.transform.position;
  145. Vector3 arrowEuler = absolute_rotation.eulerAngles;
  146. arrowEuler.z = 0; //绝对角可能是从原始九轴记录数组里取出来的,它的z可能不是0
  147. GameObject arrowCopy = GameObject.Instantiate(this.arrow, shootOutPosition, Quaternion.Euler(arrowEuler));
  148. Vector3 s1 = arrowCopy.transform.localScale;
  149. Vector3 s2 = bowCamera.transform.localScale;
  150. arrowCopy.transform.localScale = new Vector3(s1.x * s2.x, s1.y * s2.y, s1.z * s2.z);
  151. Arrow arrowComp = arrowCopy.AddComponent<Arrow>();
  152. arrowComp.armBow = this;
  153. arrowComp.shootOutPosition = shootOutPosition;
  154. arrowComp.absoluteRay = absoluteRay;
  155. arrowComp.offsetAngle = GameDebug.ins ?
  156. GameDebug.ins.GetOffsetAngle() :
  157. FormatAngle(final_rotation.eulerAngles.x) - FormatAngle(absolute_rotation.eulerAngles.x);
  158. if (ShootCheck.ins && !GameMgr.debugInEditor)
  159. {
  160. Arrow.speed = GameMgr.RealSizeToGameSize(ShootCheck.ins.shootSpeed);
  161. }
  162. //在提示板显示速度
  163. Billboard.ShowSpeed(Arrow.speed);
  164. arrowCopy.SetActive(true);
  165. arrow.SetActive(false);
  166. AudioMgr.ins.PlayShoot(AudioMgr.GetAudioSource(arrowCopy));
  167. // this.gameObject.SetActive(false);
  168. if (AimHandler.ins) AimHandler.ins.BanControlObjRotate(true);
  169. }
  170. //因为Unity引擎的规则,向上时359~270度,向下是0~90度
  171. //为了方便数学运算,换算成 向上时0~90度,向下是0~-90度
  172. float FormatAngle(float value)
  173. {
  174. if (value > 180) value = 360 - value;
  175. else value = -value;
  176. return value;
  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 IsCanShoot()
  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. }