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