using UnityEngine;
using System.Collections;
public class CameraShake : MonoBehaviour
{
//	private Vector3 originPosition;
	private Quaternion originRotation;
	float shakeIntensity, shakeDuration;
	float fShakeIntensity;
	float fShakeDuration;
	public Camera mainCamera;
	public bool bShaking;
	bool bTimeStop = true;
	void Start() 
	{
		mainCamera = Camera.main;
	}
//	void OnGUI (){
//		if (Input.GetButtonDown ("Fire1")){
//			Shake ();
//		}
//	}
	
	void Update (){
//		Debug.Log (Time.timeScale);
		if (shakeIntensity > 0)
		{
			//mainCamera.gameObject.transform.position = originPosition + Random.insideUnitSphere * shakeIntensity;
			mainCamera.gameObject.transform.rotation = new Quaternion(
				originRotation.x + Random.Range (-shakeIntensity,shakeIntensity) * .2f,
				originRotation.y + Random.Range (-shakeIntensity,shakeIntensity) * .2f,
				originRotation.z + Random.Range (-shakeIntensity,shakeIntensity) * .2f,
				originRotation.w + Random.Range (-shakeIntensity,shakeIntensity) * .2f);
			if (shakeIntensity > (fShakeIntensity*2/3) && bTimeStop)
			{
				Time.timeScale = 0;
			}else 
			{
//				Debug.Log (bTimeStop);
				Time.timeScale = 1;
			}
			shakeIntensity -= (fShakeIntensity/shakeDuration)*Time.unscaledDeltaTime;
		} else if(bShaking)
		{
//			mainCamera.gameObject.transform.position = originPosition;
			mainCamera.gameObject.transform.rotation = originRotation;
			bShaking = false;
//			Time.timeScale = 1;
		}
	}
	
//	public void Shake(){
//		//		originPosition = mainCamera.gameObject.transform.position;
//		originRotation = mainCamera.gameObject.transform.rotation;
//		fShakeIntensity = .1f;
//		fShakeDuration = .1f;
//		bStopTime = false;
//		shakeIntensity = fShakeIntensity;
//		shakeDuration = fShakeDuration;
//		bCameraNeedsReposition = true;
//	}
	public void Shake(float intensity, float duration, bool bStopTime){
//		originPosition = mainCamera.gameObject.transform.position;
		originRotation = mainCamera.gameObject.transform.rotation;
		fShakeIntensity = intensity;
		fShakeDuration = duration;
		bTimeStop = bStopTime;
		shakeIntensity = fShakeIntensity;
		shakeDuration = fShakeDuration;
		bShaking = true;
	}
}
