< Summary

Class:AvatarAnimatorLegacy
Assembly:AvatarShape
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Avatar/AvatarAnimatorLegacy.cs
Covered lines:15
Uncovered lines:92
Coverable lines:107
Total lines:283
Line coverage:14% (15 of 107)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
AvatarAnimatorLegacy()0%110100%
Start()0%110100%
OnPoolGet()0%330100%
OnPoolRelease()0%12300%
Update()0%110100%
Update(...)0%8.744033.33%
UpdateInterface()0%12300%
State_Init(...)0%6200%
State_Ground(...)0%30500%
State_Air(...)0%12300%
State_Expression(...)0%30500%
SetExpressionValues(...)0%27.816015.38%
Reset()0%6200%
SetIdleFrame()0%2100%
BindBodyShape(...)0%30500%

File(s)

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

#LineLine coverage
 1using System;
 2using DCL.Components;
 3using UnityEngine;
 4
 5public class AvatarAnimatorLegacy : MonoBehaviour, IPoolLifecycleHandler
 6{
 7    const float IDLE_TRANSITION_TIME = 0.2f;
 8    const float STRAFE_TRANSITION_TIME = 0.25f;
 9    const float RUN_TRANSITION_TIME = 0.15f;
 10    const float WALK_TRANSITION_TIME = 0.15f;
 11    const float JUMP_TRANSITION_TIME = 0.01f;
 12    const float FALL_TRANSITION_TIME = 0.5f;
 13    const float EXPRESSION_TRANSITION_TIME = 0.2f;
 14
 15    const float AIR_EXIT_TRANSITION_TIME = 0.2f;
 16    const float GROUND_BLENDTREE_TRANSITION_TIME = 0.15f;
 17
 18    const float RUN_SPEED_THRESHOLD = 0.05f;
 19    const float WALK_SPEED_THRESHOLD = 0.03f;
 20
 21    const float ELEVATION_OFFSET = 0.6f;
 22    const float RAY_OFFSET_LENGTH = 3.0f;
 23
 24    const float MAX_VELOCITY = 6.25f;
 25
 26    [System.Serializable]
 27    public class BaseClipsIds
 28    {
 29        public string idle;
 30        public string walk;
 31        public string run;
 32        public string jump;
 33        public string fall;
 34    }
 35
 36    [System.Serializable]
 37    public class BlackBoard
 38    {
 39        public float walkSpeedFactor;
 40        public float runSpeedFactor;
 41        public float movementSpeed;
 42        public float verticalSpeed;
 43        public bool isGrounded;
 44        public string expressionTriggerId;
 45        public long expressionTriggerTimestamp;
 46        public float deltaTime;
 47    }
 48
 49    [SerializeField] internal AvatarAnimationsVariable maleAnimations;
 50    [SerializeField] internal AvatarAnimationsVariable femaleAnimations;
 51
 52    public new Animation animation;
 53    public BaseClipsIds baseClipsIds;
 54    public BlackBoard blackboard;
 55    public Transform target;
 56
 107057    [SerializeField] float runMinSpeed = 6f;
 107058    [SerializeField] float walkMinSpeed = 0.1f;
 59
 60    internal System.Action<BlackBoard> currentState;
 61
 62    Vector3 lastPosition;
 63    AvatarAnimationsVariable currentAnimations;
 64    bool isOwnPlayer = false;
 65
 24866    public void Start() { OnPoolGet(); }
 67
 68    public void OnPoolGet()
 69    {
 12470        if (DCLCharacterController.i != null)
 71        {
 12472            isOwnPlayer = DCLCharacterController.i.transform == transform.parent;
 73
 74            // NOTE: disable MonoBehaviour's update to use DCLCharacterController event instead
 12475            this.enabled = !isOwnPlayer;
 76
 12477            if (isOwnPlayer)
 78            {
 12379                DCLCharacterController.i.OnUpdateFinish += Update;
 80            }
 81        }
 82
 12483        currentState = State_Init;
 12484    }
 85
 86    public void OnPoolRelease()
 87    {
 088        if (isOwnPlayer && DCLCharacterController.i)
 89        {
 090            DCLCharacterController.i.OnUpdateFinish -= Update;
 91        }
 092    }
 93
 294    void Update() { Update(Time.deltaTime); }
 95
 96    void Update(float deltaTime)
 97    {
 1810398        if (target == null || animation == null)
 1810399            return;
 100
 0101        blackboard.deltaTime = deltaTime;
 0102        UpdateInterface();
 0103        currentState?.Invoke(blackboard);
 0104    }
 105
 106    void UpdateInterface()
 107    {
 0108        Vector3 velocityTargetPosition = target.position;
 0109        Vector3 flattenedVelocity = velocityTargetPosition - lastPosition;
 110
 111        //NOTE(Brian): Vertical speed
 0112        float verticalVelocity = flattenedVelocity.y;
 0113        blackboard.verticalSpeed = verticalVelocity;
 114
 0115        flattenedVelocity.y = 0;
 116
 0117        if (isOwnPlayer)
 0118            blackboard.movementSpeed = flattenedVelocity.magnitude - DCLCharacterController.i.movingPlatformSpeed;
 119        else
 0120            blackboard.movementSpeed = flattenedVelocity.magnitude;
 121
 0122        Vector3 rayOffset = Vector3.up * RAY_OFFSET_LENGTH;
 123        //NOTE(Brian): isGrounded?
 0124        blackboard.isGrounded = Physics.Raycast(target.transform.position + rayOffset,
 125            Vector3.down,
 126            RAY_OFFSET_LENGTH - ELEVATION_OFFSET,
 127            DCLCharacterController.i.groundLayers);
 128
 129#if UNITY_EDITOR
 0130        Debug.DrawRay(target.transform.position + rayOffset, Vector3.down * (RAY_OFFSET_LENGTH - ELEVATION_OFFSET), blac
 131#endif
 132
 0133        lastPosition = velocityTargetPosition;
 0134    }
 135
 136    void State_Init(BlackBoard bb)
 137    {
 0138        if (bb.isGrounded == true)
 139        {
 0140            currentState = State_Ground;
 0141        }
 142        else
 143        {
 0144            currentState = State_Air;
 145        }
 0146    }
 147
 148    void State_Ground(BlackBoard bb)
 149    {
 0150        if (bb.deltaTime <= 0)
 151        {
 0152            Debug.LogError("deltaTime should be > 0", gameObject);
 0153            return;
 154        }
 155
 0156        animation[baseClipsIds.run].normalizedSpeed = bb.movementSpeed / bb.deltaTime * bb.runSpeedFactor;
 0157        animation[baseClipsIds.walk].normalizedSpeed = bb.movementSpeed / bb.deltaTime * bb.walkSpeedFactor;
 158
 0159        float movementSpeed = bb.movementSpeed / bb.deltaTime;
 160
 0161        if (movementSpeed > runMinSpeed)
 162        {
 0163            animation.CrossFade(baseClipsIds.run, RUN_TRANSITION_TIME);
 0164        }
 0165        else if (movementSpeed > walkMinSpeed)
 166        {
 0167            animation.CrossFade(baseClipsIds.walk, WALK_TRANSITION_TIME);
 0168        }
 169        else
 170        {
 0171            animation.CrossFade(baseClipsIds.idle, IDLE_TRANSITION_TIME);
 172        }
 173
 0174        if (!bb.isGrounded)
 175        {
 0176            currentState = State_Air;
 0177            Update(bb.deltaTime);
 178        }
 0179    }
 180
 181    void State_Air(BlackBoard bb)
 182    {
 0183        if (bb.verticalSpeed > 0)
 184        {
 0185            animation.CrossFade(baseClipsIds.jump, JUMP_TRANSITION_TIME, PlayMode.StopAll);
 0186        }
 187        else
 188        {
 0189            animation.CrossFade(baseClipsIds.fall, FALL_TRANSITION_TIME, PlayMode.StopAll);
 190        }
 191
 0192        if (bb.isGrounded)
 193        {
 0194            animation.Blend(baseClipsIds.jump, 0, AIR_EXIT_TRANSITION_TIME);
 0195            animation.Blend(baseClipsIds.fall, 0, AIR_EXIT_TRANSITION_TIME);
 0196            currentState = State_Ground;
 0197            Update(bb.deltaTime);
 198        }
 0199    }
 200
 201    internal void State_Expression(BlackBoard bb)
 202    {
 0203        var animationInfo = animation[bb.expressionTriggerId];
 0204        animation.CrossFade(bb.expressionTriggerId, EXPRESSION_TRANSITION_TIME, PlayMode.StopAll);
 205
 0206        var mustExit = Math.Abs(bb.movementSpeed) > Mathf.Epsilon || animationInfo.length - animationInfo.time < EXPRESS
 0207        if (mustExit)
 208        {
 0209            animation.Blend(bb.expressionTriggerId, 0, EXPRESSION_TRANSITION_TIME);
 0210            bb.expressionTriggerId = null;
 0211            if (!bb.isGrounded)
 0212                currentState = State_Air;
 213            else
 0214                currentState = State_Ground;
 215
 0216            Update(bb.deltaTime);
 217        }
 0218    }
 219
 220    public void SetExpressionValues(string expressionTriggerId, long expressionTriggerTimestamp)
 221    {
 2222        if (animation == null)
 2223            return;
 224
 0225        if (string.IsNullOrEmpty(expressionTriggerId))
 0226            return;
 227
 0228        var mustTriggerAnimation = !string.IsNullOrEmpty(expressionTriggerId) && blackboard.expressionTriggerTimestamp !
 229
 0230        blackboard.expressionTriggerId = expressionTriggerId;
 0231        blackboard.expressionTriggerTimestamp = expressionTriggerTimestamp;
 232
 0233        if (mustTriggerAnimation)
 234        {
 0235            if (!string.IsNullOrEmpty(expressionTriggerId))
 236            {
 0237                animation.Stop(expressionTriggerId);
 238            }
 239
 0240            currentState = State_Expression;
 0241            Update();
 242        }
 0243    }
 244
 245    public void Reset()
 246    {
 0247        if (animation == null)
 0248            return;
 249
 250        //It will set the animation to the first frame, but due to the nature of the script and its Update. It wont stop
 0251        animation.Stop();
 0252    }
 253
 0254    public void SetIdleFrame() { animation.Play(baseClipsIds.idle); }
 255
 256    public void BindBodyShape(Animation animation, string bodyShapeType, Transform target)
 257    {
 0258        this.target = target;
 0259        this.animation = animation;
 260
 0261        if (bodyShapeType.Contains(WearableLiterals.BodyShapes.MALE))
 262        {
 0263            currentAnimations = maleAnimations;
 0264        }
 0265        else if (bodyShapeType.Contains(WearableLiterals.BodyShapes.FEMALE))
 266        {
 0267            currentAnimations = femaleAnimations;
 268        }
 269
 0270        for (var i = 0; i < currentAnimations.Get().Length; i++)
 271        {
 0272            var animationToId = currentAnimations.Get()[i];
 0273            if (this.animation.GetClip(animationToId.id) == null)
 274            {
 275                // animationToId.id and animationToId.clip.name must be the same or we get big performance drop here
 276                // Already coordinated with art team to have the animations with the correct ids
 0277                this.animation.AddClip(animationToId.clip, animationToId.id);
 278            }
 279        }
 280
 0281        SetIdleFrame();
 0282    }
 283}