ArmBow.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. /* 弓对象 */
  6. public class ArmBow : MonoBehaviour
  7. {
  8. [SerializeField] AnimationPlayer AP_arm;
  9. [SerializeField] AnimationPlayer AP_bow;
  10. [SerializeField] GameObject arrow;
  11. BowCamera _bowCamera;
  12. BowCamera bowCamera {
  13. get {
  14. if (!_bowCamera) _bowCamera = GetComponentInParent<BowCamera>();
  15. return _bowCamera;
  16. }
  17. }
  18. //有效射击目标集合,可以理解为射中集合中的目标才能得分
  19. public HashSet<TargetBody> validTargets = new HashSet<TargetBody>();
  20. //射箭姿态记录索引倒退数,根据射击难度制造误差
  21. [NonSerialized] public int shootBackTime = 0;
  22. bool canShoot = false;
  23. bool pulling = false;
  24. bool readying = false;
  25. //禁止准备的外部接口
  26. [NonSerialized] public bool banReady = false;
  27. //禁止射击的外部接口
  28. [NonSerialized] public bool banShoot = false;
  29. //九轴姿态记录
  30. [NonSerialized] public Quaternion[] recordRotations = new Quaternion[100];
  31. float[] recordRotationVars = new float[99];
  32. [NonSerialized] public int recordCount = 0;
  33. public static ArmBow ins;
  34. void Awake()
  35. {
  36. ins = this;
  37. //initdata
  38. int currentShootLevel = LoginMgr.myUserInfo.shootLevel;
  39. int[] shootBackTimes = {0, 1, 5};
  40. shootBackTime = shootBackTimes[currentShootLevel];
  41. }
  42. void Start()
  43. {
  44. this.readyShoot();
  45. }
  46. void FixedUpdate()
  47. {
  48. if (this.canShoot)
  49. {
  50. if (DebugBowPower.ins) DebugBowPower.ins.DoUpdate();
  51. }
  52. }
  53. void Update()
  54. {
  55. if (Input.GetKeyDown(KeyCode.Q))
  56. {
  57. this.ADS_fire();
  58. }
  59. if (this.pulling) {
  60. this.bowCamera.updateFollowPullBow();
  61. }
  62. else if (!this.canShoot)
  63. {
  64. this.bowCamera.updateGiveUpPullBow();
  65. }
  66. }
  67. void onComplete(AnimationPlayerCompleteResult res) {
  68. if (res.index == 0) {
  69. this.readying = true;
  70. this.idle();
  71. this.Invoke("idleToADS", 0.1f);
  72. }
  73. else if (res.index == 2) {
  74. this.ADS_idle();
  75. }
  76. }
  77. public void ready() {
  78. if (banReady) return;
  79. GameMgr.ins.gameMode.PauseTimeCounting(this);
  80. GameMgr.ins.gameMode.onBowReady();
  81. this.arrow.SetActive(true);
  82. AP_arm.play(0, WrapMode.Once);
  83. AP_bow.play(0, WrapMode.Once);
  84. AP_arm.completeCallback = onComplete;
  85. this.pulling = false;
  86. this.canShoot = false;
  87. }
  88. void idle() {
  89. AP_arm.play(1, WrapMode.Loop);
  90. AP_bow.play(1, WrapMode.Loop);
  91. AP_arm.completeCallback = null;
  92. this.pulling = false;
  93. this.canShoot = false;
  94. if (DebugBowPower.ins) DebugBowPower.ins.Init();
  95. }
  96. public void idleToADS() {
  97. AP_arm.play(2, WrapMode.Once);
  98. AP_bow.play(2, WrapMode.Once);
  99. AP_arm.completeCallback = onComplete;
  100. this.pulling = true;
  101. this.canShoot = false;
  102. }
  103. void ADS_idle() {
  104. GameMgr.ins.gameMode.ResumeTimeCounting(this);
  105. this.canShoot = true;
  106. this.pulling = false;
  107. if (DebugBowPower.ins) DebugBowPower.ins.PullFinish();
  108. }
  109. public void ADS_fire() {
  110. if (!canShoot || banShoot || GameMgr.ins.gamePause) return;
  111. GameMgr.ins.gameMode.onBowShoot();
  112. this.pulling = false;
  113. this.canShoot = false;
  114. this.readying = false;
  115. AP_bow.play(0, WrapMode.Once);
  116. this.arrow.SetActive(false);
  117. shoot();
  118. }
  119. public void readyShoot() {
  120. this.bowCamera.SetCameraFieldOfViewRecord(60);
  121. this.ready();
  122. }
  123. void shoot() {
  124. // 筛选出一个稳定的发射角度
  125. #region
  126. bool useAxisData = false;//表示是否使用了九轴原始数据
  127. Quaternion absolute_rotation = this.bowCamera.transform.rotation;
  128. Quaternion final_rotation = this.bowCamera.transform.rotation;
  129. if (recordCount >= recordRotations.Length) {
  130. for (int i = 0; i < recordRotationVars.Length; i++) {
  131. recordRotationVars[i] = Quaternion.Angle(recordRotations[i], recordRotations[i + 1]);
  132. }
  133. int startCheckIndex = 5;
  134. int checkAfterCount = 5;
  135. float min_wave = float.MaxValue;
  136. for (int i = startCheckIndex; i < recordRotationVars.Length - checkAfterCount; i++)
  137. {
  138. float wave = 0;
  139. for (int j = i; j <= i + checkAfterCount; j++)
  140. {
  141. wave += recordRotationVars[j];
  142. }
  143. if (wave < min_wave)
  144. {
  145. min_wave = wave;
  146. int best_rotation_index = i;
  147. absolute_rotation = recordRotations[best_rotation_index];
  148. best_rotation_index -= this.shootBackTime;
  149. if (best_rotation_index < 0) best_rotation_index = 0;
  150. final_rotation = recordRotations[best_rotation_index];
  151. useAxisData = true;
  152. }
  153. }
  154. if (useAxisData) {//坐标轴换算,原始九轴数据直接用可能会出问题,比如相机的父节点Rotation不为identity时
  155. Quaternion baseQuat = GameMgr.ins.transform.rotation;
  156. absolute_rotation = baseQuat * absolute_rotation;
  157. final_rotation = baseQuat * final_rotation;
  158. }
  159. }
  160. #endregion
  161. Quaternion oldCameraRotation = Camera.main.transform.rotation;
  162. Camera.main.transform.rotation = absolute_rotation;
  163. RaycastHit absoluteRay = CrossHair.ins.GetRaycastHit();
  164. Camera.main.transform.rotation = oldCameraRotation;
  165. Vector3 shootOutPosition = this.bowCamera.transform.position;
  166. Vector3 arrowEuler = absolute_rotation.eulerAngles;
  167. arrowEuler.z = 0; //绝对角可能是从原始九轴记录数组里取出来的,它的z可能不是0
  168. GameObject arrowCopy = GameObject.Instantiate(this.arrow, shootOutPosition, Quaternion.Euler(arrowEuler));
  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. Arrow arrowComp = arrowCopy.AddComponent<Arrow>();
  173. arrowComp.armBow = this;
  174. arrowComp.shootOutPosition = shootOutPosition;
  175. arrowComp.absoluteRay = absoluteRay;
  176. arrowComp.offsetAngle = GameDebug.ins ?
  177. GameDebug.ins.GetOffsetAngle() :
  178. FormatAngle(Quaternion.Angle(absolute_rotation, final_rotation));
  179. if (ShootCheck.ins && !GameMgr.debugInEditor && !BowCamera.isTouchMode)
  180. {
  181. Arrow.speed = GameMgr.RealSizeToGameSize(ShootCheck.ins.shootSpeed);
  182. }
  183. arrowCopy.SetActive(true);
  184. arrow.SetActive(false);
  185. GameEventCenter.ins?.onBowArrowShootOut?.Invoke(this, arrowComp);
  186. AudioMgr.ins.PlayShoot(AudioMgr.GetAudioSource(arrowCopy));
  187. // this.gameObject.SetActive(false);
  188. if (AimHandler.ins) AimHandler.ins.BanControlObjRotate(true);
  189. }
  190. //因为Unity引擎的规则,向上时359~270度,向下是0~90度
  191. //为了方便数学运算,换算成 向上时0~90度,向下是0~-90度
  192. float FormatAngle(float value)
  193. {
  194. if (value > 180) value = 360 - value;
  195. else value = -value;
  196. return value;
  197. }
  198. public void OnEnable()
  199. {
  200. AudioMgr.GetAudioSource(this.gameObject).clip = null;
  201. }
  202. public void Hide()
  203. {
  204. this.transform.localScale = Vector3.zero;
  205. }
  206. public void Show()
  207. {
  208. this.transform.localScale = new Vector3(1, 1, 1);
  209. }
  210. public bool IsCanShoot()
  211. {
  212. return canShoot;
  213. }
  214. public void mouseDown()
  215. {
  216. if (!this.readying) return;
  217. if (this.pulling || this.canShoot) return;
  218. this.idleToADS();
  219. }
  220. public void mouseUp()
  221. {
  222. if (!this.readying) return;
  223. if (this.pulling) {
  224. this.idle();
  225. } else if (this.canShoot) {
  226. this.ADS_fire();
  227. }
  228. }
  229. }