Advertisement
imonk

Gun

Oct 17th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.89 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. public class UFOfire : MonoBehaviour
  7. {
  8. public Image HeatDisplay;
  9. public UFObeam UB;
  10. public AudioSource FireSound;
  11. public AudioClip FireAudio, CooldownAudio;
  12. public GameObject BulletUp, BulletDown, BulletLeft, BulletRight;
  13. public Transform BulletUpPosBulletDownPos, BulletLeftPos, BulletRightPos;
  14. public SpriteRenderer NoFire, FireUp, FireDown, FireLeft, FireRight;
  15. public bool CanFire;
  16. [HideInInspector]
  17. public bool isFireing, isFireingUp, isFireingLeft, isFireingRight, isFireingDown;
  18. public float Heat;
  19. int Fire;
  20.  
  21. private void Start()
  22. {
  23. StartCoroutine("CoolDown");
  24. }
  25. void Update()
  26. {
  27. HeatDisplay.fillAmount = Heat;
  28.  
  29. if (Fire == 1 && isFireing)
  30. {
  31. Fire = 2;
  32. StartCoroutine("MachineFire");
  33. }
  34. else
  35. {
  36. if(!isFireing)
  37. {
  38. Fire = 0;
  39. }
  40. }
  41. if (!UB.isBeaming)
  42. {
  43. if (Input.GetKey(KeyCode.UpArrow) && CanFire)
  44. {
  45. Heat += 0.003f;
  46.  
  47. if (Fire == 0)
  48. {
  49. Fire = 1;
  50. isFireing = true;
  51. }
  52.  
  53. NoFire.enabled = false;
  54. FireUp.enabled = true;
  55. isFireingUp = true;
  56. }
  57. else
  58. {
  59. isFireingUp = false;
  60. }
  61. else if (Input.GetKey(KeyCode.DownArrow) && CanFire)
  62. {
  63. Heat += 0.003f;
  64.  
  65. if (Fire == 0)
  66. {
  67. Fire = 1;
  68. isFireing = true;
  69. }
  70.  
  71. NoFire.enabled = false;
  72. FireDown.enabled = true;
  73. isFireingDown = true;
  74. }
  75. else
  76. {
  77. isFireingDown = false;
  78. }
  79. else if (Input.GetKey(KeyCode.LeftArrow) && CanFire)
  80. {
  81. Heat += 0.003f;
  82.  
  83. if (Fire == 0)
  84. {
  85. Fire = 1;
  86. isFireing = true;
  87. }
  88.  
  89. NoFire.enabled = false;
  90. FireLeft.enabled = true;
  91. isFireingLeft = true;
  92. }
  93. else
  94. {
  95. isFireingLeft = false;
  96. }
  97. else if (Input.GetKey(KeyCode.RightArrow) && CanFire)
  98. {
  99. Heat += 0.003f;
  100.  
  101. if (Fire == 0)
  102. {
  103. Fire = 1;
  104. isFireing = true;
  105. }
  106.  
  107. NoFire.enabled = false;
  108. FireRight.enabled = true;
  109. isFireingRight = true;
  110. }
  111. else
  112. {
  113. isFireingRight = false;
  114. }
  115. }
  116. if (!isFireingLeft && !isFireingRight && !isFireingDown && !isFireingUp)
  117. {
  118. isFireing = false;
  119. NoFire.enabled = true;
  120. FireUp.enabled = false;
  121. FireDown.enabled = false;
  122. FireLeft.enabled = false;
  123. FireRight.enabled = false;
  124. }
  125. if (Heat >= 1)
  126. {
  127. CanFire = false;
  128. }
  129.  
  130. }
  131. public IEnumerator MachineFire()
  132. {
  133. while (true)
  134. {
  135. yield return new WaitForSeconds(0.3f);
  136. if(Fire == 2)
  137. {
  138. if (isFireingUp)
  139. {
  140. var BD = GameObject.Instantiate(BulletUp, BulletUpPos.transform.position, transform.rotation);
  141. BD.GetComponent<Rigidbody2D>().AddForce(Vector2.up * 500, ForceMode2D.Force);
  142. BD.GetComponent<UFObullet>().StartCoroutine("ClearBullet");
  143. }
  144. if (isFireingDown)
  145. {
  146. var BD = GameObject.Instantiate(BulletDown, BulletDownPos.transform.position, transform.rotation);
  147. BD.GetComponent<Rigidbody2D>().AddForce(Vector2.down * 500, ForceMode2D.Force);
  148. BD.GetComponent<UFObullet>().StartCoroutine("ClearBullet");
  149. }
  150. if (isFireingLeft)
  151. {
  152. var BL = GameObject.Instantiate(BulletLeft, BulletLeftPos.transform.position, transform.rotation);
  153. BL.GetComponent<Rigidbody2D>().AddForce(Vector2.left * 500, ForceMode2D.Force);
  154. BL.GetComponent<UFObullet>().StartCoroutine("ClearBullet");
  155. }
  156. if (isFireingRight)
  157. {
  158. var BR = GameObject.Instantiate(BulletRight, BulletRightPos.transform.position, transform.rotation);
  159. BR.GetComponent<Rigidbody2D>().AddForce(Vector2.right * 500, ForceMode2D.Force);
  160. BR.GetComponent<UFObullet>().StartCoroutine("ClearBullet");
  161. }
  162.  
  163. FireSound.PlayOneShot(FireAudio);
  164. }
  165. else
  166. {
  167. yield break;
  168. }
  169. }
  170. }
  171.  
  172. public IEnumerator CoolDown()
  173. {
  174. while (true)
  175. {
  176. yield return new WaitForSeconds(0.3f);
  177. if (!isFireing)
  178. {
  179. if(Heat > 0)
  180. {
  181. CanFire = false;
  182. if (Heat <= 0)
  183. {
  184. Heat = 0;
  185. }
  186. else
  187. {
  188. Heat -= 0.1f;
  189. FireSound.PlayOneShot(CooldownAudio);
  190. }
  191. }
  192. else
  193. {
  194. CanFire = true;
  195. }
  196.  
  197. }
  198. }
  199. }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement