< Summary

Class:BIWFloorHandler
Assembly:BuilderInWorld
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/BuilderMode/Controllers/BIWFloorHandler.cs
Covered lines:73
Uncovered lines:5
Coverable lines:78
Total lines:185
Line coverage:93.5% (73 of 78)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
BIWFloorHandler()0%110100%
Init(...)0%110100%
Dispose()0%110100%
OnFloorEntityDeleted(...)0%220100%
Clean()0%110100%
ExistsFloorPlaceHolderForEntity(...)0%110100%
ChangeFloor(...)0%22090.91%
FindCurrentFloorCatalogItem()0%3.033085.71%
IsCatalogItemFloor(...)0%2100%
CreateDefaultFloor()0%110100%
CreateFloor(...)0%44094.44%
RemovePlaceHolder(...)0%110100%
OnFloorLoaded(...)0%4.054085.71%
RemovePlaceHolder(...)0%220100%
RemoveAllPlaceHolders()0%220100%
ExitEditMode()0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/BuilderMode/Controllers/BIWFloorHandler.cs

#LineLine coverage
 1using System;
 2using DCL;
 3using DCL.Configuration;
 4using DCL.Controllers;
 5using DCL.Models;
 6using System.Collections;
 7using System.Collections.Generic;
 8using System.Linq;
 9using UnityEngine;
 10
 11public interface IBIWFloorHandler
 12{
 13    void CreateDefaultFloor();
 14    void CreateFloor(CatalogItem floorSceneObject);
 15    bool IsCatalogItemFloor(CatalogItem floorSceneObject);
 16    void ChangeFloor(CatalogItem newFloorObject);
 17}
 18
 19public class BIWFloorHandler : BIWController, IBIWFloorHandler
 20{
 21    public event Action OnAllParcelsFloorLoaded;
 22
 23    private IBIWActionController actionController;
 24
 25    private IBIWEntityHandler entityHandler;
 26    private IBIWCreatorController creatorController;
 27    private IBIWSaveController saveController;
 28
 29    private GameObject floorPrefab;
 30
 31    private int numberOfParcelsLoaded;
 32
 33    private CatalogItem lastFloorCalalogItemUsed;
 3334    internal readonly Dictionary<string, GameObject> floorPlaceHolderDict = new Dictionary<string, GameObject>();
 3335    private readonly List<string> loadedFloorEntities = new List<string>();
 36    private Camera mainCamera;
 37
 38    public override void Init(BIWContext context)
 39    {
 3340        base.Init(context);
 3341        actionController = context.actionController;
 42
 3343        entityHandler = context.entityHandler;
 44
 3345        creatorController = context.creatorController;
 3346        saveController = context.saveController;
 3347        mainCamera = context.sceneReferences.mainCamera;
 48
 3349        floorPrefab = context.projectReferencesAsset.floorPlaceHolderPrefab;
 50
 3351        entityHandler.OnEntityDeleted += OnFloorEntityDeleted;
 3352    }
 53
 54    public override void Dispose()
 55    {
 3456        entityHandler.OnEntityDeleted -= OnFloorEntityDeleted;
 3457        Clean();
 3458    }
 59
 60    private void OnFloorEntityDeleted(BIWEntity entity)
 61    {
 662        if (entity.isFloor)
 463            RemovePlaceHolder(entity);
 664    }
 65
 12266    public void Clean() { RemoveAllPlaceHolders(); }
 67
 268    public bool ExistsFloorPlaceHolderForEntity(string entityId) { return floorPlaceHolderDict.ContainsKey(entityId); }
 69
 70    public void ChangeFloor(CatalogItem newFloorObject)
 71    {
 272        saveController.SetSaveActivation(false);
 273        CatalogItem lastFloor = lastFloorCalalogItemUsed;
 274        if (lastFloor == null)
 075            lastFloor = FindCurrentFloorCatalogItem();
 76
 277        entityHandler.DeleteFloorEntities();
 78
 279        CreateFloor(newFloorObject);
 80
 281        BIWCompleteAction buildAction = new BIWCompleteAction();
 82
 283        buildAction.CreateChangeFloorAction(lastFloor, newFloorObject);
 284        actionController.AddAction(buildAction);
 85
 286        saveController.SetSaveActivation(true, true);
 287    }
 88
 89    public CatalogItem FindCurrentFloorCatalogItem()
 90    {
 391        foreach (BIWEntity entity in entityHandler.GetAllEntitiesFromCurrentScene())
 92        {
 193            if (entity.isFloor)
 94            {
 195                return entity.GetCatalogItemAssociated();
 96            }
 97        }
 98
 099        return null;
 1100    }
 101
 0102    public bool IsCatalogItemFloor(CatalogItem floorSceneObject) { return string.Equals(floorSceneObject.category, BIWSe
 103
 104    public void CreateDefaultFloor()
 105    {
 1106        CatalogItem floorSceneObject = BIWUtils.CreateFloorSceneObject();
 1107        CreateFloor(floorSceneObject);
 1108    }
 109
 110    public void CreateFloor(CatalogItem floorSceneObject)
 111    {
 8112        if (sceneToEdit == null)
 0113            return;
 114
 8115        Vector3 initialPosition = new Vector3(ParcelSettings.PARCEL_SIZE / 2, 0, ParcelSettings.PARCEL_SIZE / 2);
 8116        Vector2Int[] parcelsPoints = sceneToEdit.sceneData.parcels;
 8117        numberOfParcelsLoaded = 0;
 8118        loadedFloorEntities.Clear();
 119
 32120        foreach (Vector2Int parcel in parcelsPoints)
 121        {
 8122            BIWEntity decentralandEntity = creatorController.CreateCatalogItem(
 123                floorSceneObject,
 124                WorldStateUtils.ConvertPointInSceneToUnityPosition(initialPosition, parcel),
 125                false,
 126                true,
 127                OnFloorLoaded);
 128
 129            // It may happen that when you get here, the floor entity is already loaded and it wouldn't be necessary to 
 8130            if (!loadedFloorEntities.Contains(decentralandEntity.rootEntity.entityId))
 131            {
 8132                GameObject floorPlaceHolder = GameObject.Instantiate(floorPrefab, decentralandEntity.rootEntity.gameObje
 8133                floorPlaceHolder.GetComponentInChildren<BIWFloorLoading>().Initialize(mainCamera);
 8134                floorPlaceHolderDict.Add(decentralandEntity.rootEntity.entityId, floorPlaceHolder);
 8135                decentralandEntity.OnShapeFinishLoading += RemovePlaceHolder;
 136            }
 137        }
 138
 8139        entityHandler.DeselectEntities();
 8140        lastFloorCalalogItemUsed = floorSceneObject;
 8141    }
 142
 143    private void RemovePlaceHolder(BIWEntity entity)
 144    {
 4145        entity.OnShapeFinishLoading -= RemovePlaceHolder;
 4146        RemovePlaceHolder(entity.rootEntity.entityId);
 4147    }
 148
 149    private void OnFloorLoaded(IDCLEntity entity)
 150    {
 9151        entity.OnShapeUpdated -= OnFloorLoaded;
 9152        loadedFloorEntities.Add(entity.entityId);
 9153        RemovePlaceHolder(entity.entityId);
 154
 9155        numberOfParcelsLoaded++;
 9156        if (sceneToEdit != null && numberOfParcelsLoaded >= sceneToEdit.sceneData.parcels.Count())
 9157            OnAllParcelsFloorLoaded?.Invoke();
 0158    }
 159
 160    private void RemovePlaceHolder(string entityId)
 161    {
 13162        if (!floorPlaceHolderDict.ContainsKey(entityId))
 8163            return;
 164
 5165        GameObject floorPlaceHolder = floorPlaceHolderDict[entityId];
 5166        floorPlaceHolderDict.Remove(entityId);
 5167        GameObject.Destroy(floorPlaceHolder);
 5168    }
 169
 170    private void RemoveAllPlaceHolders()
 171    {
 136172        foreach (GameObject gameObject in floorPlaceHolderDict.Values)
 173        {
 3174            GameObject.Destroy(gameObject);
 175        }
 65176        floorPlaceHolderDict.Clear();
 65177    }
 178
 179    public override void ExitEditMode()
 180    {
 4181        base.ExitEditMode();
 182
 4183        RemoveAllPlaceHolders();
 4184    }
 185}