using UnityEngine;
using System.Collections;

public class DmgPlayer : MonoBehaviour {

	public float timeBetweenAttacks = 0.5f;
	public int attackDamage = -10;

	bool playerInRange;
	GameObject player;
	PlayerHealth playerHealth;
	float timer;
	// Use this for initialization
	void Start () 
	{
		player = GameObject.FindGameObjectWithTag ("Player");
		playerHealth = player.GetComponent <PlayerHealth> ();
	}

	void OnTriggerEnter (Collider other)
	{
		if(other.gameObject == player)
		{
			playerInRange = true;
		}
	}
	
	void OnTriggerExit (Collider other)
	{
		if(other.gameObject == player)
		{
			playerInRange = false;
		}
	}
	// Update is called once per frame
	void Update () 
	{
		timer += Time.deltaTime;
		
		if(timer >= timeBetweenAttacks && playerInRange)
		{
			Attack ();
		}
	
	}

	void Attack ()
	{
		timer = 0f;
		
		if(playerHealth.currentHP> 0)
		{
			playerHealth.HPctrl (attackDamage);
		}
	}

}
