| | 1 | | using DCL.Components; |
| | 2 | | using DCL.Configuration; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using DCL.Models; |
| | 5 | | using DCL.Controllers.ParcelSceneDebug; |
| | 6 | | using System.Collections.Generic; |
| | 7 | | using System.Linq; |
| | 8 | | using MainScripts.DCL.WorldRuntime; |
| | 9 | | using UnityEngine; |
| | 10 | | using UnityEngine.Assertions; |
| | 11 | |
|
| | 12 | | namespace DCL.Controllers |
| | 13 | | { |
| | 14 | | public class ParcelScene : MonoBehaviour, IParcelScene, ISceneMessageProcessor |
| | 15 | | { |
| | 16 | | public static bool VERBOSE = false; |
| 726 | 17 | | public Dictionary<string, IDCLEntity> entities { get; private set; } = new Dictionary<string, IDCLEntity>(); |
| 1005 | 18 | | public Dictionary<string, ISharedComponent> disposableComponents { get; private set; } = new Dictionary<string, |
| 5118 | 19 | | public LoadParcelScenesMessage.UnityParcelScene sceneData { get; protected set; } |
| | 20 | |
|
| 726 | 21 | | public HashSet<Vector2Int> parcels = new HashSet<Vector2Int>(); |
| | 22 | | public SceneController ownerController; |
| 40 | 23 | | public ISceneMetricsController metricsController { get; set; } |
| | 24 | | public event System.Action<IDCLEntity> OnEntityAdded; |
| | 25 | | public event System.Action<IDCLEntity> OnEntityRemoved; |
| | 26 | | public event System.Action<IComponent> OnComponentAdded; |
| | 27 | | public event System.Action<IComponent> OnComponentRemoved; |
| | 28 | | public event System.Action OnChanged; |
| | 29 | | public event System.Action<LoadParcelScenesMessage.UnityParcelScene> OnSetData; |
| | 30 | | public event System.Action<string, ISharedComponent> OnAddSharedComponent; |
| | 31 | | public event System.Action<float> OnLoadingStateUpdated; |
| | 32 | |
|
| 107 | 33 | | public ContentProvider contentProvider { get; protected set; } |
| | 34 | |
|
| 670 | 35 | | public bool isTestScene { get; set; } = false; |
| 2301 | 36 | | public bool isPersistent { get; set; } = false; |
| 0 | 37 | | public float loadingProgress { get; private set; } |
| | 38 | |
|
| | 39 | | [System.NonSerialized] |
| | 40 | | public string sceneName; |
| | 41 | |
|
| | 42 | | [System.NonSerialized] |
| 726 | 43 | | public bool unloadWithDistance = true; |
| | 44 | |
|
| | 45 | | bool isEditModeActive = false; |
| | 46 | |
|
| | 47 | | SceneDebugPlane sceneDebugPlane = null; |
| | 48 | |
|
| | 49 | | public SceneLifecycleHandler sceneLifecycleHandler; |
| | 50 | |
|
| 0 | 51 | | public bool isReleased { get; private set; } |
| | 52 | |
|
| | 53 | | public void Awake() |
| | 54 | | { |
| 726 | 55 | | CommonScriptableObjects.worldOffset.OnChange += OnWorldReposition; |
| | 56 | |
|
| 726 | 57 | | metricsController = new SceneMetricsController(this); |
| 726 | 58 | | metricsController.Enable(); |
| | 59 | |
|
| 726 | 60 | | sceneLifecycleHandler = new SceneLifecycleHandler(this); |
| 726 | 61 | | } |
| | 62 | |
|
| 1452 | 63 | | private void OnDestroy() { CommonScriptableObjects.worldOffset.OnChange -= OnWorldReposition; } |
| | 64 | |
|
| 1452 | 65 | | void OnDisable() { metricsController.Disable(); } |
| | 66 | |
|
| | 67 | | private void Update() |
| | 68 | | { |
| 8097 | 69 | | if (sceneLifecycleHandler.state == SceneLifecycleHandler.State.READY && CommonScriptableObjects.rendererStat |
| 7124 | 70 | | SendMetricsEvent(); |
| 8097 | 71 | | } |
| | 72 | |
|
| 1373 | 73 | | protected virtual string prettyName => sceneData.basePosition.ToString(); |
| | 74 | |
|
| 0 | 75 | | public void SetEditMode(bool isActive) { isEditModeActive = isActive; } |
| | 76 | |
|
| 0 | 77 | | public bool IsEditModeActive() { return isEditModeActive; } |
| | 78 | |
|
| | 79 | | public virtual void SetData(LoadParcelScenesMessage.UnityParcelScene data) |
| | 80 | | { |
| 703 | 81 | | this.sceneData = data; |
| | 82 | |
|
| 703 | 83 | | contentProvider = new ContentProvider(); |
| 703 | 84 | | contentProvider.baseUrl = data.baseUrl; |
| 703 | 85 | | contentProvider.contents = data.contents; |
| 703 | 86 | | contentProvider.BakeHashes(); |
| | 87 | |
|
| 703 | 88 | | parcels.Clear(); |
| 2848 | 89 | | for (int i = 0; i < sceneData.parcels.Length; i++) |
| | 90 | | { |
| 721 | 91 | | parcels.Add(sceneData.parcels[i]); |
| | 92 | | } |
| | 93 | |
|
| 703 | 94 | | if (DCLCharacterController.i != null) |
| 703 | 95 | | gameObject.transform.position = PositionUtils.WorldToUnityPosition(Utils.GridToWorldPosition(data.basePo |
| | 96 | |
|
| 703 | 97 | | OnSetData?.Invoke(data); |
| 703 | 98 | | } |
| | 99 | |
|
| | 100 | | void OnWorldReposition(Vector3 current, Vector3 previous) |
| | 101 | | { |
| 5 | 102 | | Vector3 sceneWorldPos = Utils.GridToWorldPosition(sceneData.basePosition.x, sceneData.basePosition.y); |
| 5 | 103 | | gameObject.transform.position = PositionUtils.WorldToUnityPosition(sceneWorldPos); |
| 5 | 104 | | } |
| | 105 | |
|
| | 106 | | public virtual void SetUpdateData(LoadParcelScenesMessage.UnityParcelScene data) |
| | 107 | | { |
| 16 | 108 | | contentProvider = new ContentProvider(); |
| 16 | 109 | | contentProvider.baseUrl = data.baseUrl; |
| 16 | 110 | | contentProvider.contents = data.contents; |
| 16 | 111 | | contentProvider.BakeHashes(); |
| 16 | 112 | | } |
| | 113 | |
|
| | 114 | | public void InitializeDebugPlane() |
| | 115 | | { |
| 699 | 116 | | if (EnvironmentSettings.DEBUG && sceneData.parcels != null && sceneDebugPlane == null) |
| | 117 | | { |
| 697 | 118 | | sceneDebugPlane = new SceneDebugPlane(sceneData, gameObject.transform); |
| | 119 | | } |
| 699 | 120 | | } |
| | 121 | |
|
| | 122 | | public void RemoveDebugPlane() |
| | 123 | | { |
| 0 | 124 | | if (sceneDebugPlane != null) |
| | 125 | | { |
| 0 | 126 | | sceneDebugPlane.Dispose(); |
| 0 | 127 | | sceneDebugPlane = null; |
| | 128 | | } |
| 0 | 129 | | } |
| | 130 | |
|
| | 131 | | public void Cleanup(bool immediate) |
| | 132 | | { |
| 718 | 133 | | if (isReleased) |
| 0 | 134 | | return; |
| | 135 | |
|
| 718 | 136 | | if (sceneDebugPlane != null) |
| | 137 | | { |
| 697 | 138 | | sceneDebugPlane.Dispose(); |
| 697 | 139 | | sceneDebugPlane = null; |
| | 140 | | } |
| | 141 | |
|
| 718 | 142 | | DisposeAllSceneComponents(); |
| | 143 | |
|
| 718 | 144 | | if (immediate) //!CommonScriptableObjects.rendererState.Get()) |
| | 145 | | { |
| 1 | 146 | | RemoveAllEntitiesImmediate(); |
| 1 | 147 | | } |
| | 148 | | else |
| | 149 | | { |
| 717 | 150 | | if (entities.Count > 0) |
| | 151 | | { |
| 254 | 152 | | this.gameObject.transform.position = EnvironmentSettings.MORDOR; |
| 254 | 153 | | this.gameObject.SetActive(false); |
| | 154 | |
|
| 254 | 155 | | RemoveAllEntities(); |
| 254 | 156 | | } |
| | 157 | | else |
| | 158 | | { |
| 463 | 159 | | Destroy(this.gameObject); |
| | 160 | | } |
| | 161 | | } |
| | 162 | |
|
| 718 | 163 | | isReleased = true; |
| 718 | 164 | | } |
| | 165 | |
|
| 0 | 166 | | public override string ToString() { return "Parcel Scene: " + base.ToString() + "\n" + sceneData.ToString(); } |
| | 167 | |
|
| | 168 | | public bool IsInsideSceneBoundaries(Bounds objectBounds) |
| | 169 | | { |
| 164 | 170 | | if (!IsInsideSceneBoundaries(objectBounds.min + CommonScriptableObjects.worldOffset, objectBounds.max.y)) |
| 120 | 171 | | return false; |
| 44 | 172 | | if (!IsInsideSceneBoundaries(objectBounds.max + CommonScriptableObjects.worldOffset, objectBounds.max.y)) |
| 14 | 173 | | return false; |
| | 174 | |
|
| 30 | 175 | | return true; |
| | 176 | | } |
| | 177 | |
|
| | 178 | | public virtual bool IsInsideSceneBoundaries(Vector2Int gridPosition, float height = 0f) |
| | 179 | | { |
| 1001 | 180 | | if (parcels.Count == 0) |
| 0 | 181 | | return false; |
| | 182 | |
|
| 1001 | 183 | | float heightLimit = metricsController.GetLimits().sceneHeight; |
| | 184 | |
|
| 1001 | 185 | | if (height > heightLimit) |
| 0 | 186 | | return false; |
| | 187 | |
|
| 1001 | 188 | | return parcels.Contains(gridPosition); |
| | 189 | | } |
| | 190 | |
|
| | 191 | | public virtual bool IsInsideSceneBoundaries(Vector3 worldPosition, float height = 0f) |
| | 192 | | { |
| 269 | 193 | | if (parcels.Count == 0) |
| 0 | 194 | | return false; |
| | 195 | |
|
| 269 | 196 | | float heightLimit = metricsController.GetLimits().sceneHeight; |
| 269 | 197 | | if (height > heightLimit) |
| 24 | 198 | | return false; |
| | 199 | |
|
| 245 | 200 | | int noThresholdZCoordinate = Mathf.FloorToInt(worldPosition.z / ParcelSettings.PARCEL_SIZE); |
| 245 | 201 | | int noThresholdXCoordinate = Mathf.FloorToInt(worldPosition.x / ParcelSettings.PARCEL_SIZE); |
| | 202 | |
|
| | 203 | | // We check the target world position |
| 245 | 204 | | Vector2Int targetCoordinate = new Vector2Int(noThresholdXCoordinate, noThresholdZCoordinate); |
| 245 | 205 | | if (parcels.Contains(targetCoordinate)) |
| 120 | 206 | | return true; |
| | 207 | |
|
| | 208 | | // We need to check using a threshold from the target point, in order to cover correctly the parcel "border/ |
| 125 | 209 | | Vector2Int coordinateMin = new Vector2Int(); |
| 125 | 210 | | coordinateMin.x = Mathf.FloorToInt((worldPosition.x - ParcelSettings.PARCEL_BOUNDARIES_THRESHOLD) / ParcelSe |
| 125 | 211 | | coordinateMin.y = Mathf.FloorToInt((worldPosition.z - ParcelSettings.PARCEL_BOUNDARIES_THRESHOLD) / ParcelSe |
| | 212 | |
|
| 125 | 213 | | Vector2Int coordinateMax = new Vector2Int(); |
| 125 | 214 | | coordinateMax.x = Mathf.FloorToInt((worldPosition.x + ParcelSettings.PARCEL_BOUNDARIES_THRESHOLD) / ParcelSe |
| 125 | 215 | | coordinateMax.y = Mathf.FloorToInt((worldPosition.z + ParcelSettings.PARCEL_BOUNDARIES_THRESHOLD) / ParcelSe |
| | 216 | |
|
| | 217 | | // We check the east/north-threshold position |
| 125 | 218 | | targetCoordinate.Set(coordinateMax.x, coordinateMax.y); |
| 125 | 219 | | if (parcels.Contains(targetCoordinate)) |
| 0 | 220 | | return true; |
| | 221 | |
|
| | 222 | | // We check the east/south-threshold position |
| 125 | 223 | | targetCoordinate.Set(coordinateMax.x, coordinateMin.y); |
| 125 | 224 | | if (parcels.Contains(targetCoordinate)) |
| 0 | 225 | | return true; |
| | 226 | |
|
| | 227 | | // We check the west/north-threshold position |
| 125 | 228 | | targetCoordinate.Set(coordinateMin.x, coordinateMax.y); |
| 125 | 229 | | if (parcels.Contains(targetCoordinate)) |
| 0 | 230 | | return true; |
| | 231 | |
|
| | 232 | | // We check the west/south-threshold position |
| 125 | 233 | | targetCoordinate.Set(coordinateMin.x, coordinateMin.y); |
| 125 | 234 | | if (parcels.Contains(targetCoordinate)) |
| 0 | 235 | | return true; |
| | 236 | |
|
| 125 | 237 | | return false; |
| | 238 | | } |
| | 239 | |
|
| 154 | 240 | | public Transform GetSceneTransform() { return transform; } |
| | 241 | |
|
| | 242 | | public IDCLEntity CreateEntity(string id) |
| | 243 | | { |
| 543 | 244 | | if (entities.ContainsKey(id)) |
| | 245 | | { |
| 1 | 246 | | return entities[id]; |
| | 247 | | } |
| | 248 | |
|
| 542 | 249 | | var newEntity = new DecentralandEntity(); |
| 542 | 250 | | newEntity.entityId = id; |
| | 251 | |
|
| 542 | 252 | | Environment.i.world.sceneController.EnsureEntityPool(); |
| | 253 | |
|
| | 254 | | // As we know that the pool already exists, we just get one gameobject from it |
| 542 | 255 | | PoolableObject po = PoolManager.i.Get(SceneController.EMPTY_GO_POOL_NAME); |
| | 256 | |
|
| 542 | 257 | | newEntity.meshesInfo.innerGameObject = po.gameObject; |
| 542 | 258 | | newEntity.gameObject = po.gameObject; |
| | 259 | |
|
| | 260 | | #if UNITY_EDITOR |
| 542 | 261 | | newEntity.gameObject.name = "ENTITY_" + id; |
| | 262 | | #endif |
| 542 | 263 | | newEntity.gameObject.transform.SetParent(gameObject.transform, false); |
| 542 | 264 | | newEntity.gameObject.SetActive(true); |
| 542 | 265 | | newEntity.scene = this; |
| | 266 | |
|
| 542 | 267 | | newEntity.OnCleanupEvent += po.OnCleanup; |
| | 268 | |
|
| 542 | 269 | | if (Environment.i.world.sceneBoundsChecker.enabled) |
| 455 | 270 | | newEntity.OnShapeUpdated += Environment.i.world.sceneBoundsChecker.AddEntityToBeChecked; |
| | 271 | |
|
| 542 | 272 | | entities.Add(id, newEntity); |
| | 273 | |
|
| 542 | 274 | | OnEntityAdded?.Invoke(newEntity); |
| | 275 | |
|
| 542 | 276 | | return newEntity; |
| | 277 | | } |
| | 278 | |
|
| | 279 | | public void RemoveEntity(string id, bool removeImmediatelyFromEntitiesList = true) |
| | 280 | | { |
| 520 | 281 | | if (entities.ContainsKey(id)) |
| | 282 | | { |
| 520 | 283 | | IDCLEntity entity = entities[id]; |
| | 284 | |
|
| 520 | 285 | | if (!entity.markedForCleanup) |
| | 286 | | { |
| | 287 | | // This will also cleanup its children |
| 520 | 288 | | CleanUpEntityRecursively(entity, removeImmediatelyFromEntitiesList); |
| | 289 | | } |
| | 290 | |
|
| 520 | 291 | | entities.Remove(id); |
| 520 | 292 | | } |
| | 293 | | #if UNITY_EDITOR || DEVELOPMENT_BUILD |
| | 294 | | else |
| | 295 | | { |
| 0 | 296 | | Debug.LogError($"Couldn't remove entity with ID: {id} as it doesn't exist."); |
| | 297 | | } |
| | 298 | | #endif |
| 0 | 299 | | } |
| | 300 | |
|
| | 301 | | void CleanUpEntityRecursively(IDCLEntity entity, bool removeImmediatelyFromEntitiesList) |
| | 302 | | { |
| | 303 | | // Iterate through all entity children |
| 529 | 304 | | using (var iterator = entity.children.GetEnumerator()) |
| | 305 | | { |
| 538 | 306 | | while (iterator.MoveNext()) |
| | 307 | | { |
| 9 | 308 | | CleanUpEntityRecursively(iterator.Current.Value, removeImmediatelyFromEntitiesList); |
| | 309 | | } |
| 529 | 310 | | } |
| | 311 | |
|
| 529 | 312 | | OnEntityRemoved?.Invoke(entity); |
| | 313 | |
|
| 529 | 314 | | if (Environment.i.world.sceneBoundsChecker.enabled) |
| | 315 | | { |
| 34 | 316 | | entity.OnShapeUpdated -= Environment.i.world.sceneBoundsChecker.AddEntityToBeChecked; |
| 34 | 317 | | Environment.i.world.sceneBoundsChecker.RemoveEntityToBeChecked(entity); |
| | 318 | | } |
| | 319 | |
|
| 529 | 320 | | if (removeImmediatelyFromEntitiesList) |
| | 321 | | { |
| | 322 | | // Every entity ends up being removed through here |
| 33 | 323 | | entity.Cleanup(); |
| 33 | 324 | | entities.Remove(entity.entityId); |
| 33 | 325 | | } |
| | 326 | | else |
| | 327 | | { |
| 496 | 328 | | Environment.i.platform.parcelScenesCleaner.MarkForCleanup(entity); |
| | 329 | | } |
| 496 | 330 | | } |
| | 331 | |
|
| | 332 | | void RemoveAllEntities(bool instant = false) |
| | 333 | | { |
| | 334 | | //NOTE(Brian): We need to remove only the rootEntities. |
| | 335 | | // If we don't, duplicated entities will get removed when destroying |
| | 336 | | // recursively, making this more complicated than it should. |
| 255 | 337 | | List<IDCLEntity> rootEntities = new List<IDCLEntity>(); |
| | 338 | |
|
| 255 | 339 | | using (var iterator = entities.GetEnumerator()) |
| | 340 | | { |
| 764 | 341 | | while (iterator.MoveNext()) |
| | 342 | | { |
| 509 | 343 | | if (iterator.Current.Value.parent == null) |
| | 344 | | { |
| 501 | 345 | | if (instant) |
| 0 | 346 | | rootEntities.Add(iterator.Current.Value); |
| | 347 | | else |
| 501 | 348 | | Environment.i.platform.parcelScenesCleaner.MarkRootEntityForCleanup(this, iterator.Current.V |
| | 349 | | } |
| | 350 | | } |
| 255 | 351 | | } |
| | 352 | |
|
| 255 | 353 | | if (instant) |
| | 354 | | { |
| 1 | 355 | | int rootEntitiesCount = rootEntities.Count; |
| 2 | 356 | | for (int i = 0; i < rootEntitiesCount; i++) |
| | 357 | | { |
| 0 | 358 | | IDCLEntity entity = rootEntities[i]; |
| 0 | 359 | | RemoveEntity(entity.entityId, instant); |
| | 360 | | } |
| | 361 | |
|
| 1 | 362 | | entities.Clear(); |
| | 363 | |
|
| 1 | 364 | | Destroy(this.gameObject); |
| | 365 | | } |
| 255 | 366 | | } |
| | 367 | |
|
| 2 | 368 | | private void RemoveAllEntitiesImmediate() { RemoveAllEntities(instant: true); } |
| | 369 | |
|
| | 370 | | public void SetEntityParent(string entityId, string parentId) |
| | 371 | | { |
| 14 | 372 | | if (entityId == parentId) |
| | 373 | | { |
| 0 | 374 | | return; |
| | 375 | | } |
| | 376 | |
|
| 14 | 377 | | IDCLEntity me = GetEntityForUpdate(entityId); |
| | 378 | |
|
| 14 | 379 | | if (me == null) |
| 0 | 380 | | return; |
| | 381 | |
|
| 14 | 382 | | if (parentId == "FirstPersonCameraEntityReference" || parentId == "PlayerEntityReference") // PlayerEntityRe |
| | 383 | | { |
| | 384 | | // In this case, the entity will attached to the first person camera |
| | 385 | | // On first person mode, the entity will rotate with the camera. On third person mode, the entity will r |
| 1 | 386 | | me.SetParent(DCLCharacterController.i.firstPersonCameraReference); |
| 1 | 387 | | Environment.i.world.sceneBoundsChecker.AddPersistent(me); |
| 1 | 388 | | } |
| 13 | 389 | | else if (parentId == "AvatarEntityReference" || parentId == "AvatarPositionEntityReference") // AvatarPositi |
| | 390 | | { |
| | 391 | | // In this case, the entity will be attached to the avatar |
| | 392 | | // It will simply rotate with the avatar, regardless of where the camera is pointing |
| 1 | 393 | | me.SetParent(DCLCharacterController.i.avatarReference); |
| 1 | 394 | | Environment.i.world.sceneBoundsChecker.AddPersistent(me); |
| 1 | 395 | | } |
| | 396 | | else |
| | 397 | | { |
| 12 | 398 | | if (me.parent == DCLCharacterController.i.firstPersonCameraReference || me.parent == DCLCharacterControl |
| | 399 | | { |
| 1 | 400 | | Environment.i.world.sceneBoundsChecker.RemoveEntityToBeChecked(me); |
| | 401 | | } |
| | 402 | |
|
| 12 | 403 | | if (parentId == "0") |
| | 404 | | { |
| | 405 | | // The entity will be child of the scene directly |
| 2 | 406 | | me.SetParent(null); |
| 2 | 407 | | me.gameObject.transform.SetParent(gameObject.transform, false); |
| 2 | 408 | | } |
| | 409 | | else |
| | 410 | | { |
| 10 | 411 | | IDCLEntity myParent = GetEntityForUpdate(parentId); |
| | 412 | |
|
| 10 | 413 | | if (myParent != null) |
| | 414 | | { |
| 10 | 415 | | me.SetParent(myParent); |
| | 416 | | } |
| | 417 | | } |
| | 418 | | } |
| | 419 | |
|
| 14 | 420 | | Environment.i.platform.cullingController.MarkDirty(); |
| 14 | 421 | | Environment.i.platform.physicsSyncController.MarkDirty(); |
| 14 | 422 | | } |
| | 423 | |
|
| | 424 | | /** |
| | 425 | | * This method is called when we need to attach a disposable component to the entity |
| | 426 | | */ |
| | 427 | | public void SharedComponentAttach(string entityId, string id) |
| | 428 | | { |
| 348 | 429 | | IDCLEntity decentralandEntity = GetEntityForUpdate(entityId); |
| | 430 | |
|
| 348 | 431 | | if (decentralandEntity == null) |
| | 432 | | { |
| 2 | 433 | | return; |
| | 434 | | } |
| | 435 | |
|
| 346 | 436 | | if (disposableComponents.TryGetValue(id, out ISharedComponent sharedComponent)) |
| | 437 | | { |
| 344 | 438 | | sharedComponent.AttachTo(decentralandEntity); |
| | 439 | | } |
| 346 | 440 | | } |
| | 441 | |
|
| | 442 | | public IEntityComponent EntityComponentCreateOrUpdateWithModel(string entityId, CLASS_ID_COMPONENT classId, obje |
| | 443 | | { |
| 313 | 444 | | IDCLEntity entity = GetEntityForUpdate(entityId); |
| | 445 | |
|
| 313 | 446 | | if (entity == null) |
| | 447 | | { |
| 0 | 448 | | Debug.LogError($"scene '{sceneData.id}': Can't create entity component if the entity {entityId} doesn't |
| 0 | 449 | | return null; |
| | 450 | | } |
| | 451 | |
|
| 313 | 452 | | IEntityComponent newComponent = null; |
| | 453 | |
|
| 313 | 454 | | if (classId == CLASS_ID_COMPONENT.UUID_CALLBACK) |
| | 455 | | { |
| 47 | 456 | | OnPointerEvent.Model model = JsonUtility.FromJson<OnPointerEvent.Model>(data as string); |
| 47 | 457 | | classId = model.GetClassIdFromType(); |
| | 458 | | } |
| | 459 | |
|
| 313 | 460 | | if (!entity.components.ContainsKey(classId)) |
| | 461 | | { |
| 241 | 462 | | var factory = Environment.i.world.componentFactory; |
| 241 | 463 | | newComponent = factory.CreateComponent((int) classId) as IEntityComponent; |
| | 464 | |
|
| 241 | 465 | | if (newComponent != null) |
| | 466 | | { |
| 241 | 467 | | entity.components.Add(classId, newComponent); |
| 241 | 468 | | OnComponentAdded?.Invoke(newComponent); |
| | 469 | |
|
| 241 | 470 | | newComponent.Initialize(this, entity); |
| | 471 | |
|
| 241 | 472 | | if (data is string json) |
| | 473 | | { |
| 205 | 474 | | newComponent.UpdateFromJSON(json); |
| 205 | 475 | | } |
| | 476 | | else |
| | 477 | | { |
| 36 | 478 | | newComponent.UpdateFromModel(data as BaseModel); |
| | 479 | | } |
| | 480 | | } |
| 36 | 481 | | } |
| | 482 | | else |
| | 483 | | { |
| 72 | 484 | | newComponent = EntityComponentUpdate(entity, classId, data as string); |
| | 485 | | } |
| | 486 | |
|
| 313 | 487 | | if (newComponent != null && newComponent is IOutOfSceneBoundariesHandler) |
| 14 | 488 | | Environment.i.world.sceneBoundsChecker?.AddEntityToBeChecked(entity); |
| | 489 | |
|
| 313 | 490 | | OnChanged?.Invoke(); |
| 313 | 491 | | Environment.i.platform.physicsSyncController.MarkDirty(); |
| 313 | 492 | | Environment.i.platform.cullingController.MarkDirty(); |
| 313 | 493 | | return newComponent; |
| | 494 | | } |
| | 495 | |
|
| 277 | 496 | | public IEntityComponent EntityComponentCreateOrUpdate(string entityId, CLASS_ID_COMPONENT classId, string data) |
| | 497 | |
|
| | 498 | | // The EntityComponentUpdate() parameters differ from other similar methods because there is no EntityComponentU |
| | 499 | | public IEntityComponent EntityComponentUpdate(IDCLEntity entity, CLASS_ID_COMPONENT classId, |
| | 500 | | string componentJson) |
| | 501 | | { |
| 89 | 502 | | if (entity == null) |
| | 503 | | { |
| 0 | 504 | | Debug.LogError($"Can't update the {classId} component of a nonexistent entity!", this); |
| 0 | 505 | | return null; |
| | 506 | | } |
| | 507 | |
|
| 89 | 508 | | if (!entity.components.ContainsKey(classId)) |
| | 509 | | { |
| 0 | 510 | | Debug.LogError($"Entity {entity.entityId} doesn't have a {classId} component to update!", this); |
| 0 | 511 | | return null; |
| | 512 | | } |
| | 513 | |
|
| 89 | 514 | | IComponent targetComponent = entity.components[classId]; |
| 89 | 515 | | targetComponent.UpdateFromJSON(componentJson); |
| | 516 | |
|
| 89 | 517 | | return targetComponent as IEntityComponent; |
| | 518 | | } |
| | 519 | |
|
| | 520 | | public ISharedComponent SharedComponentCreate(string id, int classId) |
| | 521 | | { |
| 541 | 522 | | if (disposableComponents.TryGetValue(id, out ISharedComponent component)) |
| 4 | 523 | | return component; |
| | 524 | |
|
| 537 | 525 | | if (classId == (int) CLASS_ID.UI_SCREEN_SPACE_SHAPE || classId == (int) CLASS_ID.UI_FULLSCREEN_SHAPE) |
| | 526 | | { |
| 43 | 527 | | if (GetSharedComponent<UIScreenSpace>() != null) |
| 0 | 528 | | return null; |
| | 529 | | } |
| | 530 | |
|
| 537 | 531 | | var factory = Environment.i.world.componentFactory; |
| 537 | 532 | | ISharedComponent newComponent = factory.CreateComponent(classId) as ISharedComponent; |
| | 533 | |
|
| 537 | 534 | | if (newComponent == null) |
| 0 | 535 | | return null; |
| | 536 | |
|
| 537 | 537 | | disposableComponents.Add(id, newComponent); |
| 537 | 538 | | OnAddSharedComponent?.Invoke(id, newComponent); |
| | 539 | |
|
| 537 | 540 | | newComponent.Initialize(this, id); |
| | 541 | |
|
| 537 | 542 | | return newComponent; |
| | 543 | | } |
| | 544 | |
|
| | 545 | | public void SharedComponentDispose(string id) |
| | 546 | | { |
| 528 | 547 | | if (disposableComponents.TryGetValue(id, out ISharedComponent sharedComponent)) |
| | 548 | | { |
| 528 | 549 | | sharedComponent?.Dispose(); |
| 528 | 550 | | disposableComponents.Remove(id); |
| 528 | 551 | | OnComponentRemoved?.Invoke(sharedComponent); |
| | 552 | | } |
| 0 | 553 | | } |
| | 554 | |
|
| | 555 | | public void EntityComponentRemove(string entityId, string name) |
| | 556 | | { |
| 18 | 557 | | IDCLEntity decentralandEntity = GetEntityForUpdate(entityId); |
| | 558 | |
|
| 18 | 559 | | if (decentralandEntity == null) |
| | 560 | | { |
| 0 | 561 | | return; |
| | 562 | | } |
| | 563 | |
|
| 18 | 564 | | RemoveEntityComponent(decentralandEntity, name); |
| 18 | 565 | | } |
| | 566 | |
|
| | 567 | | public T GetSharedComponent<T>() |
| | 568 | | where T : class |
| | 569 | | { |
| 166 | 570 | | return disposableComponents.Values.FirstOrDefault(x => x is T) as T; |
| | 571 | | } |
| | 572 | |
|
| | 573 | | private void RemoveComponentType<T>(IDCLEntity entity, CLASS_ID_COMPONENT classId) |
| | 574 | | where T : MonoBehaviour |
| | 575 | | { |
| 0 | 576 | | var component = entity.components[classId] as IEntityComponent; |
| | 577 | |
|
| 0 | 578 | | if (component == null) |
| 0 | 579 | | return; |
| | 580 | |
|
| 0 | 581 | | var monoBehaviour = component.GetTransform().GetComponent<T>(); |
| | 582 | |
|
| 0 | 583 | | if (monoBehaviour != null) |
| | 584 | | { |
| 0 | 585 | | Utils.SafeDestroy(monoBehaviour); |
| | 586 | | } |
| 0 | 587 | | } |
| | 588 | |
|
| | 589 | | private void RemoveEntityComponent(IDCLEntity entity, string componentName) |
| | 590 | | { |
| | 591 | | switch (componentName) |
| | 592 | | { |
| | 593 | | case "shape": |
| 0 | 594 | | if (entity.meshesInfo.currentShape is BaseShape baseShape) |
| | 595 | | { |
| 0 | 596 | | baseShape.DetachFrom(entity); |
| | 597 | | } |
| | 598 | |
|
| 0 | 599 | | return; |
| | 600 | |
|
| | 601 | | case OnClick.NAME: |
| | 602 | | { |
| 1 | 603 | | if ( entity.TryGetBaseComponent(CLASS_ID_COMPONENT.UUID_ON_CLICK, out IEntityComponent component |
| | 604 | | { |
| 1 | 605 | | Utils.SafeDestroy(component.GetTransform().gameObject); |
| 1 | 606 | | entity.components.Remove( CLASS_ID_COMPONENT.UUID_ON_CLICK ); |
| | 607 | | } |
| | 608 | |
|
| 1 | 609 | | return; |
| | 610 | | } |
| | 611 | | case OnPointerDown.NAME: |
| | 612 | | { |
| 1 | 613 | | if ( entity.TryGetBaseComponent(CLASS_ID_COMPONENT.UUID_ON_DOWN, out IEntityComponent component |
| | 614 | | { |
| 1 | 615 | | Utils.SafeDestroy(component.GetTransform().gameObject); |
| 1 | 616 | | entity.components.Remove( CLASS_ID_COMPONENT.UUID_ON_DOWN ); |
| | 617 | | } |
| | 618 | | } |
| 1 | 619 | | return; |
| | 620 | | case OnPointerUp.NAME: |
| | 621 | | { |
| 1 | 622 | | if ( entity.TryGetBaseComponent(CLASS_ID_COMPONENT.UUID_ON_UP, out IEntityComponent component )) |
| | 623 | | { |
| 1 | 624 | | Utils.SafeDestroy(component.GetTransform().gameObject); |
| 1 | 625 | | entity.components.Remove( CLASS_ID_COMPONENT.UUID_ON_UP ); |
| | 626 | | } |
| | 627 | | } |
| 1 | 628 | | return; |
| | 629 | | } |
| 0 | 630 | | } |
| | 631 | |
|
| | 632 | | public ISharedComponent SharedComponentUpdate(string id, BaseModel model) |
| | 633 | | { |
| 27 | 634 | | if (disposableComponents.TryGetValue(id, out ISharedComponent sharedComponent)) |
| | 635 | | { |
| 27 | 636 | | sharedComponent.UpdateFromModel(model); |
| 27 | 637 | | return sharedComponent; |
| | 638 | | } |
| | 639 | |
|
| 0 | 640 | | if (gameObject == null) |
| | 641 | | { |
| 0 | 642 | | Debug.LogError($"Unknown disposableComponent {id} -- scene has been destroyed?"); |
| 0 | 643 | | } |
| | 644 | | else |
| | 645 | | { |
| 0 | 646 | | Debug.LogError($"Unknown disposableComponent {id}", gameObject); |
| | 647 | | } |
| | 648 | |
|
| 0 | 649 | | return null; |
| | 650 | | } |
| | 651 | |
|
| | 652 | | public ISharedComponent SharedComponentUpdate(string id, string json) |
| | 653 | | { |
| 560 | 654 | | if (disposableComponents.TryGetValue(id, out ISharedComponent disposableComponent)) |
| | 655 | | { |
| 560 | 656 | | disposableComponent.UpdateFromJSON(json); |
| 560 | 657 | | return disposableComponent; |
| | 658 | | } |
| | 659 | |
|
| 0 | 660 | | if (gameObject == null) |
| | 661 | | { |
| 0 | 662 | | Debug.LogError($"Unknown disposableComponent {id} -- scene has been destroyed?"); |
| 0 | 663 | | } |
| | 664 | | else |
| | 665 | | { |
| 0 | 666 | | Debug.LogError($"Unknown disposableComponent {id}", gameObject); |
| | 667 | | } |
| | 668 | |
|
| 0 | 669 | | return null; |
| | 670 | | } |
| | 671 | |
|
| | 672 | | protected virtual void SendMetricsEvent() |
| | 673 | | { |
| 7124 | 674 | | if (Time.frameCount % 10 == 0) |
| 704 | 675 | | metricsController.SendEvent(); |
| 7124 | 676 | | } |
| | 677 | |
|
| | 678 | | public ISharedComponent GetSharedComponent(string componentId) |
| | 679 | | { |
| 469 | 680 | | if (!disposableComponents.TryGetValue(componentId, out ISharedComponent result)) |
| | 681 | | { |
| 426 | 682 | | return null; |
| | 683 | | } |
| | 684 | |
|
| 43 | 685 | | return result; |
| | 686 | | } |
| | 687 | |
|
| | 688 | | private IDCLEntity GetEntityForUpdate(string entityId) |
| | 689 | | { |
| 703 | 690 | | if (string.IsNullOrEmpty(entityId)) |
| | 691 | | { |
| 0 | 692 | | Debug.LogError("Null or empty entityId"); |
| 0 | 693 | | return null; |
| | 694 | | } |
| | 695 | |
|
| 703 | 696 | | if (!entities.TryGetValue(entityId, out IDCLEntity entity)) |
| | 697 | | { |
| 2 | 698 | | return null; |
| | 699 | | } |
| | 700 | |
|
| | 701 | | //NOTE(Brian): This is for removing stray null references? This should never happen. |
| | 702 | | // Maybe move to a different 'clean-up' method to make this method have a single responsibility? |
| 701 | 703 | | if (entity == null || entity.gameObject == null) |
| | 704 | | { |
| 0 | 705 | | entities.Remove(entityId); |
| 0 | 706 | | return null; |
| | 707 | | } |
| | 708 | |
|
| 701 | 709 | | return entity; |
| | 710 | | } |
| | 711 | |
|
| | 712 | | private void DisposeAllSceneComponents() |
| | 713 | | { |
| 1252 | 714 | | List<string> allDisposableComponents = disposableComponents.Select(x => x.Key).ToList(); |
| 2504 | 715 | | foreach (string id in allDisposableComponents) |
| | 716 | | { |
| 534 | 717 | | Environment.i.platform.parcelScenesCleaner.MarkDisposableComponentForCleanup(this, id); |
| | 718 | | } |
| 718 | 719 | | } |
| | 720 | |
|
| | 721 | | public string GetStateString() |
| | 722 | | { |
| 1373 | 723 | | string baseState = isPersistent ? "global-scene" : "scene"; |
| 1373 | 724 | | switch (sceneLifecycleHandler.state) |
| | 725 | | { |
| | 726 | | case SceneLifecycleHandler.State.NOT_READY: |
| 0 | 727 | | return $"{baseState}:{prettyName} - not ready..."; |
| | 728 | | case SceneLifecycleHandler.State.WAITING_FOR_INIT_MESSAGES: |
| 703 | 729 | | return $"{baseState}:{prettyName} - waiting for init messages..."; |
| | 730 | | case SceneLifecycleHandler.State.WAITING_FOR_COMPONENTS: |
| 0 | 731 | | if (disposableComponents != null && disposableComponents.Count > 0) |
| 0 | 732 | | return $"{baseState}:{prettyName} - left to ready:{disposableComponents.Count - sceneLifecycleHa |
| | 733 | | else |
| 0 | 734 | | return $"{baseState}:{prettyName} - no components. waiting..."; |
| | 735 | | case SceneLifecycleHandler.State.READY: |
| 670 | 736 | | return $"{baseState}:{prettyName} - ready!"; |
| | 737 | | } |
| | 738 | |
|
| 0 | 739 | | return $"scene:{prettyName} - no state?"; |
| | 740 | | } |
| | 741 | |
|
| | 742 | | public void RefreshLoadingState() |
| | 743 | | { |
| 1373 | 744 | | CalculateSceneLoadingState(); |
| | 745 | |
|
| | 746 | | #if UNITY_EDITOR |
| 1373 | 747 | | gameObject.name = GetStateString(); |
| | 748 | | #endif |
| 1373 | 749 | | } |
| | 750 | |
|
| | 751 | | [ContextMenu("Get Waiting Components Debug Info")] |
| | 752 | | public void GetWaitingComponentsDebugInfo() |
| | 753 | | { |
| 0 | 754 | | switch (sceneLifecycleHandler.state) |
| | 755 | | { |
| | 756 | | case SceneLifecycleHandler.State.WAITING_FOR_COMPONENTS: |
| | 757 | |
|
| 0 | 758 | | foreach (string componentId in sceneLifecycleHandler.disposableNotReady) |
| | 759 | | { |
| 0 | 760 | | if (disposableComponents.ContainsKey(componentId)) |
| | 761 | | { |
| 0 | 762 | | var component = disposableComponents[componentId]; |
| | 763 | |
|
| 0 | 764 | | Debug.Log($"Waiting for: {component.ToString()}"); |
| | 765 | |
|
| 0 | 766 | | foreach (var entity in component.GetAttachedEntities()) |
| | 767 | | { |
| 0 | 768 | | var loader = LoadableShape.GetLoaderForEntity(entity); |
| | 769 | |
|
| 0 | 770 | | string loadInfo = "No loader"; |
| | 771 | |
|
| 0 | 772 | | if (loader != null) |
| | 773 | | { |
| 0 | 774 | | loadInfo = loader.ToString(); |
| | 775 | | } |
| | 776 | |
|
| 0 | 777 | | Debug.Log($"This shape is attached to {entity.entityId} entity. Click here for highlight |
| | 778 | | } |
| | 779 | | } |
| | 780 | | else |
| | 781 | | { |
| 0 | 782 | | Debug.Log($"Waiting for missing component? id: {componentId}"); |
| | 783 | | } |
| | 784 | | } |
| | 785 | |
|
| | 786 | | break; |
| | 787 | |
|
| | 788 | | default: |
| 0 | 789 | | Debug.Log("This scene is not waiting for any components. Its current state is " + sceneLifecycleHand |
| | 790 | | break; |
| | 791 | | } |
| 0 | 792 | | } |
| | 793 | |
|
| | 794 | | /// <summary> |
| | 795 | | /// Calculates the current loading progress of the scene and raise the event OnLoadingStateUpdated with the perc |
| | 796 | | /// </summary> |
| | 797 | | public void CalculateSceneLoadingState() |
| | 798 | | { |
| 1373 | 799 | | loadingProgress = 0f; |
| | 800 | |
|
| 1373 | 801 | | if (sceneLifecycleHandler.state == SceneLifecycleHandler.State.WAITING_FOR_COMPONENTS || |
| | 802 | | sceneLifecycleHandler.state == SceneLifecycleHandler.State.READY) |
| | 803 | | { |
| 670 | 804 | | loadingProgress = disposableComponents != null && disposableComponents.Count > 0 ? (disposableComponents |
| | 805 | | } |
| | 806 | |
|
| 1373 | 807 | | OnLoadingStateUpdated?.Invoke(loadingProgress); |
| 0 | 808 | | } |
| | 809 | | } |
| | 810 | | } |