< 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:94
Coverable lines:109
Total lines:287
Line coverage:13.7% (15 of 109)
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
 107757    [SerializeField] float runMinSpeed = 6f;
 107758    [SerializeField] float walkMinSpeed = 0.1f;
 59
 60    internal System.Action<BlackBoard> currentState;
 61
 62    Vector3 lastPosition;
 63    AvatarAnimationsVariable currentAnimations;
 64    bool isOwnPlayer = false;
 65
 25066    public void Start() { OnPoolGet(); }
 67
 68    public void OnPoolGet()
 69    {
 12570        if (DCLCharacterController.i != null)
 71        {
 12572            isOwnPlayer = DCLCharacterController.i.transform == transform.parent;
 73
 74            // NOTE: disable MonoBehaviour's update to use DCLCharacterController event instead
 12575            this.enabled = !isOwnPlayer;
 76
 12577            if (isOwnPlayer)
 78            {
 12379                DCLCharacterController.i.OnUpdateFinish += Update;
 80            }
 81        }
 82
 12583        currentState = State_Init;
 12584    }
 85
 86    public void OnPoolRelease()
 87    {
 088        if (isOwnPlayer && DCLCharacterController.i)
 89        {
 090            DCLCharacterController.i.OnUpdateFinish -= Update;
 91        }
 092    }
 93
 494    void Update() { Update(Time.deltaTime); }
 95
 96    void Update(float deltaTime)
 97    {
 2050798        if (target == null || animation == null)
 2050799            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);
 0217        }
 218        else
 219        {
 0220            animation.Blend(bb.expressionTriggerId, 1, EXPRESSION_TRANSITION_TIME / 2f);
 221        }
 0222    }
 223
 224    public void SetExpressionValues(string expressionTriggerId, long expressionTriggerTimestamp)
 225    {
 2226        if (animation == null)
 2227            return;
 228
 0229        if (string.IsNullOrEmpty(expressionTriggerId))
 0230            return;
 231
 0232        var mustTriggerAnimation = !string.IsNullOrEmpty(expressionTriggerId) && blackboard.expressionTriggerTimestamp !
 233
 0234        blackboard.expressionTriggerId = expressionTriggerId;
 0235        blackboard.expressionTriggerTimestamp = expressionTriggerTimestamp;
 236
 0237        if (mustTriggerAnimation)
 238        {
 0239            if (!string.IsNullOrEmpty(expressionTriggerId))
 240            {
 0241                animation.Stop(expressionTriggerId);
 242            }
 243
 0244            currentState = State_Expression;
 0245            Update();
 246        }
 0247    }
 248
 249    public void Reset()
 250    {
 0251        if (animation == null)
 0252            return;
 253
 254        //It will set the animation to the first frame, but due to the nature of the script and its Update. It wont stop
 0255        animation.Stop();
 0256    }
 257
 0258    public void SetIdleFrame() { animation.Play(baseClipsIds.idle); }
 259
 260    public void BindBodyShape(Animation animation, string bodyShapeType, Transform target)
 261    {
 0262        this.target = target;
 0263        this.animation = animation;
 264
 0265        if (bodyShapeType.Contains(WearableLiterals.BodyShapes.MALE))
 266        {
 0267            currentAnimations = maleAnimations;
 0268        }
 0269        else if (bodyShapeType.Contains(WearableLiterals.BodyShapes.FEMALE))
 270        {
 0271            currentAnimations = femaleAnimations;
 272        }
 273
 0274        for (var i = 0; i < currentAnimations.Get().Length; i++)
 275        {
 0276            var animationToId = currentAnimations.Get()[i];
 0277            if (this.animation.GetClip(animationToId.id) == null)
 278            {
 279                // animationToId.id and animationToId.clip.name must be the same or we get big performance drop here
 280                // Already coordinated with art team to have the animations with the correct ids
 0281                this.animation.AddClip(animationToId.clip, animationToId.id);
 282            }
 283        }
 284
 0285        SetIdleFrame();
 0286    }
 287}