| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338 |
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using System;
- #region data
- public enum CameraMoveMode
- {
- Camera_SyncFocusPosition = 0, //同步焦点和位置
- Camera_MoveFocusPosition = 1, //焦点和位置同时移动
- Camera_MoveFocusFirst = 2, //优先移动焦点
- Camera_MovePositionFirst = 3, //优先移动位置
- Camera_FollowFighterPosition = 5, //同步fighter的位置
- Camera_Follow_FixedTarget = 6,
- Camera_SyncRotPosition = 7, //同步旋转和位置
- Camera_ManualRotate = 8,
- }
- public enum CameraMode
- {
- Camera_None = 0,
- Camera_BattleAuto = 1,
- Camera_FollowLeftFighters = 2,
- Camera_LockFighter = 3,
- Camera_Fixed_Target = 4,
- Camera_Move_BattleFieldCenter = 5,
- Camera_TweeningMove = 6,
- Camera_TweeningJump = 7,
- Camera_ManualRotate = 8,
- }
- public enum BattleTransitionState
- {
- BTS_None = 0,
- BTS_Out = 1, //离场
- BTS_Black = 2, //黑屏
- BTS_In = 3, //进场
- }
- public class CameraFollowFighterHelper
- {
- public Fighter FollowFighter { get; set; }
- public Vector3 TargetOffset { get; set; }
- public Vector3 CameraOffset{ get; set; }
- public float FollowDist { get; set; }
- public float Fov{ get; set; }
- public float FollowSpeedRate{ get; set; }
- public float ContinueTime{ get; set; }
- public CameraMoveMode MoveMode{ get; set; }
- public bool IsFollow{ get; set; }
- public bool IsUnscaleTime { get; set; }
- public bool IsActive { get { return mHasStart && (IsUnscaleTime ? Time.unscaledTime < mStartTime + ContinueTime : Time.time < mStartTime + ContinueTime); } }
- float mStartTime;
- bool mHasStart;
- Vector3 mStartFocus;
- Vector3 mStartCameraPosition;
- public void Start ()
- {
- mStartTime = IsUnscaleTime ? Time.unscaledTime : Time.time;
- mHasStart = true;
- if (!IsFollow)
- {
- mStartFocus = FollowFighter == null ? Vector3.zero : FollowFighter.Position + TargetOffset;
- mStartCameraPosition = mStartFocus + CameraOffset;
- }
- }
- public void Stop ()
- {
- FollowFighter = null;
- mHasStart = false;
- }
- public Vector3 GetFocus ()
- {
- return IsFollow ? (FollowFighter == null ? Vector3.zero : FollowFighter.Position + TargetOffset) : mStartFocus;
- }
- public Vector3 GetCameraPosition ()
- {
- return IsFollow ?(GetFocus () - FollowFighter.Ctrl.transform.forward * FollowDist + CameraOffset) : mStartCameraPosition;
- }
- public Vector3 GetCameraPosition(Vector3 forward)
- {
- return GetFocus() - forward * FollowDist + CameraOffset;
- }
- public Vector3 Forward
- {
- get { return IsFollow ? (FollowFighter != null ? FollowFighter.Ctrl.transform.forward : Vector3.zero):Vector3.zero; }
- }
- }
- #endregion
- public class BattleCamera : Singleton<BattleCamera>
- {
- #region static_and_const
- public const string FighterLayerName = "Fighter";
- public const string SceneEffectLayerName = "FX";
- public const string DisableLayerName = "Disable";
- public const string BackGroudLayerName = "Background";
- public const string WaterLayerName = "Water";
- public const string GroundLayerName = "Ground";
- public const string ReflectionLayerName = "Reflection";
- public const string BackScreenLayerName = "BackScreenEffect";
- public const string HideLayerName = "Hide";
- public const string T4M5LayerName = "T4M5";
- public const float camera_view_border_blank = 0.1f;
- public const float camera_view_bottom_blank = 0.2f;
- public const float camera_far_clip = 100;
- public static int defalutLayer = LayerMask.NameToLayer("Default");
- public static int hideLayer = LayerMask.NameToLayer("Hide");
- //"Water", "MainCity", "Ground", "Reflection", "InvalidLayer"
- //public int FighterLayerMask;
- //public int FullLayerMask;
- #endregion
- #region fields
- private bool tracePosition;
- private bool mIsDoingSpecialCamera;
- private Camera mRealCam;
- public Camera RealCamera
- {
- get
- {
- return mRealCam;
- }
- }
- public bool BattleCameraEnabled
- {
- get { return mRealCam != null ? mRealCam.enabled : false; }
- }
- private Shake mShake = null;
- CameraMode currentCameraMode = CameraMode.Camera_BattleAuto;
- CameraMoveMode moveMode = CameraMoveMode.Camera_FollowFighterPosition;
- bool enableUpdateTo;
- bool enableUpdateCurrent;
- bool enableAutoFov = true;
- const float updateToInterval = 1.0f/60;
- float currentUpdateToInterval;
- const int maxCallUpdateCnt = 2;
- int callUpdateToCnt = 0;
- //相机目标属性
- Vector3 toFocus = Vector3.zero;
- Vector3 toPosition = Vector3.zero;
- Vector3 toElerAngle = Vector3.zero;
- float toFov;
- //相机当前属性
- Vector3 currentFocus = Vector3.zero;
- Vector3 currentPosition = Vector3.zero;
- Vector3 currentElerAngle = Vector3.zero;
- float currentFov;
- int cullingMask;
- public int CullingMask
- {
- get { return cullingMask; }
- }
- CameraClearFlags clearFlag;
- bool hasSetCamera;
- CameraClearFlags realCameraClearFlags;
- public bool syncSpeed = true;
- float syncFovSpeedRate = 1f;
- Transform efxBattleTransition;
- public bool enableDynamicSpeed = true;
- CameraFollowFighterHelper mFollowFighterHelper;
- GameObject camContainer = null;
- BattleCamConfig mCameraCfg = null;
- Animator camAnimator = null;
- RadialBlurEffect mBlurEffect = null;
- CameraRotateAround mCamRotateCom = null;
- private GameObject mComingCameraGo = null;
- #endregion
- public GameObject ComingCameraGo { get { return mComingCameraGo; } }
- public GameObject CamContainer { get { return camContainer; } }
- public BattleCamConfig CamCfg
- {
- get { return mCameraCfg; }
- }
- private Vector3 mCameraOffset;
- public Vector3 CameraOffset
- {
- get { return mCameraOffset; }
- }
- private BossIntroCameraCfg mBossIntroCamCfg;
- private bool mCustomFollowCamera;
- Vector3 followCameraOffset;
- float followCameraFov;
- #region properties
- Animator AnimatorCom {
- get {
- return camAnimator;
- }
- }
- public bool IsPlayingAnimation {
- get {
- if (AnimatorCom != null && AnimatorCom.isActiveAndEnabled)
- return true;
- return false;
- }
- }
- public Vector3 Forward {
- get {
- return RealCamera.transform.forward;
- }
- }
- public Vector3 Right {
- get {
- return RealCamera.transform.right;
- }
- }
- bool IsCurrentCloseToTarget {
- get {
- return currentPosition.CloseTo (toPosition) && currentFocus.CloseTo (toFocus) && Mathf.Abs (currentFov - toFov) < 0.0001f;
- }
- }
- CameraMode CurrentCameraMode {
- get {
- return currentCameraMode;
- }
- set {
- currentCameraMode = value;
- switch (currentCameraMode)
- {
- case CameraMode.Camera_None:
- enableUpdateTo = false;
- enableUpdateCurrent = false;
- mIsDoingSpecialCamera = false;
- break;
- case CameraMode.Camera_FollowLeftFighters:
- enableUpdateTo = true;
- enableUpdateCurrent = true;
- moveMode = CameraMoveMode.Camera_SyncFocusPosition;
- enableAutoFov = false;
- mIsDoingSpecialCamera = false;
- syncSpeed = true;
- enableDynamicSpeed = false;
- break;
- case CameraMode.Camera_LockFighter:
- enableUpdateTo = true;
- enableUpdateCurrent = true;
- enableAutoFov = false;
- mIsDoingSpecialCamera = false;
- enableDynamicSpeed = true;
- break;
- case CameraMode.Camera_BattleAuto:
- enableUpdateTo = true;
- enableUpdateCurrent = true;
- moveMode = CameraMoveMode.Camera_SyncFocusPosition;
- enableAutoFov = false;
- mIsDoingSpecialCamera = false;
- enableDynamicSpeed = true;
- break;
- case CameraMode.Camera_Fixed_Target:
- enableUpdateTo = false;
- enableUpdateCurrent = false;
- moveMode = CameraMoveMode.Camera_Follow_FixedTarget;
- enableAutoFov = false;
- mIsDoingSpecialCamera = false;
- enableDynamicSpeed = false;
- break;
- case CameraMode.Camera_Move_BattleFieldCenter:
- enableUpdateTo = true;
- enableUpdateCurrent = true;
- moveMode = CameraMoveMode.Camera_MoveFocusPosition;
- enableAutoFov = false;
- mIsDoingSpecialCamera = false;
- syncSpeed = false;
- mCustomFollowCamera = false;
- enableDynamicSpeed = false;
- break;
- case CameraMode.Camera_TweeningMove:
- enableUpdateTo = false;
- enableUpdateCurrent = true;
- enableAutoFov = false;
- mIsDoingSpecialCamera = true;
- enableDynamicSpeed = false;
- moveMode = CameraMoveMode.Camera_SyncFocusPosition;
- break;
- case CameraMode.Camera_ManualRotate:
- enableUpdateTo = true;
- enableUpdateCurrent = true;
- moveMode = CameraMoveMode.Camera_ManualRotate;
- enableAutoFov = false;
- mIsDoingSpecialCamera = false;
- enableDynamicSpeed = false;
- break;
- }
- }
- }
- public Vector3 CamPosition
- {
- get {
- return camContainer!= null ? camContainer.transform.position : Vector3.zero;
- }
- set
- {
- if(camContainer!=null)
- camContainer.transform.position = value;
- }
- }
- public Vector3 CamForward
- {
- get
- {
- return camContainer!= null ? camContainer.transform.forward : CamEulerAngle.normalized;
- }
- }
- public Vector3 CamEulerAngle
- {
- get { return mCurCamEulerAngle; }
- set
- {
- if(mCurCamEulerAngle!=value)
- {
- mCurCamEulerAngle = value;
- if(null != camContainer)
- camContainer.transform.rotation = Quaternion.Euler(mCurCamEulerAngle);
- }
- }
- }
- public float CamFov
- {
- get { return mRealCam != null?mRealCam.fieldOfView : 60; }
- set
- {
- if(mRealCam!=null)
- mRealCam.fieldOfView = Mathf.Max(value, 0.1f);
- }
- }
- bool initialized;
- Vector3 mCurCamEulerAngle;
- BaseBattle mBattle;
- private float mMoveSpeed = 0;
- public float MoveSpeed
- {
- get { return mMoveSpeed; }
- set { mMoveSpeed = value; }
- }
- private float mRotSpeed = 0;
- public float RotSpeed
- {
- get { return mRotSpeed; }
- set { mRotSpeed = value; }
- }
- private float mFovSpeed = 0;
- public float FovSpeed
- {
- get { return mFovSpeed; }
- set { mFovSpeed = value; }
- }
- public float MoveSpeedInBattle
- {
- get { return CamCfg.CamMoveSpeedInBattle; }
- }
- #endregion
- public override void Init()
- {
- base.Init();
- }
- public override void UnInit()
- {
- base.UnInit();
- }
- public void Initialize (LogicBattle battle)
- {
- if (initialized)
- return;
- mBattle = battle;
- //FighterLayerMask = LayerMask.GetMask (new [] { FighterLayerName });
- //FullLayerMask = LayerMask.GetMask (new [] {
- // FighterLayerName,
- // SceneEffectLayerName,
- // WaterLayerName,
- // ReflectionLayerName,
- // GroundLayerName,
- // BackGroudLayerName
- //});
- mComingCameraGo = battle.BattleScene.ComingCamera;
- camContainer = battle.BattleScene.CameraContainer;
- if (camContainer == null)
- return;
- mCameraCfg = camContainer.GetComponent<BattleCamConfig>();
- if (!camContainer.activeInHierarchy)
- camContainer.SetActive(true);
- camAnimator = camContainer.GetComponentInChildren<Animator>();
- mCamRotateCom = camContainer.GetComponentInChildren<CameraRotateAround>();
- if (mCamRotateCom != null)
- {
- mCamRotateCom.target = battle.TeamCenterPoint;
- }
- if (battle.BattleScene.BossIntroCamGo != null)
- {
- mBossIntroCamCfg = battle.BattleScene.BossIntroCamGo.GetComponent<BossIntroCameraCfg>();
- }
- if (AnimatorCom != null)
- AnimatorCom.enabled = false;
- mRealCam = battle.BattleScene.CameraTrans.GetComponent<Camera>();
- if (Camera.main == null)
- mRealCam.tag = "MainCamera";
- cullingMask = mRealCam.cullingMask;
- clearFlag = mRealCam.clearFlags;
- mRealCam.allowHDR = false;
- mRealCam.allowMSAA = false;
- mRealCam.farClipPlane = camera_far_clip;
- InitComponents ();
- initialized = true;
- toFocus = battle.BattleScene.ActorBornPoint.GetReadyPointPos(0, 1).heroPos;
- toPosition = toFocus - battle.BattleScene.ActorBornPoint.transform.forward * mCameraCfg.followDist + mCameraCfg.followCamOffset;
- toFov = mCameraCfg.followCameraFov;
- mCurCamEulerAngle = camContainer.transform.rotation.eulerAngles;
- SetCurrentCamera(false, true,0);
- mBlurEffect = mRealCam.GetComponent<RadialBlurEffect>();
- mMoveSpeed = CamCfg.CamMoveSpeed;
- mRotSpeed = CamCfg.CamRotSpeed;
- mFovSpeed = CamCfg.CamFovSpeed;
- InitShake();
- }
- public void Initialize(VersusBattle battle)
- {
- if (initialized)
- return;
- mBattle = battle;
- //FighterLayerMask = LayerMask.GetMask(new[] { FighterLayerName });
- //FullLayerMask = LayerMask.GetMask(new[] {
- // FighterLayerName,
- // SceneEffectLayerName,
- // WaterLayerName,
- // ReflectionLayerName,
- // GroundLayerName,
- // BackGroudLayerName
- //});
- if (battle == null) return;
- mComingCameraGo = battle.BattleScene.ComingCamera;
- camContainer = battle.BattleScene.CameraContainer;
- if (camContainer == null)
- return;
- if (!camContainer.activeInHierarchy)
- camContainer.SetActive(true);
- camAnimator = camContainer.GetComponentInChildren<Animator>();
- mCameraCfg = camContainer.GetComponent<BattleCamConfig>();
- mRealCam = battle.BattleScene.CameraTrans.GetComponent<Camera>();
- if (Camera.main == null)
- mRealCam.tag = "MainCamera";
- cullingMask = mRealCam.cullingMask;
- clearFlag = mRealCam.clearFlags;
- mRealCam.allowHDR = false;
- mRealCam.allowMSAA = false;
- InitComponents();
- initialized = true;
- SetCamera(battle.BornCamPos,battle.BornCenter,battle.BornCamRot,battle.BornCamFov);
- mBlurEffect = mRealCam.GetComponent<RadialBlurEffect>();
- InitShake();
- }
- public void Initialize(BossBattle battle)
- {
- if (initialized)
- return;
- mBattle = battle;
- //FighterLayerMask = LayerMask.GetMask(new[] { FighterLayerName });
- //FullLayerMask = LayerMask.GetMask(new[] {
- // FighterLayerName,
- // SceneEffectLayerName,
- // WaterLayerName,
- // ReflectionLayerName,
- // GroundLayerName,
- // BackGroudLayerName
- //});
- if (battle == null) return;
- mComingCameraGo = battle.BattleScene.ComingCamera;
- camContainer = battle.BattleScene.CameraContainer;
- if (camContainer == null)
- return;
- if (!camContainer.activeInHierarchy)
- camContainer.SetActive(true);
- camAnimator = camContainer.GetComponentInChildren<Animator>();
- mCameraCfg = camContainer.GetComponent<BattleCamConfig>();
- mRealCam = battle.BattleScene.CameraTrans.GetComponent<Camera>();
- if (Camera.main == null)
- mRealCam.tag = "MainCamera";
- cullingMask = mRealCam.cullingMask;
- clearFlag = mRealCam.clearFlags;
- mRealCam.allowHDR = false;
- mRealCam.allowMSAA = false;
- initialized = true;
- //SetCameraPosAndRot(battle.SpawnCfg.BossCamPos, battle.SpawnCfg.BossCamRot, 50);
- CurrentCameraMode = CameraMode.Camera_None;
- mBlurEffect = mRealCam.GetComponent<RadialBlurEffect>();
- InitShake();
- }
- public void Initialize(TimeBattle battle)
- {
- if (initialized)
- return;
- mBattle = battle;
- if (battle == null) return;
- mComingCameraGo = battle.BattleScene.ComingCamera;
- camContainer = battle.BattleScene.CameraContainer;
- if (camContainer == null)
- return;
- if (!camContainer.activeInHierarchy)
- camContainer.SetActive(true);
- camAnimator = camContainer.GetComponentInChildren<Animator>();
- mCameraCfg = camContainer.GetComponent<BattleCamConfig>();
- mRealCam = battle.BattleScene.CameraTrans.GetComponent<Camera>();
- if (Camera.main == null)
- mRealCam.tag = "MainCamera";
- cullingMask = mRealCam.cullingMask;
- clearFlag = mRealCam.clearFlags;
- mRealCam.allowHDR = false;
- mRealCam.allowMSAA = false;
- initialized = true;
- CurrentCameraMode = CameraMode.Camera_None;
- mBlurEffect = mRealCam.GetComponent<RadialBlurEffect>();
- InitShake();
- }
- private void InitShake()
- {
- if (mRealCam == null) return;
- mShake = mRealCam.GetComponent<Shake>();
- if (mShake == null)
- {
- mShake = mRealCam.gameObject.AddComponent<Shake>();
- }
- }
- public void Update(float deltaTime)
- {
- if (!initialized)
- {
- return;
- }
- if (manualRotating) return;
- //if (CurrentCameraMode == CameraMode.Camera_BattleAuto)
- //{
- // if (callUpdateToCnt < maxCallUpdateCnt)
- // {
- // if (currentUpdateToInterval <= 0f)
- // {
- // callUpdateToCnt++;
- // UpdateTo();
- // currentUpdateToInterval = updateToInterval;
- // }
- // currentUpdateToInterval -= deltaTime;
- // }
- //}
- //else
- //{
- //}
- UpdateTo();
- UpdateCurrent(deltaTime);
- }
- public void Clear()
- {
- DisableBattleCam();
- //Camera_UI3D.Instance.Clear();
- if(mCamRotateCom!=null)
- mCamRotateCom.target = null;
- mFollowFighterHelper = null;
- initialized = false;
- camContainer = null;
- camAnimator = null;
- mBossIntroCamCfg = null;
- mRealCam = null;
- mCamRotateCom = null;
- CurrentCameraMode = CameraMode.Camera_None;
- }
- void InitComponents ()
- {
- mFollowFighterHelper = new CameraFollowFighterHelper ();
- }
- void UpdateFollowFighter ()
- {
- if (mBattle != null)
- {
- toFocus = mBattle.TeamCenter + mFollowFighterHelper.TargetOffset;
- toFov = mFollowFighterHelper.Fov;
- toPosition = toFocus - mFollowFighterHelper.FollowFighter.Ctrl.transform.forward * mFollowFighterHelper.FollowDist + mFollowFighterHelper.CameraOffset;
- }
- else
- {
- toFocus = mFollowFighterHelper.GetFocus();
- toFov = mFollowFighterHelper.Fov;
- toPosition = mFollowFighterHelper.GetCameraPosition();
- }
- }
- void UpdateFollowFighter(Vector3 forward)
- {
- if(mBattle!=null)
- {
- toFocus = mBattle.TeamCenter + mFollowFighterHelper.TargetOffset;
- toFov = mFollowFighterHelper.Fov;
- toPosition = toFocus - forward * mFollowFighterHelper.FollowDist + mFollowFighterHelper.CameraOffset;
- }
- else
- {
- toFocus = mFollowFighterHelper.GetFocus();
- toFov = mFollowFighterHelper.Fov;
- toPosition = mFollowFighterHelper.GetCameraPosition(forward);
- }
- }
- void UpdateTo ()
- {
- if (!enableUpdateTo)
- return;
- if (IsPlayingAnimation)
- return;
- if(CurrentCameraMode == CameraMode.Camera_BattleAuto)
- {
- if(mFollowFighterHelper != null && mFollowFighterHelper.IsActive)
- {
- UpdateFollowFighter(mBattleAutoForward);
- }
- }
- else if (mFollowFighterHelper != null && mFollowFighterHelper.IsActive)
- {
- UpdateFollowFighter();
- }
- else if (CurrentCameraMode == CameraMode.Camera_FollowLeftFighters)
- {
- if(mBattle!=null)
- {
- toFocus = mBattle.FighterMgr.TeamFighterFocus;
- Vector3 focusForward = mBattle.FighterMgr.TeamFighterForward;
- toPosition = toFocus + FollowCameraOffset;
- toFov = FollowCameraFov;
- }
- }
- }
- void UpdateCurrent (float deltaTime)
- {
- if (!enableUpdateCurrent) {
- return;
- }
- switch (MoveMode)
- {
- case CameraMoveMode.Camera_SyncFocusPosition:
- CamSyncFocusPosition(deltaTime);
- break;
- case CameraMoveMode.Camera_SyncRotPosition:
- currentPosition = Vector3.MoveTowards(currentPosition, toPosition, deltaTime * MoveSpeed);
- currentElerAngle = Vector3.MoveTowards(currentElerAngle, toElerAngle, deltaTime * RotSpeed);
- break;
- case CameraMoveMode.Camera_MoveFocusPosition:
- currentFocus = Vector3.MoveTowards(currentFocus, toFocus, deltaTime * MoveSpeed);
- currentPosition = Vector3.MoveTowards(currentPosition, toPosition, deltaTime * MoveSpeed);
- break;
- case CameraMoveMode.Camera_MoveFocusFirst:
- currentFocus = Vector3.MoveTowards(currentFocus, toFocus, deltaTime * MoveSpeed);
- if (currentFocus.CloseTo(toFocus))
- currentPosition = Vector3.MoveTowards(currentPosition, toPosition, deltaTime * MoveSpeed);
- break;
- case CameraMoveMode.Camera_MovePositionFirst:
- currentPosition = Vector3.MoveTowards(currentPosition, toPosition, deltaTime * MoveSpeed);
- if (currentPosition.CloseTo(toPosition))
- currentElerAngle = Vector3.MoveTowards(currentElerAngle, toElerAngle, deltaTime * RotSpeed);
- break;
- case CameraMoveMode.Camera_FollowFighterPosition:
- currentFocus = Vector3.MoveTowards(currentFocus, toFocus, deltaTime * MoveSpeed);
- currentPosition = Vector3.MoveTowards(currentPosition, toPosition, deltaTime * MoveSpeed);
- break;
- case CameraMoveMode.Camera_Follow_FixedTarget:
- currentFocus = Vector3.MoveTowards(currentFocus, toFocus, deltaTime * MoveSpeed);
- currentPosition = Vector3.MoveTowards(currentPosition, toPosition, deltaTime * MoveSpeed);
- break;
- case CameraMoveMode.Camera_ManualRotate:
- //currentFocus = Vector3.MoveTowards(currentFocus, toFocus, deltaTime * MoveSpeed);
- currentFocus = toFocus;
- if (currentFocus.CloseTo(toFocus))
- currentPosition = Vector3.MoveTowards(currentPosition, toPosition, deltaTime * MoveSpeed);
- break;
- }
- SetCurrentCamera (EnableAutoFov, !hasSetCamera,deltaTime);
- }
- void CamSyncFocusPosition(float deltaTime)
- {
- float currentMoveSpeed = MoveSpeed;
- float currentRotateSpeed = RotSpeed;
- float currentFovSpeed = FovSpeed;
- Vector3 vTo = toFocus - toPosition;
- currentPosition = CamPosition;
- currentFocus = currentPosition + CamForward * vTo.magnitude;
- Vector3 vCurrent = currentFocus - currentPosition;
- float angleBetween = Vector3.Angle(vTo, vCurrent);
- float magnitudeDiff = Mathf.Abs(vTo.magnitude - vCurrent.magnitude);
- float positionDistance = Vector3.SqrMagnitude(toPosition - currentPosition);
- float focusDistance = Vector3.SqrMagnitude(toFocus - currentFocus);
- float magnitudeSpeed = MoveSpeed;
- if (syncSpeed)
- {
- if (focusDistance < positionDistance)
- {
- if (enableDynamicSpeed)
- {
- if (focusDistance < 0.5f)
- currentMoveSpeed = Mathf.Max(0.1f, 0.05f * MoveSpeed * focusDistance * 2f);
- else if (focusDistance < 10f)
- currentMoveSpeed = MoveSpeed * focusDistance * 0.1f;
- }
- float time1 = focusDistance / currentMoveSpeed;
- float time2 = angleBetween / currentRotateSpeed;
- float time = Mathf.Max(time1, time2);
- if (time > 0f)
- {
- currentMoveSpeed = focusDistance / time;
- currentRotateSpeed = angleBetween / time;
- magnitudeSpeed = magnitudeDiff / time;
- }
- }
- else
- {
- if (enableDynamicSpeed)
- {
- if (positionDistance < 0.5f)
- currentMoveSpeed = Mathf.Max(0.1f, 0.05f * MoveSpeed * positionDistance * 2f);
- else if (positionDistance < 10f)
- currentMoveSpeed = MoveSpeed * positionDistance * 0.1f;
- }
- float time1 = positionDistance / currentMoveSpeed;
- float time2 = angleBetween / currentRotateSpeed;
- float time = Mathf.Max(time1, time2);
- if (time > 0f)
- {
- currentMoveSpeed = focusDistance / time;
- currentRotateSpeed = angleBetween / time;
- magnitudeSpeed = magnitudeDiff / time;
- }
- }
- syncFovSpeedRate = currentFovSpeed / FovSpeed;
- }
- else
- {
- if (angleBetween > 0f && currentRotateSpeed > 0f)
- magnitudeSpeed = magnitudeDiff / (angleBetween / currentRotateSpeed);
- }
- //Vector3 v = Vector3.MoveTowards(vCurrent, vTo, deltaTime * currentMoveSpeed);
- //currentPosition = Vector3.MoveTowards(currentPosition, toPosition, deltaTime * currentMoveSpeed);
- //currentFocus = currentPosition + v;
- if (magnitudeSpeed < 0.5f)
- {
- magnitudeSpeed = 0.5f;
- }
- Vector3 v = Vector3.RotateTowards(vCurrent, vTo, deltaTime * currentRotateSpeed * Mathf.Deg2Rad, deltaTime * magnitudeSpeed);
- if (focusDistance < positionDistance)
- {
- currentFocus = Vector3.MoveTowards(currentFocus, toFocus, deltaTime * currentMoveSpeed);
- currentPosition = currentFocus - v;
- }
- else
- {
- currentPosition = Vector3.MoveTowards(currentPosition, toPosition, deltaTime * currentMoveSpeed);
- currentFocus = currentPosition + v;
- }
- }
- Quaternion tempQuaternion;
- void SetCurrentCamera (bool autoFov, bool immediate,float deltaTime)
- {
- if(immediate)
- {
- //DebugHelper.LogError("currentPosition:" + currentPosition + " CamEulerAngle:" + CamEulerAngle);
- currentPosition = toPosition;
- currentFocus = toFocus;
- }
-
- CamPosition = currentPosition;
- tempQuaternion.SetLookRotation(currentFocus - currentPosition, Vector3.up);
- CamEulerAngle = tempQuaternion.eulerAngles;
- if (immediate)
- {
- currentFov = toFov;
- }
- else
- {
- if (syncSpeed && MoveMode == CameraMoveMode.Camera_SyncFocusPosition)
- currentFov = Mathf.MoveTowards(currentFov, toFov, syncFovSpeedRate * FovSpeed * deltaTime);
- else
- currentFov = Mathf.MoveTowards(currentFov, toFov, FovSpeed * deltaTime);
- }
- CamFov = currentFov;
- hasSetCamera |= immediate;
- //DebugHelper.LogError("currentPosition:" + currentPosition + " CamEulerAngle:" + CamEulerAngle);
- }
-
- void GetCurrentCameraValues()
- {
- currentPosition = CamPosition;
- currentElerAngle = CamEulerAngle;
- currentFov = CamFov;
- }
- public void LockFighter (Fighter fighter, Vector3 CameraOffset, Vector3 forward,float fov,float followDist,bool immediate)
- {
- if (mRealCam != null)
- {
- mRealCam.farClipPlane = camera_far_clip;
- }
-
- if (mFollowFighterHelper.IsActive)
- {
- currentUpdateToInterval = 0;
- mMoveSpeed = CamCfg.CamMoveSpeed;
- mRotSpeed = CamCfg.CamRotSpeed;
- if (manualRotating)
- {
- backupCameraMode = CameraMode.Camera_LockFighter;
- }
- else
- {
- SetCameraMode(CameraMode.Camera_LockFighter);
- }
-
- return;
- }
- mFollowFighterHelper.Fov = fov.FEqual (0) ? 30 : fov;
- mFollowFighterHelper.FollowFighter = fighter;
- mFollowFighterHelper.TargetOffset = Vector3.zero;
- mFollowFighterHelper.CameraOffset = CameraOffset;
- mFollowFighterHelper.FollowSpeedRate = 1;
- mFollowFighterHelper.ContinueTime = 10000f;
- mFollowFighterHelper.MoveMode = CameraMoveMode.Camera_SyncFocusPosition;
- mFollowFighterHelper.FollowDist = followDist;
- mFollowFighterHelper.IsFollow = true;
- mFollowFighterHelper.IsUnscaleTime = false;
- mFollowFighterHelper.Start ();
- if (manualRotating)
- {
- backupCameraMode = CameraMode.Camera_LockFighter;
- }
- else
- {
- SetCameraMode(CameraMode.Camera_LockFighter);
- mMoveSpeed = CamCfg.CamMoveSpeed;
- mRotSpeed = CamCfg.CamRotSpeed;
- if (immediate)
- {
- UpdateFollowFighter(forward);
- Vector3 oldRot = CamEulerAngle;
- currentPosition = CamPosition = toPosition;
- currentFocus = toFocus;
- tempQuaternion.SetLookRotation(currentFocus - currentPosition, Vector3.up);
- currentElerAngle = CamEulerAngle = tempQuaternion.eulerAngles;
- DebugHelper.Log("TeamCenter:" + mBattle.TeamCenter + " CameraOffset:" + CameraOffset.ToString());
- }
- }
- }
- public void StopLockFighter ()
- {
- if(mFollowFighterHelper!=null)
- mFollowFighterHelper.Stop ();
- }
-
- public void SetCameraWithFocus (Vector3 targetPosition, Vector3 targetFocus, float targetFov)
- {
- StopLockFighter();
- toFocus = targetFocus;
- toPosition = targetPosition;
- toFov = targetFov;
- SetCameraMode(CameraMode.Camera_Move_BattleFieldCenter);
- }
- private float mRecordRotSpeed = 0;
- private float mRecordMoveSpeed = 0;
- public void SetCameraPosAndRot(Vector3 pos,Vector3 rot,float fov = 0,float far = 0)
- {
- if(manualRotating)
- {
- RestoreCamRotate();
- }
- StopLockFighter();
- toPosition = pos;
- toElerAngle = rot;
- currentFov = toFov = fov < 1 ? mCameraCfg.bossCameraFov:fov;
- SetCameraMode(CameraMode.Camera_Fixed_Target);
- CamFov = currentFov;
- currentPosition = CamPosition = toPosition;
- currentElerAngle = CamEulerAngle = toElerAngle;
- if(far > 0)
- {
- mRealCam.farClipPlane = far;
- }
- }
- public void SetCamera(Vector3 targetPos,Vector3 targetFocus,Vector3 targetAngles,float targetFov)
- {
- toPosition = targetPos;
- toFocus = targetFocus;
- toElerAngle = targetAngles;
- currentFov = toFov = targetFov;
- CamFov = currentFov;
- currentPosition = CamPosition = toPosition;
- currentElerAngle = CamEulerAngle = toElerAngle;
- currentFocus = toFocus;
- SetCameraMode(CameraMode.Camera_None);
- }
- Vector3 mBattleAutoForward = Vector3.zero;
- public void SetBattleAuto(Vector3 forward)
- {
- mBattleAutoForward = forward;
- MoveSpeed = CamCfg.CamMoveSpeedInBattle;
- RotSpeed = CamCfg.CamRotSpeedInBattle;
- if(!manualRotating)
- {
- SetCameraMode(CameraMode.Camera_BattleAuto);
- }
- else
- {
- backupCameraMode = CurrentCameraMode;
- }
- }
- public void SetCameraMode (CameraMode mode)
- {
- if (mIsDoingSpecialCamera)
- BattleMgr.Instance.StopCoroutine(specialCameraCoroutine);
- if(currentCameraMode!=mode)
- CurrentCameraMode = mode;
- //DebugHelper.LogError("--------------------------SetCameraMode---------------------"+ mode);
- }
- public void DisableDynamicCamera ()
- {
- SetCameraMode (CameraMode.Camera_None);
- }
- public bool IsDoingSpecialCamera {
- get {
- return mIsDoingSpecialCamera;
- }
- }
- public Vector3 FollowCameraOffset {
- get {
- return mCustomFollowCamera ? followCameraOffset : mCameraCfg.followCamOffset;
- }
- }
- public float FollowCameraFov {
- get {
- return mCustomFollowCamera ? followCameraFov : mCameraCfg.followCameraFov;
- }
- }
- public bool EnableAutoFov {
- get {
- if (mFollowFighterHelper != null && mFollowFighterHelper.IsActive)
- return false;
- return enableAutoFov;
- }
- }
- public CameraMoveMode MoveMode {
- get {
- if (mFollowFighterHelper != null && mFollowFighterHelper.IsActive)
- return mFollowFighterHelper.MoveMode;
- return moveMode;
- }
- }
- static string mEffectLayerNameInBattle = SceneEffectLayerName;
- public static string CurrentEffectLayerName {
- get { return BattleMgr.Instance != null ? mEffectLayerNameInBattle : SceneEffectLayerName; }
- set {
- if (BattleMgr.Instance != null)
- mEffectLayerNameInBattle = value;
- }
- }
- public bool IsLoadingTransitionEffect { get; private set; }
- public void EnableBattleCam()
- {
- if(RealCamera!=null /*&& !RealCamera.enabled*/)
- {
- RealCamera.cullingMask = cullingMask;
- }
- }
- public void DisableBattleCam()
- {
- if (RealCamera != null/* && RealCamera.enabled*/)
- {
- RealCamera.cullingMask = 0;
- }
- }
- public void EnterBossIntroCam()
- {
- if (mBossIntroCamCfg != null)
- {
- mBossIntroCamCfg.Begin();
- DisableBattleCam();
- //Camera_UI3D.Instance.SetCameraEnabled(false);
- }
- }
- public void ExitBossIntroCam()
- {
- if (mBossIntroCamCfg != null)
- {
- mBossIntroCamCfg.Stop();
- EnableBattleCam();
- //Camera_UI3D.Instance.SetCameraEnabled(true);
- }
- }
- bool manualRotating= false;
- CameraMode backupCameraMode = CameraMode.Camera_None;
- public void BeginCamRotate()
- {
- if (mCamRotateCom == null)
- return;
- manualRotating = true;
- mCamRotateCom.StartRotate();
- backupCameraMode = CurrentCameraMode;
- }
- public void RestoreCamRotate()
- {
- if (mCamRotateCom == null)
- return;
- manualRotating = false;
- SetCameraMode(backupCameraMode);
- mCamRotateCom.StopRotate();
- }
- public void Shake(float leftRight, float upDown, float forwardBackward, float time, int cycle)
- {
- if (mShake == null) return;
- mShake.positionShake = new Vector3(leftRight, upDown, forwardBackward);
- mShake.angleShake = Vector3.zero;
- if (Mathf.Abs(time) < Mathf.Epsilon)
- time = 0.1f;
- mShake.cycleTime = time;
- mShake.fCycleCount = cycle;
- mShake.unscaleTime = false;
- mShake.bothDir = true;
- mShake.autoDisable = true;
- mShake.Restart();
- }
- public void SetBlurEffectEnabled(bool flag)
- {
- if (mBlurEffect == null) return;
- if(flag)
- {
- mBlurEffect.FadeIn();
- }
- else
- {
- mBlurEffect.FadeOut();
- }
- }
- #region 转场镜头
- Coroutine specialCameraCoroutine;
- public void PerformStartCamera(List<MapCameraConfig> camCfgs, bool tweenTo)
- {
- BattleMgr.Instance.StartCoroutine(DoStartCamera(camCfgs, tweenTo));
- }
- IEnumerator DoStartCamera(List<MapCameraConfig> camCfgs, bool tweenTo)
- {
- if (camCfgs == null || camCfgs.Count == 0) yield break;
- mIsDoingSpecialCamera = true;
- syncSpeed = false;
- //DebugHelper.LogError("start camera start");
- MapCameraConfig cfg = camCfgs[0];
- if (tweenTo)
- {
- tempQuaternion.eulerAngles = cfg.rot;
- Vector3 targetForward = (tempQuaternion * Vector3.forward).normalized;
- Vector3 targetFocus = cfg.pos + targetForward * 20;
- ReadyForSpecailCamera();
- MoveSpeed = cfg.moveSpeed;
- RotSpeed = cfg.rotSpeed;
- FovSpeed = cfg.fovSpeed;
- CamEulerAngle = cfg.rot;
- specialCameraCoroutine =BattleMgr.Instance.StartCoroutine(DoTween(cfg.pos, targetFocus, cfg.fov, 0, CameraMoveMode.Camera_SyncFocusPosition));
- while (mIsDoingSpecialCamera)
- yield return 1;
- }
- else
- {
- SetCamera(cfg.pos,cfg.pos, cfg.rot, cfg.fov);
- }
- for (int i = 1; i < camCfgs.Count; i++)
- {
- MapCameraConfig nextCfg = camCfgs[i];
- tempQuaternion.eulerAngles = nextCfg.rot;
- Vector3 targetForward = (tempQuaternion * Vector3.forward).normalized;
- Vector3 targetFocus = nextCfg.pos + targetForward * 20;
- ReadyForSpecailCamera();
- MoveSpeed = cfg.moveSpeed;
- RotSpeed = cfg.rotSpeed;
- FovSpeed = cfg.fovSpeed;
- CamEulerAngle = nextCfg.rot;
- specialCameraCoroutine = BattleMgr.Instance.StartCoroutine(DoTween(nextCfg.pos, targetFocus, nextCfg.fov, 0, CameraMoveMode.Camera_SyncFocusPosition));
- while (mIsDoingSpecialCamera)
- yield return 1;
- }
- //DebugHelper.LogError("start camera end");
- syncSpeed = true;
- mIsDoingSpecialCamera = false;
- }
- IEnumerator DoTween(Vector3 targetPosition, Vector3 targetFocus, float targetFov, float tweenSpeedRate, CameraMoveMode tweenMoveMode)
- {
- if (tweenSpeedRate.FEqual(0))
- tweenSpeedRate = 1f;
- CurrentCameraMode = CameraMode.Camera_TweeningMove;
- moveMode = tweenMoveMode;
- toPosition = targetPosition;
- toFocus = targetFocus;
- toFov = targetFov;
- //DebugHelper.LogError("tween from11 {0}, {1}, {2} to {3}, {4}, {5}, has set : {6}", currentPosition, currentFocus, currentFov, toPosition, toFocus, toFov, hasSetCamera);
- while (!IsCurrentCloseToTarget)
- yield return 1;
- RestoreModeAfterSpecialCamera();
- }
- CameraMode modeBackupBeforeSpecialCamera;
- float moveSpeedBeforeSpecialCamera;
- float rotSpeedBeforeSpecialCamera;
- float fovSpeedBeforeSpecialCamera;
- Vector3 posBackupBeforeSpecialCamera;
- Vector3 rotBackupBeforeSpecialCamera;
- float fovBackupBeforeSpecialCamera;
- void BackupModeBeforeSpecialCamera()
- {
- modeBackupBeforeSpecialCamera = CurrentCameraMode;
- moveSpeedBeforeSpecialCamera = MoveSpeed;
- rotSpeedBeforeSpecialCamera = RotSpeed;
- fovSpeedBeforeSpecialCamera = FovSpeed;
- posBackupBeforeSpecialCamera = CamPosition;
- rotBackupBeforeSpecialCamera = CamEulerAngle;
- fovBackupBeforeSpecialCamera = CamFov;
- }
- void RestoreModeAfterSpecialCamera()
- {
- CurrentCameraMode = modeBackupBeforeSpecialCamera;
- MoveSpeed = moveSpeedBeforeSpecialCamera;
- RotSpeed = rotSpeedBeforeSpecialCamera;
- FovSpeed = fovSpeedBeforeSpecialCamera;
- CamPosition = posBackupBeforeSpecialCamera;
- CamEulerAngle = rotBackupBeforeSpecialCamera;
- CamFov = fovBackupBeforeSpecialCamera;
- }
- void ReadyForSpecailCamera()
- {
- if (specialCameraCoroutine != null && mIsDoingSpecialCamera)
- {
- BattleMgr.Instance.StopCoroutine(specialCameraCoroutine);
- RestoreModeAfterSpecialCamera();
- }
- BackupModeBeforeSpecialCamera();
- }
- #endregion
- }
|