using UnityEngine;
using System.Collections;

public class CameraMovement : MonoBehaviour {

	public Transform cameraTarget;
	public float speed = 0.5f;

	public AnimationCurve shakeCurve;

	private float shakeDuration;
	private float shakeIntensity;
	private float shakeTime = 1.0f;
	private float initialPosition = 0.0f;

	// Use this for initialization
	void Start ()
	{
		shakeDuration = 0.0f;
		shakeIntensity = 0.0f;
	}
	
	// Update is called once per frame
	void Update () 
	{
		if (shakeTime < 1.0f)
		{
			transform.position = new Vector3(shakeCurve.Evaluate(shakeTime) * shakeIntensity + initialPosition, transform.position.y, transform.position.z);
			shakeTime += Time.deltaTime / shakeDuration;
		}
		else
		{
			transform.position = Vector3.MoveTowards(transform.position, cameraTarget.position, speed * Time.deltaTime);
		}
	}

	public void Shake(float duration, float intensity)
	{
		shakeDuration = duration;
		shakeIntensity = intensity;
		shakeTime = 0.0f;
		initialPosition = transform.position.x;
	}
}
