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

public class DestructionEffectController : MonoBehaviour {
	[SerializeField] private Vector3 flyDirection = Vector3.up * 0.05f;
	[SerializeField] private float rotationSpeed = 0;

	float animationTime = 0;
	bool isActive = false;
	Vector3 startPosition;
	Transform selfTransform;

	public void StartDestruction() {
		isActive = true;
		animationTime = 0;
		selfTransform = transform;
		startPosition = transform.position;
		flyDirection = transform.rotation * flyDirection;
	}

	void Update () {
		if (!isActive) {
			return;
		}
		animationTime += Time.deltaTime;
		selfTransform.position = startPosition + (flyDirection) * animationTime;
		selfTransform.localEulerAngles = new Vector3 (0, 0, animationTime * rotationSpeed);
	}

	#if UNITY_EDITOR
	[ContextMenu("Calculate fly direction")]
	private void CalculateFlyDirection () {
		Vector2 parentPosition = transform.parent.position;
		Vector2 distance = -(Vector2)transform.localPosition;
		flyDirection = (distance + Random.insideUnitCircle * 0.05f).normalized * -0.05f;
		rotationSpeed = Random.Range (-30f, 30f);
	}

	void OnDrawGizmos () {
		Gizmos.DrawLine (transform.position, transform.position + (transform.rotation * flyDirection) * 10f);
	}
	#endif
}
