319 lines
8.9 KiB
C#
319 lines
8.9 KiB
C#
// ***********************************************************************
|
|
// Assembly : Unity
|
|
// Author : Kimch
|
|
// Created : 2018-2-29
|
|
// Description : 游戏相机
|
|
// Last Modified By :
|
|
// Last Modified On :
|
|
// ***********************************************************************
|
|
// <copyright file= "GameCamera" company=""></copyright>
|
|
// <summary></summary>
|
|
// ***********************************************************************
|
|
using UnityEngine;
|
|
|
|
namespace G
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public class GameCamera : MonoBehaviour
|
|
{
|
|
public static GameCamera Instance;
|
|
|
|
public float view1F = 40f;
|
|
public Vector3 view1CameraP = new Vector3(0f, 1.3f, -1.04f);
|
|
public Vector3 view1CameraR = new Vector3(80f, 0f, 0f);
|
|
|
|
public Vector3 view2CameraP;
|
|
public Vector3 view2CameraR = new Vector3(42f, 0f, 0f);
|
|
|
|
public Vector3 hit_shake1 = new Vector3(0.06f, 0f, 0.03f);
|
|
|
|
public Vector3 hit_shake2 = new Vector3(0f, 0.03f, 0.02f);
|
|
|
|
private Camera _camera;
|
|
|
|
private int _shakeDirection = 1;
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
private bool _zoom;
|
|
private int _zoomSpeed;
|
|
private float _zoomTime;
|
|
private float _zoomDelay;
|
|
|
|
private float _fov = 30f;
|
|
private float _originFov = 30f;
|
|
|
|
private Transform _target;
|
|
private Vector3 _targetOffset;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
private bool _resetStart;
|
|
private float _resetDelay;
|
|
|
|
private bool _fovChange;
|
|
private float _fovbackDelay;
|
|
|
|
private float _boundFactor = 1f;
|
|
private float _limitX = 2.1f;
|
|
private float _limitY_b = -3.3f;
|
|
private float _limitY_f = 0.85f;
|
|
|
|
private int _topviewOn;
|
|
private float _topviewDelay = 3f;
|
|
|
|
private float _moveSpeed = 6f;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public void Topview()
|
|
{
|
|
_targetOffset = new Vector3(0f, 1.3f, -0.1f);
|
|
_topviewOn = 1;
|
|
_topviewDelay = 3f;
|
|
_moveSpeed = 5f;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="target"></param>
|
|
/// <param name="fov"></param>
|
|
/// <param name="delay"></param>
|
|
public void LookTarget(Transform target, int fov, float delay)
|
|
{
|
|
_target = target;
|
|
if (delay > 0f)
|
|
{
|
|
_resetStart = true;
|
|
_resetDelay = delay;
|
|
}
|
|
if (fov != 0)
|
|
{
|
|
ZoomIn(-1, fov, delay);
|
|
}
|
|
}
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="speed"></param>
|
|
/// <param name="fov"></param>
|
|
/// <param name="delay"></param>
|
|
public void ZoomIn(int speed, int fov, float delay)
|
|
{
|
|
_zoom = true;
|
|
_zoomSpeed = speed;
|
|
_zoomTime = delay;
|
|
_fov = fov;
|
|
_fovChange = true;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public void Hitcam()
|
|
{
|
|
UI_Ingame.Instance.ComboPlus(0.01f);
|
|
|
|
if (Random.value < 0.8f)
|
|
return;
|
|
|
|
_shakeDirection = -_shakeDirection;
|
|
this.transform.position += hit_shake1 * _shakeDirection;
|
|
|
|
Vibrate(1f);
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="factor"></param>
|
|
public void Hitcam2(float factor)
|
|
{
|
|
this.transform.position += hit_shake2 * factor;
|
|
|
|
Vibrate(factor);
|
|
}
|
|
|
|
void Vibrate(float factor)
|
|
{
|
|
if (GameLevel.Instance.vibrateSwitch)
|
|
{
|
|
int type = 0;
|
|
if (factor < 1f)
|
|
{
|
|
type = 2;
|
|
}
|
|
else if (factor < 1.1f)
|
|
{
|
|
type = 1;
|
|
}
|
|
KPlatform.Instance.VibrateShort(0);
|
|
}
|
|
}
|
|
|
|
public void Reset()
|
|
{
|
|
_target = EntityMainPlayer.Instance.transform;
|
|
_fov = _originFov;
|
|
_fovChange = true;
|
|
_resetStart = false;
|
|
|
|
_zoom = false;
|
|
_zoomDelay = 0f;
|
|
}
|
|
|
|
public void SetMode(int mode)
|
|
{
|
|
int kind = 6;
|
|
if (kind > 5)
|
|
{
|
|
//_topviewOn = 0;
|
|
_limitX = 1.6f;
|
|
_limitY_b = -3.4f;
|
|
_limitY_f = -1f;
|
|
}
|
|
}
|
|
|
|
#region Unity
|
|
|
|
private void Awake()
|
|
{
|
|
Instance = this;
|
|
_camera = this.GetComponent<Camera>();
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
_originFov = _camera.fieldOfView;
|
|
_targetOffset = view1CameraP;
|
|
Reset();
|
|
}
|
|
|
|
private void LateUpdate()
|
|
{
|
|
if (_resetStart)
|
|
{
|
|
if (_resetDelay > 0f)
|
|
{
|
|
_resetDelay -= Time.deltaTime;
|
|
}
|
|
else
|
|
{
|
|
_resetDelay = 0f;
|
|
_resetStart = false;
|
|
Reset();
|
|
//_cha1.GetComponent<EntityMainPlayer>().StartControl();
|
|
}
|
|
}
|
|
|
|
if (!_target)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var targetPos = _target.position + _targetOffset;
|
|
var thisPos = this.transform.position;
|
|
thisPos = Vector3.Lerp(thisPos, targetPos, Time.deltaTime * _moveSpeed);
|
|
|
|
if (_topviewOn != 1)
|
|
{
|
|
thisPos.z = Mathf.Clamp(thisPos.z, _limitY_b * _boundFactor, _limitY_f * _boundFactor);
|
|
}
|
|
thisPos.x = Mathf.Clamp(thisPos.x, (0f - _limitX) * _boundFactor, _limitX * _boundFactor);
|
|
|
|
if (_topviewOn == 2)
|
|
{
|
|
this.transform.position = Vector3.MoveTowards(transform.position, thisPos, Time.deltaTime * 3f);
|
|
}
|
|
else
|
|
{
|
|
this.transform.position = thisPos;
|
|
}
|
|
|
|
if (_fovChange)
|
|
{
|
|
_boundFactor = 0.8f + 0.2f * _originFov / _camera.fieldOfView;
|
|
|
|
if (_zoom)
|
|
{
|
|
if (_zoomSpeed == -1)
|
|
{
|
|
_camera.fieldOfView = _fov;
|
|
}
|
|
else
|
|
{
|
|
_camera.fieldOfView = Mathf.Lerp(_camera.fieldOfView, _fov, Time.deltaTime * _zoomSpeed);
|
|
}
|
|
|
|
if (_zoomDelay < _zoomTime)
|
|
{
|
|
_zoomDelay += Time.deltaTime;
|
|
}
|
|
else
|
|
{
|
|
_zoom = false;
|
|
_zoomDelay = 0f;
|
|
_fovbackDelay = 0f;
|
|
}
|
|
}
|
|
else if (_fovbackDelay < 2f)
|
|
{
|
|
_fovbackDelay += Time.deltaTime;
|
|
_camera.fieldOfView = Mathf.Lerp(_camera.fieldOfView, _originFov, Time.deltaTime * 3f);
|
|
}
|
|
else
|
|
{
|
|
_fovbackDelay = 0f;
|
|
_fovChange = false;
|
|
_camera.fieldOfView = _originFov;
|
|
}
|
|
}
|
|
|
|
if (_topviewOn <= 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_topviewDelay -= Time.deltaTime;
|
|
if (_topviewOn == 1)
|
|
{
|
|
this.transform.rotation = Quaternion.Lerp(this.transform.rotation, Quaternion.Euler(view1CameraR), Time.deltaTime * 5f);
|
|
_camera.fieldOfView = Mathf.Lerp(_camera.fieldOfView, view1F, Time.deltaTime * 3f);
|
|
if (_topviewDelay < 1.5f)
|
|
{
|
|
_targetOffset = view1CameraP;
|
|
_topviewOn = 2;
|
|
_moveSpeed = 5;
|
|
}
|
|
}
|
|
else if (_topviewOn == 2)
|
|
{
|
|
this.transform.rotation = Quaternion.Lerp(this.transform.rotation, Quaternion.Euler(view2CameraR), Time.deltaTime * 5f);
|
|
_camera.fieldOfView = Mathf.Lerp(_camera.fieldOfView, _originFov, Time.deltaTime * 3f);
|
|
if (_topviewDelay < 0f)
|
|
{
|
|
_topviewOn = 0;
|
|
_moveSpeed = 10;
|
|
this.transform.rotation = Quaternion.Euler(view2CameraR);
|
|
}
|
|
}
|
|
}
|
|
|
|
#if UNITY_EDITOR
|
|
private void OnDrawGizmos()
|
|
{
|
|
var sizeY = _limitY_f * _boundFactor - _limitY_b * _boundFactor;
|
|
var centerY = _limitY_f * _boundFactor + _limitY_b * _boundFactor;
|
|
Gizmos.DrawWireCube(new Vector3(0, 0, centerY / 2), new Vector3(_limitX * 2 * _boundFactor, 0.2f, sizeY));
|
|
}
|
|
#endif
|
|
|
|
#endregion
|
|
}
|
|
}
|