using System.Collections; using System.Collections.Generic; using UnityEngine; public class Bunker : MonoBehaviour { // Instantiate a prefab with an attached Missile script public fireballMissile missilePrefab; public static int Health = 1000; public Sprite[] hitSprites; Camera cam; Transform my; Rigidbody2D body; private GameObject fireball; private GameObject fireballStart; private Level_Manager levelManager; private float InstantiationTimer = 0f; private int spriteIndex; // HealthBar Variables public float max_Health; public float cur_Health; public GameObject healthBar; void Awake () { cam = Camera.main; my = GetComponent (); body = GetComponent (); // HealthBar max_Health = 1000f; } void Start () { fireball = GameObject.Find("Fireball"); fireballStart = GameObject.Find("fireballStart"); levelManager = GameObject.FindObjectOfType(); } void Update () { cur_Health = Bunker.Health; SpriteChange(); InstantiationTimer -= Time.deltaTime; bunkerMove(); if (Input.GetMouseButtonDown(0)){ CreatePrefab(); } // Health Bar decreasehealth(); } void CreatePrefab (){ if (InstantiationTimer <= 0){ GetComponent().Play(); Vector3 clonePosition = fireballStart.transform.position; //Instantiate plugged in prefab, start location, start rotation fireballMissile clone = (fireballMissile)Instantiate(missilePrefab, clonePosition, transform.rotation); InstantiationTimer = 2f; } } void bunkerMove () { // Distance from camera to object. We need this to get the proper calculation. float camDis = cam.transform.position.y - my.position.y; // Get the mouse position in world space. Using camDis for the Z axis. Vector3 mouse = cam.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, camDis)); float AngleRad = Mathf.Atan2 (mouse.y - my.position.y, mouse.x - my.position.x); print (("Angle_Rad ") + (AngleRad)); float angle = (180 / Mathf.PI) * AngleRad; body.rotation = angle; } void SpriteChange () { this.GetComponent().sprite = hitSprites[spriteIndex]; if ( Health <= 300 ) { spriteIndex = 2; } else if ( Health <= 700 ) { spriteIndex = 1; } else if ( Health >= 701 ) { spriteIndex = 0; } if ( Health <= 0 ) { levelManager.LoadLevel("Lose"); } } // Health Bar Method void decreasehealth () { float calc_Health = cur_Health/ max_Health; //if cur 80 / 100 = 0.8f SetHealthBar(calc_Health); } // Health Bar Method public void SetHealthBar(float myHealth) { //myHealth value 0-1, healthBar.transform.localScale = new Vector3( myHealth, healthBar.transform.localScale.y, healthBar.transform.localScale.z); } }