ArmBow.cs 9.2 KB

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