Advertisement
renderbydavid

UFOfire 2

Oct 17th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.98 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 BulletDown, BulletLeft, BulletRight;
  13.     public Transform BulletDownPos, BulletLeftPos, BulletRightPos;
  14.     public SpriteRenderer NoFire, FireDown, FireLeft, FireRight;
  15.     public bool CanFire;
  16.     [HideInInspector]
  17.     public bool isFireing, 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.DownArrow) && 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.                 FireDown.enabled = true;
  55.                 isFireingDown = true;
  56.             }
  57.             else
  58.             {
  59.                 isFireingDown = false;
  60.             }
  61.             if (Input.GetKey(KeyCode.LeftArrow) && 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.                 FireLeft.enabled = true;
  73.                 isFireingLeft = true;
  74.             }
  75.             else
  76.             {
  77.                 isFireingLeft = false;
  78.             }
  79.             if (Input.GetKey(KeyCode.RightArrow) && 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.                 FireRight.enabled = true;
  91.                 isFireingRight = true;
  92.             }
  93.             else
  94.             {
  95.                 isFireingRight = false;
  96.             }
  97.         }
  98.         if (!isFireingLeft && !isFireingRight && !isFireingDown)
  99.         {
  100.             isFireing = false;
  101.             NoFire.enabled = true;
  102.             FireDown.enabled = false;
  103.             FireLeft.enabled = false;
  104.             FireRight.enabled = false;
  105.         }
  106.         if (Heat >= 1)
  107.         {
  108.             CanFire = false;
  109.         }
  110.        
  111.     }
  112.     public IEnumerator MachineFire()
  113.     {
  114.         while (true)
  115.         {
  116.             yield return new WaitForSeconds(0.3f);
  117.             if(Fire == 2)
  118.             {
  119.                 if (isFireingDown)
  120.                 {
  121.                     var BD = GameObject.Instantiate(BulletDown, BulletDownPos.transform.position, transform.rotation);
  122.                     BD.GetComponent<Rigidbody2D>().AddForce(Vector2.down * 500, ForceMode2D.Force);
  123.                     BD.GetComponent<UFObullet>().StartCoroutine("ClearBullet");
  124.                 }
  125.                 if (isFireingLeft)
  126.                 {
  127.                     var BL = GameObject.Instantiate(BulletLeft, BulletLeftPos.transform.position, transform.rotation);
  128.                     BL.GetComponent<Rigidbody2D>().AddForce(Vector2.left * 500, ForceMode2D.Force);
  129.                     BL.GetComponent<UFObullet>().StartCoroutine("ClearBullet");
  130.                 }
  131.                 if (isFireingRight)
  132.                 {
  133.                     var BR = GameObject.Instantiate(BulletRight, BulletRightPos.transform.position, transform.rotation);
  134.                     BR.GetComponent<Rigidbody2D>().AddForce(Vector2.right * 500, ForceMode2D.Force);
  135.                     BR.GetComponent<UFObullet>().StartCoroutine("ClearBullet");
  136.                 }
  137.  
  138.                 FireSound.PlayOneShot(FireAudio);
  139.             }
  140.             else
  141.             {
  142.                 yield break;
  143.             }
  144.         }
  145.     }
  146.  
  147.     public IEnumerator CoolDown()
  148.     {
  149.         while (true)
  150.         {
  151.             yield return new WaitForSeconds(0.3f);            
  152.             if (!isFireing)
  153.             {
  154.                 if(Heat > 0)
  155.                 {
  156.                     CanFire = false;
  157.                     if (Heat <= 0)
  158.                     {
  159.                         Heat = 0;
  160.                     }
  161.                     else
  162.                     {
  163.                         Heat -= 0.1f;
  164.                         FireSound.PlayOneShot(CooldownAudio);
  165.                     }
  166.                 }
  167.                 else
  168.                 {
  169.                     CanFire = true;
  170.                 }
  171.                
  172.             }
  173.         }
  174.     }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement