| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using Cinemachine; |
| | 5 | | using DCL.Helpers; |
| | 6 | | using DCL.Interface; |
| | 7 | | using DCL; |
| | 8 | | using UnityEngine; |
| | 9 | |
|
| | 10 | | namespace DCL.Camera |
| | 11 | | { |
| | 12 | | public class CameraController : MonoBehaviour |
| | 13 | | { |
| | 14 | | [SerializeField] |
| | 15 | | internal new UnityEngine.Camera camera; |
| | 16 | |
|
| | 17 | | private Transform cameraTransform; |
| | 18 | |
|
| | 19 | | [SerializeField] |
| | 20 | | internal CinemachineFreeLook thirdPersonCamera; |
| | 21 | |
|
| | 22 | | [Header("Virtual Cameras")] |
| | 23 | | [SerializeField] |
| | 24 | | internal CinemachineBrain cameraBrain; |
| | 25 | |
|
| | 26 | | [SerializeField] |
| | 27 | | internal CameraStateBase[] cameraModes; |
| | 28 | |
|
| | 29 | | [Header("InputActions")] |
| | 30 | | [SerializeField] |
| | 31 | | internal InputAction_Trigger cameraChangeAction; |
| | 32 | | [SerializeField] |
| | 33 | | internal InputAction_Measurable mouseWheelAction; |
| | 34 | |
|
| | 35 | | internal Dictionary<CameraMode.ModeId, CameraStateBase> cachedModeToVirtualCamera; |
| | 36 | |
|
| | 37 | | public delegate void CameraBlendStarted(); |
| | 38 | |
|
| | 39 | | public event CameraBlendStarted onCameraBlendStarted; |
| | 40 | |
|
| | 41 | | public delegate void CameraBlendFinished(); |
| | 42 | |
|
| | 43 | | public event CameraBlendFinished onCameraBlendFinished; |
| | 44 | |
|
| | 45 | | private bool wasBlendingLastFrame; |
| | 46 | |
|
| 598 | 47 | | private float mouseWheelThreshold = 0.04f; |
| | 48 | |
|
| 5548 | 49 | | private Vector3Variable cameraForward => CommonScriptableObjects.cameraForward; |
| 5548 | 50 | | private Vector3Variable cameraRight => CommonScriptableObjects.cameraRight; |
| 5548 | 51 | | private Vector3Variable cameraPosition => CommonScriptableObjects.cameraPosition; |
| 1194 | 52 | | private Vector3Variable worldOffset => CommonScriptableObjects.worldOffset; |
| 5548 | 53 | | private BooleanVariable cameraIsBlending => CommonScriptableObjects.cameraIsBlending; |
| | 54 | |
|
| 5580 | 55 | | public CameraStateBase currentCameraState => cachedModeToVirtualCamera[CommonScriptableObjects.cameraMode]; |
| | 56 | |
|
| | 57 | | [HideInInspector] |
| | 58 | | public Action<CameraMode.ModeId> onSetCameraMode; |
| | 59 | |
|
| | 60 | | private void Awake() |
| | 61 | | { |
| 597 | 62 | | cameraTransform = this.camera.transform; |
| | 63 | |
|
| 597 | 64 | | CommonScriptableObjects.rendererState.OnChange += OnRenderingStateChanged; |
| 597 | 65 | | OnRenderingStateChanged(CommonScriptableObjects.rendererState.Get(), false); |
| | 66 | |
|
| 597 | 67 | | CommonScriptableObjects.cameraBlocked.OnChange += CameraBlocked_OnChange; |
| | 68 | |
|
| 4179 | 69 | | cachedModeToVirtualCamera = cameraModes.ToDictionary(x => x.cameraModeId, x => x); |
| | 70 | |
|
| 597 | 71 | | using (var iterator = cachedModeToVirtualCamera.GetEnumerator()) |
| | 72 | | { |
| 2388 | 73 | | while (iterator.MoveNext()) |
| | 74 | | { |
| 1791 | 75 | | iterator.Current.Value.Initialize(camera); |
| | 76 | | } |
| 597 | 77 | | } |
| | 78 | |
|
| 597 | 79 | | cameraChangeAction.OnTriggered += OnCameraChangeAction; |
| 597 | 80 | | mouseWheelAction.OnValueChanged += OnMouseWheelChangeValue; |
| 597 | 81 | | worldOffset.OnChange += OnWorldReposition; |
| 597 | 82 | | CommonScriptableObjects.cameraMode.OnChange += OnCameraModeChange; |
| | 83 | |
|
| 597 | 84 | | OnCameraModeChange(CommonScriptableObjects.cameraMode, CameraMode.ModeId.FirstPerson); |
| | 85 | |
|
| 597 | 86 | | if (CommonScriptableObjects.isFullscreenHUDOpen) |
| 0 | 87 | | OnFullscreenUIVisibilityChange(CommonScriptableObjects.isFullscreenHUDOpen.Get(), !CommonScriptableObjec |
| | 88 | |
|
| 597 | 89 | | CommonScriptableObjects.isFullscreenHUDOpen.OnChange += OnFullscreenUIVisibilityChange; |
| | 90 | |
|
| 597 | 91 | | DataStore.i.camera.outputTexture.OnChange += OnOutputTextureChange; |
| 597 | 92 | | OnOutputTextureChange(DataStore.i.camera.outputTexture.Get(), null); |
| | 93 | |
|
| 597 | 94 | | DataStore.i.camera.invertYAxis.OnChange += SetInvertYAxis; |
| 597 | 95 | | SetInvertYAxis(DataStore.i.camera.invertYAxis.Get(), false); |
| | 96 | |
|
| 597 | 97 | | wasBlendingLastFrame = false; |
| 597 | 98 | | } |
| | 99 | |
|
| | 100 | | void OnFullscreenUIVisibilityChange(bool visibleState, bool prevVisibleState) |
| | 101 | | { |
| 0 | 102 | | if (visibleState == prevVisibleState) |
| 0 | 103 | | return; |
| | 104 | |
|
| 0 | 105 | | camera.enabled = !visibleState && CommonScriptableObjects.rendererState.Get(); |
| 0 | 106 | | } |
| | 107 | |
|
| | 108 | | void OnOutputTextureChange(RenderTexture current, RenderTexture previous) |
| | 109 | | { |
| 597 | 110 | | camera.targetTexture = current; |
| 597 | 111 | | } |
| | 112 | |
|
| | 113 | | public bool TryGetCameraStateByType<T>(out CameraStateBase searchedCameraState) |
| | 114 | | { |
| 539 | 115 | | foreach (CameraStateBase cameraMode in cameraModes) |
| | 116 | | { |
| 231 | 117 | | if (cameraMode.GetType() == typeof(T)) |
| | 118 | | { |
| 77 | 119 | | searchedCameraState = cameraMode; |
| 77 | 120 | | return true; |
| | 121 | | } |
| | 122 | | } |
| | 123 | |
|
| 0 | 124 | | searchedCameraState = null; |
| 0 | 125 | | return false; |
| | 126 | | } |
| | 127 | |
|
| | 128 | | private void OnRenderingStateChanged(bool enabled, bool prevState) |
| | 129 | | { |
| 599 | 130 | | camera.enabled = enabled && !CommonScriptableObjects.isFullscreenHUDOpen; |
| 599 | 131 | | } |
| | 132 | |
|
| | 133 | | private void CameraBlocked_OnChange(bool current, bool previous) |
| | 134 | | { |
| 0 | 135 | | foreach (CameraStateBase cam in cameraModes) |
| | 136 | | { |
| 0 | 137 | | cam.OnBlock(current); |
| | 138 | | } |
| 0 | 139 | | } |
| | 140 | |
|
| | 141 | | private void OnMouseWheelChangeValue(DCLAction_Measurable action, float value) |
| | 142 | | { |
| 0 | 143 | | if (value > -mouseWheelThreshold && value < mouseWheelThreshold) return; |
| 0 | 144 | | if (Utils.IsPointerOverUIElement()) return; |
| | 145 | |
|
| 0 | 146 | | if (CommonScriptableObjects.cameraMode == CameraMode.ModeId.FirstPerson && value < -mouseWheelThreshold) |
| 0 | 147 | | SetCameraMode(CameraMode.ModeId.ThirdPerson); |
| | 148 | |
|
| 0 | 149 | | if (CommonScriptableObjects.cameraMode == CameraMode.ModeId.ThirdPerson && value > mouseWheelThreshold) |
| 0 | 150 | | SetCameraMode(CameraMode.ModeId.FirstPerson); |
| 0 | 151 | | } |
| | 152 | |
|
| | 153 | | private void OnCameraChangeAction(DCLAction_Trigger action) |
| | 154 | | { |
| 1 | 155 | | if (CommonScriptableObjects.cameraMode == CameraMode.ModeId.FirstPerson) |
| | 156 | | { |
| 0 | 157 | | SetCameraMode(CameraMode.ModeId.ThirdPerson); |
| 0 | 158 | | } |
| | 159 | | else |
| | 160 | | { |
| 1 | 161 | | SetCameraMode(CameraMode.ModeId.FirstPerson); |
| | 162 | | } |
| 1 | 163 | | } |
| | 164 | |
|
| | 165 | | public void SetCameraMode(CameraMode.ModeId newMode) |
| | 166 | | { |
| 3 | 167 | | CommonScriptableObjects.cameraMode.Set(newMode); |
| 3 | 168 | | } |
| | 169 | |
|
| | 170 | | private void OnCameraModeChange(CameraMode.ModeId current, CameraMode.ModeId previous) |
| | 171 | | { |
| 599 | 172 | | cachedModeToVirtualCamera[previous].OnUnselect(); |
| 599 | 173 | | cachedModeToVirtualCamera[current].OnSelect(); |
| | 174 | |
|
| 599 | 175 | | WebInterface.ReportCameraChanged(current); |
| | 176 | |
|
| 599 | 177 | | onSetCameraMode?.Invoke(current); |
| 2 | 178 | | } |
| | 179 | |
|
| | 180 | | public CameraStateBase GetCameraMode(CameraMode.ModeId mode) |
| | 181 | | { |
| 1716 | 182 | | return cameraModes.FirstOrDefault(x => x.cameraModeId == mode); |
| | 183 | | } |
| | 184 | |
|
| | 185 | | private void OnWorldReposition(Vector3 newValue, Vector3 oldValue) |
| | 186 | | { |
| 2 | 187 | | transform.position += newValue - oldValue; |
| 2 | 188 | | } |
| | 189 | |
|
| | 190 | | private void Update() |
| | 191 | | { |
| 5548 | 192 | | cameraForward.Set(cameraTransform.forward); |
| 5548 | 193 | | cameraRight.Set(cameraTransform.right); |
| 5548 | 194 | | DataStore.i.camera.rotation.Set(cameraTransform.rotation); |
| 5548 | 195 | | cameraPosition.Set(cameraTransform.position); |
| 5548 | 196 | | cameraIsBlending.Set(cameraBrain.IsBlending); |
| | 197 | |
|
| 5548 | 198 | | if (cameraBrain.IsBlending) |
| | 199 | | { |
| 101 | 200 | | if (!wasBlendingLastFrame) |
| 38 | 201 | | onCameraBlendStarted?.Invoke(); |
| | 202 | |
|
| 101 | 203 | | wasBlendingLastFrame = true; |
| 101 | 204 | | } |
| 5447 | 205 | | else if (wasBlendingLastFrame) |
| | 206 | | { |
| 0 | 207 | | onCameraBlendFinished?.Invoke(); |
| | 208 | |
|
| 0 | 209 | | wasBlendingLastFrame = false; |
| | 210 | | } |
| | 211 | |
|
| 5548 | 212 | | currentCameraState?.OnUpdate(); |
| 5548 | 213 | | } |
| | 214 | |
|
| | 215 | | public void SetRotation(string setRotationPayload) |
| | 216 | | { |
| 6 | 217 | | var payload = Utils.FromJsonWithNulls<SetRotationPayload>(setRotationPayload); |
| 6 | 218 | | currentCameraState?.OnSetRotation(payload); |
| 6 | 219 | | } |
| | 220 | |
|
| | 221 | | public void SetRotation(float x, float y, float z, Vector3? cameraTarget = null) |
| | 222 | | { |
| 0 | 223 | | currentCameraState?.OnSetRotation(new SetRotationPayload() { x = x, y = y, z = z, cameraTarget = cameraTarge |
| 0 | 224 | | } |
| | 225 | |
|
| | 226 | | public Vector3 GetRotation() |
| | 227 | | { |
| 12 | 228 | | if (currentCameraState != null) |
| 12 | 229 | | return currentCameraState.OnGetRotation(); |
| | 230 | |
|
| 0 | 231 | | return Vector3.zero; |
| | 232 | | } |
| | 233 | |
|
| | 234 | | public Vector3 GetPosition() |
| | 235 | | { |
| 0 | 236 | | return CinemachineCore.Instance.GetActiveBrain(0).ActiveVirtualCamera.State.FinalPosition; |
| | 237 | | } |
| | 238 | |
|
| | 239 | | public UnityEngine.Camera GetCamera() |
| | 240 | | { |
| 0 | 241 | | return camera; |
| | 242 | | } |
| | 243 | |
|
| | 244 | | private void SetInvertYAxis(bool current, bool previous) |
| | 245 | | { |
| 610 | 246 | | thirdPersonCamera.m_YAxis.m_InvertInput = !current; |
| 610 | 247 | | } |
| | 248 | |
|
| | 249 | | private void OnDestroy() |
| | 250 | | { |
| 597 | 251 | | if (cachedModeToVirtualCamera != null) |
| | 252 | | { |
| 597 | 253 | | using (var iterator = cachedModeToVirtualCamera.GetEnumerator()) |
| | 254 | | { |
| 2388 | 255 | | while (iterator.MoveNext()) |
| | 256 | | { |
| 1791 | 257 | | iterator.Current.Value.Cleanup(); |
| | 258 | | } |
| 597 | 259 | | } |
| | 260 | | } |
| | 261 | |
|
| 597 | 262 | | worldOffset.OnChange -= OnWorldReposition; |
| 597 | 263 | | cameraChangeAction.OnTriggered -= OnCameraChangeAction; |
| 597 | 264 | | mouseWheelAction.OnValueChanged -= OnMouseWheelChangeValue; |
| 597 | 265 | | CommonScriptableObjects.rendererState.OnChange -= OnRenderingStateChanged; |
| 597 | 266 | | CommonScriptableObjects.cameraBlocked.OnChange -= CameraBlocked_OnChange; |
| 597 | 267 | | CommonScriptableObjects.isFullscreenHUDOpen.OnChange -= OnFullscreenUIVisibilityChange; |
| 597 | 268 | | CommonScriptableObjects.cameraMode.OnChange -= OnCameraModeChange; |
| 597 | 269 | | DataStore.i.camera.outputTexture.OnChange -= OnOutputTextureChange; |
| 597 | 270 | | DataStore.i.camera.invertYAxis.OnChange -= SetInvertYAxis; |
| 597 | 271 | | } |
| | 272 | |
|
| | 273 | | [Serializable] |
| | 274 | | public class SetRotationPayload |
| | 275 | | { |
| | 276 | | public float x; |
| | 277 | | public float y; |
| | 278 | | public float z; |
| | 279 | | public Vector3? cameraTarget; |
| | 280 | | } |
| | 281 | | } |
| | 282 | | } |