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

public class projectileColliding : MonoBehaviour
{

    public string fretID;

    public float spawnTime;

    // Use this for initialization
    void Start()
    {
        spawnTime = Time.time;
    }

    // Update is called once per frame
    void Update()
    {

        //Move projectiles left or right depending on direction
        if (gameObject.tag == "leftProjectile")
        {
            transform.Translate(Vector3.right * 1f * Time.deltaTime);
        }
        else if (gameObject.tag == "rightProjectile")
        {
            transform.Translate(Vector3.left * 1f * Time.deltaTime);
        }

        //Despawn if the spawn time is too high
        if (Time.time - spawnTime > 0.5f && (gameObject.tag == "leftShield" || gameObject.tag == "rightShield"))
        {
            Destroy(gameObject);
        }
        else if (Time.time - spawnTime > 5f)
        {
            Destroy(gameObject);
        }
    }

    private void OnTriggerEnter(Collider col)
    {
        if (gameObject.tag == "leftProjectile")
        {
            //Projectiles and shields destroy each other
            if (col.gameObject.tag == "rightShield")
            {
                //if their fret IDs are the same.
                if (col.gameObject.GetComponent<projectileColliding>().fretID == fretID)
                {
                    gameControl.P2Stam++;
                    Destroy(col.gameObject);
                    Destroy(gameObject);
                }
            }

            //Player 2 has been hit and loses HP.
            if (col.gameObject.tag == "rightSpeaker")
            {
                gameControl.P2HP -= 10;
                col.gameObject.GetComponent<ParticleSystem>().Play();
                Destroy(gameObject);
            }
        }
        else
        {
            //Projectiles and shields destroy each other
            if (col.gameObject.tag == "leftShield")
            {
                //if their fret IDs are the same.
                if (col.gameObject.GetComponent<projectileColliding>().fretID == fretID)
                {
                    gameControl.P1Stam++;
                    Destroy(col.gameObject);

                    Destroy(gameObject);
                }
            }

            //Player 1 has been hit and loses HP.
            if (col.gameObject.tag == "leftSpeaker")
            {
                gameControl.P1HP -= 10;
                col.gameObject.GetComponent<ParticleSystem>().Play();
                Destroy(gameObject);
            }
        }
    }
}
