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

public class Spaceship : MonoBehaviour {
    public float speed = 4.0f;
    private Rigidbody2D rb;
    private int time = 0;
    public GameObject blueblast;
    private Animator anim;
    void Awake()
    {
        rb = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
    }
    // Use this for initialization
    void Start () {
        anim.SetBool("die",false);
	}
	
	// Update is called once per frame
	void Update ()
    {
        if (Input.GetKey(KeyCode.Escape))
        {
            SceneManager.LoadScene("Meny");
        }
		if (Input.GetKey(KeyCode.LeftArrow) &&
            transform.localPosition.x > -7)
        {
            transform.position = transform.position +
                Vector3.left * speed * Time.deltaTime;
            anim.Play("rotation");
        }
        if (Input.GetKey(KeyCode.RightArrow) &&
            transform.localPosition.x < 5.5)
        {
            transform.position = transform.position +
                Vector3.right * speed * Time.deltaTime;
            anim.Play("rotation");
        }
        if (Input.GetKey(KeyCode.UpArrow) &&
            transform.localPosition.y < 6)
        {
            transform.position = transform.position +
                Vector3.up *
                speed * Time.deltaTime;
        }
        if (Input.GetKey(KeyCode.DownArrow) &&
            transform.localPosition.y > -4)
        {
            transform.position = transform.position +
                Vector3.down * speed * Time.deltaTime;
        }

        if (Input.GetKeyDown(KeyCode.Space))
        {
            Instantiate(blueblast, transform.position, Quaternion.identity); // ovo je za kreiranje vise objekata
            // koji prosledjujemo kao 1 argument sa odredjene pozicije sto je drugi argument i 
            //dok treci argument sluzi da ne dodje do rotacije vec da objekat prati pravac roditelja odnosno naseg broda
        }
    }

    void OnCollisionEnter2D(Collision2D col)
    {
        //kada dodje u sudar sa drugim objektom tj metkom ili brodom
        if (col.gameObject.name != "blue_blast(Clone)")
        {
            anim.SetBool("die", true);
            anim.Play("explosion");
        }
    }
}
