ArmBow.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. public static ArmBow ins;
  15. public Quaternion[] recordRotations = new Quaternion[100];
  16. public float[] recordRotationVars = new float[99];
  17. public int recordCount = 0;
  18. void Start()
  19. {
  20. ins = this;
  21. this.readyShoot();
  22. }
  23. void OnDestroy()
  24. {
  25. ins = null;
  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. CrossHair.ins.gameObject.SetActive(this.canShoot);
  48. }
  49. void onComplete(AnimationPlayerCompleteResult res) {
  50. if (res.index == 0) {
  51. this.readying = true;
  52. this.idle();
  53. this.Invoke("idleToADS", 0.1f);
  54. }
  55. else if (res.index == 2) {
  56. this.ADS_idle();
  57. }
  58. }
  59. void ready() {
  60. this.arrow.SetActive(true);
  61. AP_arm.play(0, WrapMode.Once);
  62. AP_bow.play(0, WrapMode.Once);
  63. AP_arm.completeCallback = onComplete;
  64. this.pulling = false;
  65. this.canShoot = false;
  66. }
  67. void idle() {
  68. AP_arm.play(1, WrapMode.Loop);
  69. AP_bow.play(1, WrapMode.Loop);
  70. AP_arm.completeCallback = null;
  71. this.pulling = false;
  72. this.canShoot = false;
  73. if (DebugBowPower.ins) DebugBowPower.ins.Init();
  74. }
  75. public void idleToADS() {
  76. AP_arm.play(2, WrapMode.Once);
  77. AP_bow.play(2, WrapMode.Once);
  78. AP_arm.completeCallback = onComplete;
  79. this.pulling = true;
  80. this.canShoot = false;
  81. }
  82. void ADS_idle() {
  83. this.canShoot = true;
  84. this.pulling = false;
  85. if (DebugBowPower.ins) DebugBowPower.ins.PullFinish();
  86. }
  87. public void ADS_fire() {
  88. this.pulling = false;
  89. this.canShoot = false;
  90. this.readying = false;
  91. AP_arm.play(3, WrapMode.Once);
  92. AP_bow.play(3, WrapMode.Once);
  93. AP_arm.completeCallback = null;
  94. this.arrow.SetActive(false);
  95. this.Invoke("shoot", 0.1f);
  96. }
  97. public void readyShoot() {
  98. this.bowCamera.setFieldOfView(60, false);
  99. this.gameObject.SetActive(true);
  100. this.ready();
  101. }
  102. public void shoot() {
  103. // Vector3 rayHitPoint = CrossHair.ins.getRayHitPoint();
  104. // 筛选出一个稳定的发射角度---start
  105. Quaternion best_rotation = this.bowCamera.transform.rotation;
  106. if (recordCount >= recordRotations.Length) {
  107. int single_check_count = 6;
  108. float min_wave = float.MaxValue;
  109. for (int i = 0; i < recordRotationVars.Length; i++)
  110. {
  111. recordRotationVars[i] = Quaternion.Angle(recordRotations[i], recordRotations[i + 1]);
  112. if (i >= single_check_count - 1)
  113. {
  114. float wave = 0;
  115. for (int j = i; j > i - single_check_count; j--)
  116. {
  117. wave += recordRotationVars[j];
  118. }
  119. if (wave < min_wave)
  120. {
  121. min_wave = wave;
  122. best_rotation = recordRotations[i - single_check_count + 1];
  123. }
  124. }
  125. }
  126. }
  127. // 筛选出一个稳定的发射角度---end
  128. GameObject arrowCopy = GameObject.Instantiate(this.arrow, this.bowCamera.transform.position, best_rotation);
  129. Vector3 s1 = arrowCopy.transform.localScale;
  130. Vector3 s2 = bowCamera.transform.localScale;
  131. arrowCopy.transform.localScale = new Vector3(s1.x * s2.x, s1.y * s2.y, s1.z * s2.z);
  132. // arrowCopy.transform.LookAt(rayHitPoint);
  133. Arrow arrowComp = arrowCopy.AddComponent<Arrow>();
  134. arrowComp.armBow = this;
  135. // Arrow.speed = DebugBowPower.ins.getPowerPercent() * 100f;
  136. // Arrow.speed = BaseSpeedSlider.ins.getValue();
  137. // Arrow.speed = Mathf.Pow(ShootCheck.ins.shootSpeed, ShootCheck.ins.shootSpeed < 13 ? 2f: (ShootCheck.ins.shootSpeed < 15 ? 2.5f : 3.0f));
  138. Arrow.speed = ShootCheck.ins.shootSpeed/16*80;
  139. arrowCopy.SetActive(true);
  140. arrow.SetActive(false);
  141. AudioMgr.ins.PlayShoot(AudioMgr.GetAudioSource(arrowCopy));
  142. this.gameObject.SetActive(false);
  143. }
  144. public void OnEnable()
  145. {
  146. AudioMgr.GetAudioSource(this.gameObject).clip = null;
  147. }
  148. //debug
  149. public void mouseDown()
  150. {
  151. if (!this.readying) return;
  152. if (this.pulling || this.canShoot) return;
  153. this.idleToADS();
  154. }
  155. public void mouseUp()
  156. {
  157. if (!this.readying) return;
  158. if (this.pulling) {
  159. this.idle();
  160. } else if (this.canShoot) {
  161. this.ADS_fire();
  162. }
  163. }
  164. }
  165. // public class ArmBow : MonoBehaviour
  166. // {
  167. // [SerializeField] AnimationPlayer AP_arm;
  168. // [SerializeField] Animator AP_bow;
  169. // [SerializeField] AnimationPlayer AP_bowForArrow;
  170. // [SerializeField] GameObject arrow;
  171. // [SerializeField] BowCamera bowCamera;
  172. // public HashSet<TargetBody> validTargets = new HashSet<TargetBody>();
  173. // private bool canShoot = false;
  174. // private bool pulling = false;
  175. // private bool readying = false;
  176. // public static ArmBow ins;
  177. // void Start()
  178. // {
  179. // ins = this;
  180. // this.readyShoot();
  181. // }
  182. // void OnDestroy()
  183. // {
  184. // ins = null;
  185. // }
  186. // void FixedUpdate()
  187. // {
  188. // // if (this.canShoot)
  189. // // {
  190. // // DebugBowPower.ins.DoUpdate();
  191. // // }
  192. // }
  193. // void Update()
  194. // {
  195. // if (Input.GetKeyDown(KeyCode.Q))
  196. // {
  197. // this.ADS_fire();
  198. // }
  199. // if (this.pulling) {
  200. // this.bowCamera.updateFollowPullBow();
  201. // } else if (!this.canShoot)
  202. // {
  203. // this.bowCamera.updateGiveUpPullBow();
  204. // }
  205. // CrossHair.ins.gameObject.SetActive(this.canShoot);
  206. // }
  207. // void onComplete(AnimationPlayerCompleteResult res) {
  208. // if (res.index == 1) {
  209. // this.ADS_idle();
  210. // } else if (res.index == 3) {
  211. // this.idle();
  212. // }
  213. // }
  214. // void idle() {
  215. // this.arrow.SetActive(false);
  216. // AP_arm.play(0, WrapMode.Loop);
  217. // AP_bowForArrow.play(0, WrapMode.Loop);
  218. // // AP_bow.SetBool("ads", false);
  219. // // AP_bow.SetBool("fire", false);
  220. // AP_bow.Play("gong_daiji");
  221. // AP_arm.completeCallback = null;
  222. // this.pulling = false;
  223. // this.canShoot = false;
  224. // // DebugBowPower.ins.Init();
  225. // }
  226. // public void idleToADS() {
  227. // this.arrow.SetActive(true);
  228. // AP_arm.play(1, WrapMode.Once);
  229. // AP_bowForArrow.play(1, WrapMode.Once);
  230. // // AP_bow.SetBool("ads", true);
  231. // // AP_bow.SetBool("idle", false);
  232. // AP_bow.Play("gong_lagong");
  233. // AP_arm.completeCallback = onComplete;
  234. // this.pulling = true;
  235. // this.canShoot = false;
  236. // }
  237. // void ADS_idle() {
  238. // AP_arm.play(2, WrapMode.Loop);
  239. // AP_bowForArrow.play(2, WrapMode.Loop);
  240. // AP_bow.Play("gong_lagongdaiji");
  241. // AP_arm.completeCallback = null;
  242. // this.canShoot = true;
  243. // this.pulling = false;
  244. // // DebugBowPower.ins.PullFinish();
  245. // }
  246. // public void ADS_fire() {
  247. // if (!this.canShoot)
  248. // {
  249. // return;
  250. // }
  251. // this.pulling = false;
  252. // this.canShoot = false;
  253. // this.readying = false;
  254. // AP_arm.play(3, WrapMode.Once);
  255. // AP_bowForArrow.play(3, WrapMode.Once);
  256. // // AP_bow.SetBool("fire", true);
  257. // AP_bow.Play("gong_songshou");
  258. // AP_arm.completeCallback = onComplete;
  259. // this.Invoke("shoot", 0.1f);
  260. // }
  261. // public void readyShoot() {
  262. // this.readying = true;
  263. // this.bowCamera.setFieldOfView(60, false);
  264. // this.idle();
  265. // this.gameObject.SetActive(true);
  266. // this.Invoke("idleToADS", 0.5f);
  267. // }
  268. // public void shoot() {
  269. // Vector3 rayHitPoint = CrossHair.ins.getRayHitPoint();
  270. // GameObject arrowCopy = GameObject.Instantiate(this.arrow, this.bowCamera.transform.position, this.bowCamera.transform.rotation);
  271. // Vector3 s1 = arrowCopy.transform.localScale;
  272. // Vector3 s2 = bowCamera.transform.localScale;
  273. // arrowCopy.transform.localScale = new Vector3(s1.x * s2.x, s1.y * s2.y, s1.z * s2.z);
  274. // arrowCopy.transform.LookAt(rayHitPoint);
  275. // Arrow arrowComp = arrowCopy.AddComponent<Arrow>();
  276. // arrowComp.armBow = this;
  277. // // arrowComp.calculateSpeed(rayHitPoint);
  278. // // Arrow.speed = BaseSpeedSlider.ins.getValue() * DebugBowPower.ins.getPowerPercent();
  279. // // Arrow.speed = BaseSpeedSlider.ins.getValue();
  280. // Arrow.speed = Mathf.Pow(ShootCheck.ins.shootSpeed, ShootCheck.ins.shootSpeed < 13 ? 2f: (ShootCheck.ins.shootSpeed < 15 ? 2.5f : 3.0f));
  281. // arrowCopy.SetActive(true);
  282. // arrow.SetActive(false);
  283. // AudioMgr.ins.PlayShoot(AudioMgr.GetAudioSource(arrowCopy));
  284. // this.gameObject.SetActive(false);
  285. // }
  286. // public void OnEnable()
  287. // {
  288. // AudioMgr.GetAudioSource(this.gameObject).clip = null;
  289. // }
  290. // //debug
  291. // public void mouseDown()
  292. // {
  293. // if (!this.readying) return;
  294. // if (this.pulling || this.canShoot) return;
  295. // this.idleToADS();
  296. // }
  297. // public void mouseUp()
  298. // {
  299. // if (!this.readying) return;
  300. // if (this.pulling) {
  301. // this.idle();
  302. // } else if (this.canShoot) {
  303. // this.ADS_fire();
  304. // }
  305. // }
  306. // }