ArmBow.cs 9.5 KB

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