using UnityEngine;
using System.Collections;

public class PartDestructionController : MonoBehaviour
{
	public Vector3 destructionBaseVelocity;
	public float randomFactor = 0.5f;

	Vector3 destructionVelocity;
	Quaternion destructionAngularVelocity;
	float currentAngle;

	void Start ()
	{
		enabled = false;
		destructionAngularVelocity = Quaternion.Lerp (Quaternion.identity, Random.rotation, 0.1f);
	}

	public void StartDestruction ()
	{
		if (!this.enabled)
		{
			this.enabled = true;
			destructionVelocity = destructionBaseVelocity + (Vector3)Random.insideUnitCircle * randomFactor;
			for (int i = 0; i < 4; i++)
			{
				Invoke ("Blow", Random.Range (0f, 1.4f));
			}
		}
	}

	void Blow ()
	{
		BlowEffect.Show (transform.position + (Vector3)(Random.insideUnitSphere * 0.7f));
	}

	void Update ()
	{
		transform.localPosition += destructionVelocity * Time.deltaTime;
		transform.rotation = Quaternion.Lerp (transform.rotation, transform.rotation * destructionAngularVelocity, Time.deltaTime);
	}
}
