ArmBow.cs 10 KB

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