< Summary

Class:AvatarModifierArea
Assembly:AvatarShape
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Avatar/AvatarModifiers/AvatarModifierArea.cs
Covered lines:55
Uncovered lines:5
Coverable lines:60
Total lines:159
Line coverage:91.6% (55 of 60)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
GetDataFromJSON(...)0%110100%
AvatarModifierArea()0%110100%
ApplyChanges(...)0%110100%
OnDestroy()0%3.023087.5%
Update()0%13.213089.47%
AreSetEquals(...)0%5.25080%
DetectAllAvatarsInArea()0%550100%
RemoveAllModifiers()0%110100%
RemoveAllModifiers(...)0%770100%
ApplyCurrentModel()0%440100%
GetClassId()0%2100%

File(s)

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

#LineLine coverage
 1using System;
 2using DCL;
 3using DCL.Components;
 4using System.Collections;
 5using System.Collections.Generic;
 6using System.Linq;
 7using DCL.Helpers;
 8using UnityEngine;
 9using DCL.Models;
 10
 11public class AvatarModifierArea : BaseComponent
 12{
 13    [Serializable]
 14    public class Model : BaseModel
 15    {
 16        // TODO: Change to TriggerArea and handle deserialization with subclasses
 17        public BoxTriggerArea area;
 18        public string[] modifiers;
 19
 1020        public override BaseModel GetDataFromJSON(string json) { return Utils.SafeFromJson<Model>(json); }
 21    }
 22
 623    private Model cachedModel = new Model();
 24
 625    private HashSet<GameObject> avatarsInArea = new HashSet<GameObject>();
 26    private event Action<GameObject> OnAvatarEnter;
 27    private event Action<GameObject> OnAvatarExit;
 28    internal readonly Dictionary<string, AvatarModifier> modifiers;
 29
 630    public AvatarModifierArea()
 31    {
 32        // Configure all available modifiers
 633        this.modifiers = new Dictionary<string, AvatarModifier>()
 34        {
 35            { "HIDE_AVATARS", new HideAvatarsModifier() },
 36            { "DISABLE_PASSPORTS", new DisablePassportModifier() }
 37        };
 638        model = new Model();
 639    }
 40
 41    public override IEnumerator ApplyChanges(BaseModel newModel)
 42    {
 43
 44        // Clean up
 1045        RemoveAllModifiers();
 1046        OnAvatarEnter = null;
 1047        OnAvatarExit = null;
 48
 1049        ApplyCurrentModel();
 50
 1051        return null;
 52    }
 53
 54    private void OnDestroy()
 55    {
 556        var toRemove = new HashSet<GameObject>();
 557        if (avatarsInArea != null)
 558            toRemove.UnionWith(avatarsInArea);
 59
 560        var currentInArea = DetectAllAvatarsInArea();
 561        if (currentInArea != null)
 062            toRemove.UnionWith(currentInArea);
 63
 564        RemoveAllModifiers(toRemove);
 565    }
 66
 67    private void Update()
 68    {
 3169        if (cachedModel?.area == null)
 70        {
 071            return;
 72        }
 73
 74        // Find avatars currently on the area
 3175        HashSet<GameObject> newAvatarsInArea = DetectAllAvatarsInArea();
 3176        if (AreSetEquals(avatarsInArea, newAvatarsInArea))
 177            return;
 78
 3079        if (avatarsInArea == null)
 080            avatarsInArea = new HashSet<GameObject>();
 81
 3082        if (newAvatarsInArea == null)
 2683            newAvatarsInArea = new HashSet<GameObject>();
 84
 85        // Call event for avatars that just entered the area
 6886        foreach (GameObject avatarThatEntered in newAvatarsInArea.Except(avatarsInArea))
 87        {
 488            OnAvatarEnter?.Invoke(avatarThatEntered);
 89        }
 90
 91        // Call events for avatars that just exited the area
 6492        foreach (GameObject avatarThatExited in avatarsInArea.Except(newAvatarsInArea))
 93        {
 294            OnAvatarExit?.Invoke(avatarThatExited);
 95        }
 96
 3097        avatarsInArea = newAvatarsInArea;
 3098    }
 99
 100    private bool AreSetEquals(HashSet<GameObject> set1, HashSet<GameObject> set2)
 101    {
 31102        if (set1 == null && set2 == null)
 0103            return true;
 104
 31105        if (set1 == null || set2 == null)
 26106            return false;
 107
 5108        return set1.SetEquals(set2);
 109    }
 110
 111    private HashSet<GameObject> DetectAllAvatarsInArea()
 112    {
 46113        if (entity?.gameObject == null)
 114        {
 4115            return null;
 116        }
 117
 42118        Vector3 center = entity.gameObject.transform.position;
 42119        Quaternion rotation = entity.gameObject.transform.rotation;
 42120        return cachedModel.area?.DetectAvatars(center, rotation);
 121    }
 122
 20123    private void RemoveAllModifiers() { RemoveAllModifiers(DetectAllAvatarsInArea()); }
 124
 125    private void RemoveAllModifiers(HashSet<GameObject> avatars)
 126    {
 15127        if (cachedModel?.area == null)
 128        {
 5129            return;
 130        }
 131
 10132        if (avatars != null)
 133        {
 14134            foreach (GameObject avatar in avatars)
 135            {
 2136                OnAvatarExit?.Invoke(avatar);
 137            }
 138        }
 10139    }
 140
 141    private void ApplyCurrentModel()
 142    {
 10143        cachedModel = (Model)this.model;
 10144        if (cachedModel.modifiers != null)
 145        {
 146            // Add all listeners
 30147            foreach (string modifierKey in cachedModel.modifiers)
 148            {
 5149                if (!modifiers.TryGetValue(modifierKey, out AvatarModifier modifier))
 150                    continue;
 151
 5152                OnAvatarEnter += modifier.ApplyModifier;
 5153                OnAvatarExit += modifier.RemoveModifier;
 154            }
 155        }
 10156    }
 157
 0158    public override int GetClassId() { return (int) CLASS_ID_COMPONENT.AVATAR_MODIFIER_AREA; }
 159}