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

public class EVAController : MonoBehaviour {
	private Rigidbody2D rigidbody2d;

	// Use this for initialization
	void Start () {
		rigidbody2d = GetComponent<Rigidbody2D>();
	}
	
	// Update is called once per frame
	void Update () {
		GameController.mainCamera.transform.position = new Vector3(transform.position.x, transform.position.y, GameController.mainCamera.transform.position.z);

		Vector2 speedGain = new Vector2(Input.GetAxis("Horizontal") * 0.05f, Input.GetAxis("Vertical") * 0.05f);
		if (speedGain.magnitude > Mathf.Epsilon) {
			rigidbody2d.velocity += speedGain.Rotate(GameController.mainCamera.transform.rotation.eulerAngles.z);
			transform.rotation = Quaternion.AngleAxis(rigidbody2d.velocity.ToAngle(), Vector3.forward);
		}

		if (Input.GetKeyDown("space")) {
			foreach (GameObject spaceShip in GameController.largeSpaceShips) {
				if (spaceShip.transform.FindChild("Collider").GetComponent<PolygonCollider2D>().OverlapPoint(new Vector2(transform.position.x, transform.position.y))) {
					GameObject chara = GameController.instantiate(TemplateManager.getChar(0), transform.position, transform.rotation.eulerAngles.z);
					chara.transform.SetParent(spaceShip.transform.FindChild("Internals/Characters"));
					Destroy(gameObject);
				}
			}
		}
	}
}
