using UnityEngine;
using System.Collections;

public class ShipPiece : MonoBehaviour 
{
	[SerializeField]
	private Rigidbody2D attachedTo;

	[SerializeField]
	private GameObject[] childObjects;

	private DistanceJoint2D[] childObjectsDJ;
	private Rigidbody2D[] childObjectsRB;

	void Awake () 
	{
		childObjectsDJ = new DistanceJoint2D[childObjects.Length];
		childObjectsRB = new Rigidbody2D[childObjects.Length];

		for (int i=0; i<childObjects.Length; i++)
		{
			childObjectsDJ[i] = childObjects[i].GetComponent<DistanceJoint2D>();
			childObjectsRB[i] = childObjects[i].GetComponent<Rigidbody2D>();
		}

		ConfigurePiece ();
	}

	void ConfigurePiece ()
	{
		childObjectsDJ[0].connectedBody = attachedTo;

		for (int i=1; i<childObjectsDJ.Length; i++)
		{
			//Debug.Log (i + ": Configuring piece: " + childObjectsDJ[i]);

			childObjectsDJ[i].connectedBody = childObjectsRB[i-1];
		}
	}
}
