< Summary

Class:AvatarEditorHUDAudioHandler
Assembly:AvatarEditorHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/AvatarEditorHUD/Scripts/AvatarEditorHUDAudioHandler.cs
Covered lines:7
Uncovered lines:94
Coverable lines:101
Total lines:240
Line coverage:6.9% (7 of 101)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Start()0%220100%
OnSelectWearable(...)0%11903400%
OnEyeColorChanged(...)0%2100%
OnSkinColorChanged(...)0%2100%
OnHairColorChanged(...)0%2100%
OnClickRandomize()0%2100%
ResetLastClickedWearable()0%2100%
OnAvatarAppear(...)0%42600%
PlayRarity()0%12300%
PlayVoiceReaction(...)0%1101000%
OnSetAvatarEditorVisibility(...)0%42600%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/AvatarEditorHUD/Scripts/AvatarEditorHUDAudioHandler.cs

#LineLine coverage
 1using DCL;
 2using System.Collections;
 3using System.Collections.Generic;
 4using UnityEngine;
 5using UnityEngine.UI;
 6using Categories = WearableLiterals.Categories;
 7using Rarity = WearableLiterals.ItemRarity;
 8
 9public class AvatarEditorHUDAudioHandler : MonoBehaviour
 10{
 11    [SerializeField]
 12    AvatarEditorHUDView view;
 13
 14    [SerializeField]
 15    ItemSelector nftItemSelector;
 16
 17    [SerializeField]
 18    Button randomizeButton;
 19
 20    [SerializeField]
 21    AudioEvent eventMusic, eventRarity, eventAvatarAppear, eventReactionMale, eventReactionFemale, eventWearableClothing
 22               eventWearableFootwear, eventWearableHair, eventWearableHatMask, eventWearableRarity;
 23
 24    WearableItem lastSelectedWearable = null;
 25    bool hasClickedRandomize = false, wearableIsSameAsPrevious = false;
 26
 27    IEnumerator musicFadeOut;
 28
 29    private void Start()
 30    {
 131        int nPairs = view.wearableGridPairs.Length;
 3632        for (int i = 0; i < nPairs; i++)
 33        {
 1734            view.wearableGridPairs[i].selector.OnItemClicked += OnSelectWearable;
 35        }
 36
 137        nftItemSelector.OnItemClicked += OnSelectWearable;
 38
 139        view.OnSetVisibility += OnSetAvatarEditorVisibility;
 140        view.OnRandomize += OnClickRandomize;
 141    }
 42
 43    void OnSelectWearable(string wearableId)
 44    {
 045        CatalogController.wearableCatalog.TryGetValue(wearableId, out var wearable);
 046        wearableIsSameAsPrevious = (wearable == lastSelectedWearable);
 047        if (wearableIsSameAsPrevious)
 048            return;
 49
 050        lastSelectedWearable = wearable;
 051        if (wearable == null)
 052            return;
 53
 054        switch (wearable.data.category)
 55        {
 56            case Categories.EYEBROWS:
 057                eventWearableHair.Play(true);
 058                break;
 59            case "facial_hair":
 060                eventWearableHair.Play(true);
 061                break;
 62            case Categories.FEET:
 063                eventWearableFootwear.Play(true);
 064                break;
 65            case Categories.HAIR:
 066                eventWearableHair.Play(true);
 067                break;
 68            case Categories.LOWER_BODY:
 069                eventWearableClothing.Play(true);
 070                break;
 71            case Categories.UPPER_BODY:
 072                eventWearableClothing.Play(true);
 073                break;
 74            case "eyewear":
 075                eventWearableEyewear.Play(true);
 076                break;
 77            case "tiara":
 078                eventWearableJewelry.Play(true);
 079                break;
 80            case "earring":
 081                eventWearableJewelry.Play(true);
 082                break;
 83            case "hat":
 084                eventWearableHatMask.Play(true);
 085                break;
 86            case "top_head":
 087                eventWearableFootwear.Play(true);
 088                break;
 89            case "helmet":
 090                eventWearableFootwear.Play(true);
 091                break;
 92            case "mask":
 093                eventWearableHatMask.Play(true);
 94                break;
 95            default:
 96                break;
 97        }
 098    }
 99
 0100    void OnEyeColorChanged(Color color) { ResetLastClickedWearable(); }
 101
 0102    void OnSkinColorChanged(Color color) { ResetLastClickedWearable(); }
 103
 0104    void OnHairColorChanged(Color color) { ResetLastClickedWearable(); }
 105
 106    void OnClickRandomize()
 107    {
 0108        hasClickedRandomize = true;
 0109        ResetLastClickedWearable();
 0110    }
 111
 0112    void ResetLastClickedWearable() { lastSelectedWearable = null; }
 113
 114    void OnAvatarAppear(AvatarModel model)
 115    {
 0116        if (!view.isOpen)
 0117            return;
 118
 0119        eventAvatarAppear.Play(true);
 120
 0121        if (!wearableIsSameAsPrevious)
 122        {
 0123            if (lastSelectedWearable != null)
 0124                PlayRarity();
 125
 0126            if (lastSelectedWearable != null || hasClickedRandomize)
 0127                PlayVoiceReaction(model.bodyShape);
 128        }
 129
 0130        hasClickedRandomize = false;
 0131    }
 132
 133    void PlayRarity()
 134    {
 0135        if (lastSelectedWearable.rarity == null)
 0136            return;
 137
 138        /*switch (lastClickedWearable.rarity)
 139         {
 140             case Rarity.RARE:
 141                 eventRarity.SetIndex(0);
 142                 break;
 143             case Rarity.EPIC:
 144                 eventRarity.SetIndex(1);
 145                 break;
 146             case Rarity.LEGENDARY:
 147                 eventRarity.SetIndex(2);
 148                 break;
 149             case Rarity.MYTHIC:
 150                 eventRarity.SetIndex(3);
 151                 break;
 152             case Rarity.UNIQUE:
 153                 eventRarity.SetIndex(4);
 154                 break;
 155             default:
 156                 eventRarity.SetIndex(0);
 157                 break;
 158         }*/
 159
 0160        if (lastSelectedWearable.rarity == Rarity.UNIQUE)
 0161            eventRarity.SetIndex(1);
 162        else
 0163            eventRarity.SetIndex(0);
 164
 0165        eventRarity.Play(true);
 0166    }
 167
 168    void PlayVoiceReaction(string bodyShape)
 169    {
 0170        float chanceToPlay = 0.75f;
 0171        AudioEvent eventReaction = null;
 172
 0173        if (bodyShape.Contains("Female"))
 0174            eventReaction = eventReactionFemale;
 175        else
 0176            eventReaction = eventReactionMale;
 177
 0178        if (lastSelectedWearable != null)
 179        {
 0180            if (lastSelectedWearable.rarity == Rarity.EPIC
 181                || lastSelectedWearable.rarity == Rarity.LEGENDARY
 182                || lastSelectedWearable.rarity == Rarity.MYTHIC
 183                || lastSelectedWearable.rarity == Rarity.UNIQUE)
 184            {
 0185                eventReaction.RandomizeIndex(14, 28);
 0186                chanceToPlay = 1f;
 0187            }
 188            else
 189            {
 0190                eventReaction.RandomizeIndex(0, 14);
 191            }
 192        }
 193
 0194        if (eventReaction != null && Random.Range(0f, 1f) <= chanceToPlay)
 195        {
 0196            if (!eventReaction.source.isPlaying)
 197            {
 0198                eventReaction.PlayScheduled(0.6f);
 199            }
 200        }
 0201    }
 202
 203    void OnSetAvatarEditorVisibility(bool visible)
 204    {
 0205        AudioScriptableObjects.listItemAppear.ResetPitch();
 206
 0207        if (visible)
 208        {
 0209            if (DataStore.i.common.isSignUpFlow.Get())
 210            {
 0211                if (musicFadeOut != null)
 212                {
 0213                    StopCoroutine(musicFadeOut);
 0214                    StartCoroutine(eventMusic.FadeIn(1f));
 215                }
 216
 0217                if (!eventMusic.source.isPlaying)
 0218                    eventMusic.Play();
 219            }
 220
 0221            view.eyeColorSelector.OnColorChanged += OnEyeColorChanged;
 0222            view.skinColorSelector.OnColorChanged += OnSkinColorChanged;
 0223            view.hairColorSelector.OnColorChanged += OnHairColorChanged;
 0224            view.OnAvatarAppear += OnAvatarAppear;
 0225        }
 226        else
 227        {
 0228            if (DataStore.i.common.isSignUpFlow.Get())
 229            {
 0230                musicFadeOut = eventMusic.FadeOut(2f);
 0231                StartCoroutine(musicFadeOut);
 232            }
 233
 0234            view.eyeColorSelector.OnColorChanged -= OnEyeColorChanged;
 0235            view.skinColorSelector.OnColorChanged -= OnSkinColorChanged;
 0236            view.hairColorSelector.OnColorChanged -= OnHairColorChanged;
 0237            view.OnAvatarAppear -= OnAvatarAppear;
 238        }
 0239    }
 240}