using UnityEngine;
using System.Collections;

public class CameraShake : MonoBehaviour 
{
	private Vector3 originPos;
	private Quaternion originRotation;
	public float shakeDecay;
	public float shakeIntensity;
	private bool shake = false;

	void Start ()
	{
		shakeIntensity = 0.3f;
//		shakeDecay = 0.002f;
	}

	void Update () 
	{
		if (shake)
		{
			if (shakeIntensity > 0)
			{
				transform.position = originPos + Random.insideUnitSphere * shakeIntensity;
				transform.rotation = new Quaternion (originRotation.x + Random.Range(-shakeIntensity, shakeIntensity) * 0.2f, originRotation.y + Random.Range(-shakeIntensity, shakeIntensity) * 0.2f,
														originRotation.z + Random.Range(-shakeIntensity, shakeIntensity) * 0.2f, originRotation.w + Random.Range(-shakeIntensity, shakeIntensity) * 0.2f);
				shakeIntensity -= shakeDecay;
			}

			if (shakeIntensity <= 0)
			{
				shake = false;
			}
		}

		if (shakeIntensity <= 0)
		{
			transform.position = originPos;
			transform.rotation = originRotation;
		}
	}

	public void Shake ()
	{
		originPos = transform.position;
		originRotation = transform.rotation;
		shakeDecay = 0.002f;
		shake = true;
	}
}
