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