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