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

public class EnemPatrol : MonoBehaviour{
	public GameObject player;
	public Transform[] points;
	public float initSpeed = 15.0f;
	public GameObject enemy;
	private UnityEngine.AI.NavMeshAgent agent;

	private int destPoints = 0;

	// Use this for initialization
	void Start () {
		agent = GetComponent<UnityEngine.AI.NavMeshAgent>();
		agent.autoBraking = true;
		GotoNextPoint();
	}

	//Waypoint Patrol for Enemy
	void GotoNextPoint(){
		if(points.Length == 0) return;
		agent.destination = points[destPoints].position;
		destPoints = (destPoints + 1) % points.Length;
	}

	// Update is called once per frame
	void Update () {
		if(agent.remainingDistance < 0.5f){
			GotoNextPoint();
			agent.speed = initSpeed;
		}
	}
	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();
		}
	}
}
