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


public class PlayerMananger : MonoBehaviour
{
    // Start is called before the first frame update
    public int speed;
    public int rotSpeed;
    public float coolDown;
    public float maxPower;
    float delta = 3;
    public GameObject shot;
    public Transform shotSpawn;
    static public float charge = 0;
    public Image fillBar;
    public string alienTagSetter;
    static public string alienTag;
    public GameObject pointer;
    public AudioClip shoot1;
    public AudioClip shoot2;
    public AudioSource sound;

    void Start()
    {
        alienTag = alienTagSetter;
       
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        
        delta += Time.deltaTime;
        if (delta > coolDown)
        {
            if (Input.GetKey("space") || Input.GetButton("Fire1"))
            {
                if (charge < maxPower)
                {
                    charge += 0.025f;
                }
            }
            if (Input.GetKeyUp("space") || Input.GetButtonUp("Fire1"))
            {

                Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
                if (charge / maxPower < 0.5f)
                {
                    sound.clip = shoot1;
                    sound.Play();
                }
                else
                {
                    sound.clip = shoot2;
                    sound.Play();
                }
                delta = 0;



            }
        }
        fillBar.fillAmount = charge / maxPower;
        RotateTo();
    }

    void RotateTo()
    {
        Vector3 direction = Camera.main.ScreenToWorldPoint(Input.mousePosition) - pointer.transform.position;
        Vector2 trueDir = new Vector2(direction.x, direction.z);

        float angle = Mathf.Atan2(trueDir.x, trueDir.y) * Mathf.Rad2Deg;

        Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.up);
        pointer.transform.rotation = Quaternion.Slerp(pointer.transform.rotation, rotation, rotSpeed * Time.deltaTime);
    }
}
