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

public class ScreenShakeManager : MonoBehaviour
{
    public static ScreenShakeManager instance;

    void Awake()
    {
        if (instance == null)
        {
            instance = this;
        }
        else
        {
            Destroy(this);
        }
    }

    [SerializeField]
    float p_myDistance;
    [SerializeField]
    int p_myRepetition;

    GameObject camera;
    Vector3 initPos;

    void Start()
    {
        camera = GameObject.FindGameObjectWithTag("MainCamera").transform.parent.gameObject;
        initPos = camera.transform.localPosition;
    }

    public void ScreenShake()
    {
        StartCoroutine(Shake());
    }

    IEnumerator Shake()
    {
        for (int i = 0; i < p_myRepetition; i++)
        {
            Vector3 newPos = initPos;
            newPos.x += RandomManager.getFloatRandom(p_myDistance * 2) - p_myDistance;
            newPos.y += RandomManager.getFloatRandom(p_myDistance * 2) - p_myDistance;
            newPos.z += RandomManager.getFloatRandom(p_myDistance * 2) - p_myDistance;
            camera.transform.position = newPos;
            yield return new WaitForEndOfFrame();
        }
        camera.transform.localPosition = initPos;
    }
}