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

public class PlayerScript : MonoBehaviour
{
    public float speed;

    Rigidbody2D rb;
    Camera cam;

	void Start ()
    {
        rb = GetComponent<Rigidbody2D>();
        cam = GameObject.FindGameObjectWithTag(Tags.mainCamera).GetComponent<Camera>();
	}
	
	void FixedUpdate()
	{
        float hor = Input.GetAxis("Horizontal");
        float ver = Input.GetAxis("Vertical");

        rb.velocity = new Vector2(hor * speed * Time.deltaTime, 
                                  ver * speed * Time.deltaTime);
		
		if(Input.GetKeyDown(KeyCode.Escape))
			Application.LoadLevel(0);
	}
    
	void Update()
    {

        Vector2 mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
        transform.LookAt2D(mousePos);
        transform.eulerAngles -= new Vector3(0,0,90);

        if (Input.GetMouseButtonDown(0))
        {
            Vector2 transPos = transform.position;
			Vector2 delta = mousePos - transPos;
			float angle = (350/(delta.magnitude + 1));
			float lifetime = -.25f * angle + 90;
            Wave.createWave(transPos, delta, angle, 20, lifetime);
			gameObject.GetComponent<AudioSource>().Play();
        }
    }
}
