| | 1 | | using Cinemachine; |
| | 2 | | using UnityEngine; |
| | 3 | |
|
| | 4 | | namespace DCL.Camera |
| | 5 | | { |
| | 6 | | public class CameraStateFPS : CameraStateBase |
| | 7 | | { |
| 0 | 8 | | protected Vector3Variable cameraForward => CommonScriptableObjects.cameraForward; |
| 16397 | 9 | | protected Vector3NullableVariable characterForward => CommonScriptableObjects.characterForward; |
| | 10 | |
|
| | 11 | | private CinemachinePOV pov; |
| | 12 | |
|
| | 13 | | public override void Init(UnityEngine.Camera cameraTransform) |
| | 14 | | { |
| 125 | 15 | | base.Init(cameraTransform); |
| | 16 | |
|
| 125 | 17 | | if (defaultVirtualCamera is CinemachineVirtualCamera vcamera) |
| 125 | 18 | | pov = vcamera.GetCinemachineComponent<CinemachinePOV>(); |
| 125 | 19 | | } |
| | 20 | |
|
| | 21 | | public override void OnSelect() |
| | 22 | | { |
| 146 | 23 | | base.OnSelect(); |
| 146 | 24 | | CommonScriptableObjects.playerIsOnMovingPlatform.OnChange += OnMovingPlatformStart; |
| 146 | 25 | | if (CommonScriptableObjects.playerIsOnMovingPlatform.Get()) |
| 0 | 26 | | OnMovingPlatformStart(true, true); |
| 146 | 27 | | } |
| | 28 | |
|
| | 29 | | public override void OnUnselect() |
| | 30 | | { |
| 146 | 31 | | base.OnUnselect(); |
| 146 | 32 | | CommonScriptableObjects.playerIsOnMovingPlatform.OnChange -= OnMovingPlatformStart; |
| 146 | 33 | | CommonScriptableObjects.movingPlatformRotationDelta.OnChange -= OnMovingPlatformRotate; |
| 146 | 34 | | } |
| | 35 | | private void OnMovingPlatformStart(bool current, bool previous) |
| | 36 | | { |
| 0 | 37 | | if (current) |
| | 38 | | { |
| 0 | 39 | | CommonScriptableObjects.movingPlatformRotationDelta.OnChange += OnMovingPlatformRotate; |
| 0 | 40 | | } |
| | 41 | | else |
| | 42 | | { |
| 0 | 43 | | CommonScriptableObjects.movingPlatformRotationDelta.OnChange -= OnMovingPlatformRotate; |
| | 44 | | } |
| 0 | 45 | | } |
| | 46 | | private void OnMovingPlatformRotate(Quaternion current, Quaternion previous) |
| | 47 | | { |
| 0 | 48 | | Vector3 diff = current.eulerAngles; |
| 0 | 49 | | if (pov != null) |
| | 50 | | { |
| 0 | 51 | | pov.m_HorizontalAxis.Value += diff.y; |
| 0 | 52 | | pov.m_VerticalAxis.Value += diff.x; |
| | 53 | | } |
| 0 | 54 | | } |
| | 55 | |
|
| | 56 | | public override void OnUpdate() |
| | 57 | | { |
| 16397 | 58 | | var xzPlaneForward = Vector3.Scale(cameraTransform.forward, new Vector3(1, 0, 1)); |
| 16397 | 59 | | characterForward.Set(xzPlaneForward); |
| 16397 | 60 | | } |
| | 61 | |
|
| | 62 | | public override Vector3 OnGetRotation() |
| | 63 | | { |
| 12 | 64 | | if (pov != null) |
| 12 | 65 | | return new Vector3(pov.m_VerticalAxis.Value, pov.m_HorizontalAxis.Value, 0); |
| | 66 | |
|
| 0 | 67 | | return Vector3.zero; |
| | 68 | | } |
| | 69 | |
|
| | 70 | | public override void OnSetRotation(CameraController.SetRotationPayload payload) |
| | 71 | | { |
| 39 | 72 | | var eulerDir = Vector3.zero; |
| | 73 | |
|
| 39 | 74 | | if (payload.cameraTarget.HasValue) |
| | 75 | | { |
| 34 | 76 | | var newPos = new Vector3(payload.x, payload.y, payload.z); |
| 34 | 77 | | var cameraTarget = payload.cameraTarget.GetValueOrDefault(); |
| 34 | 78 | | var dirToLook = (cameraTarget - newPos); |
| 34 | 79 | | eulerDir = Quaternion.LookRotation(dirToLook).eulerAngles; |
| | 80 | | } |
| | 81 | |
|
| 39 | 82 | | if (pov != null) |
| | 83 | | { |
| 39 | 84 | | pov.m_HorizontalAxis.Value = eulerDir.y; |
| 39 | 85 | | pov.m_VerticalAxis.Value = eulerDir.x; |
| | 86 | | } |
| 39 | 87 | | } |
| | 88 | |
|
| | 89 | | public override void OnBlock(bool blocked) |
| | 90 | | { |
| 12 | 91 | | base.OnBlock(blocked); |
| | 92 | |
|
| 12 | 93 | | if (pov != null) |
| | 94 | | { |
| 12 | 95 | | if (blocked) |
| | 96 | | { |
| | 97 | | // Before block the virtual camera, we make the main camera point to the same point as the virtual o |
| 6 | 98 | | defaultVirtualCamera.transform.rotation = Quaternion.Euler(pov.m_VerticalAxis.Value, pov.m_Horizonta |
| | 99 | | } |
| | 100 | |
|
| 12 | 101 | | pov.enabled = !blocked; |
| | 102 | |
|
| | 103 | | // NOTE(Santi): After modify the 'CinemachinePOV.enabled' parameter, the only (and not very elegant) way |
| | 104 | | // offers to force the update is deactivating and re-activating the virtual camera. |
| 12 | 105 | | defaultVirtualCamera.enabled = false; |
| 12 | 106 | | defaultVirtualCamera.enabled = true; |
| | 107 | | } |
| 12 | 108 | | } |
| | 109 | | } |
| | 110 | | } |