< Summary

Class:DCL.AvatarMovementController
Assembly:AvatarShape
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Avatar/AvatarMovementController.cs
Covered lines:8
Uncovered lines:51
Coverable lines:59
Total lines:155
Line coverage:13.5% (8 of 59)
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%20400%
UpdateLerp(...)0%6200%
UpdateRotation(...)0%2100%
UpdateMovement(...)0%12300%
Update()0%6200%

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        Transform avatarTransform
 18        {
 19            get
 20            {
 021                if (avatarTransformValue == null)
 022                    avatarTransformValue = GetComponent<AvatarShape>().entity.gameObject.transform;
 23
 024                return avatarTransformValue;
 25            }
 26        }
 27
 28        Transform avatarTransformValue;
 29
 30        Vector3 currentPosition
 31        {
 032            get { return currentWorldPosition; }
 33            set
 34            {
 035                currentWorldPosition = value;
 036                avatarTransform.position = PositionUtils.WorldToUnityPosition(currentWorldPosition);
 037            }
 38        }
 39
 52140        Vector3 currentWorldPosition = Vector3.zero;
 41
 042        Quaternion currentRotation { get { return avatarTransform.rotation; } set { avatarTransform.rotation = value; } 
 43
 44        Vector3 targetPosition;
 45        Quaternion targetRotation;
 46
 52147        float movementSpeed = SPEED_SLOW;
 48
 149        public void OnPoolGet() { }
 50
 51        public void OnPoolRelease()
 52        {
 153            avatarTransformValue = null;
 154            currentWorldPosition = Vector3.zero;
 155        }
 56
 457        void OnEnable() { CommonScriptableObjects.worldOffset.OnChange += OnWorldReposition; }
 58
 459        void OnDisable() { CommonScriptableObjects.worldOffset.OnChange -= OnWorldReposition; }
 60
 061        void OnWorldReposition(Vector3 current, Vector3 previous) { avatarTransform.position = PositionUtils.WorldToUnit
 62
 63        public void OnTransformChanged(object model)
 64        {
 065            DCLTransform.Model transformModel = (DCLTransform.Model)model;
 66
 067            MoveTo(
 68                transformModel.position - Vector3.up * DCLCharacterController.i.characterController.height / 2, // To fi
 69                transformModel.rotation);
 070        }
 71
 72        public void MoveTo(Vector3 position, Quaternion rotation, bool immediate = false)
 73        {
 074            if (immediate)
 75            {
 076                currentPosition = position;
 077                avatarTransform.rotation = rotation;
 78            }
 79
 080            Vector3 flatEulerRotation = rotation.eulerAngles;
 081            flatEulerRotation.z = flatEulerRotation.x = 0;
 082            rotation = Quaternion.Euler(flatEulerRotation);
 83
 084            targetPosition = position;
 085            targetRotation = rotation;
 86
 087            float distance = Vector3.Distance(targetPosition, currentWorldPosition);
 88
 89            //NOTE(Brian): More distance to goal = faster movement.
 090            if (distance >= 50)
 91            {
 092                this.movementSpeed = float.MaxValue;
 093            }
 094            else if (distance >= 3)
 95            {
 096                this.movementSpeed = Mathf.Lerp(SPEED_SLOW, SPEED_ULTRA_FAST, (distance - 3) / 10.0f);
 097            }
 98            else
 99            {
 0100                this.movementSpeed = SPEED_SLOW;
 101            }
 0102        }
 103
 104        void UpdateLerp(float deltaTime)
 105        {
 0106            if (Vector3.SqrMagnitude(currentPosition - targetPosition) < SPEED_EPSILON)
 107            {
 0108                UpdateRotation(deltaTime, targetRotation);
 0109                return;
 110            }
 111
 112            //NOTE(Brian): When we update movement we don't update rotation, because the Avatar will face the movement f
 0113            UpdateMovement(deltaTime);
 0114        }
 115
 0116        private void UpdateRotation(float deltaTime, Quaternion targetRotation) { currentRotation = Quaternion.Slerp(cur
 117
 118        private void UpdateMovement(float deltaTime)
 119        {
 0120            Vector3 flattenedDiff = targetPosition - currentPosition;
 0121            flattenedDiff.y = 0;
 122
 123            //NOTE(Brian): Avoid Unity error when computing look rotation for 0 magnitude vectors.
 124            //             Note that this isn't the same as the previous distance check because this
 125            //             is computed with a flattened vector.
 0126            if (flattenedDiff != Vector3.zero)
 127            {
 0128                Quaternion lookRotation = Quaternion.LookRotation(flattenedDiff, Vector3.up);
 0129                UpdateRotation(deltaTime, lookRotation);
 130            }
 131
 0132            Vector3 direction = (targetPosition - currentPosition).normalized;
 0133            Vector3 delta = direction * (movementSpeed * deltaTime);
 134
 135            //NOTE(Brian): We need a separate value for Y movement because the gravity has to be lerped faster.
 0136            delta.y = direction.y * SPEED_GRAVITY * deltaTime;
 137
 138            //NOTE(Brian): If we overshoot targetPosition we adjust the delta value accordingly.
 0139            if (delta.sqrMagnitude > Vector3.SqrMagnitude(targetPosition - currentPosition))
 140            {
 0141                delta = targetPosition - currentPosition;
 142            }
 143
 0144            currentPosition += delta;
 0145        }
 146
 147        void Update()
 148        {
 0149            if (avatarTransformValue == null)
 0150                return;
 151
 0152            UpdateLerp(Time.deltaTime);
 0153        }
 154    }
 155}