using DG.Tweening; using System; using System.Collections; using System.Collections.Generic; using UnityEngine; namespace UnityCommon { public abstract class CameraState { public Action onExit; public CameraState preCameraState; protected float speedScaleFactor = 1; public CameraState(Camera camera) { m_camera = camera; } public virtual void Enter() { speedScaleFactor = 1; m_camera.enabled = true; } public virtual void Exit() { m_camera.enabled = false; onExit?.Invoke(); } public virtual void Update() { } protected Camera m_camera { get; set; } /// /// 切换完毕 /// public bool m_isAllReady { get; set; } /// /// 镜头前后、左右、上下移动 /// /// x前后 y左右 z上下 public virtual void OnMove(Vector3 vecDir) { } /// /// 镜头旋转 /// /// x左右 y上下 public virtual void OnRotate(Vector2 vector2) { } /// /// 缩放 /// /// public virtual void OnZoom(float zoom) { } /// /// 平移 /// /// public virtual void OnTranlation(Vector2 vector2) { } /// /// 加速 /// /// public virtual void OnAddSpeed(float addSpeed) { } /// /// 缩放速度因子,移动、旋转、镜头推拉的速度缩放比例 /// /// public virtual void OnChangeSpeedScale(float speedScale) { if (speedScale > 0) Math.Clamp(speedScaleFactor *= 2, 0.125f, 16); else if (speedScale < 0) Math.Clamp(speedScaleFactor /= 2, 0.125f, 16); } public void SetAnimDuration(float duration) { animDuration = duration; } float animDuration = 1; Sequence sequence; protected void SetCameraPosition(Vector3 position, Quaternion rotation, Action onCompleted = null) { if (animDuration <= 0) { m_camera.transform.position = position; m_camera.transform.rotation = rotation; m_isAllReady = true; onCompleted?.Invoke(); } else { m_isAllReady = false; sequence?.Kill(); sequence = DOTween.Sequence(); sequence.Join(m_camera.transform.DOMove(position, animDuration)); sequence.Join(m_camera.transform.DORotate(rotation.eulerAngles, animDuration)); sequence.OnComplete(() => { m_isAllReady = true; onCompleted?.Invoke(); }); } } } }