< Summary

Class:DCL.Camera.CameraStateTPS
Assembly:Camera
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/Camera/CameraStateTPS.cs
Covered lines:38
Uncovered lines:56
Coverable lines:94
Total lines:201
Line coverage:40.4% (38 of 94)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
CameraStateTPS()0%110100%
Initialize(...)0%110100%
Cleanup()0%220100%
OnEnable()0%110100%
OnDisable()0%110100%
UpdateMovingPlatformCamera(...)0%6200%
OnSelect()0%220100%
OnUpdate()0%110100%
UpdateAvatarRotationDamping()0%39.498021.05%
OnGetRotation()0%2100%
OnSetRotation(...)0%6200%
OnBlock(...)0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/Camera/CameraStateTPS.cs

#LineLine coverage
 1using Cinemachine;
 2using UnityEngine;
 3
 4namespace DCL.Camera
 5{
 6    public class CameraStateTPS : CameraStateBase
 7    {
 08        private CinemachineFreeLook defaultVirtualCameraAsFreeLook => defaultVirtualCamera as CinemachineFreeLook;
 9
 10        [SerializeField] private CinemachineVirtualCamera fallingVirtualCamera;
 11        [SerializeField] private InputAction_Measurable characterYAxis;
 12        [SerializeField] private InputAction_Measurable characterXAxis;
 13        private CinemachineTransposer freeLookTopRig;
 14        private CinemachineTransposer freeLookMidRig;
 15        private CinemachineTransposer freeLookBotRig;
 16        private Vector3 freeLookTopRigOriginalBodyDamping;
 17        private Vector3 freeLookMidRigOriginalBodyDamping;
 18        private Vector3 freeLookBotRigOriginalBodyDamping;
 19
 020        protected Vector3Variable characterPosition => CommonScriptableObjects.playerUnityPosition;
 10021        protected Vector3NullableVariable characterForward => CommonScriptableObjects.characterForward;
 022        protected Vector3Variable cameraForward => CommonScriptableObjects.cameraForward;
 023        protected Vector3Variable cameraRight => CommonScriptableObjects.cameraRight;
 024        protected Vector3Variable cameraPosition => CommonScriptableObjects.cameraPosition;
 25
 26        public FollowWithDamping cameraTargetProbe;
 27
 28        [Header("Avatar Feel Settings")]
 59829        public float avatarRotationLerpSpeed = 10;
 30
 31        [SerializeField]
 59832        private CameraFreefall.Settings cameraFreefallSettings = new CameraFreefall.Settings();
 33
 34        public CameraFreefall cameraFreefall;
 35
 36        [SerializeField]
 59837        private CameraDampOnGroundType.Settings cameraDampOnGroundTypeSettings = new CameraDampOnGroundType.Settings();
 38
 39        public CameraDampOnGroundType cameraDampOnGroundType;
 40
 41        [SerializeField]
 59842        private CameraDampOnSprint.Settings cameraSprintDampingSettings = new CameraDampOnSprint.Settings();
 43
 44        public CameraDampOnSprint cameraDampOnSprint;
 45
 46        public override void Initialize(UnityEngine.Camera camera)
 47        {
 59748            freeLookTopRig = defaultVirtualCameraAsFreeLook.GetRig(0).GetCinemachineComponent<CinemachineTransposer>();
 59749            freeLookTopRigOriginalBodyDamping = new Vector3(freeLookTopRig.m_XDamping, freeLookTopRig.m_YDamping, freeLo
 59750            freeLookMidRig = defaultVirtualCameraAsFreeLook.GetRig(1).GetCinemachineComponent<CinemachineTransposer>();
 59751            freeLookMidRigOriginalBodyDamping = new Vector3(freeLookMidRig.m_XDamping, freeLookMidRig.m_YDamping, freeLo
 59752            freeLookBotRig = defaultVirtualCameraAsFreeLook.GetRig(2).GetCinemachineComponent<CinemachineTransposer>();
 59753            freeLookBotRigOriginalBodyDamping = new Vector3(freeLookBotRig.m_XDamping, freeLookBotRig.m_YDamping, freeLo
 54
 59755            cameraDampOnSprint = new CameraDampOnSprint(cameraSprintDampingSettings, defaultVirtualCameraAsFreeLook, cha
 59756            cameraDampOnGroundType = new CameraDampOnGroundType(cameraDampOnGroundTypeSettings, cameraTargetProbe);
 59757            cameraFreefall = new CameraFreefall(cameraFreefallSettings, defaultVirtualCameraAsFreeLook);
 58
 59759            base.Initialize(camera);
 59760        }
 61
 62        public override void Cleanup()
 63        {
 59764            if (cameraTargetProbe != null)
 59765                Destroy(cameraTargetProbe.gameObject);
 59766        }
 67
 68        private void OnEnable()
 69        {
 9870            CommonScriptableObjects.playerIsOnMovingPlatform.OnChange += UpdateMovingPlatformCamera;
 9871        }
 72
 19673        private void OnDisable() { CommonScriptableObjects.playerIsOnMovingPlatform.OnChange -= UpdateMovingPlatformCame
 74
 75        void UpdateMovingPlatformCamera(bool isOnMovingPlatform, bool wasOnMovingPlatform)
 76        {
 077            if (isOnMovingPlatform)
 78            {
 079                freeLookTopRig.m_XDamping = 0;
 080                freeLookTopRig.m_YDamping = 0;
 081                freeLookTopRig.m_ZDamping = 0;
 82
 083                freeLookMidRig.m_XDamping = 0;
 084                freeLookMidRig.m_YDamping = 0;
 085                freeLookMidRig.m_ZDamping = 0;
 86
 087                freeLookBotRig.m_XDamping = 0;
 088                freeLookBotRig.m_YDamping = 0;
 089                freeLookBotRig.m_ZDamping = 0;
 090            }
 91            else
 92            {
 093                freeLookTopRig.m_XDamping = freeLookTopRigOriginalBodyDamping.x;
 094                freeLookTopRig.m_YDamping = freeLookTopRigOriginalBodyDamping.y;
 095                freeLookTopRig.m_ZDamping = freeLookTopRigOriginalBodyDamping.z;
 96
 097                freeLookMidRig.m_XDamping = freeLookMidRigOriginalBodyDamping.x;
 098                freeLookMidRig.m_YDamping = freeLookMidRigOriginalBodyDamping.y;
 099                freeLookMidRig.m_ZDamping = freeLookMidRigOriginalBodyDamping.z;
 100
 0101                freeLookBotRig.m_XDamping = freeLookBotRigOriginalBodyDamping.x;
 0102                freeLookBotRig.m_YDamping = freeLookBotRigOriginalBodyDamping.y;
 0103                freeLookBotRig.m_ZDamping = freeLookBotRigOriginalBodyDamping.z;
 104            }
 0105        }
 106
 107        public override void OnSelect()
 108        {
 98109            if (characterForward.Get().HasValue)
 110            {
 2111                defaultVirtualCameraAsFreeLook.m_XAxis.Value = Quaternion.LookRotation(characterForward.Get().Value, Vec
 2112                defaultVirtualCameraAsFreeLook.m_YAxis.Value = 0.5f;
 113            }
 114
 98115            base.OnSelect();
 98116        }
 117
 118        public override void OnUpdate()
 119        {
 2198120            defaultVirtualCameraAsFreeLook.m_BindingMode = CinemachineTransposer.BindingMode.WorldSpace;
 121
 2198122            bool didHit = DCLCharacterController.i.CastGroundCheckingRays(20, 0.1f, out RaycastHit hitInfo);
 123
 2198124            cameraDampOnSprint.Update();
 2198125            cameraDampOnGroundType.Update(didHit, hitInfo);
 2198126            cameraFreefall.Update(didHit, hitInfo);
 127
 2198128            UpdateAvatarRotationDamping();
 2198129        }
 130
 131        /// <summary>
 132        /// This methods ensures the Avatar rotates smoothly when changing direction.
 133        /// Note that this will NOT affect the player movement, is only cosmetic.
 134        /// TODO(Brian): This is not camera feel, move this elsewhere
 135        /// </summary>
 136        private void UpdateAvatarRotationDamping()
 137        {
 2198138            var xzPlaneForward = Vector3.Scale(cameraTransform.forward, new Vector3(1, 0, 1));
 2198139            var xzPlaneRight = Vector3.Scale(cameraTransform.right, new Vector3(1, 0, 1));
 140
 2198141            if (characterYAxis.GetValue() != 0f || characterXAxis.GetValue() != 0f)
 142            {
 0143                Vector3 forwardTarget = Vector3.zero;
 144
 0145                if (characterYAxis.GetValue() > 0)
 0146                    forwardTarget += xzPlaneForward;
 147
 0148                if (characterYAxis.GetValue() < 0)
 0149                    forwardTarget -= xzPlaneForward;
 150
 0151                if (characterXAxis.GetValue() > 0)
 0152                    forwardTarget += xzPlaneRight;
 153
 0154                if (characterXAxis.GetValue() < 0)
 0155                    forwardTarget -= xzPlaneRight;
 156
 0157                forwardTarget.Normalize();
 158
 0159                if (!characterForward.HasValue())
 160                {
 0161                    characterForward.Set(forwardTarget);
 0162                }
 163                else
 164                {
 0165                    var lerpedForward = Vector3.Slerp(characterForward.Get().Value, forwardTarget, avatarRotationLerpSpe
 0166                    characterForward.Set(lerpedForward);
 167                }
 168            }
 2198169        }
 170
 0171        public override Vector3 OnGetRotation() { return new Vector3(defaultVirtualCameraAsFreeLook.m_YAxis.Value, defau
 172
 173        public override void OnSetRotation(CameraController.SetRotationPayload payload)
 174        {
 0175            var eulerDir = Vector3.zero;
 176
 0177            if (payload.cameraTarget.HasValue)
 178            {
 0179                var cameraTarget = payload.cameraTarget.GetValueOrDefault();
 180
 0181                var horizontalAxisLookAt = payload.y - cameraTarget.y;
 0182                var verticalAxisLookAt = new Vector3(cameraTarget.x - payload.x, 0, cameraTarget.z - payload.z);
 183
 0184                eulerDir.y = Vector3.SignedAngle(Vector3.forward, verticalAxisLookAt, Vector3.up);
 0185                eulerDir.x = Mathf.Atan2(horizontalAxisLookAt, verticalAxisLookAt.magnitude) * Mathf.Rad2Deg;
 186            }
 187
 0188            defaultVirtualCameraAsFreeLook.m_XAxis.Value = eulerDir.y;
 189
 190            //value range 0 to 1, being 0 the bottom orbit and 1 the top orbit
 0191            var yValue = Mathf.InverseLerp(-90, 90, eulerDir.x);
 0192            defaultVirtualCameraAsFreeLook.m_YAxis.Value = yValue;
 0193        }
 194
 195        public override void OnBlock(bool blocked)
 196        {
 0197            base.OnBlock(blocked);
 0198            defaultVirtualCameraAsFreeLook.enabled = !blocked;
 0199        }
 200    }
 201}