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

public class ControlEnemy : MonoBehaviour
{
	public Text nameTxt;
	public string nameStr = "Kill-Guard";

    public Text hpText;
    public int hpMax = 100;

    private HpSrc hp;

	void Start ()
	{
		nameTxt = nameTxt.GetComponent<Text>();
		nameTxt.text = nameStr;
		hpText = hpText.GetComponent<Text>();
		hp = this.GetComponent<HpSrc>();

        SetText();
	}
	
	void Update()
	{
        SetText();
		
		if(hp.hp == 0)
		{
			Destroy(gameObject);
			Application.LoadLevel("endScene"); //월드맵
		}
	}
	
	void OnCollisionEnter2D(Collision2D collision)
	{
        ShotMove beam = collision.gameObject.GetComponent<ShotMove>();

        if ( beam.tag == "ATK_to_Enemy" )
		{
			Destroy(collision.gameObject);

			if(hp != null)
			{
                GetComponent<AudioSource>().Play();

                if ( this.hp.hp > 0 )	{	this.hp.hp -= beam.damage;	}
				else					{	this.hp.hp = 0;				}
			}
		}
	}
	
	void SetText()
	{
		hpText.text = "HP: " + hp.hp.ToString() + " / " + hp.hpMax.ToString();
	}
}