ArmBow.cs 9.1 KB

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