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

public class Cam : MonoBehaviour {

	public Vector3 originalPos;
	public AnimationCurve shakeCurve;
	public float maxShakePower = 0.75f;
	public float maxShakeDuration = 0.075f;

	// Use this for initialization
	void Start () {
		originalPos = transform.position;
	}
	
	// Update is called once per frame
	void Update () {
		
	}

	public IEnumerator ShakeCamera(float intensity) {
		// float curve = shakeCurve.Evaluate(intensity);
		float curve = intensity;
		float shakePower = maxShakePower * curve;
		float shakeDuration = maxShakeDuration * curve;
		shakePower = Mathf.Min(shakePower, maxShakePower);
		shakeDuration = Mathf.Min(shakeDuration, maxShakeDuration);

		while (shakeDuration > 0) {
			transform.position = originalPos;
			Vector2 shakePos = Random.insideUnitCircle * shakePower;
			transform.position = new Vector3(transform.position.x + shakePos.x, transform.position.y + shakePos.y, transform.position.z);
			shakeDuration -= Time.deltaTime;

			yield return new WaitForEndOfFrame();
		}

		transform.position = originalPos;
	}
}
