| | 1 | | using System; |
| | 2 | | using Builder.MeshLoadIndicator; |
| | 3 | | using DCL; |
| | 4 | | using DCL.Configuration; |
| | 5 | | using DCL.Controllers; |
| | 6 | | using DCL.Models; |
| | 7 | | using System.Collections; |
| | 8 | | using System.Collections.Generic; |
| | 9 | | using System.Linq; |
| | 10 | | using UnityEngine; |
| | 11 | |
|
| | 12 | | public class BIWFloorHandler : BIWController |
| | 13 | | { |
| | 14 | | [Header("Design Variables")] |
| 198 | 15 | | public float secondsToTimeOut = 10f; |
| | 16 | |
|
| | 17 | | [Header("Prefab References")] |
| | 18 | | public ActionController actionController; |
| | 19 | |
|
| | 20 | | public BuilderInWorldEntityHandler builderInWorldEntityHandler; |
| | 21 | | public DCLBuilderMeshLoadIndicatorController dclBuilderMeshLoadIndicatorController; |
| | 22 | | public DCLBuilderMeshLoadIndicator meshLoadIndicator; |
| | 23 | | public BIWCreatorController biwCreatorController; |
| | 24 | | public BIWSaveController biwSaveController; |
| | 25 | |
|
| | 26 | | [Header("Prefabs")] |
| | 27 | | public GameObject floorPrefab; |
| | 28 | |
|
| | 29 | | public event Action OnAllParcelsFloorLoaded; |
| | 30 | | private int numberOfParcelsLoaded; |
| | 31 | |
|
| | 32 | | private CatalogItem lastFloorCalalogItemUsed; |
| 198 | 33 | | private readonly Dictionary<string, GameObject> floorPlaceHolderDict = new Dictionary<string, GameObject>(); |
| 198 | 34 | | private readonly List<string> loadedFloorEntities = new List<string>(); |
| | 35 | |
|
| | 36 | | private void Start() |
| | 37 | | { |
| 0 | 38 | | builderInWorldEntityHandler.OnEntityDeleted += OnFloorEntityDeleted; |
| 0 | 39 | | meshLoadIndicator.SetCamera(Camera.main); |
| 0 | 40 | | } |
| | 41 | |
|
| | 42 | | private void OnDestroy() |
| | 43 | | { |
| 0 | 44 | | builderInWorldEntityHandler.OnEntityDeleted -= OnFloorEntityDeleted; |
| | 45 | |
|
| 0 | 46 | | Clean(); |
| 0 | 47 | | } |
| | 48 | |
|
| | 49 | | private void OnFloorEntityDeleted(DCLBuilderInWorldEntity entity) |
| | 50 | | { |
| 0 | 51 | | if (entity.isFloor) |
| 0 | 52 | | RemovePlaceHolder(entity); |
| 0 | 53 | | } |
| | 54 | |
|
| | 55 | | public void Clean() |
| | 56 | | { |
| 20 | 57 | | RemoveAllPlaceHolders(); |
| 20 | 58 | | dclBuilderMeshLoadIndicatorController.Dispose(); |
| 20 | 59 | | } |
| | 60 | |
|
| 2 | 61 | | public bool ExistsFloorPlaceHolderForEntity(string entityId) { return floorPlaceHolderDict.ContainsKey(entityId); } |
| | 62 | |
|
| | 63 | | public void ChangeFloor(CatalogItem newFloorObject) |
| | 64 | | { |
| 2 | 65 | | biwSaveController.SetSaveActivation(false); |
| 2 | 66 | | CatalogItem lastFloor = lastFloorCalalogItemUsed; |
| 2 | 67 | | if (lastFloor == null) |
| 0 | 68 | | lastFloor = FindCurrentFloorCatalogItem(); |
| | 69 | |
|
| 2 | 70 | | builderInWorldEntityHandler.DeleteFloorEntities(); |
| | 71 | |
|
| 2 | 72 | | CreateFloor(newFloorObject); |
| | 73 | |
|
| 2 | 74 | | BuildInWorldCompleteAction buildAction = new BuildInWorldCompleteAction(); |
| | 75 | |
|
| 2 | 76 | | buildAction.CreateChangeFloorAction(lastFloor, newFloorObject); |
| 2 | 77 | | actionController.AddAction(buildAction); |
| | 78 | |
|
| 2 | 79 | | biwSaveController.SetSaveActivation(true, true); |
| 2 | 80 | | } |
| | 81 | |
|
| | 82 | | public CatalogItem FindCurrentFloorCatalogItem() |
| | 83 | | { |
| 3 | 84 | | foreach (DCLBuilderInWorldEntity entity in builderInWorldEntityHandler.GetAllEntitiesFromCurrentScene()) |
| | 85 | | { |
| 1 | 86 | | if (entity.isFloor) |
| | 87 | | { |
| 1 | 88 | | return entity.GetCatalogItemAssociated(); |
| | 89 | | } |
| | 90 | | } |
| | 91 | |
|
| 0 | 92 | | return null; |
| 1 | 93 | | } |
| | 94 | |
|
| 1 | 95 | | public bool IsCatalogItemFloor(CatalogItem floorSceneObject) { return string.Equals(floorSceneObject.category, Build |
| | 96 | |
|
| | 97 | | public void CreateDefaultFloor() |
| | 98 | | { |
| 0 | 99 | | CatalogItem floorSceneObject = BuilderInWorldUtils.CreateFloorSceneObject(); |
| 0 | 100 | | CreateFloor(floorSceneObject); |
| 0 | 101 | | } |
| | 102 | |
|
| | 103 | | public void CreateFloor(CatalogItem floorSceneObject) |
| | 104 | | { |
| 7 | 105 | | Vector3 initialPosition = new Vector3(ParcelSettings.PARCEL_SIZE / 2, 0, ParcelSettings.PARCEL_SIZE / 2); |
| 7 | 106 | | Vector2Int[] parcelsPoints = sceneToEdit.sceneData.parcels; |
| 7 | 107 | | numberOfParcelsLoaded = 0; |
| 7 | 108 | | loadedFloorEntities.Clear(); |
| | 109 | |
|
| 28 | 110 | | foreach (Vector2Int parcel in parcelsPoints) |
| | 111 | | { |
| 7 | 112 | | DCLBuilderInWorldEntity decentralandEntity = biwCreatorController.CreateCatalogItem( |
| | 113 | | floorSceneObject, |
| | 114 | | WorldStateUtils.ConvertPointInSceneToUnityPosition(initialPosition, parcel), |
| | 115 | | false, |
| | 116 | | true, |
| | 117 | | OnFloorLoaded); |
| | 118 | |
|
| | 119 | | // It may happen that when you get here, the floor entity is already loaded and it wouldn't be necessary to |
| 7 | 120 | | if (!loadedFloorEntities.Contains(decentralandEntity.rootEntity.entityId)) |
| | 121 | | { |
| 7 | 122 | | dclBuilderMeshLoadIndicatorController.ShowIndicator(decentralandEntity.rootEntity.gameObject.transform.p |
| 7 | 123 | | GameObject floorPlaceHolder = GameObject.Instantiate(floorPrefab, decentralandEntity.rootEntity.gameObje |
| 7 | 124 | | floorPlaceHolderDict.Add(decentralandEntity.rootEntity.entityId, floorPlaceHolder); |
| 7 | 125 | | decentralandEntity.OnShapeFinishLoading += RemovePlaceHolder; |
| | 126 | | } |
| | 127 | | } |
| | 128 | |
|
| 7 | 129 | | builderInWorldEntityHandler.DeselectEntities(); |
| 7 | 130 | | lastFloorCalalogItemUsed = floorSceneObject; |
| 7 | 131 | | } |
| | 132 | |
|
| | 133 | | private void RemovePlaceHolder(DCLBuilderInWorldEntity entity) |
| | 134 | | { |
| 0 | 135 | | entity.OnShapeFinishLoading -= RemovePlaceHolder; |
| 0 | 136 | | RemovePlaceHolder(entity.rootEntity.entityId); |
| 0 | 137 | | } |
| | 138 | |
|
| | 139 | | private void OnFloorLoaded(IDCLEntity entity) |
| | 140 | | { |
| 8 | 141 | | entity.OnShapeUpdated -= OnFloorLoaded; |
| 8 | 142 | | loadedFloorEntities.Add(entity.entityId); |
| 8 | 143 | | RemovePlaceHolder(entity.entityId); |
| | 144 | |
|
| 8 | 145 | | numberOfParcelsLoaded++; |
| 8 | 146 | | if (sceneToEdit != null && numberOfParcelsLoaded >= sceneToEdit.sceneData.parcels.Count()) |
| 8 | 147 | | OnAllParcelsFloorLoaded?.Invoke(); |
| 0 | 148 | | } |
| | 149 | |
|
| | 150 | | private void RemovePlaceHolder(string entityId) |
| | 151 | | { |
| 8 | 152 | | if (!floorPlaceHolderDict.ContainsKey(entityId)) |
| 3 | 153 | | return; |
| | 154 | |
|
| 5 | 155 | | GameObject floorPlaceHolder = floorPlaceHolderDict[entityId]; |
| 5 | 156 | | floorPlaceHolderDict.Remove(entityId); |
| 5 | 157 | | GameObject.Destroy(floorPlaceHolder); |
| 5 | 158 | | dclBuilderMeshLoadIndicatorController.HideIndicator(entityId); |
| 5 | 159 | | } |
| | 160 | |
|
| | 161 | | private void RemoveAllPlaceHolders() |
| | 162 | | { |
| 44 | 163 | | foreach (GameObject gameObject in floorPlaceHolderDict.Values) |
| | 164 | | { |
| 2 | 165 | | GameObject.Destroy(gameObject); |
| | 166 | | } |
| 20 | 167 | | floorPlaceHolderDict.Clear(); |
| 20 | 168 | | } |
| | 169 | |
|
| | 170 | | public override void ExitEditMode() |
| | 171 | | { |
| 0 | 172 | | base.ExitEditMode(); |
| | 173 | |
|
| 0 | 174 | | RemoveAllPlaceHolders(); |
| 0 | 175 | | dclBuilderMeshLoadIndicatorController.HideAllIndicators(); |
| 0 | 176 | | } |
| | 177 | | } |