using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class Character : MonoBehaviour {
    public float health = 100f;
    public GameObject deathEffect;
    public int killPoints = 0;

    [SerializeField]
    float currHealth = 0;
    SpriteRenderer _sr;
	
    // Use this for initialization
	void Start () {
        currHealth = health;
        _sr = this.GetComponent<SpriteRenderer>();
	}

    public void Hit(float damage)
    {
        currHealth -= damage;
        _sr.color = new Color(1, currHealth / health, currHealth / health);
        if (currHealth <= 0) Die();
    }

    public void Die(bool countPoints = true)
    {
        if (deathEffect != null)
        {
            GameObject d = (GameObject)Instantiate(deathEffect, this.transform.position, this.transform.rotation);
            d.name = "Explosion";
        }

        if (countPoints)
            FindObjectOfType<GameManager>().IncreaseScore(killPoints);

        Destroy(this.gameObject);
    }

}
