using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class XwingController : MonoBehaviour { //Regenerate Health private int second = 1; private float uptoSecond; private int addHealthLW; private int addHealthRW; private int addHealthCP; //Ship Health public static int maxcockpitHealth = 500; public static int maxleftwingHealth = 100; public static int maxrightwingHealth = 100; public static int cockpitHealth = 500; public static int leftwingHealth = 100; public static int rightwingHealth = 100; //Repairing private bool cockpitDestroyed = false; private bool leftwingDestroyed = false; private bool rightwingDestroyed = false; private float LWrepairTime = 0f; private float RWrepairTime = 0f; public Text RepairWarningLW; public Text RepairWarningRW; //Countdown RepairTime public bool LWrepairsInitiated = false; public bool RWrepairsInitiated = false; private float uptoSecondCountdown; //Ship Move private float leftSpeed = 5.0f; private float rightSpeed = 5.0f; public float padding = 1f; float xmin; float xmax; //Ship Rotate public Transform shipRotation; private float turnSpeed = 400f; private float rotTime; private float colTime; private float startRot; private bool aKeyDown = false; private bool dKeyDown = false; private bool revertRotateA = false; private bool revertRotateB = false; //Projectile Variables public GameObject projectile; public AudioClip fireSound; public float projectileSpeed; public float firingRate = 1f; public static int upgradeLevel; //LaserArrays public Transform LaserArray1; public Transform LaserArray2; public Transform LaserArray3; public Transform LaserArray4; //Upgrade Lv.1 public Transform LaserArray5; public Transform LaserArray6; //Upgrade Lv.2 public Transform LaserArray7; public Transform LaserArray8; // Use this for initialization void Start () { float distance = transform.position.z - Camera.main.transform.position.z; Vector3 leftmost = Camera.main.ViewportToWorldPoint(new Vector3(0,0,distance)); Vector3 rightmost = Camera.main.ViewportToWorldPoint(new Vector3(1,0,distance)); xmin = leftmost.x + padding; xmax = rightmost.x - padding; } // Update is called once per frame void Update () { MovePlayer(); Counter(); Repairing(); Damaged(); Health(); Firing(); } void Firing () { //FIRE 1 if(Input.GetKeyDown(KeyCode.UpArrow)){ InvokeRepeating("Fire1", .00001f, firingRate); InvokeRepeating("Fire3", .1f, firingRate); InvokeRepeating("Fire2", .05f, firingRate); InvokeRepeating("Fire4", .15f, firingRate); InvokeRepeating("FireSound", .00001f, firingRate); if(upgradeLevel == 1){ InvokeRepeating("Fire5", .00001f, firingRate); InvokeRepeating("Fire6", .00001f, firingRate); } if(upgradeLevel == 2){ InvokeRepeating("Fire5", .00001f, firingRate); InvokeRepeating("Fire6", .00001f, firingRate); InvokeRepeating("Fire7", .00001f, firingRate); InvokeRepeating("Fire8", .00001f, firingRate); } } if(Input.GetKeyUp(KeyCode.UpArrow)){ CancelInvoke("Fire1"); CancelInvoke("Fire2"); CancelInvoke("Fire3"); CancelInvoke("Fire4"); CancelInvoke("FireSound"); if(upgradeLevel == 1){ CancelInvoke("Fire5"); CancelInvoke("Fire6"); } if(upgradeLevel == 2){ CancelInvoke("Fire5"); CancelInvoke("Fire6"); CancelInvoke("Fire7"); CancelInvoke("Fire8"); } } } void Fire1 () { if(leftwingDestroyed == false){ GameObject beam = Instantiate(projectile, LaserArray1.position, shipRotation.rotation) as GameObject; beam.GetComponent().AddForce(shipRotation.up * projectileSpeed); } } void Fire3 () { if(leftwingDestroyed == false){ GameObject beam = Instantiate(projectile, LaserArray3.position, shipRotation.rotation) as GameObject; beam.GetComponent().AddForce(shipRotation.up * projectileSpeed); } } void Fire2 () { if(rightwingDestroyed == false){ GameObject beam = Instantiate(projectile, LaserArray2.position, shipRotation.rotation) as GameObject; beam.GetComponent().AddForce(shipRotation.up * projectileSpeed); } } void Fire4 () { if(rightwingDestroyed == false){ GameObject beam = Instantiate(projectile, LaserArray4.position, shipRotation.rotation) as GameObject; beam.GetComponent().AddForce(shipRotation.up * projectileSpeed); } } void Fire5 () { if(rightwingDestroyed == false){ GameObject beam = Instantiate(projectile, LaserArray5.position, LaserArray5.rotation) as GameObject; beam.GetComponent().AddForce(LaserArray5.up * projectileSpeed); } } void Fire6 () { if(leftwingDestroyed == false){ GameObject beam = Instantiate(projectile, LaserArray6.position, LaserArray6.rotation) as GameObject; beam.GetComponent().AddForce(LaserArray6.up * projectileSpeed); } } void Fire7 () { if(rightwingDestroyed == false){ GameObject beam = Instantiate(projectile, LaserArray7.position, LaserArray7.rotation) as GameObject; beam.GetComponent().AddForce(LaserArray7.up * projectileSpeed); } } void Fire8 () { if(leftwingDestroyed == false){ GameObject beam = Instantiate(projectile, LaserArray8.position, LaserArray8.rotation) as GameObject; beam.GetComponent().AddForce(LaserArray8.up * projectileSpeed); } } void FireSound () { if(leftwingDestroyed == false || rightwingDestroyed == false){ AudioSource.PlayClipAtPoint(fireSound, transform.position); } } void Counter (){ uptoSecond += Time.deltaTime; if(uptoSecond >= 1){ addHealthLW += 1; addHealthRW += 1; addHealthCP += 1; uptoSecond = 0f; } } void Health (){ //Max Health if(cockpitHealth >= maxcockpitHealth){ cockpitHealth = maxcockpitHealth; } if(leftwingHealth >= maxleftwingHealth){ leftwingHealth = maxleftwingHealth; } if(rightwingHealth >= maxrightwingHealth){ rightwingHealth = maxrightwingHealth; } //Healing if (cockpitHealth > 0){ if(leftwingHealth <= 0){ leftwingHealth = 0; } if(rightwingHealth <= 0){ rightwingHealth = 0; } if(cockpitHealth <= 0){ cockpitHealth = 0; } if(cockpitHealth < maxcockpitHealth){ if(addHealthCP >= 1){ cockpitHealth += second; addHealthCP = 0; } } if(leftwingHealth < maxleftwingHealth){ if(addHealthLW >= 1){ leftwingHealth += second; addHealthLW = 0; } } if(rightwingHealth < maxleftwingHealth){ if(addHealthRW >= 1){ rightwingHealth += second; addHealthRW = 0; } } } } void Damaged (){ if(leftwingHealth <= 0){ leftwingDestroyed = true; LWrepairsInitiated = true; LWrepairTime = 6f; upgradeLevel = 0; } if(rightwingHealth <= 0){ rightwingDestroyed = true; RWrepairsInitiated = true; RWrepairTime = 6f; upgradeLevel = 0; } if(cockpitHealth <= 0){ cockpitDestroyed = true; upgradeLevel = 0; Application.LoadLevel("LoseScreen"); } } void Repairing (){ if (LWrepairsInitiated == true){ if(LWrepairTime > 0){ LWrepairTime -= Time.deltaTime; leftSpeed = 2.5f; Debug.Log("LeftWingRepairing " + (LWrepairTime)); } if(LWrepairTime <= 0){ leftwingDestroyed = false; leftSpeed = 5f; LWrepairsInitiated = false; } } if (RWrepairsInitiated == true){ if(RWrepairTime > 0){ RWrepairTime -= Time.deltaTime; rightSpeed = 2.5f; } if(RWrepairTime <= 0){ rightwingDestroyed = false; rightSpeed = 5f; RWrepairsInitiated = false; } } // Repairing Systems if (LWrepairsInitiated == true){ RepairWarningLW.text = "R2D2 Repairing " + ((int)LWrepairTime) + " seconds"; } else { RepairWarningLW.text = ""; } if (RWrepairsInitiated == true){ RepairWarningRW.text = "R2D2 Repairing " + ((int)RWrepairTime) + " seconds"; } else { RepairWarningRW.text = ""; } } void MovePlayer () { if(colTime == 0){ aKeyDown = false; dKeyDown = false; } if(Input.GetKey(KeyCode.A)){ if(dKeyDown == false){ aKeyDown = true; rotTime = turnSpeed * Time.deltaTime; colTime += rotTime; //MOVE transform.position += Vector3.left * leftSpeed * Time.deltaTime; //ROTATION if (colTime < 30f) { shipRotation.rotation = Quaternion.Euler(30,0,colTime); } else if (colTime > 30f) { shipRotation.rotation = Quaternion.Euler(30,0,30f); colTime = 30f; } } } if(Input.GetKeyUp(KeyCode.A)){ //rotTime = 0; revertRotateA = true; } if (revertRotateA == true){ colTime -= turnSpeed * Time.deltaTime; shipRotation.rotation = Quaternion.Euler(30,0,colTime); if (colTime < 0){ colTime = 0; shipRotation.rotation = Quaternion.Euler(30,0,colTime); revertRotateA = false; } } if(Input.GetKey(KeyCode.D)){ if(aKeyDown == false){ dKeyDown = true; rotTime = turnSpeed * Time.deltaTime; colTime += rotTime; //MOVE transform.position += Vector3.right * rightSpeed * Time.deltaTime; //ROTATION if (colTime < 30f) { shipRotation.rotation = Quaternion.Euler(30,0, -colTime); } else if (colTime > 30f) { shipRotation.rotation = Quaternion.Euler(30,0, -30f); colTime = 30f; } } } if(Input.GetKeyUp(KeyCode.D)){ revertRotateB = true; } if (revertRotateB == true){ colTime -= turnSpeed * Time.deltaTime; shipRotation.rotation = Quaternion.Euler(30,0,-colTime); if (colTime < 0){ colTime = 0; shipRotation.rotation = Quaternion.Euler(30,0,-colTime); revertRotateB = false; } } float newX = Mathf.Clamp(transform.position.x, xmin, xmax); transform.position = new Vector3(newX, transform.position.y, transform.position.z); } }