< Summary

Class:BIWFloorHandler
Assembly:BuilderInWorld
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/BuilderMode/Controllers/BIWFloorHandler.cs
Covered lines:70
Uncovered lines:9
Coverable lines:79
Total lines:189
Line coverage:88.6% (70 of 79)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
BIWFloorHandler()0%110100%
Initialize(...)0%110100%
Dispose()0%110100%
OnFloorEntityDeleted(...)0%220100%
CleanUp()0%110100%
ExistsFloorPlaceHolderForEntity(...)0%110100%
ChangeFloor(...)0%22090.91%
FindCurrentFloorCatalogItem()0%3.033085.71%
IsCatalogItemFloor(...)0%2100%
CreateDefaultFloor()0%2100%
CreateFloor(...)0%44094.74%
RemovePlaceHolder(...)0%110100%
OnFloorLoaded(...)0%4.054085.71%
RemovePlaceHolder(...)0%220100%
RemoveAllPlaceHolders()0%2.152066.67%
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 : IBIWController
 12{
 13    void CreateDefaultFloor();
 14    void CreateFloor(CatalogItem floorSceneObject);
 15    bool IsCatalogItemFloor(CatalogItem floorSceneObject);
 16    void ChangeFloor(CatalogItem newFloorObject);
 17    event Action OnAllParcelsFloorLoaded;
 18    Dictionary<string, GameObject> floorPlaceHolderDict { get; set; }
 19    void CleanUp();
 20}
 21
 22public class BIWFloorHandler : BIWController, IBIWFloorHandler
 23{
 24    public event Action OnAllParcelsFloorLoaded;
 25
 26    private IBIWActionController actionController;
 27
 28    private IBIWEntityHandler entityHandler;
 29    private IBIWCreatorController creatorController;
 30    private IBIWSaveController saveController;
 31
 32    private GameObject floorPrefab;
 33
 34    private int numberOfParcelsLoaded;
 35
 36    private CatalogItem lastFloorCalalogItemUsed;
 3237    public Dictionary<string, GameObject> floorPlaceHolderDict { get; set; } = new Dictionary<string, GameObject>();
 3238    private readonly List<string> loadedFloorEntities = new List<string>();
 39    private Camera mainCamera;
 40
 41    public override void Initialize(BIWContext context)
 42    {
 3243        base.Initialize(context);
 44
 3245        actionController = context.actionController;
 3246        entityHandler = context.entityHandler;
 3247        creatorController = context.creatorController;
 3248        saveController = context.saveController;
 3249        mainCamera = context.sceneReferences.mainCamera;
 3250        floorPrefab = context.projectReferencesAsset.floorPlaceHolderPrefab;
 51
 3252        entityHandler.OnEntityDeleted += OnFloorEntityDeleted;
 3253    }
 54
 55    public override void Dispose()
 56    {
 3257        entityHandler.OnEntityDeleted -= OnFloorEntityDeleted;
 3258        CleanUp();
 3259    }
 60
 61    private void OnFloorEntityDeleted(BIWEntity entity)
 62    {
 663        if (entity.isFloor)
 464            RemovePlaceHolder(entity);
 665    }
 66
 11267    public void CleanUp() { RemoveAllPlaceHolders(); }
 68
 269    public bool ExistsFloorPlaceHolderForEntity(string entityId) { return floorPlaceHolderDict.ContainsKey(entityId); }
 70
 71    public void ChangeFloor(CatalogItem newFloorObject)
 72    {
 273        saveController.SetSaveActivation(false);
 274        CatalogItem lastFloor = lastFloorCalalogItemUsed;
 275        if (lastFloor == null)
 076            lastFloor = FindCurrentFloorCatalogItem();
 77
 278        entityHandler.DeleteFloorEntities();
 79
 280        CreateFloor(newFloorObject);
 81
 282        BIWCompleteAction buildAction = new BIWCompleteAction();
 83
 284        buildAction.CreateChangeFloorAction(lastFloor, newFloorObject);
 285        actionController.AddAction(buildAction);
 86
 287        saveController.SetSaveActivation(true, true);
 288    }
 89
 90    public CatalogItem FindCurrentFloorCatalogItem()
 91    {
 392        foreach (BIWEntity entity in entityHandler.GetAllEntitiesFromCurrentScene())
 93        {
 194            if (entity.isFloor)
 95            {
 196                return entity.GetCatalogItemAssociated();
 97            }
 98        }
 99
 0100        return null;
 1101    }
 102
 0103    public bool IsCatalogItemFloor(CatalogItem floorSceneObject) { return string.Equals(floorSceneObject.category, BIWSe
 104
 105    public void CreateDefaultFloor()
 106    {
 0107        CatalogItem floorSceneObject = BIWUtils.CreateFloorSceneObject();
 0108        CreateFloor(floorSceneObject);
 0109    }
 110
 111    public void CreateFloor(CatalogItem floorSceneObject)
 112    {
 7113        if (sceneToEdit == null)
 0114            return;
 115
 7116        Vector3 initialPosition = new Vector3(ParcelSettings.PARCEL_SIZE / 2, 0, ParcelSettings.PARCEL_SIZE / 2);
 7117        Vector2Int[] parcelsPoints = sceneToEdit.sceneData.parcels;
 7118        numberOfParcelsLoaded = 0;
 7119        loadedFloorEntities.Clear();
 120
 28121        foreach (Vector2Int parcel in parcelsPoints)
 122        {
 7123            BIWEntity entity = creatorController.CreateCatalogItem(
 124                floorSceneObject,
 125                WorldStateUtils.ConvertPointInSceneToUnityPosition(initialPosition, parcel),
 126                false,
 127                true,
 128                OnFloorLoaded);
 129
 7130            string rootEntityId = entity.rootEntity.entityId;
 131
 132            // It may happen that when you get here, the floor entity is already loaded and it wouldn't be necessary to 
 7133            if (!loadedFloorEntities.Contains(rootEntityId))
 134            {
 6135                GameObject floorPlaceHolder = UnityEngine.Object.Instantiate(floorPrefab, entity.rootEntity.gameObject.t
 6136                floorPlaceHolder.GetComponentInChildren<BIWFloorLoading>().Initialize(mainCamera);
 6137                floorPlaceHolderDict.Add(entity.rootEntity.entityId, floorPlaceHolder);
 6138                entity.OnShapeFinishLoading += RemovePlaceHolder;
 139            }
 140        }
 141
 7142        entityHandler.DeselectEntities();
 7143        lastFloorCalalogItemUsed = floorSceneObject;
 7144    }
 145
 146    private void RemovePlaceHolder(BIWEntity entity)
 147    {
 7148        entity.OnShapeFinishLoading -= RemovePlaceHolder;
 7149        RemovePlaceHolder(entity.rootEntity.entityId);
 7150    }
 151
 152    private void OnFloorLoaded(IDCLEntity entity)
 153    {
 12154        entity.OnShapeUpdated -= OnFloorLoaded;
 12155        loadedFloorEntities.Add(entity.entityId);
 12156        RemovePlaceHolder(entity.entityId);
 157
 12158        numberOfParcelsLoaded++;
 12159        if (sceneToEdit != null && numberOfParcelsLoaded >= sceneToEdit.sceneData.parcels.Count())
 12160            OnAllParcelsFloorLoaded?.Invoke();
 0161    }
 162
 163    private void RemovePlaceHolder(string entityId)
 164    {
 19165        if (!floorPlaceHolderDict.ContainsKey(entityId))
 13166            return;
 167
 6168        GameObject floorPlaceHolder = floorPlaceHolderDict[entityId];
 6169        floorPlaceHolderDict.Remove(entityId);
 6170        UnityEngine.Object.Destroy(floorPlaceHolder);
 6171    }
 172
 173    private void RemoveAllPlaceHolders()
 174    {
 120175        foreach (GameObject gameObject in floorPlaceHolderDict.Values)
 176        {
 0177            UnityEngine.Object.Destroy(gameObject);
 178        }
 179
 60180        floorPlaceHolderDict.Clear();
 60181    }
 182
 183    public override void ExitEditMode()
 184    {
 4185        base.ExitEditMode();
 186
 4187        RemoveAllPlaceHolders();
 4188    }
 189}