using UnityEngine;
using System.Collections;

public class Mines : MonoBehaviour {


    public AudioClip torpedoBoomSFX;
    public AudioSource source1;
    public GameObject boom;
   

    public float vitesseTrans = 2.0f;
    public float vitesseRot = 2.0f;
    public Vector3 axe = Vector3.up;
    int p = 1;
    public float posInitialy;
    // Use this for initialization------------------------------------------------------------------------------------------
    void Start () {
        posInitialy = transform.position.y;
        boom.SetActive(false);
    }
	
	// Update is called once per frame----------------------------------------------------------------------------------------
	void Update () {
        
        if (transform.position.y < posInitialy-10.0f)
        {
            p = 1;
        }

        if (transform.position.y > posInitialy+10.0f)
        {
            p = -1;
        }

        transform.Translate(new Vector3(0, 1, 0) * Time.deltaTime * vitesseTrans * p);
        transform.Rotate(axe, vitesseRot * Time.deltaTime);

    
    }
    //----------------------------------------------------------------------------------------------------------------------------
    void OnTriggerEnter(Collider collision)
    {

        //Debug.Log("I touch " + collision.gameObject.name);


        Missile torpille = collision.gameObject.GetComponent<Missile>();
        MoveSubmarine submarinePlayer = collision.gameObject.GetComponent<MoveSubmarine>();

        if (submarinePlayer != null)
        {
            
            StartCoroutine("explosionM");

            HealthSubmarine.mtouched = true;

        }

        if(torpille !=null)
        {
            StartCoroutine("explosionM");
        }
        

    }
    //---------------------------------------------------------------------------------------------------------------------------
    public IEnumerator explosionM()
    {
        Debug.Log("Zbra");
        boom.SetActive(true);
        GetComponent<Renderer>().enabled = false;
        source1.clip = torpedoBoomSFX;
        source1.Play();
        yield return new WaitForSeconds(1.0f);
        Destroy(gameObject);

    }
}
