< Summary

Class:DCL.AvatarMovementController
Assembly:AvatarShape
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Avatar/AvatarMovementController.cs
Covered lines:30
Uncovered lines:42
Coverable lines:72
Total lines:186
Line coverage:41.6% (30 of 72)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
AvatarMovementController()0%110100%
OnPoolGet()0%110100%
OnPoolRelease()0%110100%
SetAvatarTransform(...)0%110100%
OnEnable()0%110100%
OnDisable()0%110100%
OnWorldReposition(...)0%2100%
OnTransformChanged(...)0%2100%
OnTransformChanged(...)0%2100%
MoveTo(...)0%20400%
UpdateLerp(...)0%2.262060%
UpdateRotation(...)0%110100%
UpdateMovement(...)0%12300%
Update()0%220100%
SetMovementLerpWait(...)0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Avatar/AvatarMovementController.cs

#LineLine coverage
 1using AvatarSystem;
 2using DCL.Components;
 3using DCL.Helpers;
 4using UnityEngine;
 5
 6namespace DCL
 7{
 8    public class AvatarMovementController : MonoBehaviour, IPoolLifecycleHandler, IAvatarMovementController
 9    {
 10        private const float SPEED_SLOW = 2.0f;
 11        private const float SPEED_FAST = 4.0f;
 12        private const float SPEED_ULTRA_FAST = 8.0f;
 13        private const float SPEED_GRAVITY = 8.0f;
 14        private const float ROTATION_SPEED = 6.25f;
 15        private const float SPEED_EPSILON = 0.0001f;
 42616        private float movementSpeed = SPEED_SLOW;
 17
 18        private Transform avatarTransformValue;
 19
 20        private Quaternion targetRotation;
 42621        private Vector3 currentWorldPosition = Vector3.zero;
 22        private Vector3 targetPosition;
 23
 24        private float movementLerpWait = 0f;
 25        private float movementLerpWaitCounter = 0f;
 26
 27
 28        private Transform AvatarTransform
 29        {
 30            get
 31            {
 232                if (avatarTransformValue == null)
 33                {
 134                    avatarTransformValue = GetComponent<AvatarShape>().entity.gameObject.transform;
 135                    enabled = true;
 36                }
 37
 238                return avatarTransformValue;
 39            }
 40            set
 41            {
 2342                avatarTransformValue = value;
 43
 2344                if (value == null)
 545                    enabled = false;
 2346            }
 47        }
 48
 49        private Vector3 CurrentPosition
 50        {
 051            get { return currentWorldPosition; }
 52            set
 53            {
 054                currentWorldPosition = value;
 055                AvatarTransform.position = PositionUtils.WorldToUnityPosition(currentWorldPosition);
 056            }
 57        }
 58
 59        private Quaternion CurrentRotation
 60        {
 161            get { return AvatarTransform.rotation; }
 262            set { AvatarTransform.rotation = value; }
 63        }
 64
 565        public void OnPoolGet() { }
 66
 67        public void OnPoolRelease()
 68        {
 569            AvatarTransform = null;
 70
 571            currentWorldPosition = Vector3.zero;
 572        }
 73
 74        public void SetAvatarTransform(Transform avatarTransform)
 75        {
 1876            AvatarTransform = avatarTransform;
 1877        }
 78
 85479        private void OnEnable() { CommonScriptableObjects.worldOffset.OnChange += OnWorldReposition; }
 80
 85481        private void OnDisable() { CommonScriptableObjects.worldOffset.OnChange -= OnWorldReposition; }
 82
 83        private void OnWorldReposition(Vector3 current, Vector3 previous)
 84        {
 085            AvatarTransform.position = PositionUtils.WorldToUnityPosition(currentWorldPosition);
 086        }
 87
 88        public void OnTransformChanged(object model)
 89        {
 090            DCLTransform.Model transformModel = (DCLTransform.Model)model;
 091            OnTransformChanged(transformModel.position, transformModel.rotation, false);
 092        }
 93
 94        public void OnTransformChanged(in Vector3 position, in Quaternion rotation, bool inmediate)
 95        {
 096            var offsetPosition = new Vector3(0, DCLCharacterController.i.characterController.height * 0.5f, 0);
 097            MoveTo(
 98                position - offsetPosition, // To fix the "always flying" avatars issue, We report the chara's centered p
 99                rotation,
 100                inmediate);
 0101        }
 102
 103        public void MoveTo(Vector3 position, Quaternion rotation, bool immediate = false)
 104        {
 0105            if (immediate)
 106            {
 0107                CurrentPosition = position;
 0108                AvatarTransform.rotation = rotation;
 109            }
 110
 0111            Vector3 flatEulerRotation = rotation.eulerAngles;
 0112            flatEulerRotation.z = flatEulerRotation.x = 0;
 0113            rotation = Quaternion.Euler(flatEulerRotation);
 114
 0115            targetPosition = position;
 0116            targetRotation = rotation;
 117
 0118            float distance = Vector3.Distance(targetPosition, currentWorldPosition);
 119
 0120            if (distance >= 50)
 0121                movementSpeed = float.MaxValue;
 0122            else if (distance >= 3)
 0123                movementSpeed = Mathf.Lerp(SPEED_SLOW, SPEED_ULTRA_FAST, (distance - 3) / 10.0f);
 124            else
 0125                movementSpeed = SPEED_SLOW;
 0126        }
 127
 128        void UpdateLerp(float deltaTime)
 129        {
 1130            if (Vector3.SqrMagnitude(CurrentPosition - targetPosition) < SPEED_EPSILON)
 131            {
 1132                UpdateRotation(deltaTime, targetRotation);
 1133                return;
 134            }
 135
 136            //NOTE(Brian): When we update movement we don't update rotation, because the Avatar will face the movement f
 0137            UpdateMovement(deltaTime);
 0138        }
 139
 140        private void UpdateRotation(float deltaTime, Quaternion targetRotation)
 141        {
 1142            CurrentRotation = Quaternion.Slerp(CurrentRotation, targetRotation, ROTATION_SPEED * deltaTime);
 1143        }
 144
 145        private void UpdateMovement(float deltaTime)
 146        {
 0147            Vector3 flattenedDiff = targetPosition - CurrentPosition;
 0148            flattenedDiff.y = 0;
 149
 150            //NOTE(Brian): Avoid Unity error when computing look rotation for 0 magnitude vectors.
 151            //             Note that this isn't the same as the previous distance check because this
 152            //             is computed with a flattened vector.
 0153            if (flattenedDiff != Vector3.zero)
 154            {
 0155                Quaternion lookRotation = Quaternion.LookRotation(flattenedDiff, Vector3.up);
 0156                UpdateRotation(deltaTime, lookRotation);
 157            }
 158
 0159            Vector3 direction = (targetPosition - CurrentPosition).normalized;
 0160            Vector3 delta = direction * (movementSpeed * deltaTime);
 161
 162            //NOTE(Brian): We need a separate value for Y movement because the gravity has to be lerped faster.
 0163            delta.y = direction.y * SPEED_GRAVITY * deltaTime;
 164
 165            //NOTE(Brian): If we overshoot targetPosition we adjust the delta value accordingly.
 0166            if (delta.sqrMagnitude > Vector3.SqrMagnitude(targetPosition - CurrentPosition))
 167            {
 0168                delta = targetPosition - CurrentPosition;
 169            }
 170
 0171            CurrentPosition += delta;
 0172        }
 173
 174        private void Update()
 175        {
 1176            movementLerpWaitCounter += Time.deltaTime;
 1177            if (movementLerpWaitCounter >= movementLerpWait)
 178            {
 1179                UpdateLerp(movementLerpWaitCounter);
 1180                movementLerpWaitCounter = 0f;
 181            }
 1182        }
 183
 0184        public void SetMovementLerpWait(float secondsBetweenUpdates) { movementLerpWait = secondsBetweenUpdates; }
 185    }
 186}