| | 1 | | using System; |
| | 2 | | using DCL; |
| | 3 | | using DCL.Configuration; |
| | 4 | | using DCL.Controllers; |
| | 5 | | using DCL.Models; |
| | 6 | | using System.Collections; |
| | 7 | | using System.Collections.Generic; |
| | 8 | | using System.Linq; |
| | 9 | | using UnityEngine; |
| | 10 | |
|
| | 11 | | public interface IBIWFloorHandler |
| | 12 | | { |
| | 13 | | void CreateDefaultFloor(); |
| | 14 | | void CreateFloor(CatalogItem floorSceneObject); |
| | 15 | | bool IsCatalogItemFloor(CatalogItem floorSceneObject); |
| | 16 | | void ChangeFloor(CatalogItem newFloorObject); |
| | 17 | | } |
| | 18 | |
|
| | 19 | | public 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; |
| 33 | 34 | | internal readonly Dictionary<string, GameObject> floorPlaceHolderDict = new Dictionary<string, GameObject>(); |
| 33 | 35 | | private readonly List<string> loadedFloorEntities = new List<string>(); |
| | 36 | | private Camera mainCamera; |
| | 37 | |
|
| | 38 | | public override void Init(BIWContext context) |
| | 39 | | { |
| 33 | 40 | | base.Init(context); |
| 33 | 41 | | actionController = context.actionController; |
| | 42 | |
|
| 33 | 43 | | entityHandler = context.entityHandler; |
| | 44 | |
|
| 33 | 45 | | creatorController = context.creatorController; |
| 33 | 46 | | saveController = context.saveController; |
| 33 | 47 | | mainCamera = context.sceneReferences.mainCamera; |
| | 48 | |
|
| 33 | 49 | | floorPrefab = context.projectReferencesAsset.floorPlaceHolderPrefab; |
| | 50 | |
|
| 33 | 51 | | entityHandler.OnEntityDeleted += OnFloorEntityDeleted; |
| 33 | 52 | | } |
| | 53 | |
|
| | 54 | | public override void Dispose() |
| | 55 | | { |
| 34 | 56 | | entityHandler.OnEntityDeleted -= OnFloorEntityDeleted; |
| 34 | 57 | | Clean(); |
| 34 | 58 | | } |
| | 59 | |
|
| | 60 | | private void OnFloorEntityDeleted(BIWEntity entity) |
| | 61 | | { |
| 6 | 62 | | if (entity.isFloor) |
| 4 | 63 | | RemovePlaceHolder(entity); |
| 6 | 64 | | } |
| | 65 | |
|
| 122 | 66 | | public void Clean() { RemoveAllPlaceHolders(); } |
| | 67 | |
|
| 2 | 68 | | public bool ExistsFloorPlaceHolderForEntity(string entityId) { return floorPlaceHolderDict.ContainsKey(entityId); } |
| | 69 | |
|
| | 70 | | public void ChangeFloor(CatalogItem newFloorObject) |
| | 71 | | { |
| 2 | 72 | | saveController.SetSaveActivation(false); |
| 2 | 73 | | CatalogItem lastFloor = lastFloorCalalogItemUsed; |
| 2 | 74 | | if (lastFloor == null) |
| 0 | 75 | | lastFloor = FindCurrentFloorCatalogItem(); |
| | 76 | |
|
| 2 | 77 | | entityHandler.DeleteFloorEntities(); |
| | 78 | |
|
| 2 | 79 | | CreateFloor(newFloorObject); |
| | 80 | |
|
| 2 | 81 | | BIWCompleteAction buildAction = new BIWCompleteAction(); |
| | 82 | |
|
| 2 | 83 | | buildAction.CreateChangeFloorAction(lastFloor, newFloorObject); |
| 2 | 84 | | actionController.AddAction(buildAction); |
| | 85 | |
|
| 2 | 86 | | saveController.SetSaveActivation(true, true); |
| 2 | 87 | | } |
| | 88 | |
|
| | 89 | | public CatalogItem FindCurrentFloorCatalogItem() |
| | 90 | | { |
| 3 | 91 | | foreach (BIWEntity entity in entityHandler.GetAllEntitiesFromCurrentScene()) |
| | 92 | | { |
| 1 | 93 | | if (entity.isFloor) |
| | 94 | | { |
| 1 | 95 | | return entity.GetCatalogItemAssociated(); |
| | 96 | | } |
| | 97 | | } |
| | 98 | |
|
| 0 | 99 | | return null; |
| 1 | 100 | | } |
| | 101 | |
|
| 0 | 102 | | public bool IsCatalogItemFloor(CatalogItem floorSceneObject) { return string.Equals(floorSceneObject.category, BIWSe |
| | 103 | |
|
| | 104 | | public void CreateDefaultFloor() |
| | 105 | | { |
| 1 | 106 | | CatalogItem floorSceneObject = BIWUtils.CreateFloorSceneObject(); |
| 1 | 107 | | CreateFloor(floorSceneObject); |
| 1 | 108 | | } |
| | 109 | |
|
| | 110 | | public void CreateFloor(CatalogItem floorSceneObject) |
| | 111 | | { |
| 8 | 112 | | if (sceneToEdit == null) |
| 0 | 113 | | return; |
| | 114 | |
|
| 8 | 115 | | Vector3 initialPosition = new Vector3(ParcelSettings.PARCEL_SIZE / 2, 0, ParcelSettings.PARCEL_SIZE / 2); |
| 8 | 116 | | Vector2Int[] parcelsPoints = sceneToEdit.sceneData.parcels; |
| 8 | 117 | | numberOfParcelsLoaded = 0; |
| 8 | 118 | | loadedFloorEntities.Clear(); |
| | 119 | |
|
| 32 | 120 | | foreach (Vector2Int parcel in parcelsPoints) |
| | 121 | | { |
| 8 | 122 | | 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 |
| 8 | 130 | | if (!loadedFloorEntities.Contains(decentralandEntity.rootEntity.entityId)) |
| | 131 | | { |
| 8 | 132 | | GameObject floorPlaceHolder = GameObject.Instantiate(floorPrefab, decentralandEntity.rootEntity.gameObje |
| 8 | 133 | | floorPlaceHolder.GetComponentInChildren<BIWFloorLoading>().Initialize(mainCamera); |
| 8 | 134 | | floorPlaceHolderDict.Add(decentralandEntity.rootEntity.entityId, floorPlaceHolder); |
| 8 | 135 | | decentralandEntity.OnShapeFinishLoading += RemovePlaceHolder; |
| | 136 | | } |
| | 137 | | } |
| | 138 | |
|
| 8 | 139 | | entityHandler.DeselectEntities(); |
| 8 | 140 | | lastFloorCalalogItemUsed = floorSceneObject; |
| 8 | 141 | | } |
| | 142 | |
|
| | 143 | | private void RemovePlaceHolder(BIWEntity entity) |
| | 144 | | { |
| 4 | 145 | | entity.OnShapeFinishLoading -= RemovePlaceHolder; |
| 4 | 146 | | RemovePlaceHolder(entity.rootEntity.entityId); |
| 4 | 147 | | } |
| | 148 | |
|
| | 149 | | private void OnFloorLoaded(IDCLEntity entity) |
| | 150 | | { |
| 9 | 151 | | entity.OnShapeUpdated -= OnFloorLoaded; |
| 9 | 152 | | loadedFloorEntities.Add(entity.entityId); |
| 9 | 153 | | RemovePlaceHolder(entity.entityId); |
| | 154 | |
|
| 9 | 155 | | numberOfParcelsLoaded++; |
| 9 | 156 | | if (sceneToEdit != null && numberOfParcelsLoaded >= sceneToEdit.sceneData.parcels.Count()) |
| 9 | 157 | | OnAllParcelsFloorLoaded?.Invoke(); |
| 0 | 158 | | } |
| | 159 | |
|
| | 160 | | private void RemovePlaceHolder(string entityId) |
| | 161 | | { |
| 13 | 162 | | if (!floorPlaceHolderDict.ContainsKey(entityId)) |
| 8 | 163 | | return; |
| | 164 | |
|
| 5 | 165 | | GameObject floorPlaceHolder = floorPlaceHolderDict[entityId]; |
| 5 | 166 | | floorPlaceHolderDict.Remove(entityId); |
| 5 | 167 | | GameObject.Destroy(floorPlaceHolder); |
| 5 | 168 | | } |
| | 169 | |
|
| | 170 | | private void RemoveAllPlaceHolders() |
| | 171 | | { |
| 136 | 172 | | foreach (GameObject gameObject in floorPlaceHolderDict.Values) |
| | 173 | | { |
| 3 | 174 | | GameObject.Destroy(gameObject); |
| | 175 | | } |
| 65 | 176 | | floorPlaceHolderDict.Clear(); |
| 65 | 177 | | } |
| | 178 | |
|
| | 179 | | public override void ExitEditMode() |
| | 180 | | { |
| 4 | 181 | | base.ExitEditMode(); |
| | 182 | |
|
| 4 | 183 | | RemoveAllPlaceHolders(); |
| 4 | 184 | | } |
| | 185 | | } |