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

public class Movement : MonoBehaviour {


    public Rigidbody2D shipRigidbody;

    public GameObject laser;
    public GameObject laser_spawn;


    public float laser_cooldown = 0.5f;
    float time_since_last = 0.5f;
	// Use this for initialization
	void Start () {
        shipRigidbody = GetComponent<Rigidbody2D>();
		
	}
	
	// Update is called once per frame
	void FixedUpdate () {
        if(AstroidSpawner.instance.currentMode == AstroidSpawner.Mode.play)
        {
            if (Input.GetKey(KeyCode.W))
            {
                shipRigidbody.AddRelativeForce(Vector2.up * 10.0f);
            }

            else if (Input.GetKey(KeyCode.S))
            {
                shipRigidbody.AddRelativeForce(Vector2.up * 10.0f * -1.0f);
            }

            if (Input.GetKey(KeyCode.A))
            {

                shipRigidbody.AddTorque(20.0f);
            }

            else if (Input.GetKey(KeyCode.D))
            {
                shipRigidbody.AddTorque(-20.0f);
            }

            
        }
        
        
		
	}

    private void Update()
    {
        if (Input.GetKeyUp(KeyCode.Escape))
        {
            AstroidSpawner.instance.EndGame();
        }

        if (Input.GetKeyDown(KeyCode.Space))
        {
            Instantiate(laser, laser_spawn.transform.position, transform.rotation);
        }

        if (Input.GetKey(KeyCode.Space))
        {
            if (time_since_last <= 0.0f)
            {
                Instantiate(laser, laser_spawn.transform.position, transform.rotation);
                time_since_last = 0.2f;
            }
            else
            {
                time_since_last -= Time.deltaTime;
            }

        }
        else if (Input.GetKeyUp(KeyCode.Space))
        {
            time_since_last = 0.2f;
        }
    }
}
