ArmBow.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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.SetCameraFieldOfView(60);
  121. this.ready();
  122. }
  123. void shoot() {
  124. // 筛选出一个稳定的发射角度
  125. Quaternion absolute_rotation = this.bowCamera.transform.rotation;
  126. Quaternion final_rotation = this.bowCamera.transform.rotation;
  127. if (recordCount >= recordRotations.Length) {
  128. int single_check_count = 6;
  129. float min_wave = float.MaxValue;
  130. for (int i = 0; i < recordRotationVars.Length; i++)
  131. {
  132. recordRotationVars[i] = Quaternion.Angle(recordRotations[i], recordRotations[i + 1]);
  133. if (i >= single_check_count - 1)
  134. {
  135. float wave = 0;
  136. for (int j = i; j > i - single_check_count; j--)
  137. {
  138. wave += recordRotationVars[j];
  139. }
  140. if (wave < min_wave)
  141. {
  142. min_wave = wave;
  143. int best_rotation_index = i - single_check_count + 1;
  144. absolute_rotation = recordRotations[best_rotation_index];
  145. best_rotation_index -= this.shootBackTime;
  146. if (best_rotation_index < 0) best_rotation_index = 0;
  147. final_rotation = recordRotations[best_rotation_index];
  148. }
  149. }
  150. }
  151. }
  152. Quaternion oldCameraRotation = Camera.main.transform.rotation;
  153. Camera.main.transform.rotation = absolute_rotation;
  154. RaycastHit absoluteRay = CrossHair.ins.GetRaycastHit();
  155. Camera.main.transform.rotation = oldCameraRotation;
  156. Vector3 shootOutPosition = this.bowCamera.transform.position;
  157. Vector3 arrowEuler = absolute_rotation.eulerAngles;
  158. arrowEuler.z = 0; //绝对角可能是从原始九轴记录数组里取出来的,它的z可能不是0
  159. GameObject arrowCopy = GameObject.Instantiate(this.arrow, shootOutPosition, Quaternion.Euler(arrowEuler));
  160. Vector3 s1 = arrowCopy.transform.localScale;
  161. Vector3 s2 = bowCamera.transform.localScale;
  162. arrowCopy.transform.localScale = new Vector3(s1.x * s2.x, s1.y * s2.y, s1.z * s2.z);
  163. Arrow arrowComp = arrowCopy.AddComponent<Arrow>();
  164. arrowComp.armBow = this;
  165. arrowComp.shootOutPosition = shootOutPosition;
  166. arrowComp.absoluteRay = absoluteRay;
  167. arrowComp.offsetAngle = GameDebug.ins ?
  168. GameDebug.ins.GetOffsetAngle() :
  169. FormatAngle(final_rotation.eulerAngles.x) - FormatAngle(absolute_rotation.eulerAngles.x);
  170. if (ShootCheck.ins && !GameMgr.debugInEditor)
  171. {
  172. Arrow.speed = GameMgr.RealSizeToGameSize(ShootCheck.ins.shootSpeed);
  173. }
  174. arrowCopy.SetActive(true);
  175. arrow.SetActive(false);
  176. AudioMgr.ins.PlayShoot(AudioMgr.GetAudioSource(arrowCopy));
  177. // this.gameObject.SetActive(false);
  178. if (AimHandler.ins) AimHandler.ins.BanControlObjRotate(true);
  179. }
  180. //因为Unity引擎的规则,向上时359~270度,向下是0~90度
  181. //为了方便数学运算,换算成 向上时0~90度,向下是0~-90度
  182. float FormatAngle(float value)
  183. {
  184. if (value > 180) value = 360 - value;
  185. else value = -value;
  186. return value;
  187. }
  188. public void OnEnable()
  189. {
  190. AudioMgr.GetAudioSource(this.gameObject).clip = null;
  191. }
  192. public void Hide()
  193. {
  194. this.transform.localScale = Vector3.zero;
  195. }
  196. public void Show()
  197. {
  198. this.transform.localScale = new Vector3(1, 1, 1);
  199. }
  200. public bool IsCanShoot()
  201. {
  202. return canShoot;
  203. }
  204. public void mouseDown()
  205. {
  206. if (!this.readying) return;
  207. if (this.pulling || this.canShoot) return;
  208. this.idleToADS();
  209. }
  210. public void mouseUp()
  211. {
  212. if (!this.readying) return;
  213. if (this.pulling) {
  214. this.idle();
  215. } else if (this.canShoot) {
  216. this.ADS_fire();
  217. }
  218. }
  219. }