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