< 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:71
Uncovered lines:16
Coverable lines:87
Total lines:185
Line coverage:81.6% (71 of 87)
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.035088.89%
SetSimpleAvatar()0%5.035088.89%
SetImpostor()0%5.035088.89%
SetInvisible()0%5.275077.78%
SetThrottling(...)0%330100%
SetNameVisible(...)0%20400%
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        void SetThrottling(int framesBetweenUpdates);
 17        void SetNameVisible(bool visible);
 18    }
 19
 20    public class AvatarLODController : IAvatarLODController
 21    {
 22        internal enum State
 23        {
 24            Invisible,
 25            FullAvatar,
 26            SimpleAvatar,
 27            Impostor,
 28        }
 29
 30        private const float TRANSITION_DURATION = 0.5f;
 31
 032        public Player player { get; }
 33
 34        internal float avatarFade;
 35        internal float impostorFade;
 36
 37        internal bool SSAOEnabled;
 38        internal bool facialFeaturesEnabled;
 39
 40        internal Coroutine currentTransition = null;
 41        internal State? lastRequestedState = null;
 42
 1543        public AvatarLODController(Player player)
 44        {
 1545            this.player = player;
 1546            avatarFade = 1;
 1547            impostorFade = 0;
 1548            SSAOEnabled = true;
 1549            facialFeaturesEnabled = true;
 1550            if (player?.renderer == null)
 051                return;
 1552            player.renderer.SetAvatarFade(avatarFade);
 1553            player.renderer.SetImpostorFade(impostorFade);
 1554        }
 55
 56        public void SetFullAvatar()
 57        {
 458            if (lastRequestedState == State.FullAvatar)
 059                return;
 60
 461            lastRequestedState = State.FullAvatar;
 462            if (player?.renderer == null)
 163                return;
 64
 365            player.onPointerDownCollider.SetColliderEnabled(true);
 66
 367            SetAvatarFeatures(true, true);
 368            StartTransition(1, 0);
 369        }
 70
 71        public void SetSimpleAvatar()
 72        {
 373            if (lastRequestedState == State.SimpleAvatar)
 074                return;
 75
 376            lastRequestedState = State.SimpleAvatar;
 377            if (player?.renderer == null)
 178                return;
 79
 280            player.onPointerDownCollider.SetColliderEnabled(true);
 81
 282            SetAvatarFeatures(false, false);
 283            StartTransition(1, 0);
 284        }
 85
 86        public void SetImpostor()
 87        {
 388            if (lastRequestedState == State.Impostor)
 089                return;
 90
 391            lastRequestedState = State.Impostor;
 392            if (player?.renderer == null)
 193                return;
 94
 295            player.onPointerDownCollider.SetColliderEnabled(false);
 96
 297            SetAvatarFeatures(false, false);
 298            StartTransition(0, 1);
 299        }
 100
 101        public void SetInvisible()
 102        {
 1103            if (lastRequestedState == State.Invisible)
 0104                return;
 105
 1106            lastRequestedState = State.Invisible;
 1107            if (player?.renderer == null)
 0108                return;
 109
 1110            player.onPointerDownCollider.SetColliderEnabled(false);
 111
 1112            SetAvatarFeatures(false, false);
 1113            StartTransition(0, 0, TRANSITION_DURATION * 1.5f);
 1114        }
 115
 6116        public void SetThrottling(int framesBetweenUpdates) { player?.renderer?.SetThrottling(framesBetweenUpdates); }
 117        public void SetNameVisible(bool visible)
 118        {
 0119            if (visible)
 0120                player?.playerName.Show();
 121            else
 0122                player?.playerName.Hide();
 0123        }
 124
 125        private void StartTransition(float newTargetAvatarFade, float newTargetImpostorFade, float transitionDuration = 
 126        {
 8127            CoroutineStarter.Stop(currentTransition);
 8128            currentTransition = CoroutineStarter.Start(Transition(newTargetAvatarFade, newTargetImpostorFade, transition
 8129        }
 130
 131        internal IEnumerator Transition(float targetAvatarFade, float targetImpostorFade, float transitionDuration = TRA
 132        {
 9133            while (!player.renderer.isReady)
 134            {
 0135                yield return null;
 136            }
 137
 9138            player.renderer.SetAvatarFade(avatarFade);
 9139            player.renderer.SetImpostorFade(impostorFade);
 9140            player.renderer.SetRendererEnabled(true);
 9141            player.renderer.SetImpostorVisibility(true);
 142
 565143            while (!Mathf.Approximately(avatarFade, targetAvatarFade) || !Mathf.Approximately(impostorFade, targetImpost
 144            {
 556145                avatarFade = Mathf.MoveTowards(avatarFade, targetAvatarFade, (1f / transitionDuration) * Time.deltaTime)
 556146                impostorFade = Mathf.MoveTowards(impostorFade, targetImpostorFade, (1f / transitionDuration) * Time.delt
 556147                player.renderer.SetAvatarFade(avatarFade);
 556148                player.renderer.SetImpostorFade(impostorFade);
 556149                yield return null;
 150            }
 151
 9152            avatarFade = targetAvatarFade;
 9153            impostorFade = targetImpostorFade;
 154
 9155            bool avatarVisibility = !Mathf.Approximately(avatarFade, 0);
 9156            player.renderer.SetRendererEnabled(avatarVisibility);
 9157            bool impostorVisibility = !Mathf.Approximately(impostorFade, 0);
 9158            player.renderer.SetImpostorVisibility(impostorVisibility);
 9159            currentTransition = null;
 9160        }
 161
 162        private void SetAvatarFeatures(bool newSSAOEnabled, bool newFacialFeaturesEnabled)
 163        {
 8164            if (SSAOEnabled != newSSAOEnabled)
 165            {
 7166                player.renderer.SetSSAOEnabled(newSSAOEnabled);
 7167                SSAOEnabled = newSSAOEnabled;
 168            }
 169
 8170            if (facialFeaturesEnabled != newFacialFeaturesEnabled)
 171            {
 7172                player.renderer.SetFacialFeaturesVisible(newFacialFeaturesEnabled);
 7173                facialFeaturesEnabled = newFacialFeaturesEnabled;
 174            }
 8175        }
 176
 0177        public void UpdateImpostorTint(float distanceToMainPlayer) { player.renderer.SetImpostorColor(AvatarRendererHelp
 178
 179        public void Dispose()
 180        {
 0181            lastRequestedState = null;
 0182            CoroutineStarter.Stop(currentTransition);
 0183        }
 184    }
 185}