< Summary

Class:DCL.AvatarMovementController
Assembly:AvatarShape
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Avatar/AvatarMovementController.cs
Covered lines:12
Uncovered lines:56
Coverable lines:68
Total lines:176
Line coverage:17.6% (12 of 68)
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%6200%
UpdateRotation(...)0%2100%
UpdateMovement(...)0%12300%
Update()0%6.283028.57%
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        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        private float movementLerpWait = 0f;
 18        private float movementLerpWaitCounter = 0f;
 19
 20        Transform avatarTransform
 21        {
 22            get
 23            {
 024                if (avatarTransformValue == null)
 025                    avatarTransformValue = GetComponent<AvatarShape>().entity.gameObject.transform;
 26
 027                return avatarTransformValue;
 28            }
 29        }
 30
 31        Transform avatarTransformValue;
 32
 33        Vector3 currentPosition
 34        {
 035            get { return currentWorldPosition; }
 36            set
 37            {
 038                currentWorldPosition = value;
 039                avatarTransform.position = PositionUtils.WorldToUnityPosition(currentWorldPosition);
 040            }
 41        }
 42
 42343        Vector3 currentWorldPosition = Vector3.zero;
 44
 045        Quaternion currentRotation { get { return avatarTransform.rotation; } set { avatarTransform.rotation = value; } 
 46
 47        Vector3 targetPosition;
 48        Quaternion targetRotation;
 49
 42350        float movementSpeed = SPEED_SLOW;
 51
 552        public void OnPoolGet() { }
 53
 54        public void OnPoolRelease()
 55        {
 556            avatarTransformValue = null;
 557            currentWorldPosition = Vector3.zero;
 558        }
 59
 60        public void SetAvatarTransform(Transform avatarTransform)
 61        {
 1862            avatarTransformValue = avatarTransform;
 1863        }
 64
 84865        void OnEnable() { CommonScriptableObjects.worldOffset.OnChange += OnWorldReposition; }
 66
 84867        void OnDisable() { CommonScriptableObjects.worldOffset.OnChange -= OnWorldReposition; }
 68
 069        void OnWorldReposition(Vector3 current, Vector3 previous) { avatarTransform.position = PositionUtils.WorldToUnit
 70
 71        public void OnTransformChanged(object model)
 72        {
 073            DCLTransform.Model transformModel = (DCLTransform.Model)model;
 074            OnTransformChanged(transformModel.position, transformModel.rotation, false);
 075        }
 76
 77        public void OnTransformChanged(in Vector3 position, in Quaternion rotation, bool inmediate)
 78        {
 079            var offsetPosition = new Vector3(0, DCLCharacterController.i.characterController.height * 0.5f, 0);
 080            MoveTo(
 81                position - offsetPosition, // To fix the "always flying" avatars issue, We report the chara's centered p
 82                rotation,
 83                inmediate);
 084        }
 85
 86        public void MoveTo(Vector3 position, Quaternion rotation, bool immediate = false)
 87        {
 088            if (immediate)
 89            {
 090                currentPosition = position;
 091                avatarTransform.rotation = rotation;
 92            }
 93
 094            Vector3 flatEulerRotation = rotation.eulerAngles;
 095            flatEulerRotation.z = flatEulerRotation.x = 0;
 096            rotation = Quaternion.Euler(flatEulerRotation);
 97
 098            targetPosition = position;
 099            targetRotation = rotation;
 100
 0101            float distance = Vector3.Distance(targetPosition, currentWorldPosition);
 102
 103            //NOTE(Brian): More distance to goal = faster movement.
 0104            if (distance >= 50)
 105            {
 0106                this.movementSpeed = float.MaxValue;
 0107            }
 0108            else if (distance >= 3)
 109            {
 0110                this.movementSpeed = Mathf.Lerp(SPEED_SLOW, SPEED_ULTRA_FAST, (distance - 3) / 10.0f);
 0111            }
 112            else
 113            {
 0114                this.movementSpeed = SPEED_SLOW;
 115            }
 0116        }
 117
 118        void UpdateLerp(float deltaTime)
 119        {
 0120            if (Vector3.SqrMagnitude(currentPosition - targetPosition) < SPEED_EPSILON)
 121            {
 0122                UpdateRotation(deltaTime, targetRotation);
 0123                return;
 124            }
 125
 126            //NOTE(Brian): When we update movement we don't update rotation, because the Avatar will face the movement f
 0127            UpdateMovement(deltaTime);
 0128        }
 129
 0130        private void UpdateRotation(float deltaTime, Quaternion targetRotation) { currentRotation = Quaternion.Slerp(cur
 131
 132        private void UpdateMovement(float deltaTime)
 133        {
 0134            Vector3 flattenedDiff = targetPosition - currentPosition;
 0135            flattenedDiff.y = 0;
 136
 137            //NOTE(Brian): Avoid Unity error when computing look rotation for 0 magnitude vectors.
 138            //             Note that this isn't the same as the previous distance check because this
 139            //             is computed with a flattened vector.
 0140            if (flattenedDiff != Vector3.zero)
 141            {
 0142                Quaternion lookRotation = Quaternion.LookRotation(flattenedDiff, Vector3.up);
 0143                UpdateRotation(deltaTime, lookRotation);
 144            }
 145
 0146            Vector3 direction = (targetPosition - currentPosition).normalized;
 0147            Vector3 delta = direction * (movementSpeed * deltaTime);
 148
 149            //NOTE(Brian): We need a separate value for Y movement because the gravity has to be lerped faster.
 0150            delta.y = direction.y * SPEED_GRAVITY * deltaTime;
 151
 152            //NOTE(Brian): If we overshoot targetPosition we adjust the delta value accordingly.
 0153            if (delta.sqrMagnitude > Vector3.SqrMagnitude(targetPosition - currentPosition))
 154            {
 0155                delta = targetPosition - currentPosition;
 156            }
 157
 0158            currentPosition += delta;
 0159        }
 160
 161        void Update()
 162        {
 1163            if (avatarTransformValue == null)
 1164                return;
 165
 0166            movementLerpWaitCounter += Time.deltaTime;
 0167            if (movementLerpWaitCounter >= movementLerpWait)
 168            {
 0169                UpdateLerp(movementLerpWaitCounter);
 0170                movementLerpWaitCounter = 0f;
 171            }
 0172        }
 173
 0174        public void SetMovementLerpWait(float secondsBetweenUpdates) { movementLerpWait = secondsBetweenUpdates; }
 175    }
 176}