< Summary

Class:DCL.AvatarMovementController
Assembly:AvatarShape
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Avatar/AvatarMovementController.cs
Covered lines:42
Uncovered lines:24
Coverable lines:66
Total lines:172
Line coverage:63.6% (42 of 66)
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%
OnTransformChanged(...)0%110100%
MoveTo(...)0%4.214076.47%
UpdateLerp(...)0%2.262060%
UpdateRotation(...)0%110100%
UpdateMovement(...)0%12300%
Update()0%330100%
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    [RequireComponent(typeof(AvatarShape))]
 9    public class AvatarMovementController : MonoBehaviour, IPoolLifecycleHandler, IAvatarMovementController
 10    {
 11        const float SPEED_SLOW = 2.0f;
 12        const float SPEED_FAST = 4.0f;
 13        const float SPEED_ULTRA_FAST = 8.0f;
 14        const float SPEED_GRAVITY = 8.0f;
 15        const float ROTATION_SPEED = 6.25f;
 16        const float SPEED_EPSILON = 0.0001f;
 17
 18        private float movementLerpWait = 0f;
 19        private float movementLerpWaitCounter = 0f;
 20
 21        Transform avatarTransform
 22        {
 23            get
 24            {
 425                if (avatarTransformValue == null)
 126                    avatarTransformValue = GetComponent<AvatarShape>().entity.gameObject.transform;
 27
 428                return avatarTransformValue;
 29            }
 30        }
 31
 32        Transform avatarTransformValue;
 33
 34        Vector3 currentPosition
 35        {
 036            get { return currentWorldPosition; }
 37            set
 38            {
 139                currentWorldPosition = value;
 140                avatarTransform.position = PositionUtils.WorldToUnityPosition(currentWorldPosition);
 141            }
 42        }
 43
 68744        Vector3 currentWorldPosition = Vector3.zero;
 45
 346        Quaternion currentRotation { get { return avatarTransform.rotation; } set { avatarTransform.rotation = value; } 
 47
 48        Vector3 targetPosition;
 49        Quaternion targetRotation;
 50
 68751        float movementSpeed = SPEED_SLOW;
 52
 253        public void OnPoolGet() { }
 54
 55        public void OnPoolRelease()
 56        {
 257            avatarTransformValue = null;
 258            currentWorldPosition = Vector3.zero;
 259        }
 60
 137661        void OnEnable() { CommonScriptableObjects.worldOffset.OnChange += OnWorldReposition; }
 62
 137663        void OnDisable() { CommonScriptableObjects.worldOffset.OnChange -= OnWorldReposition; }
 64
 065        void OnWorldReposition(Vector3 current, Vector3 previous) { avatarTransform.position = PositionUtils.WorldToUnit
 66
 67        public void OnTransformChanged(object model)
 68        {
 069            DCLTransform.Model transformModel = (DCLTransform.Model)model;
 070            OnTransformChanged(transformModel.position, transformModel.rotation, false);
 071        }
 72
 73        public void OnTransformChanged(in Vector3 position, in Quaternion rotation, bool inmediate)
 74        {
 175            var offsetPosition = new Vector3(0, DCLCharacterController.i.characterController.height * 0.5f, 0);
 176            MoveTo(
 77                position - offsetPosition, // To fix the "always flying" avatars issue, We report the chara's centered p
 78                rotation,
 79                inmediate);
 180        }
 81
 82        public void MoveTo(Vector3 position, Quaternion rotation, bool immediate = false)
 83        {
 184            if (immediate)
 85            {
 186                currentPosition = position;
 187                avatarTransform.rotation = rotation;
 88            }
 89
 190            Vector3 flatEulerRotation = rotation.eulerAngles;
 191            flatEulerRotation.z = flatEulerRotation.x = 0;
 192            rotation = Quaternion.Euler(flatEulerRotation);
 93
 194            targetPosition = position;
 195            targetRotation = rotation;
 96
 197            float distance = Vector3.Distance(targetPosition, currentWorldPosition);
 98
 99            //NOTE(Brian): More distance to goal = faster movement.
 1100            if (distance >= 50)
 101            {
 0102                this.movementSpeed = float.MaxValue;
 0103            }
 1104            else if (distance >= 3)
 105            {
 0106                this.movementSpeed = Mathf.Lerp(SPEED_SLOW, SPEED_ULTRA_FAST, (distance - 3) / 10.0f);
 0107            }
 108            else
 109            {
 1110                this.movementSpeed = SPEED_SLOW;
 111            }
 1112        }
 113
 114        void UpdateLerp(float deltaTime)
 115        {
 1116            if (Vector3.SqrMagnitude(currentPosition - targetPosition) < SPEED_EPSILON)
 117            {
 1118                UpdateRotation(deltaTime, targetRotation);
 1119                return;
 120            }
 121
 122            //NOTE(Brian): When we update movement we don't update rotation, because the Avatar will face the movement f
 0123            UpdateMovement(deltaTime);
 0124        }
 125
 2126        private void UpdateRotation(float deltaTime, Quaternion targetRotation) { currentRotation = Quaternion.Slerp(cur
 127
 128        private void UpdateMovement(float deltaTime)
 129        {
 0130            Vector3 flattenedDiff = targetPosition - currentPosition;
 0131            flattenedDiff.y = 0;
 132
 133            //NOTE(Brian): Avoid Unity error when computing look rotation for 0 magnitude vectors.
 134            //             Note that this isn't the same as the previous distance check because this
 135            //             is computed with a flattened vector.
 0136            if (flattenedDiff != Vector3.zero)
 137            {
 0138                Quaternion lookRotation = Quaternion.LookRotation(flattenedDiff, Vector3.up);
 0139                UpdateRotation(deltaTime, lookRotation);
 140            }
 141
 0142            Vector3 direction = (targetPosition - currentPosition).normalized;
 0143            Vector3 delta = direction * (movementSpeed * deltaTime);
 144
 145            //NOTE(Brian): We need a separate value for Y movement because the gravity has to be lerped faster.
 0146            delta.y = direction.y * SPEED_GRAVITY * deltaTime;
 147
 148            //NOTE(Brian): If we overshoot targetPosition we adjust the delta value accordingly.
 0149            if (delta.sqrMagnitude > Vector3.SqrMagnitude(targetPosition - currentPosition))
 150            {
 0151                delta = targetPosition - currentPosition;
 152            }
 153
 0154            currentPosition += delta;
 0155        }
 156
 157        void Update()
 158        {
 2159            if (avatarTransformValue == null)
 1160                return;
 161
 1162            movementLerpWaitCounter += Time.deltaTime;
 1163            if (movementLerpWaitCounter >= movementLerpWait)
 164            {
 1165                UpdateLerp(movementLerpWaitCounter);
 1166                movementLerpWaitCounter = 0f;
 167            }
 1168        }
 169
 0170        public void SetMovementLerpWait(float secondsBetweenUpdates) { movementLerpWait = secondsBetweenUpdates; }
 171    }
 172}