| | 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 DCL.Builder; |
| | 10 | | using DCL.Components; |
| | 11 | | using UnityEngine; |
| | 12 | |
|
| | 13 | | public class BIWFloorHandler : BIWController, IBIWFloorHandler |
| | 14 | | { |
| | 15 | | public event Action OnAllParcelsFloorLoaded; |
| | 16 | |
|
| | 17 | | private IBIWActionController actionController; |
| | 18 | |
|
| | 19 | | private IBIWEntityHandler entityHandler; |
| | 20 | | private IBIWCreatorController creatorController; |
| | 21 | | private IBIWSaveController saveController; |
| | 22 | |
|
| | 23 | | private GameObject floorPrefab; |
| | 24 | |
|
| | 25 | | private int numberOfParcelsLoaded; |
| | 26 | |
|
| | 27 | | private CatalogItem lastFloorCalalogItemUsed; |
| 7 | 28 | | public Dictionary<long, GameObject> floorPlaceHolderDict { get; set; } = new Dictionary<long, GameObject>(); |
| 7 | 29 | | private readonly List<long> loadedFloorEntities = new List<long>(); |
| | 30 | | private Camera mainCamera; |
| | 31 | |
|
| | 32 | | public override void Initialize(IContext context) |
| | 33 | | { |
| 7 | 34 | | base.Initialize(context); |
| | 35 | |
|
| 7 | 36 | | actionController = context.editorContext.actionController; |
| 7 | 37 | | entityHandler = context.editorContext.entityHandler; |
| 7 | 38 | | creatorController = context.editorContext.creatorController; |
| 7 | 39 | | saveController = context.editorContext.saveController; |
| 7 | 40 | | mainCamera = context.sceneReferences.mainCamera; |
| 7 | 41 | | floorPrefab = context.projectReferencesAsset.floorPlaceHolderPrefab; |
| | 42 | |
|
| 7 | 43 | | entityHandler.OnEntityDeleted += OnFloorEntityDeleted; |
| 7 | 44 | | } |
| | 45 | |
|
| | 46 | | public override void EnterEditMode(IBuilderScene scene) |
| | 47 | | { |
| 9 | 48 | | base.EnterEditMode(scene); |
| 28 | 49 | | foreach (BIWEntity entity in entityHandler.GetAllEntitiesFromCurrentScene()) |
| | 50 | | { |
| 5 | 51 | | if(!entity.isFloor) |
| | 52 | | continue; |
| | 53 | |
|
| 0 | 54 | | if (!entity.isLoaded) |
| 0 | 55 | | entity.rootEntity.OnShapeLoaded += OnFloorLoaded; |
| | 56 | | else |
| 0 | 57 | | OnFloorLoaded(entity.rootEntity); |
| | 58 | | } |
| 9 | 59 | | } |
| | 60 | |
|
| | 61 | | public override void Dispose() |
| | 62 | | { |
| 8 | 63 | | entityHandler.OnEntityDeleted -= OnFloorEntityDeleted; |
| 8 | 64 | | CleanUp(); |
| 8 | 65 | | } |
| | 66 | |
|
| | 67 | | private void OnFloorEntityDeleted(BIWEntity entity) |
| | 68 | | { |
| 6 | 69 | | if (entity.isFloor) |
| 4 | 70 | | RemovePlaceHolder(entity); |
| 6 | 71 | | } |
| | 72 | |
|
| 16 | 73 | | public void CleanUp() { RemoveAllPlaceHolders(); } |
| | 74 | |
|
| 2 | 75 | | public bool ExistsFloorPlaceHolderForEntity(long entityId) { return floorPlaceHolderDict.ContainsKey(entityId); } |
| | 76 | |
|
| | 77 | | public void ChangeFloor(CatalogItem newFloorObject) |
| | 78 | | { |
| 2 | 79 | | saveController.SetSaveActivation(false); |
| 2 | 80 | | CatalogItem lastFloor = lastFloorCalalogItemUsed; |
| 2 | 81 | | if (lastFloor == null) |
| 0 | 82 | | lastFloor = FindCurrentFloorCatalogItem(); |
| | 83 | |
|
| 2 | 84 | | entityHandler.DeleteFloorEntities(); |
| | 85 | |
|
| 2 | 86 | | CreateFloor(newFloorObject); |
| | 87 | |
|
| 2 | 88 | | BIWCompleteAction buildAction = new BIWCompleteAction(); |
| | 89 | |
|
| 2 | 90 | | buildAction.CreateChangeFloorAction(lastFloor, newFloorObject); |
| 2 | 91 | | actionController.AddAction(buildAction); |
| | 92 | |
|
| 2 | 93 | | saveController.SetSaveActivation(true, true); |
| 2 | 94 | | } |
| | 95 | |
|
| | 96 | | public CatalogItem FindCurrentFloorCatalogItem() |
| | 97 | | { |
| 3 | 98 | | foreach (BIWEntity entity in entityHandler.GetAllEntitiesFromCurrentScene()) |
| | 99 | | { |
| 1 | 100 | | if (entity.isFloor) |
| | 101 | | { |
| 1 | 102 | | return entity.GetCatalogItemAssociated(); |
| | 103 | | } |
| | 104 | | } |
| | 105 | |
|
| 0 | 106 | | return null; |
| 1 | 107 | | } |
| | 108 | |
|
| 0 | 109 | | public bool IsCatalogItemFloor(CatalogItem floorSceneObject) { return string.Equals(floorSceneObject.category, BIWSe |
| | 110 | |
|
| | 111 | | public void CreateDefaultFloor() |
| | 112 | | { |
| 0 | 113 | | CatalogItem floorSceneObject = BIWUtils.CreateFloorCatalogItem(); |
| 0 | 114 | | CreateFloor(floorSceneObject); |
| 0 | 115 | | } |
| | 116 | |
|
| | 117 | | public void CreateFloor(CatalogItem floorSceneObject) |
| | 118 | | { |
| 7 | 119 | | if (sceneToEdit == null) |
| 0 | 120 | | return; |
| | 121 | |
|
| 7 | 122 | | Vector3 initialPosition = new Vector3(ParcelSettings.PARCEL_SIZE / 2, 0, ParcelSettings.PARCEL_SIZE / 2); |
| 7 | 123 | | Vector2Int[] parcelsPoints = sceneToEdit.sceneData.parcels; |
| 7 | 124 | | numberOfParcelsLoaded = 0; |
| 7 | 125 | | loadedFloorEntities.Clear(); |
| | 126 | |
|
| 28 | 127 | | foreach (Vector2Int parcel in parcelsPoints) |
| | 128 | | { |
| 7 | 129 | | BIWEntity entity = creatorController.CreateCatalogItem( |
| | 130 | | floorSceneObject, |
| | 131 | | WorldStateUtils.ConvertPointInSceneToUnityPosition(initialPosition, parcel), |
| | 132 | | false, |
| | 133 | | true, |
| | 134 | | OnFloorLoaded); |
| | 135 | |
|
| 7 | 136 | | long rootEntityId = entity.rootEntity.entityId; |
| | 137 | |
|
| | 138 | | // It may happen that when you get here, the floor entity is already loaded and it wouldn't be necessary to |
| 7 | 139 | | if (!loadedFloorEntities.Contains(rootEntityId)) |
| | 140 | | { |
| 7 | 141 | | GameObject floorPlaceHolder = UnityEngine.Object.Instantiate(floorPrefab, entity.rootEntity.gameObject.t |
| 7 | 142 | | floorPlaceHolder.GetComponentInChildren<BIWFloorLoading>().Initialize(mainCamera); |
| 7 | 143 | | floorPlaceHolderDict.Add(entity.rootEntity.entityId, floorPlaceHolder); |
| 7 | 144 | | entity.OnShapeFinishLoading += RemovePlaceHolder; |
| | 145 | | } |
| | 146 | | } |
| | 147 | |
|
| 7 | 148 | | entityHandler.DeselectEntities(); |
| 7 | 149 | | lastFloorCalalogItemUsed = floorSceneObject; |
| 7 | 150 | | } |
| | 151 | |
|
| | 152 | | private void RemovePlaceHolder(BIWEntity entity) |
| | 153 | | { |
| 4 | 154 | | entity.OnShapeFinishLoading -= RemovePlaceHolder; |
| 4 | 155 | | RemovePlaceHolder(entity.rootEntity.entityId); |
| 4 | 156 | | } |
| | 157 | |
|
| | 158 | | private void OnFloorLoaded(IDCLEntity entity) |
| | 159 | | { |
| 1 | 160 | | entity.OnShapeLoaded -= OnFloorLoaded; |
| 1 | 161 | | loadedFloorEntities.Add(entity.entityId); |
| 1 | 162 | | RemovePlaceHolder(entity.entityId); |
| | 163 | |
|
| 1 | 164 | | numberOfParcelsLoaded++; |
| 1 | 165 | | if (sceneToEdit != null && numberOfParcelsLoaded >= sceneToEdit.sceneData.parcels.Count()) |
| 1 | 166 | | OnAllParcelsFloorLoaded?.Invoke(); |
| 0 | 167 | | } |
| | 168 | |
|
| | 169 | | private void RemovePlaceHolder(long entityId) |
| | 170 | | { |
| 5 | 171 | | if (!floorPlaceHolderDict.ContainsKey(entityId)) |
| 0 | 172 | | return; |
| | 173 | |
|
| 5 | 174 | | GameObject floorPlaceHolder = floorPlaceHolderDict[entityId]; |
| 5 | 175 | | floorPlaceHolderDict.Remove(entityId); |
| 5 | 176 | | UnityEngine.Object.Destroy(floorPlaceHolder); |
| 5 | 177 | | } |
| | 178 | |
|
| | 179 | | private void RemoveAllPlaceHolders() |
| | 180 | | { |
| 20 | 181 | | foreach (GameObject gameObject in floorPlaceHolderDict.Values) |
| | 182 | | { |
| 2 | 183 | | UnityEngine.Object.Destroy(gameObject); |
| | 184 | | } |
| | 185 | |
|
| 8 | 186 | | floorPlaceHolderDict.Clear(); |
| 8 | 187 | | } |
| | 188 | |
|
| | 189 | | public override void ExitEditMode() |
| | 190 | | { |
| 0 | 191 | | base.ExitEditMode(); |
| | 192 | |
|
| 0 | 193 | | numberOfParcelsLoaded = 0; |
| 0 | 194 | | RemoveAllPlaceHolders(); |
| 0 | 195 | | } |
| | 196 | | } |