#region HeadComments
/* ========================================================================
* Copyright (C) 2015 ArthunGame
*
* 作    者：Arthun
* 文件名称：CameraController
* 功    能：相机控制器
* 创建时间：2016/01/30 02:48:35
* 
* =========================================================================
*/
#endregion

using UnityEngine;

public class CameraController : MonoBehaviour
{
    public static CameraController _instance;
    public static CameraController GetInstance()
    {
        return _instance;
    }

    public Transform _Target;//观察目标
    [HideInInspector]
    public bool _isLookTarget;//注视目标(不移动)

    #region 初始值
    public float _Height = 0.5f;//摄像机高度

    public float _Distance = 5f;//观察距离

    public float _X = -13f;//旋转角度 x
    public float _Y = 10f;//旋转角度 y
    #endregion

    public float _SpeedX = 5f;//旋转速度 X
    public float _SpeedY = 5f;//旋转速度 Y

    //X 为360度旋转，所以就不限制了
    public float _MinLimitY = 0f;//最小 Y
    public float _MaxLimitY = 50f;//最大 Y

    public float _MinDistance = 1.5f;//鼠标缩放距离最小值
    public float _MaxDistance = 5f;//鼠标缩放距离最大值

    public float _ZoomSpeed = 2f;//鼠标缩放速率

    private Quaternion _Rotation;

    void Awake()
    {
        _instance = this;
    }

    void Start()
    {
        _Rotation = Quaternion.Euler(_Y, _X, 0);
        transform.rotation = _Rotation;
    }

    void LateUpdate()
    {
        if (_Target == null)
            return;

        if (_isLookTarget)
        {
            this.transform.LookAt(_Target);
            return;
        }

        //if (Input.GetMouseButton(1))
        {
            _X += Input.GetAxis("Mouse X") * _SpeedX;
            _Y -= Input.GetAxis("Mouse Y") * _SpeedY;
            _Y = Mathf.Clamp(_Y, _MinLimitY, _MaxLimitY);

            _Rotation = Quaternion.Euler(_Y, _X, 0);
            transform.rotation = _Rotation;
        }

        _Distance -= Input.GetAxis("Mouse ScrollWheel") * _ZoomSpeed * 0.01f;
        _Distance = Mathf.Clamp(_Distance, _MinDistance, _MaxDistance);

        transform.position = _Rotation * new Vector3(0f, _Height, -_Distance) + _Target.position;
    }
}