using System.Collections; using System.Collections.Generic; using UnityEngine; public class Enemy : MonoBehaviour { private CameraContoller cam; private SpriteRenderer sRBase; private bool isDead; private Vector3 spawnPosition; public float maxHealth; private float health; public int damagePower; private GameObject healthBarPos; public Texture healthTex; public Texture healthTexBack; public GameObject healthPickup; public bool OnScreen; // Use this for initialization public virtual void Start () { cam = Camera.main.gameObject.GetComponent (); sRBase = GetComponent (); healthBarPos = transform.Find ("HealthBar").gameObject; health = maxHealth; spawnPosition = transform.position; isDead = false; OnScreen = false; } void OnBecameInvisible() { OnScreen = false; } void OnBecameVisible() { OnScreen = true; } // Update is called once per frame public virtual void Update () { if (health <= 0 && !getIsDead()) { Die (); } if (sRBase.color != Color.white) { sRBase.color = new Color (sRBase.color.r + 0.04f, 1.0f, 1.0f); } } void OnGUI() { if (health > 0) { Vector2 healthBarVec = Camera.main.WorldToScreenPoint(healthBarPos.transform.position); GUI.DrawTexture(new Rect(healthBarVec.x - 30.0f * (1.0f / cam.GetZoom()), Screen.height - healthBarVec.y + 5.0f, 60.0f * (1.0f / cam.GetZoom()), 10.0f * (1.0f / cam.GetZoom())), healthTexBack); GUI.DrawTexture(new Rect(healthBarVec.x - 30.0f * (1.0f / cam.GetZoom()), Screen.height - healthBarVec.y + 5.0f, 60.0f * (1.0f / cam.GetZoom()) * ((health / maxHealth)), 10.0f * (1.0f / cam.GetZoom())), healthTex); } } public virtual void Damage (float damageToDo, Vector3 hitPos, float knockback) { health -= damageToDo; sRBase.color = new Color (0.5f, 1.0f, 1.0f); Freeze(); } public void SetIsDead(bool newIsDead) { isDead = newIsDead; } public bool getIsDead() { return isDead; } public virtual void Die() { sRBase.enabled = false; SetIsDead (true); int random = Random.Range (0,6); if (healthPickup && random % 6 == 0) { GameObject health = Instantiate (healthPickup, transform.position, Quaternion.identity); HealthPickup pickup = health.GetComponent (); if (pickup) { pickup.SetHealth (10.0f); } } } public virtual void Respawn() { health = maxHealth; sRBase.enabled = true; SetIsDead (false); transform.position = spawnPosition; } public void Freeze() { Rigidbody2D rb = GetComponent(); if (rb) { rb.simulated = false; } StartCoroutine(FreezeTimer()); } public void Unfreeze() { Rigidbody2D rb = GetComponent(); if (rb) { rb.simulated = true; } } public int GetDamage() { return damagePower; } IEnumerator FreezeTimer() { yield return new WaitForSeconds(0.1f); Unfreeze(); } }