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

public class Shoot : MonoBehaviour
{
    public GameObject bubblePrefab;
    public GameObject fizzPrefab;
    public float launchForce = 10;
    public Transform launchPoint;
    public GameObject myCannon;
    public float forceBuild = 0;
    public AudioSource audioSource;
    public AudioClip popSound;
    public Player playerScript;


    // Start is called before the first frame update
    void Start()
    {
        audioSource = GetComponent<AudioSource>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey(KeyCode.S) && gameObject.tag == "left" && playerScript.isLoaded)
        {
            forceBuild += 0.05f;
            if (forceBuild > 15)
                forceBuild = 15;
        }

        if (Input.GetKeyUp(KeyCode.S) && gameObject.tag == "left" && playerScript.isLoaded)
        {
            FireBubble();
            forceBuild = 0;
            playerScript.isLoaded = false;
        }
                

        if (Input.GetKey(KeyCode.K) && gameObject.tag == "right" && playerScript.isLoaded)
        {
            forceBuild += 0.05f;
            if (forceBuild > 15)
                forceBuild = 15;
        }

        if (Input.GetKeyUp(KeyCode.K) && gameObject.tag == "right" && playerScript.isLoaded)
        {
            FireBubble();
            forceBuild = 0;
            playerScript.isLoaded = false;
        }
    }

    public void FireBubble()
    {
        GameObject bubble = Instantiate(bubblePrefab, launchPoint.position, launchPoint.rotation);
        GameObject fizz = Instantiate(fizzPrefab, launchPoint.position, launchPoint.rotation);
        audioSource.clip = popSound;
        if (!audioSource.isPlaying)
        {
            audioSource.Play();
        }
        Rigidbody bubbleRB = bubble.GetComponent<Rigidbody>();
        bubbleRB.AddForce(launchPoint.forward * (launchForce + forceBuild), ForceMode.Impulse);
        Bubble thisBubble = bubble.GetComponent<Bubble>();
        Destroy(fizz, 2);
        if (gameObject.tag == "left")
        {
            thisBubble.playerNum = GameObject.Find("Player1");
        }

        if (gameObject.tag == "right")
        {
            thisBubble.playerNum = GameObject.Find("Player2");
        }

    }
}
