using UnityEngine;
using System.Collections;

public class CameraShake : MonoBehaviour
{
    // Class credit
    // http://answers.unity3d.com/questions/212189/camera-shake.html

    public bool Shaking;
    private float ShakeDecay = 0.6f;
    private float ShakeIntensity;

    private Vector3 OriginalPos;

    private Vector3 cameraDisplacement;

    void Start()
    {
        Shaking = false;
    }

    // Update is called once per frame
    void Update()
    {
        if (ShakeIntensity > 0.01f)
        {
            Vector3 adjustVector = cameraDisplacement * ShakeIntensity;
            transform.position = OriginalPos + adjustVector;

            /*
            transform.position = OriginalPos + Random.insideUnitSphere * ShakeIntensity;


            transform.rotation = new Quaternion(OriginalRot.x + Random.Range(-ShakeIntensity, ShakeIntensity) * .2f,
                                            OriginalRot.y + Random.Range(-ShakeIntensity, ShakeIntensity) * .2f,
                                            OriginalRot.z + Random.Range(-ShakeIntensity, ShakeIntensity) * .2f,
                                            OriginalRot.w + Random.Range(-ShakeIntensity, ShakeIntensity) * .2f);
                                            */
            ShakeIntensity *= ShakeDecay;
        }
        else if (Shaking)
        {
            transform.position = OriginalPos;
            Shaking = false;
        }
    }

    public float steps = 6.0f;
    public void DoShake(float flIntensity)
    {
        // Pick a random angle
        ShakeIntensity = flIntensity;
        cameraDisplacement = Random.onUnitSphere;

        OriginalPos = transform.position;

        Shaking = true;
    }

}
