| | 1 | | using Cinemachine; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Linq; |
| | 5 | | using DCL; |
| | 6 | | using UnityEngine; |
| | 7 | | using UnityEngine.Serialization; |
| | 8 | |
|
| | 9 | | namespace DCL.Camera |
| | 10 | | { |
| | 11 | | public class CameraController : MonoBehaviour |
| | 12 | | { |
| | 13 | | [SerializeField] |
| | 14 | | internal new UnityEngine.Camera camera; |
| | 15 | |
|
| | 16 | | private Transform cameraTransform; |
| | 17 | |
|
| | 18 | | [Header("Virtual Cameras")] |
| | 19 | | [SerializeField] |
| | 20 | | internal CinemachineBrain cameraBrain; |
| | 21 | |
|
| | 22 | | [SerializeField] |
| | 23 | | internal CameraStateBase[] cameraModes; |
| | 24 | |
|
| | 25 | | [Header("InputActions")] |
| | 26 | | [SerializeField] |
| | 27 | | internal InputAction_Trigger cameraChangeAction; |
| | 28 | |
|
| | 29 | | internal Dictionary<CameraMode.ModeId, CameraStateBase> cachedModeToVirtualCamera; |
| | 30 | |
|
| 13010 | 31 | | private Vector3Variable cameraForward => CommonScriptableObjects.cameraForward; |
| 13010 | 32 | | private Vector3Variable cameraRight => CommonScriptableObjects.cameraRight; |
| 13010 | 33 | | private Vector3Variable cameraPosition => CommonScriptableObjects.cameraPosition; |
| 231 | 34 | | private Vector3Variable worldOffset => CommonScriptableObjects.worldOffset; |
| 13010 | 35 | | private BooleanVariable cameraIsBlending => CommonScriptableObjects.cameraIsBlending; |
| | 36 | |
|
| 13378 | 37 | | public CameraStateBase currentCameraState => cachedModeToVirtualCamera[CommonScriptableObjects.cameraMode]; |
| | 38 | |
|
| | 39 | | [HideInInspector] |
| | 40 | | public System.Action<CameraMode.ModeId> onSetCameraMode; |
| | 41 | |
|
| | 42 | | private void Start() |
| | 43 | | { |
| 116 | 44 | | cameraTransform = this.camera.transform; |
| | 45 | |
|
| 116 | 46 | | CommonScriptableObjects.rendererState.OnChange += OnRenderingStateChanged; |
| 116 | 47 | | OnRenderingStateChanged(CommonScriptableObjects.rendererState.Get(), false); |
| | 48 | |
|
| 116 | 49 | | CommonScriptableObjects.cameraBlocked.OnChange += CameraBlocked_OnChange; |
| | 50 | |
|
| 812 | 51 | | cachedModeToVirtualCamera = cameraModes.ToDictionary(x => x.cameraModeId, x => x); |
| | 52 | |
|
| 116 | 53 | | using (var iterator = cachedModeToVirtualCamera.GetEnumerator()) |
| | 54 | | { |
| 464 | 55 | | while (iterator.MoveNext()) |
| | 56 | | { |
| 348 | 57 | | iterator.Current.Value.Init(camera); |
| | 58 | | } |
| 116 | 59 | | } |
| | 60 | |
|
| 116 | 61 | | cameraChangeAction.OnTriggered += OnCameraChangeAction; |
| 116 | 62 | | worldOffset.OnChange += OnWorldReposition; |
| | 63 | |
|
| 116 | 64 | | SetCameraMode(CommonScriptableObjects.cameraMode); |
| | 65 | |
|
| 116 | 66 | | if (CommonScriptableObjects.isFullscreenHUDOpen) |
| 104 | 67 | | OnFullscreenUIVisibilityChange(CommonScriptableObjects.isFullscreenHUDOpen.Get(), !CommonScriptableObjec |
| | 68 | |
|
| 116 | 69 | | CommonScriptableObjects.isFullscreenHUDOpen.OnChange += OnFullscreenUIVisibilityChange; |
| 116 | 70 | | } |
| | 71 | |
|
| 314 | 72 | | private float prevRenderScale = 1.0f; |
| | 73 | |
|
| | 74 | | void OnFullscreenUIVisibilityChange(bool visibleState, bool prevVisibleState) |
| | 75 | | { |
| 107 | 76 | | if (visibleState == prevVisibleState) |
| 0 | 77 | | return; |
| | 78 | |
|
| 107 | 79 | | camera.enabled = !visibleState; |
| 107 | 80 | | } |
| | 81 | |
|
| 406 | 82 | | private void OnRenderingStateChanged(bool enabled, bool prevState) { camera.enabled = enabled; } |
| | 83 | |
|
| | 84 | | private void CameraBlocked_OnChange(bool current, bool previous) |
| | 85 | | { |
| 0 | 86 | | foreach (CameraStateBase cam in cameraModes) |
| | 87 | | { |
| 0 | 88 | | cam.OnBlock(current); |
| | 89 | | } |
| 0 | 90 | | } |
| | 91 | |
|
| | 92 | | private void OnCameraChangeAction(DCLAction_Trigger action) |
| | 93 | | { |
| 1 | 94 | | if (CommonScriptableObjects.cameraMode == CameraMode.ModeId.FirstPerson) |
| | 95 | | { |
| 0 | 96 | | SetCameraMode(CameraMode.ModeId.ThirdPerson); |
| 0 | 97 | | } |
| | 98 | | else |
| | 99 | | { |
| 1 | 100 | | SetCameraMode(CameraMode.ModeId.FirstPerson); |
| | 101 | | } |
| 1 | 102 | | } |
| | 103 | |
|
| | 104 | | public void SetCameraMode(CameraMode.ModeId newMode) |
| | 105 | | { |
| 147 | 106 | | currentCameraState.OnUnselect(); |
| 147 | 107 | | CommonScriptableObjects.cameraMode.Set(newMode); |
| 147 | 108 | | currentCameraState.OnSelect(); |
| | 109 | |
|
| 147 | 110 | | DCL.Interface.WebInterface.ReportCameraChanged(newMode); |
| | 111 | |
|
| 147 | 112 | | onSetCameraMode.Invoke(newMode); |
| 147 | 113 | | } |
| | 114 | |
|
| | 115 | | public CameraStateBase GetCameraMode( CameraMode.ModeId mode ) |
| | 116 | | { |
| 1095 | 117 | | return cameraModes.FirstOrDefault( x => x.cameraModeId == mode ); |
| | 118 | | } |
| | 119 | |
|
| 6 | 120 | | private void OnWorldReposition(Vector3 newValue, Vector3 oldValue) { transform.position += newValue - oldValue; |
| | 121 | |
|
| | 122 | | private void Update() |
| | 123 | | { |
| 13010 | 124 | | cameraForward.Set(cameraTransform.forward); |
| 13010 | 125 | | cameraRight.Set(cameraTransform.right); |
| 13010 | 126 | | cameraPosition.Set(cameraTransform.position); |
| 13010 | 127 | | cameraIsBlending.Set(cameraBrain.IsBlending); |
| | 128 | |
|
| 13010 | 129 | | currentCameraState?.OnUpdate(); |
| 13010 | 130 | | } |
| | 131 | |
|
| | 132 | | public void SetRotation(string setRotationPayload) |
| | 133 | | { |
| 8 | 134 | | var payload = Utils.FromJsonWithNulls<SetRotationPayload>(setRotationPayload); |
| 8 | 135 | | currentCameraState?.OnSetRotation(payload); |
| 8 | 136 | | } |
| | 137 | |
|
| 64 | 138 | | public void SetRotation(float x, float y, float z, Vector3? cameraTarget = null) { currentCameraState?.OnSetRota |
| | 139 | |
|
| | 140 | | public Vector3 GetRotation() |
| | 141 | | { |
| 12 | 142 | | if (currentCameraState != null) |
| 12 | 143 | | return currentCameraState.OnGetRotation(); |
| | 144 | |
|
| 0 | 145 | | return Vector3.zero; |
| | 146 | | } |
| | 147 | |
|
| 0 | 148 | | public Vector3 GetPosition() { return CinemachineCore.Instance.GetActiveBrain(0).ActiveVirtualCamera.State.Final |
| | 149 | |
|
| | 150 | | private void OnDestroy() |
| | 151 | | { |
| 115 | 152 | | worldOffset.OnChange -= OnWorldReposition; |
| 115 | 153 | | cameraChangeAction.OnTriggered -= OnCameraChangeAction; |
| 115 | 154 | | CommonScriptableObjects.rendererState.OnChange -= OnRenderingStateChanged; |
| 115 | 155 | | CommonScriptableObjects.cameraBlocked.OnChange -= CameraBlocked_OnChange; |
| 115 | 156 | | CommonScriptableObjects.isFullscreenHUDOpen.OnChange -= OnFullscreenUIVisibilityChange; |
| 115 | 157 | | } |
| | 158 | |
|
| | 159 | | [System.Serializable] |
| | 160 | | public class SetRotationPayload |
| | 161 | | { |
| | 162 | | public float x; |
| | 163 | | public float y; |
| | 164 | | public float z; |
| | 165 | | public Vector3? cameraTarget; |
| | 166 | | } |
| | 167 | | } |
| | 168 | | } |