using UnityEngine;
using System.Collections;

public class PirateManager : MonoBehaviour 
{
	public static float timePirateShoot;		//Frequence des tirs du pirate en seconde
	
	public static bool boatDead;				
	public BateauPlayer BateauP;				//Type BateauPlayer me permettant de recuperer les variables du script "BateauPlayer"
	public BoatManager BoatM;					//Type BoatManager me permettant de recuperer les variables du script "BoatManager"
	public GameObject bottleEnnemi;				//Le Prefab "BottleEnnemy"
	public GameObject explosion;				//Le Prefab "Explosion"
	
	// Use this for initialization
	void Start () 
	{
		AudioClip pirateSound = (AudioClip) Resources.Load("Sounds/Ha! Ha!", typeof(AudioClip));
		audio.PlayOneShot(pirateSound);
		timePirateShoot = Random.Range(1.0f,6.0f);
		StartCoroutine(Compteur());
	}
	
	IEnumerator Compteur()
	{
		while(1==1)
		{
			yield return new WaitForSeconds(timePirateShoot);
			TireEnnemi();
		}
	}
	
	// Update is called once per frame
	void Update () 
	{
		transform.Translate(new Vector3(0,0,BoatManager.boatSpeed)*Time.deltaTime,Space.World);
	}
	
	void OnTriggerEnter(Collider ObjetInfo)
	{
		if(ObjetInfo.gameObject.tag == "bottle")   //Si le pirate touche une de nos bouteilles
		{	
			Destroy(gameObject);
			Destroy(ObjetInfo.gameObject);
			
			BoatM.boatLife -= 30; 					//On enleve 30 de vie de "Ensemble bateau"
			
			GameManager.score += 30;				//On gagne 30 points
			
			if(BoatM.boatLife <= 0)					//si la vie de "Ensemble bateau" est à 0
			{
				BoatManager.deadBoat= true;
				Destroy(gameObject);
				Destroy(transform.parent.gameObject);
				GameObject particule = (GameObject)Instantiate(explosion,transform.position,transform.rotation);
				
			}
		}
	}
	
	//Gestion des tires des pirates
	void TireEnnemi()
	{
			Ray ray = new Ray(transform.position,BateauPlayer.positionPlayer-transform.position);
			GameObject boum = Instantiate(bottleEnnemi,transform.position,transform.rotation) as GameObject;
			boum.rigidbody.AddForce(ray.direction*750);
	}
}
