< Summary

Class:DCL.AvatarLODController
Assembly:AvatarLODController
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Avatar/AvatarLOD/AvatarLODController/AvatarLODController.cs
Covered lines:60
Uncovered lines:18
Coverable lines:78
Total lines:166
Line coverage:76.9% (60 of 78)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
AvatarLODController(...)0%4.014090.91%
SetFullAvatar()0%5.055087.5%
SetSimpleAvatar()0%5.055087.5%
SetImpostor()0%5.055087.5%
SetInvisible()0%30500%
StartTransition(...)0%110100%
Transition()0%7.017095%
SetAvatarFeatures(...)0%330100%
UpdateImpostorTint(...)0%2100%
Dispose()0%2100%

File(s)

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

#LineLine coverage
 1using System;
 2using System.Collections;
 3using UnityEngine;
 4
 5namespace DCL
 6{
 7    //TODO Rename to IAvatar (not now to avoid conflicts)
 8    public interface IAvatarLODController : IDisposable
 9    {
 10        Player player { get; }
 11        void SetFullAvatar();
 12        void SetSimpleAvatar();
 13        void SetImpostor();
 14        void SetInvisible();
 15        void UpdateImpostorTint(float distanceToMainPlayer);
 16    }
 17
 18    public class AvatarLODController : IAvatarLODController
 19    {
 20        internal enum State
 21        {
 22            Invisible,
 23            FullAvatar,
 24            SimpleAvatar,
 25            Impostor,
 26        }
 27
 28        private const float TRANSITION_DURATION = 0.5f;
 29
 030        public Player player { get; }
 31
 32        internal float avatarFade;
 33        internal float impostorFade;
 34
 35        internal bool SSAOEnabled;
 36        internal bool facialFeaturesEnabled;
 37
 38        internal Coroutine currentTransition = null;
 39        internal State? lastRequestedState = null;
 40
 1141        public AvatarLODController(Player player)
 42        {
 1143            this.player = player;
 1144            avatarFade = 1;
 1145            impostorFade = 0;
 1146            SSAOEnabled = true;
 1147            facialFeaturesEnabled = true;
 1148            if (player?.renderer == null)
 049                return;
 1150            player.renderer.SetAvatarFade(avatarFade);
 1151            player.renderer.SetImpostorFade(impostorFade);
 1152        }
 53
 54        public void SetFullAvatar()
 55        {
 356            if (lastRequestedState == State.FullAvatar)
 057                return;
 58
 359            lastRequestedState = State.FullAvatar;
 360            if (player?.renderer == null)
 161                return;
 62
 263            SetAvatarFeatures(true, true);
 264            StartTransition(1, 0);
 265        }
 66
 67        public void SetSimpleAvatar()
 68        {
 369            if (lastRequestedState == State.SimpleAvatar)
 070                return;
 71
 372            lastRequestedState = State.SimpleAvatar;
 373            if (player?.renderer == null)
 174                return;
 75
 276            SetAvatarFeatures(false, false);
 277            StartTransition(1, 0);
 278        }
 79
 80        public void SetImpostor()
 81        {
 382            if (lastRequestedState == State.Impostor)
 083                return;
 84
 385            lastRequestedState = State.Impostor;
 386            if (player?.renderer == null)
 187                return;
 88
 289            SetAvatarFeatures(false, false);
 290            StartTransition(0, 1);
 291        }
 92
 93        public void SetInvisible()
 94        {
 095            if (lastRequestedState == State.Invisible)
 096                return;
 97
 098            lastRequestedState = State.Invisible;
 099            if (player?.renderer == null)
 0100                return;
 101
 0102            SetAvatarFeatures(false, false);
 0103            StartTransition(0, 0);
 0104        }
 105
 106        private void StartTransition(float newTargetAvatarFade, float newTargetImpostorFade)
 107        {
 6108            CoroutineStarter.Stop(currentTransition);
 6109            currentTransition = CoroutineStarter.Start(Transition(newTargetAvatarFade, newTargetImpostorFade));
 6110        }
 111
 112        internal IEnumerator Transition(float targetAvatarFade, float targetImpostorFade, float transitionDuration = TRA
 113        {
 7114            while (!player.renderer.isReady)
 115            {
 0116                yield return null;
 117            }
 118
 7119            player.renderer.SetAvatarFade(avatarFade);
 7120            player.renderer.SetImpostorFade(impostorFade);
 7121            player.renderer.SetRendererEnabled(true);
 7122            player.renderer.SetImpostorVisibility(true);
 123
 305124            while (!Mathf.Approximately(avatarFade, targetAvatarFade) || !Mathf.Approximately(impostorFade, targetImpost
 125            {
 298126                avatarFade = Mathf.MoveTowards(avatarFade, targetAvatarFade, (1f / transitionDuration) * Time.deltaTime)
 298127                impostorFade = Mathf.MoveTowards(impostorFade, targetImpostorFade, (1f / transitionDuration) * Time.delt
 298128                player.renderer.SetAvatarFade(avatarFade);
 298129                player.renderer.SetImpostorFade(impostorFade);
 298130                yield return null;
 131            }
 132
 7133            avatarFade = targetAvatarFade;
 7134            impostorFade = targetImpostorFade;
 135
 7136            bool avatarVisibility = !Mathf.Approximately(avatarFade, 0);
 7137            player.renderer.SetRendererEnabled(avatarVisibility);
 7138            bool impostorVisibility = !Mathf.Approximately(impostorFade, 0);
 7139            player.renderer.SetImpostorVisibility(impostorVisibility);
 7140            currentTransition = null;
 7141        }
 142
 143        private void SetAvatarFeatures(bool newSSAOEnabled, bool newFacialFeaturesEnabled)
 144        {
 6145            if (SSAOEnabled != newSSAOEnabled)
 146            {
 5147                player.renderer.SetSSAOEnabled(newSSAOEnabled);
 5148                SSAOEnabled = newSSAOEnabled;
 149            }
 150
 6151            if (facialFeaturesEnabled != newFacialFeaturesEnabled)
 152            {
 5153                player.renderer.SetFacialFeaturesVisible(newFacialFeaturesEnabled);
 5154                facialFeaturesEnabled = newFacialFeaturesEnabled;
 155            }
 6156        }
 157
 0158        public void UpdateImpostorTint(float distanceToMainPlayer) { player.renderer.SetImpostorColor(AvatarRendererHelp
 159
 160        public void Dispose()
 161        {
 0162            lastRequestedState = null;
 0163            CoroutineStarter.Stop(currentTransition);
 0164        }
 165    }
 166}