using UnityEngine;
using System.Collections;

public class Connecter : MonoBehaviour {

    private Thruster thruster;
    private Rigidbody2D rigid;

    private bool connected = false;

	// Use this for initialization
	void Start () {
        thruster = GetComponent<Thruster>();
        rigid = GetComponent<Rigidbody2D>();
	}
	
	// Update is called once per frame
	void Update () {
	
	}

    void OnCollisionEnter2D(Collision2D coll) {
        if (coll.gameObject.tag == "Player" && !connected) {
            connected = true;
            thruster.attached = true;

            Rigidbody2D collRigid = coll.rigidbody;

            collRigid.mass += rigid.mass;

            thruster.rigid = collRigid;
            Destroy(rigid);
            transform.parent = coll.transform;

            Vector3 tempRot = transform.localEulerAngles;
            tempRot.z = Mathf.Round(tempRot.z / 90) * 90;
            transform.localRotation = Quaternion.Euler(tempRot);

            Vector3 tempPos = transform.localPosition;
            float scale = coll.gameObject.GetComponent<BoxCollider2D>().size.x;
            tempPos.x = Mathf.Round(tempPos.x / scale) * scale;
            tempPos.y = Mathf.Round(tempPos.y / scale) * scale;
            transform.localPosition = tempPos;



            Destroy(this);



        }



    }
}