ArmBow.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class ArmBow : MonoBehaviour
  5. {
  6. [SerializeField] AnimationPlayer AP_arm;
  7. [SerializeField] AnimationPlayer AP_bow;
  8. [SerializeField] GameObject arrow;
  9. [SerializeField] BowCamera bowCamera;
  10. public HashSet<TargetBody> validTargets = new HashSet<TargetBody>();
  11. private bool canShoot = false;
  12. private bool pulling = false;
  13. private bool readying = false;
  14. //禁止准备的外部接口
  15. public bool banReady = false;
  16. //禁止射击的外部接口
  17. public bool banShoot = false;
  18. public static ArmBow ins;
  19. public Quaternion[] recordRotations = new Quaternion[100];
  20. public float[] recordRotationVars = new float[99];
  21. public int recordCount = 0;
  22. void Start()
  23. {
  24. ins = this;
  25. this.readyShoot();
  26. }
  27. void FixedUpdate()
  28. {
  29. if (this.canShoot)
  30. {
  31. if (DebugBowPower.ins) DebugBowPower.ins.DoUpdate();
  32. }
  33. }
  34. void Update()
  35. {
  36. if (Input.GetKeyDown(KeyCode.Q))
  37. {
  38. this.ADS_fire();
  39. }
  40. if (this.pulling) {
  41. this.bowCamera.updateFollowPullBow();
  42. }
  43. else if (!this.canShoot)
  44. {
  45. this.bowCamera.updateGiveUpPullBow();
  46. }
  47. }
  48. void onComplete(AnimationPlayerCompleteResult res) {
  49. if (res.index == 0) {
  50. this.readying = true;
  51. this.idle();
  52. this.Invoke("idleToADS", 0.1f);
  53. }
  54. else if (res.index == 2) {
  55. this.ADS_idle();
  56. }
  57. }
  58. public void ready() {
  59. if (banReady) return;
  60. GameMgr.ins.gameMode.PauseTimeCounting(this);
  61. GameMgr.ins.gameMode.onBowReady();
  62. this.arrow.SetActive(true);
  63. AP_arm.play(0, WrapMode.Once);
  64. AP_bow.play(0, WrapMode.Once);
  65. AP_arm.completeCallback = onComplete;
  66. this.pulling = false;
  67. this.canShoot = false;
  68. }
  69. void idle() {
  70. AP_arm.play(1, WrapMode.Loop);
  71. AP_bow.play(1, WrapMode.Loop);
  72. AP_arm.completeCallback = null;
  73. this.pulling = false;
  74. this.canShoot = false;
  75. if (DebugBowPower.ins) DebugBowPower.ins.Init();
  76. }
  77. public void idleToADS() {
  78. AP_arm.play(2, WrapMode.Once);
  79. AP_bow.play(2, WrapMode.Once);
  80. AP_arm.completeCallback = onComplete;
  81. this.pulling = true;
  82. this.canShoot = false;
  83. }
  84. void ADS_idle() {
  85. GameMgr.ins.gameMode.ResumeTimeCounting(this);
  86. this.canShoot = true;
  87. this.pulling = false;
  88. if (DebugBowPower.ins) DebugBowPower.ins.PullFinish();
  89. }
  90. public void ADS_fire() {
  91. if (!canShoot || banShoot || GameMgr.ins.gamePause) return;
  92. GameMgr.ins.gameMode.onBowShoot();
  93. this.pulling = false;
  94. this.canShoot = false;
  95. this.readying = false;
  96. AP_bow.play(0, WrapMode.Once);
  97. this.arrow.SetActive(false);
  98. shoot();
  99. }
  100. public void readyShoot() {
  101. this.bowCamera.setFieldOfView(60, false);
  102. // this.gameObject.SetActive(true);
  103. this.ready();
  104. }
  105. void shoot() {
  106. // Vector3 rayHitPoint = CrossHair.ins.getRayHitPoint();
  107. // 筛选出一个稳定的发射角度---start
  108. Quaternion best_rotation = this.bowCamera.transform.rotation;
  109. if (recordCount >= recordRotations.Length) {
  110. int single_check_count = 6;
  111. float min_wave = float.MaxValue;
  112. for (int i = 0; i < recordRotationVars.Length; i++)
  113. {
  114. recordRotationVars[i] = Quaternion.Angle(recordRotations[i], recordRotations[i + 1]);
  115. if (i >= single_check_count - 1)
  116. {
  117. float wave = 0;
  118. for (int j = i; j > i - single_check_count; j--)
  119. {
  120. wave += recordRotationVars[j];
  121. }
  122. if (wave < min_wave)
  123. {
  124. min_wave = wave;
  125. best_rotation = recordRotations[i - single_check_count + 1];
  126. }
  127. }
  128. }
  129. }
  130. // 筛选出一个稳定的发射角度---end
  131. Vector3 shootOutPosition = this.bowCamera.transform.position;
  132. //如果弓指向太过朝下,在弓口发射可能会射穿地面,因此需要角度适当时,才从弓口射出。
  133. if (BowCamera.ins.transform.localEulerAngles.x < 30) shootOutPosition += this.bowCamera.transform.forward * 1.5f;
  134. GameObject arrowCopy = GameObject.Instantiate(this.arrow, shootOutPosition, best_rotation);
  135. Vector3 s1 = arrowCopy.transform.localScale;
  136. Vector3 s2 = bowCamera.transform.localScale;
  137. arrowCopy.transform.localScale = new Vector3(s1.x * s2.x, s1.y * s2.y, s1.z * s2.z);
  138. // arrowCopy.transform.LookAt(rayHitPoint);
  139. Arrow arrowComp = arrowCopy.AddComponent<Arrow>();
  140. arrowComp.armBow = this;
  141. arrowComp.shootOutPosition = shootOutPosition;
  142. // Arrow.speed = DebugBowPower.ins.getPowerPercent() * 100f;
  143. // Arrow.speed = BaseSpeedSlider.ins.getValue();
  144. // Arrow.speed = Mathf.Pow(ShootCheck.ins.shootSpeed, ShootCheck.ins.shootSpeed < 13 ? 2f: (ShootCheck.ins.shootSpeed < 15 ? 2.5f : 3.0f));
  145. if (ShootCheck.ins && !GameMgr.debugInEditor)
  146. {
  147. float accRange = LoginMgr.myUserInfo.deviceAccValue;
  148. float pullDistance = ShootCheck.ins.shootSpeed / accRange * 36;
  149. int bowPound = DeviceMgr.ins.GetCurrentBowPound();
  150. if (bowPound == 18)
  151. {
  152. float x = pullDistance;
  153. double p1 = -1.188748333;
  154. double p2 = 0.033890372;
  155. double p3 = 3.408473747;
  156. double y = 1 / (p1 + p2 * Mathf.Pow(x, 0.5f) * Mathf.Log(x) + p3 / Mathf.Pow(x, 0.5f));
  157. Arrow.speed = GameMgr.RealSizeToGameSize((float) y);
  158. }
  159. else if (bowPound == 25)
  160. {
  161. float x = pullDistance;
  162. double p1 = -1.567293395;
  163. double p2 = 0.039774484;
  164. double p3 = 4.73453844;
  165. double y = 1 / (p1 + p2 * Mathf.Pow(x, 0.5f) * Mathf.Log(x) + p3 / Mathf.Pow(x, 0.5f));
  166. Arrow.speed = GameMgr.RealSizeToGameSize((float) y);
  167. }
  168. }
  169. arrowCopy.SetActive(true);
  170. arrow.SetActive(false);
  171. AudioMgr.ins.PlayShoot(AudioMgr.GetAudioSource(arrowCopy));
  172. // this.gameObject.SetActive(false);
  173. }
  174. public void OnEnable()
  175. {
  176. AudioMgr.GetAudioSource(this.gameObject).clip = null;
  177. }
  178. public void Hide()
  179. {
  180. this.transform.localScale = Vector3.zero;
  181. }
  182. public void Show()
  183. {
  184. this.transform.localScale = new Vector3(1, 1, 1);
  185. }
  186. public bool NeedCrossHair()
  187. {
  188. return canShoot;
  189. }
  190. //debug
  191. public void mouseDown()
  192. {
  193. if (!this.readying) return;
  194. if (this.pulling || this.canShoot) return;
  195. this.idleToADS();
  196. }
  197. public void mouseUp()
  198. {
  199. if (!this.readying) return;
  200. if (this.pulling) {
  201. this.idle();
  202. } else if (this.canShoot) {
  203. this.ADS_fire();
  204. }
  205. }
  206. }