using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class EnemListener : MonoBehaviour {
	public GameObject player;
	public float initSpeed = 10.0f;
	private UnityEngine.AI.NavMeshAgent agent;

	private Vector3 startingPoint;
	private float delay = 2.0f;
	private float timer = 0.0f;
	private float speedInc = 10.0f;
	private bool alert = false;

	// Use this for initialization
	void Start () {
		agent = GetComponent<UnityEngine.AI.NavMeshAgent>();
		agent.autoBraking = true;
		agent.speed = initSpeed;
		startingPoint = transform.position;
	}

	// Update is called once per frame
	void Update () {
		if(Input.GetKeyUp(KeyCode.Space)){
			agent.destination = player.transform.position;
			agent.speed += speedInc;
			alert = true;
			timer = 0;
			delay += delay;
		}

		if(!alert){
			agent.destination = startingPoint;
		} else{
			timer += Time.deltaTime;
			if(timer > delay){
				timer = 0.0f;
				delay = 2.0f;
				alert = false;
			}
		}
	}
	void OnTriggerEnter(Collider other){
		if(other.name == "killZone"){
			SceneManager.LoadScene(0);
		} else if(other.name == "presenceZone"){
			GetComponent<AudioSource>().Play();
		}
	}
	void OnTriggerExit(Collider other){
		if(other.name == "presenceZone"){
			GetComponent<AudioSource>().Pause();
		}
	}
}
