#region HeadComments
/* ========================================================================
* Copyright (C) 2015 ArthunGame
*
* 作    者：Arthun
* 文件名称：SwitchCamera
* 功    能：
* 创建时间：2016/01/30 20:54:29
* 
* =========================================================================
*/
#endregion

using System.Collections;
using UnityEngine;

public class SwitchCamera : MonoBehaviour
{
    //震屏结束回调
    public delegate void CallBackShakeCamerOver();
    public CallBackShakeCamerOver _CallBackShakeCamerOver;

    //持续时间
    private float _continTime = 2f;
    private float _time = 0f;
    private bool _isShake = false;
    private float _shakeRange = 50f;

    private Vector3 deltaPos = Vector3.zero;

    void LateUpdate()
    {
        if (_isShake)
        {
            _shakeRange -= Time.deltaTime * 50f;
            if (_shakeRange <= 5f)
                _shakeRange = 5f;

            _time += Time.deltaTime;
            if (_time >= _continTime)
            {
                _time = 0f;
                _isShake = false;

                if (_CallBackShakeCamerOver != null)
                {
                    _CallBackShakeCamerOver();
                }
            }

            transform.localPosition -= deltaPos;
            deltaPos = Random.insideUnitSphere / _shakeRange;
            transform.localPosition += deltaPos;
        }
    }

    /// <summary>
    /// 震屏
    /// </summary>
    /// <param name="continTime">持续时间</param>
    /// <param name="overCallBack">结束回调</param>
    public void SetShakeCamera(float continTime, float delayTrigger = 0f, CallBackShakeCamerOver overCallBack = null)
    {
        StartCoroutine(delayShake(continTime, delayTrigger, overCallBack));
    }

    /// <summary>
    /// 延迟震屏
    /// </summary>
    /// <param name="continTime"></param>
    /// <param name="delayTrigger"></param>
    /// <param name="overCallBack"></param>
    /// <returns></returns>
    IEnumerator delayShake(float continTime, float delayTrigger = 0f, CallBackShakeCamerOver overCallBack = null)
    {
        yield return new WaitForSeconds(delayTrigger);
        _CallBackShakeCamerOver = overCallBack;
        _continTime = continTime;
        _isShake = true;
    }
}
