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

public class ArmScript : MonoBehaviour {

    public Transform shot;    

	// Use this for initialization
	void Start () {
        if (PlayerPrefs.GetInt("HasArm") == 0) Destroy(gameObject);
	}
	
	// Update is called once per frame
	void Update () {

        float sdx, sdy;

        // get percent across screen
        float aa = Input.mousePosition.x / (float)Screen.width;
        // rescale to ensure aiming extends easily to ends of range- raw should be 0-1
        // bounds check below will stop it at horizontal
        aa = -0.2f + 1.4f * aa;

        // now get the angle
        float ang;
        int f = this.GetComponentInParent<GuyScript>().facing;

        if (f < 0)
        {
            ang = -90 + 180 * aa;
        }

        else
        {
            ang = 90 - 180 * aa;        
        }

        if (ang < -90) ang = -90;
        if (ang > 90) ang = 90;

        // set arm rotation
        this.transform.rotation = Quaternion.identity;
        this.transform.Rotate(new Vector3(0, 0, ang));

        // set shot velocity
        sdy = Mathf.Cos(Mathf.Deg2Rad*(ang));
        sdx = Mathf.Sin(Mathf.Deg2Rad*(ang));
        if (f == 1) sdx = -sdx;


        if (Input.GetButtonDown("Fire1"))
        {
            Transform t = Instantiate(shot, new Vector3(this.transform.position.x + 0.6f*sdx, this.transform.position.y + 0.6f*sdy), Quaternion.identity);
            t.GetComponent<Rigidbody2D>().velocity = new Vector3(sdx*3, sdy*3, 0);
            PlayerPrefs.SetInt("NeedShot", 1);
        }

    }

}
