using UnityEngine;

namespace ScrambleKit.FX
{
    public class CameraFX : MonoBehaviour
    {
        private static float rumbleStart;
        private static float rumbleDuration;

        private static float rumbleUntil;
        private static float rumbleSize;
        public static void Rumble(float time, float size = 0.1f)
        {
            rumbleDuration = time;
            rumbleStart = Time.time;
            rumbleUntil = Time.time + time;
            rumbleSize = size;
        }

        public float RumbleRate = 3f;

        private  float origSize;
        private Camera cam;

        void Start()
        {
            cam = GetComponent<Camera>();
            origSize = cam.orthographicSize;
        }

        void LateUpdate()
        {
            if (rumbleUntil > Time.time)
                Rumble();
            else
                cam.orthographicSize = origSize;
        }

        private void Rumble()
        {
            float portion = (Time.time - rumbleStart) / rumbleDuration;
            float phase = -Mathf.Sin(portion * Mathf.PI);

            //float phase = Mathf.Sin((Time.time - rumbleStart) * RumbleRate);
            cam.orthographicSize = origSize + phase * rumbleSize;
        }
    }
}