< Summary

Class:DCL.AvatarMovementController
Assembly:AvatarShape
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Avatar/AvatarMovementController.cs
Covered lines:29
Uncovered lines:33
Coverable lines:62
Total lines:163
Line coverage:46.7% (29 of 62)
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%
OnEnable()0%110100%
OnDisable()0%110100%
OnWorldReposition(...)0%2100%
OnTransformChanged(...)0%2100%
MoveTo(...)0%4.214076.47%
UpdateLerp(...)0%6200%
UpdateRotation(...)0%2100%
UpdateMovement(...)0%12300%
Update()0%6.283028.57%

File(s)

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

#LineLine coverage
 1using DCL.Components;
 2using DCL.Helpers;
 3using UnityEngine;
 4
 5namespace DCL
 6{
 7    [RequireComponent(typeof(AvatarShape))]
 8    public class AvatarMovementController : MonoBehaviour, IPoolLifecycleHandler
 9    {
 10        const float SPEED_SLOW = 2.0f;
 11        const float SPEED_FAST = 4.0f;
 12        const float SPEED_ULTRA_FAST = 8.0f;
 13        const float SPEED_GRAVITY = 8.0f;
 14        const float ROTATION_SPEED = 6.25f;
 15        const float SPEED_EPSILON = 0.0001f;
 16
 17        public float movementLerpWait = 0f;
 18        private float movementLerpWaitCounter = 0f;
 19
 20        Transform avatarTransform
 21        {
 22            get
 23            {
 224                if (avatarTransformValue == null)
 125                    avatarTransformValue = GetComponent<AvatarShape>().entity.gameObject.transform;
 26
 227                return avatarTransformValue;
 28            }
 29        }
 30
 31        Transform avatarTransformValue;
 32
 33        Vector3 currentPosition
 34        {
 035            get { return currentWorldPosition; }
 36            set
 37            {
 138                currentWorldPosition = value;
 139                avatarTransform.position = PositionUtils.WorldToUnityPosition(currentWorldPosition);
 140            }
 41        }
 42
 66243        Vector3 currentWorldPosition = Vector3.zero;
 44
 045        Quaternion currentRotation { get { return avatarTransform.rotation; } set { avatarTransform.rotation = value; } 
 46
 47        Vector3 targetPosition;
 48        Quaternion targetRotation;
 49
 66250        float movementSpeed = SPEED_SLOW;
 51
 252        public void OnPoolGet() { }
 53
 54        public void OnPoolRelease()
 55        {
 256            avatarTransformValue = null;
 257            currentWorldPosition = Vector3.zero;
 258        }
 59
 132660        void OnEnable() { CommonScriptableObjects.worldOffset.OnChange += OnWorldReposition; }
 61
 132662        void OnDisable() { CommonScriptableObjects.worldOffset.OnChange -= OnWorldReposition; }
 63
 064        void OnWorldReposition(Vector3 current, Vector3 previous) { avatarTransform.position = PositionUtils.WorldToUnit
 65
 66        public void OnTransformChanged(object model)
 67        {
 068            DCLTransform.Model transformModel = (DCLTransform.Model)model;
 69
 070            MoveTo(
 71                transformModel.position - Vector3.up * DCLCharacterController.i.characterController.height / 2, // To fi
 72                transformModel.rotation);
 073        }
 74
 75        public void MoveTo(Vector3 position, Quaternion rotation, bool immediate = false)
 76        {
 177            if (immediate)
 78            {
 179                currentPosition = position;
 180                avatarTransform.rotation = rotation;
 81            }
 82
 183            Vector3 flatEulerRotation = rotation.eulerAngles;
 184            flatEulerRotation.z = flatEulerRotation.x = 0;
 185            rotation = Quaternion.Euler(flatEulerRotation);
 86
 187            targetPosition = position;
 188            targetRotation = rotation;
 89
 190            float distance = Vector3.Distance(targetPosition, currentWorldPosition);
 91
 92            //NOTE(Brian): More distance to goal = faster movement.
 193            if (distance >= 50)
 94            {
 095                this.movementSpeed = float.MaxValue;
 096            }
 197            else if (distance >= 3)
 98            {
 099                this.movementSpeed = Mathf.Lerp(SPEED_SLOW, SPEED_ULTRA_FAST, (distance - 3) / 10.0f);
 0100            }
 101            else
 102            {
 1103                this.movementSpeed = SPEED_SLOW;
 104            }
 1105        }
 106
 107        void UpdateLerp(float deltaTime)
 108        {
 0109            if (Vector3.SqrMagnitude(currentPosition - targetPosition) < SPEED_EPSILON)
 110            {
 0111                UpdateRotation(deltaTime, targetRotation);
 0112                return;
 113            }
 114
 115            //NOTE(Brian): When we update movement we don't update rotation, because the Avatar will face the movement f
 0116            UpdateMovement(deltaTime);
 0117        }
 118
 0119        private void UpdateRotation(float deltaTime, Quaternion targetRotation) { currentRotation = Quaternion.Slerp(cur
 120
 121        private void UpdateMovement(float deltaTime)
 122        {
 0123            Vector3 flattenedDiff = targetPosition - currentPosition;
 0124            flattenedDiff.y = 0;
 125
 126            //NOTE(Brian): Avoid Unity error when computing look rotation for 0 magnitude vectors.
 127            //             Note that this isn't the same as the previous distance check because this
 128            //             is computed with a flattened vector.
 0129            if (flattenedDiff != Vector3.zero)
 130            {
 0131                Quaternion lookRotation = Quaternion.LookRotation(flattenedDiff, Vector3.up);
 0132                UpdateRotation(deltaTime, lookRotation);
 133            }
 134
 0135            Vector3 direction = (targetPosition - currentPosition).normalized;
 0136            Vector3 delta = direction * (movementSpeed * deltaTime);
 137
 138            //NOTE(Brian): We need a separate value for Y movement because the gravity has to be lerped faster.
 0139            delta.y = direction.y * SPEED_GRAVITY * deltaTime;
 140
 141            //NOTE(Brian): If we overshoot targetPosition we adjust the delta value accordingly.
 0142            if (delta.sqrMagnitude > Vector3.SqrMagnitude(targetPosition - currentPosition))
 143            {
 0144                delta = targetPosition - currentPosition;
 145            }
 146
 0147            currentPosition += delta;
 0148        }
 149
 150        void Update()
 151        {
 1152            if (avatarTransformValue == null)
 1153                return;
 154
 0155            movementLerpWaitCounter += Time.deltaTime;
 0156            if (movementLerpWaitCounter >= movementLerpWait)
 157            {
 0158                UpdateLerp(movementLerpWaitCounter);
 0159                movementLerpWaitCounter = 0f;
 160            }
 0161        }
 162    }
 163}