336 lines
11 KiB
C#
336 lines
11 KiB
C#
using Thousandto.Code.Center;
|
|
using Thousandto.Code.Global;
|
|
using Thousandto.Core.Framework;
|
|
using Thousandto.Plugins.Common.UniScene;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using UnityEngine;
|
|
|
|
namespace Thousandto.Code.Logic
|
|
{
|
|
|
|
/// <summary>
|
|
/// 摄像参数调整的处理器
|
|
/// </summary>
|
|
public class CameraAdjustHandler
|
|
{
|
|
#region//私有变量
|
|
//摄像机的控制器
|
|
private CameraControl _cameraControl = null;
|
|
//最后的基础距离
|
|
private float _lastBaseLength = 0.0f; // log last touch dis
|
|
//当前的基础距离
|
|
private float _currentBaseLength = 0.0f;
|
|
//记录缩放比例
|
|
private float _touchScale = 1.0f;
|
|
//记录摄像机距离改变的帧数
|
|
private int _cameraDistanceTouchframeCounter = 0;
|
|
//最大的放大值
|
|
private float _maxMagnify = 1.5f;
|
|
//最小的放大值
|
|
private float _minMagnify = 0.5f;
|
|
//多触发点的第二个点开始位置
|
|
private Vector2 _multiTouchFrontPos = Vector2.zero;
|
|
|
|
private bool _isEnableCameraMove = false;
|
|
|
|
private int _fingerIndex = 0;
|
|
|
|
//是否可用
|
|
private bool _isEnableCameraHandle = true;
|
|
//是否自动跟随主角旋转Y轴
|
|
private bool _isEnableCameraAutoYaw = true;
|
|
private int _frontCheckScreenWidth2 = 0;
|
|
private Rect _moveChangeCameraDisArea;
|
|
#endregion
|
|
|
|
#region //公共方法
|
|
|
|
public bool IsEnableCameraHandle
|
|
{
|
|
get
|
|
{
|
|
return _isEnableCameraHandle;
|
|
}
|
|
set
|
|
{
|
|
_isEnableCameraHandle = value;
|
|
}
|
|
}
|
|
|
|
//是否自动跟随主角旋转Y轴
|
|
public bool IsEnableCameraAutoYaw
|
|
{
|
|
get
|
|
{
|
|
return _isEnableCameraAutoYaw;
|
|
}
|
|
set
|
|
{
|
|
_isEnableCameraAutoYaw = value;
|
|
}
|
|
}
|
|
public int FingerIndex
|
|
{
|
|
get
|
|
{
|
|
return _fingerIndex;
|
|
}
|
|
}
|
|
|
|
//滑动改变摄像机距离区域
|
|
public Rect MoveChangeCameraDisArea
|
|
{
|
|
get
|
|
{
|
|
if (_frontCheckScreenWidth2 != Screen.width)
|
|
{
|
|
_moveChangeCameraDisArea.x = 0f;
|
|
_moveChangeCameraDisArea.y = Screen.height - 200f;
|
|
_moveChangeCameraDisArea.width = Screen.width;
|
|
_moveChangeCameraDisArea.height = 200f;
|
|
_frontCheckScreenWidth2 = Screen.width;
|
|
}
|
|
return _moveChangeCameraDisArea;
|
|
}
|
|
}
|
|
|
|
//摄像机是否已经锁定
|
|
public bool CameraIsLock
|
|
{
|
|
get
|
|
{
|
|
if(_cameraControl != null)
|
|
{
|
|
return _cameraControl.IsLock;
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
//刷新信息
|
|
public void Refresh(CameraControl control)
|
|
{
|
|
_cameraControl = control;
|
|
//GameCenter.RegFixEventHandle(LogicEventDefine.EID_EVENT_MOVEDIR_UPDATE, UpdateLocalPlayerMoveDir);
|
|
}
|
|
|
|
public void Reset()
|
|
{
|
|
_cameraControl = null;
|
|
//GameCenter.UnRegFixEventHandle(LogicEventDefine.EID_EVENT_MOVEDIR_UPDATE, UpdateLocalPlayerMoveDir);
|
|
}
|
|
|
|
//主角移动方向更新
|
|
private void UpdateLocalPlayerMoveDir(object o,object sender = null)
|
|
{
|
|
//if (!_isEnableCameraHandle)
|
|
// return;
|
|
//if (_isEnableCameraMove)
|
|
// return;
|
|
//if (_cameraControl.IsLock)
|
|
// return;
|
|
//if (!_isEnableCameraAutoYaw)
|
|
// return;
|
|
|
|
//Vector2 moveDir = (Vector2)o;
|
|
//if (moveDir == Vector2.zero)
|
|
// return;
|
|
//Vector3 dir = new Vector3(moveDir.x, 0f, moveDir.y);
|
|
|
|
//// [0, PI]
|
|
//var yaw = Mathf.Acos(Vector3.Dot(dir, Vector3.forward));
|
|
//if (dir.x < 0)
|
|
//{
|
|
// yaw = -yaw; //[-PI, 0]
|
|
//}
|
|
|
|
//yaw = yaw / Mathf.PI * 180f;
|
|
//float curYaw = _cameraControl.CurYaw;
|
|
|
|
//if (Mathf.Abs(yaw - curYaw) > 180f)
|
|
//{
|
|
// if (curYaw > 0f)
|
|
// {
|
|
// yaw = yaw + 360f;
|
|
// }
|
|
// else
|
|
// {
|
|
// yaw = -180 - (180f - yaw);
|
|
// }
|
|
//}
|
|
|
|
//if (curYaw < yaw)
|
|
//{
|
|
// curYaw += Time.deltaTime * 50f;
|
|
// if (curYaw > yaw)
|
|
// {
|
|
// curYaw = yaw;
|
|
// }
|
|
//}
|
|
//else if (curYaw > yaw)
|
|
//{
|
|
// curYaw -= Time.deltaTime * 50f;
|
|
// if (curYaw < yaw)
|
|
// {
|
|
// curYaw = yaw;
|
|
// }
|
|
//}
|
|
//_cameraControl.CurYaw = curYaw;
|
|
}
|
|
|
|
//开始调整摄像机的距离
|
|
public void OnStartCameraDistanceAdjust(Vector2 screenMultiTouchPos_first, Vector2 screenMultiTouchPos_end)
|
|
{
|
|
if (!_isEnableCameraHandle)
|
|
return;
|
|
if (IsValid())
|
|
{
|
|
//记录基础距离,通过多点触控的结束点和开始点
|
|
_lastBaseLength = (screenMultiTouchPos_end - screenMultiTouchPos_first).sqrMagnitude;
|
|
_cameraDistanceTouchframeCounter = 0;
|
|
}
|
|
}
|
|
//更新调整摄像机的距离
|
|
public void OnUpdateCameraDistanceAdjust(Vector2 screenMultiTouchPos_first, Vector2 screenMultiTouchPos_end)
|
|
{
|
|
if (!_isEnableCameraHandle)
|
|
return;
|
|
if (IsValid())
|
|
{
|
|
if (Input.touchCount != 2)
|
|
{
|
|
OnEndCameraDistanceAdjust();
|
|
return;
|
|
}
|
|
if (_cameraDistanceTouchframeCounter == 0)
|
|
{
|
|
// start touch
|
|
_lastBaseLength = (screenMultiTouchPos_end - screenMultiTouchPos_first).sqrMagnitude;
|
|
}
|
|
else if (_cameraDistanceTouchframeCounter > 0)
|
|
{
|
|
_currentBaseLength = (screenMultiTouchPos_end - screenMultiTouchPos_first).sqrMagnitude;
|
|
//通过当前两个点的距离和开始时的距离进行对比,计算出缩放比率
|
|
_touchScale = (_currentBaseLength / _lastBaseLength);
|
|
// apply scale
|
|
if (_touchScale > _maxMagnify)
|
|
{
|
|
_touchScale = _maxMagnify;
|
|
}
|
|
if (_touchScale < _minMagnify)
|
|
{
|
|
_touchScale = _minMagnify;
|
|
}
|
|
_touchScale = 1 / _touchScale;
|
|
if (_cameraControl != null)
|
|
{
|
|
if (_touchScale != 1.0f)
|
|
{
|
|
// 根据缩放比例来调整摄像机与角色之间的距离
|
|
float currentDis = _cameraControl.TarDis;
|
|
if (currentDis * _touchScale < CameraControlDefine.CurMinDis)
|
|
{
|
|
_cameraControl.TarDis = CameraControlDefine.CurMinDis;
|
|
_touchScale = 1.0f;
|
|
}
|
|
else if (currentDis * _touchScale > CameraControlDefine.CurMaxDis)
|
|
{
|
|
_cameraControl.TarDis = CameraControlDefine.CurMaxDis;
|
|
_touchScale = 1.0f;
|
|
}
|
|
else
|
|
{
|
|
_cameraControl.TarDis = currentDis * _touchScale;
|
|
}
|
|
}
|
|
}
|
|
_lastBaseLength = _currentBaseLength;
|
|
}
|
|
_cameraDistanceTouchframeCounter++;
|
|
}
|
|
}
|
|
//完成摄像机距离调整
|
|
public void OnEndCameraDistanceAdjust()
|
|
{
|
|
_lastBaseLength = 0.0f;
|
|
_currentBaseLength = 0.0f;
|
|
_touchScale = 1.0f;
|
|
_cameraDistanceTouchframeCounter = 0;
|
|
}
|
|
|
|
// 摄像机平射角度改变开始
|
|
public void OnStartCameraYawMultiTouch(int fingerIndex, Vector2 screenMultiTouchPos)
|
|
{
|
|
if (!_isEnableCameraHandle)
|
|
return;
|
|
//已经锁定视角
|
|
//if (_cameraControl.IsLock)
|
|
// return;
|
|
if (IsValid())
|
|
{
|
|
_multiTouchFrontPos = screenMultiTouchPos;
|
|
_fingerIndex = fingerIndex;
|
|
_isEnableCameraMove = true;
|
|
}
|
|
}
|
|
// 摄像机平射角度持续更新改变
|
|
public void OnUpdateCameraYawMultiTouch(int fingerIndex, Vector2 screenMultiTouchPos)
|
|
{
|
|
if (!_isEnableCameraHandle)
|
|
return;
|
|
|
|
if (IsValid() && _isEnableCameraMove && _fingerIndex == fingerIndex)
|
|
{
|
|
var moveVec = screenMultiTouchPos - _multiTouchFrontPos;
|
|
if (moveVec == Vector2.zero)
|
|
return;
|
|
|
|
//已经锁定视角
|
|
if (_cameraControl.IsLock)
|
|
{
|
|
return;
|
|
}
|
|
if(MoveChangeCameraDisArea.Contains(screenMultiTouchPos))
|
|
{
|
|
// 根据缩放比例来调整摄像机与角色之间的距离
|
|
float currentDis = _cameraControl.TarDis;
|
|
currentDis += moveVec.x * 0.1f;
|
|
if (currentDis < CameraControlDefine.CurMinDis)
|
|
{
|
|
currentDis = CameraControlDefine.CurMinDis;
|
|
}
|
|
else if (currentDis > CameraControlDefine.CurMaxDis)
|
|
{
|
|
currentDis = CameraControlDefine.CurMaxDis;
|
|
}
|
|
_cameraControl.TarDis = currentDis;
|
|
}
|
|
else
|
|
{
|
|
_cameraControl.CurYaw += moveVec.x * 0.25f;
|
|
_cameraControl.CurPitch -= moveVec.y * 0.2f;
|
|
}
|
|
_multiTouchFrontPos = screenMultiTouchPos;
|
|
}
|
|
}
|
|
// 摄像机平射角度结束该改变
|
|
public void OnEndCameraYawMultiTouch(int fingerIndex)
|
|
{
|
|
if (_fingerIndex != fingerIndex)
|
|
return;
|
|
_isEnableCameraMove = false;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region//私有方法
|
|
//是否有效
|
|
private bool IsValid()
|
|
{
|
|
return _cameraControl != null;
|
|
}
|
|
#endregion
|
|
}
|
|
}
|