| | 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; |
| 10382 | 9 | | protected Vector3NullableVariable characterForward => CommonScriptableObjects.characterForward; |
| | 10 | |
|
| | 11 | | private CinemachinePOV pov; |
| | 12 | |
|
| | 13 | | public override void Init(UnityEngine.Camera cameraTransform) |
| | 14 | | { |
| 124 | 15 | | base.Init(cameraTransform); |
| | 16 | |
|
| 124 | 17 | | if (defaultVirtualCamera is CinemachineVirtualCamera vcamera) |
| 124 | 18 | | pov = vcamera.GetCinemachineComponent<CinemachinePOV>(); |
| 124 | 19 | | } |
| | 20 | |
|
| | 21 | | public override void OnUpdate() |
| | 22 | | { |
| 10382 | 23 | | var xzPlaneForward = Vector3.Scale(cameraTransform.forward, new Vector3(1, 0, 1)); |
| 10382 | 24 | | characterForward.Set(xzPlaneForward); |
| 10382 | 25 | | } |
| | 26 | |
|
| | 27 | | public override Vector3 OnGetRotation() |
| | 28 | | { |
| 12 | 29 | | if (pov != null) |
| 12 | 30 | | return new Vector3(pov.m_VerticalAxis.Value, pov.m_HorizontalAxis.Value, 0); |
| | 31 | |
|
| 0 | 32 | | return Vector3.zero; |
| | 33 | | } |
| | 34 | |
|
| | 35 | | public override void OnSetRotation(CameraController.SetRotationPayload payload) |
| | 36 | | { |
| 40 | 37 | | var eulerDir = Vector3.zero; |
| | 38 | |
|
| 40 | 39 | | if (payload.cameraTarget.HasValue) |
| | 40 | | { |
| 34 | 41 | | var newPos = new Vector3(payload.x, payload.y, payload.z); |
| 34 | 42 | | var cameraTarget = payload.cameraTarget.GetValueOrDefault(); |
| 34 | 43 | | var dirToLook = (cameraTarget - newPos); |
| 34 | 44 | | eulerDir = Quaternion.LookRotation(dirToLook).eulerAngles; |
| | 45 | | } |
| | 46 | |
|
| 40 | 47 | | if (pov != null) |
| | 48 | | { |
| 40 | 49 | | pov.m_HorizontalAxis.Value = eulerDir.y; |
| 40 | 50 | | pov.m_VerticalAxis.Value = eulerDir.x; |
| | 51 | | } |
| 40 | 52 | | } |
| | 53 | |
|
| | 54 | | public override void OnBlock(bool blocked) |
| | 55 | | { |
| 12 | 56 | | base.OnBlock(blocked); |
| | 57 | |
|
| 12 | 58 | | if (pov != null) |
| | 59 | | { |
| 12 | 60 | | if (blocked) |
| | 61 | | { |
| | 62 | | // Before block the virtual camera, we make the main camera point to the same point as the virtual o |
| 6 | 63 | | defaultVirtualCamera.transform.rotation = Quaternion.Euler(pov.m_VerticalAxis.Value, pov.m_Horizonta |
| | 64 | | } |
| | 65 | |
|
| 12 | 66 | | pov.enabled = !blocked; |
| | 67 | |
|
| | 68 | | // NOTE(Santi): After modify the 'CinemachinePOV.enabled' parameter, the only (and not very elegant) way |
| | 69 | | // offers to force the update is deactivating and re-activating the virtual camera. |
| 12 | 70 | | defaultVirtualCamera.enabled = false; |
| 12 | 71 | | defaultVirtualCamera.enabled = true; |
| | 72 | | } |
| 12 | 73 | | } |
| | 74 | | } |
| | 75 | | } |