Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public class UFOfire : MonoBehaviour
- {
- public Image HeatDisplay;
- public UFObeam UB;
- public AudioSource FireSound;
- public AudioClip FireAudio, CooldownAudio;
- public GameObject BulletUp, BulletDown, BulletLeft, BulletRight;
- public Transform BulletUpPosBulletDownPos, BulletLeftPos, BulletRightPos;
- public SpriteRenderer NoFire, FireUp, FireDown, FireLeft, FireRight;
- public bool CanFire;
- [HideInInspector]
- public bool isFireing, isFireingUp, isFireingLeft, isFireingRight, isFireingDown;
- public float Heat;
- int Fire;
- private void Start()
- {
- StartCoroutine("CoolDown");
- }
- void Update()
- {
- HeatDisplay.fillAmount = Heat;
- if (Fire == 1 && isFireing)
- {
- Fire = 2;
- StartCoroutine("MachineFire");
- }
- else
- {
- if(!isFireing)
- {
- Fire = 0;
- }
- }
- if (!UB.isBeaming)
- {
- if (Input.GetKey(KeyCode.UpArrow) && CanFire)
- {
- Heat += 0.003f;
- if (Fire == 0)
- {
- Fire = 1;
- isFireing = true;
- }
- NoFire.enabled = false;
- FireUp.enabled = true;
- isFireingUp = true;
- }
- else
- {
- isFireingUp = false;
- }
- else if (Input.GetKey(KeyCode.DownArrow) && CanFire)
- {
- Heat += 0.003f;
- if (Fire == 0)
- {
- Fire = 1;
- isFireing = true;
- }
- NoFire.enabled = false;
- FireDown.enabled = true;
- isFireingDown = true;
- }
- else
- {
- isFireingDown = false;
- }
- else if (Input.GetKey(KeyCode.LeftArrow) && CanFire)
- {
- Heat += 0.003f;
- if (Fire == 0)
- {
- Fire = 1;
- isFireing = true;
- }
- NoFire.enabled = false;
- FireLeft.enabled = true;
- isFireingLeft = true;
- }
- else
- {
- isFireingLeft = false;
- }
- else if (Input.GetKey(KeyCode.RightArrow) && CanFire)
- {
- Heat += 0.003f;
- if (Fire == 0)
- {
- Fire = 1;
- isFireing = true;
- }
- NoFire.enabled = false;
- FireRight.enabled = true;
- isFireingRight = true;
- }
- else
- {
- isFireingRight = false;
- }
- }
- if (!isFireingLeft && !isFireingRight && !isFireingDown && !isFireingUp)
- {
- isFireing = false;
- NoFire.enabled = true;
- FireUp.enabled = false;
- FireDown.enabled = false;
- FireLeft.enabled = false;
- FireRight.enabled = false;
- }
- if (Heat >= 1)
- {
- CanFire = false;
- }
- }
- public IEnumerator MachineFire()
- {
- while (true)
- {
- yield return new WaitForSeconds(0.3f);
- if(Fire == 2)
- {
- if (isFireingUp)
- {
- var BD = GameObject.Instantiate(BulletUp, BulletUpPos.transform.position, transform.rotation);
- BD.GetComponent<Rigidbody2D>().AddForce(Vector2.up * 500, ForceMode2D.Force);
- BD.GetComponent<UFObullet>().StartCoroutine("ClearBullet");
- }
- if (isFireingDown)
- {
- var BD = GameObject.Instantiate(BulletDown, BulletDownPos.transform.position, transform.rotation);
- BD.GetComponent<Rigidbody2D>().AddForce(Vector2.down * 500, ForceMode2D.Force);
- BD.GetComponent<UFObullet>().StartCoroutine("ClearBullet");
- }
- if (isFireingLeft)
- {
- var BL = GameObject.Instantiate(BulletLeft, BulletLeftPos.transform.position, transform.rotation);
- BL.GetComponent<Rigidbody2D>().AddForce(Vector2.left * 500, ForceMode2D.Force);
- BL.GetComponent<UFObullet>().StartCoroutine("ClearBullet");
- }
- if (isFireingRight)
- {
- var BR = GameObject.Instantiate(BulletRight, BulletRightPos.transform.position, transform.rotation);
- BR.GetComponent<Rigidbody2D>().AddForce(Vector2.right * 500, ForceMode2D.Force);
- BR.GetComponent<UFObullet>().StartCoroutine("ClearBullet");
- }
- FireSound.PlayOneShot(FireAudio);
- }
- else
- {
- yield break;
- }
- }
- }
- public IEnumerator CoolDown()
- {
- while (true)
- {
- yield return new WaitForSeconds(0.3f);
- if (!isFireing)
- {
- if(Heat > 0)
- {
- CanFire = false;
- if (Heat <= 0)
- {
- Heat = 0;
- }
- else
- {
- Heat -= 0.1f;
- FireSound.PlayOneShot(CooldownAudio);
- }
- }
- else
- {
- CanFire = true;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement