ArmBow.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. //过去几帧的镜头值记录
  34. Quaternion[] cameraRotations = new Quaternion[6];
  35. int cameraRotationHasRecordCount = 0;
  36. public static ArmBow ins;
  37. void Awake()
  38. {
  39. ins = this;
  40. //initdata
  41. int currentShootLevel = LoginMgr.myUserInfo.shootLevel;
  42. int[] shootBackTimes = {0, 1, 5};
  43. shootBackTime = shootBackTimes[currentShootLevel];
  44. }
  45. void Start()
  46. {
  47. this.readyShoot();
  48. }
  49. void FixedUpdate()
  50. {
  51. if (this.canShoot)
  52. {
  53. if (DebugBowPower.ins) DebugBowPower.ins.DoUpdate();
  54. }
  55. // 记录一些旋转角---start
  56. if (this.bowCamera) {
  57. for (int i = cameraRotations.Length - 1; i > 0 ; i--) {
  58. cameraRotations[i] = cameraRotations[i - 1];
  59. }
  60. cameraRotations[0] = this.bowCamera.transform.rotation;
  61. cameraRotationHasRecordCount++;
  62. }
  63. // 记录一些旋转角---end
  64. }
  65. void Update()
  66. {
  67. if (Input.GetKeyDown(KeyCode.Q))
  68. {
  69. this.ADS_fire();
  70. }
  71. if (this.pulling) {
  72. this.bowCamera.updateFollowPullBow();
  73. }
  74. else if (!this.canShoot)
  75. {
  76. this.bowCamera.updateGiveUpPullBow();
  77. }
  78. }
  79. void onComplete(AnimationPlayerCompleteResult res) {
  80. if (res.index == 0) {
  81. this.readying = true;
  82. this.idle();
  83. this.Invoke("idleToADS", 0.1f);
  84. }
  85. else if (res.index == 2) {
  86. this.ADS_idle();
  87. }
  88. }
  89. public void ready() {
  90. if (banReady) return;
  91. GameMgr.ins.gameMode.PauseTimeCounting(this);
  92. GameMgr.ins.gameMode.onBowReady();
  93. this.arrow.SetActive(true);
  94. AP_arm.play(0, WrapMode.Once);
  95. AP_bow.play(0, WrapMode.Once);
  96. AP_arm.completeCallback = onComplete;
  97. this.pulling = false;
  98. this.canShoot = false;
  99. }
  100. void idle() {
  101. AP_arm.play(1, WrapMode.Loop);
  102. AP_bow.play(1, WrapMode.Loop);
  103. AP_arm.completeCallback = null;
  104. this.pulling = false;
  105. this.canShoot = false;
  106. if (DebugBowPower.ins) DebugBowPower.ins.Init();
  107. }
  108. public void idleToADS() {
  109. AP_arm.play(2, WrapMode.Once);
  110. AP_bow.play(2, WrapMode.Once);
  111. AP_arm.completeCallback = onComplete;
  112. this.pulling = true;
  113. this.canShoot = false;
  114. }
  115. void ADS_idle() {
  116. GameMgr.ins.gameMode.ResumeTimeCounting(this);
  117. this.canShoot = true;
  118. this.pulling = false;
  119. if (DebugBowPower.ins) DebugBowPower.ins.PullFinish();
  120. }
  121. public void ADS_fire() {
  122. if (!canShoot || banShoot || GameMgr.ins.gamePause) return;
  123. GameMgr.ins.gameMode.onBowShoot();
  124. this.pulling = false;
  125. this.canShoot = false;
  126. this.readying = false;
  127. AP_bow.play(0, WrapMode.Once);
  128. this.arrow.SetActive(false);
  129. shoot();
  130. }
  131. public void readyShoot() {
  132. this.bowCamera.SetCameraFieldOfViewRecord(60);
  133. this.ready();
  134. }
  135. void shoot() {
  136. // 筛选出一个稳定的发射角度
  137. // #region
  138. // bool useAxisData = false;//表示是否使用了九轴原始数据
  139. // Quaternion absolute_rotation = this.bowCamera.transform.rotation;
  140. // Quaternion final_rotation = this.bowCamera.transform.rotation;
  141. // if (recordCount >= recordRotations.Length) {
  142. // for (int i = 0; i < recordRotationVars.Length; i++) {
  143. // recordRotationVars[i] = Quaternion.Angle(recordRotations[i], recordRotations[i + 1]);
  144. // }
  145. // int startCheckIndex = 5;
  146. // int checkAfterCount = 5;
  147. // float min_wave = float.MaxValue;
  148. // for (int i = startCheckIndex; i < recordRotationVars.Length - checkAfterCount; i++)
  149. // {
  150. // float wave = 0;
  151. // for (int j = i; j <= i + checkAfterCount; j++)
  152. // {
  153. // wave += recordRotationVars[j];
  154. // }
  155. // if (wave < min_wave)
  156. // {
  157. // min_wave = wave;
  158. // int best_rotation_index = i;
  159. // absolute_rotation = recordRotations[best_rotation_index];
  160. // best_rotation_index -= this.shootBackTime;
  161. // if (best_rotation_index < 0) best_rotation_index = 0;
  162. // final_rotation = recordRotations[best_rotation_index];
  163. // useAxisData = true;
  164. // }
  165. // }
  166. // if (useAxisData) {//坐标轴换算,原始九轴数据直接用可能会出问题,比如相机的父节点Rotation不为identity时
  167. // Quaternion baseQuat = GameMgr.ins.transform.rotation;
  168. // absolute_rotation = baseQuat * absolute_rotation;
  169. // final_rotation = baseQuat * final_rotation;
  170. // }
  171. // }
  172. // #endregion
  173. #region
  174. Quaternion absolute_rotation = this.bowCamera.transform.rotation;
  175. Quaternion final_rotation = this.bowCamera.transform.rotation;
  176. if (cameraRotationHasRecordCount >= cameraRotations.Length) {
  177. absolute_rotation = cameraRotations[5];
  178. final_rotation = cameraRotations[5 - this.shootBackTime];
  179. }
  180. #endregion
  181. Quaternion oldCameraRotation = Camera.main.transform.rotation;
  182. Camera.main.transform.rotation = absolute_rotation;
  183. RaycastHit absoluteRay = CrossHair.ins.GetRaycastHit();
  184. Camera.main.transform.rotation = oldCameraRotation;
  185. Vector3 shootOutPosition = this.bowCamera.transform.position;
  186. Vector3 arrowEuler = absolute_rotation.eulerAngles;
  187. arrowEuler.z = 0; //绝对角可能是从原始九轴记录数组里取出来的,它的z可能不是0
  188. GameObject arrowCopy = GameObject.Instantiate(this.arrow, shootOutPosition, Quaternion.Euler(arrowEuler));
  189. Vector3 s1 = arrowCopy.transform.localScale;
  190. Vector3 s2 = bowCamera.transform.localScale;
  191. arrowCopy.transform.localScale = new Vector3(s1.x * s2.x, s1.y * s2.y, s1.z * s2.z);
  192. Arrow arrowComp = arrowCopy.AddComponent<Arrow>();
  193. arrowComp.armBow = this;
  194. arrowComp.shootOutPosition = shootOutPosition;
  195. arrowComp.absoluteRay = absoluteRay;
  196. arrowComp.offsetAngle = GameDebug.ins ?
  197. GameDebug.ins.GetOffsetAngle() :
  198. FormatAngle(Quaternion.Angle(absolute_rotation, final_rotation));
  199. arrowComp.finalAngleAfterOffset = final_rotation.eulerAngles;
  200. if (ShootCheck.ins && !GameMgr.debugInEditor && !BowCamera.isTouchMode)
  201. {
  202. Arrow.speed = GameMgr.RealSizeToGameSize(ShootCheck.ins.shootSpeed);
  203. }
  204. arrowCopy.SetActive(true);
  205. arrow.SetActive(false);
  206. GameEventCenter.ins.onBowArrowShootOut?.Invoke(this, arrowComp);
  207. AudioMgr.ins.PlayShoot(AudioMgr.GetAudioSource(arrowCopy));
  208. // this.gameObject.SetActive(false);
  209. if (AimHandler.ins) AimHandler.ins.BanControlObjRotate(true);
  210. }
  211. //因为Unity引擎的规则,向上时359~270度,向下是0~90度
  212. //为了方便数学运算,换算成 向上时0~90度,向下是0~-90度
  213. float FormatAngle(float value)
  214. {
  215. if (value > 180) value = 360 - value;
  216. else value = -value;
  217. return value;
  218. }
  219. public void OnEnable()
  220. {
  221. AudioMgr.GetAudioSource(this.gameObject).clip = null;
  222. }
  223. public void Hide()
  224. {
  225. this.transform.localScale = Vector3.zero;
  226. }
  227. public void Show()
  228. {
  229. this.transform.localScale = new Vector3(1, 1, 1);
  230. }
  231. public bool IsCanShoot()
  232. {
  233. return canShoot;
  234. }
  235. public void mouseDown()
  236. {
  237. if (!this.readying) return;
  238. if (this.pulling || this.canShoot) return;
  239. this.idleToADS();
  240. }
  241. public void mouseUp()
  242. {
  243. if (!this.readying) return;
  244. if (this.pulling) {
  245. this.idle();
  246. } else if (this.canShoot) {
  247. this.ADS_fire();
  248. }
  249. }
  250. }