using UnityEngine;
using System.Collections;

public class EffectManager : MonoBehaviour
	{

	public GameObject[] elements;

	Vector3[] saved_position;
	float shakeUntil = -10;
	bool isShaking = false;
	float shakingStrength;

	public void shake( float duration , float strength )
		{
		shakeUntil = Mathf.Max ( shakeUntil ,  Time.time + duration );
		isShaking = true;
		shakingStrength = Mathf.Max (strength,shakingStrength);
		}

	void Start ()
		{
		saved_position = new Vector3[ elements.Length ];
		for( int i = 0 ; i < elements.Length ; i++ )
			saved_position[i] = elements[i].transform.position;

		// Debug
		//shake( 3 , 1 );
		}

	void resetPositions()
		{
		for( int i = 0 ; i < elements.Length ; i++ )
			elements[i].transform.position = saved_position[i];
		}

	void randomShake()
		{
		float s = shakingStrength;
		if( shakeUntil - Time.time < 1 )
			s *= (shakeUntil - Time.time);
		for( int i = 0 ; i < elements.Length ; i++ )
			elements[i].transform.position = saved_position[i] + Random.insideUnitSphere * s;
		}

	void Update ()
		{
		if( Time.time < shakeUntil )
			randomShake();
		else if( isShaking )
			{
			shakingStrength = 0;
			isShaking = false;
			resetPositions();
			}
		}

	}
