| | 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 DCL.CRDT; |
| | 8 | | using DCL.Interface; |
| | 9 | | using UnityEngine; |
| | 10 | | using UnityEngine.Assertions; |
| | 11 | |
|
| | 12 | | namespace DCL.Controllers |
| | 13 | | { |
| | 14 | | public class ParcelScene : MonoBehaviour, IParcelScene |
| | 15 | | { |
| 930 | 16 | | public Dictionary<long, IDCLEntity> entities { get; private set; } = new Dictionary<long, IDCLEntity>(); |
| 3153 | 17 | | public IECSComponentsManagerLegacy componentsManagerLegacy { get; private set; } |
| 2948 | 18 | | public LoadParcelScenesMessage.UnityParcelScene sceneData { get; protected set; } |
| | 19 | |
|
| 564 | 20 | | public HashSet<Vector2Int> parcels = new HashSet<Vector2Int>(); |
| 49 | 21 | | public ISceneMetricsCounter metricsCounter { get; set; } |
| | 22 | | public event System.Action<IDCLEntity> OnEntityAdded; |
| | 23 | | public event System.Action<IDCLEntity> OnEntityRemoved; |
| | 24 | | public event System.Action<LoadParcelScenesMessage.UnityParcelScene> OnSetData; |
| | 25 | | public event System.Action<float> OnLoadingStateUpdated; |
| | 26 | |
|
| 148 | 27 | | public ContentProvider contentProvider { get; set; } |
| | 28 | |
|
| 0 | 29 | | public bool isTestScene { get; set; } = false; |
| 575 | 30 | | public bool isPersistent { get; set; } = false; |
| 0 | 31 | | public float loadingProgress { get; private set; } |
| | 32 | |
|
| | 33 | | [System.NonSerialized] |
| | 34 | | public string sceneName; |
| | 35 | |
|
| | 36 | | [System.NonSerialized] |
| 564 | 37 | | public bool unloadWithDistance = true; |
| | 38 | |
|
| | 39 | | SceneDebugPlane sceneDebugPlane = null; |
| | 40 | |
|
| | 41 | | public SceneLifecycleHandler sceneLifecycleHandler; |
| | 42 | |
|
| 0 | 43 | | public ICRDTExecutor crdtExecutor { get; private set; } |
| | 44 | |
|
| 0 | 45 | | public bool isReleased { get; private set; } |
| | 46 | |
|
| | 47 | | public void Awake() |
| | 48 | | { |
| 564 | 49 | | CommonScriptableObjects.worldOffset.OnChange += OnWorldReposition; |
| 564 | 50 | | componentsManagerLegacy = new ECSComponentsManagerLegacy(this); |
| 564 | 51 | | sceneLifecycleHandler = new SceneLifecycleHandler(this); |
| 564 | 52 | | metricsCounter = new SceneMetricsCounter(DataStore.i.sceneWorldObjects); |
| 564 | 53 | | crdtExecutor = new CRDTExecutor(this); |
| 564 | 54 | | } |
| | 55 | |
|
| | 56 | | private void OnDestroy() |
| | 57 | | { |
| 564 | 58 | | CommonScriptableObjects.worldOffset.OnChange -= OnWorldReposition; |
| 564 | 59 | | metricsCounter?.Dispose(); |
| 564 | 60 | | crdtExecutor?.Dispose(); |
| 564 | 61 | | } |
| | 62 | |
|
| 1136 | 63 | | void OnDisable() { metricsCounter?.Disable(); } |
| | 64 | |
|
| | 65 | | private void Update() |
| | 66 | | { |
| 14788 | 67 | | if (sceneLifecycleHandler.state == SceneLifecycleHandler.State.READY |
| | 68 | | && CommonScriptableObjects.rendererState.Get()) |
| 14151 | 69 | | SendMetricsEvent(); |
| 14788 | 70 | | } |
| | 71 | |
|
| 1082 | 72 | | protected virtual string prettyName => sceneData.basePosition.ToString(); |
| | 73 | |
|
| | 74 | | public virtual void SetData(LoadParcelScenesMessage.UnityParcelScene data) |
| | 75 | | { |
| 557 | 76 | | Assert.IsTrue( !string.IsNullOrEmpty(data.id), "Scene must have an ID!" ); |
| | 77 | |
|
| 557 | 78 | | this.sceneData = data; |
| | 79 | |
|
| 557 | 80 | | contentProvider = new ContentProvider(); |
| 557 | 81 | | contentProvider.baseUrl = data.baseUrl; |
| 557 | 82 | | contentProvider.contents = data.contents; |
| 557 | 83 | | contentProvider.BakeHashes(); |
| | 84 | |
|
| 557 | 85 | | parcels.Clear(); |
| | 86 | |
|
| 2260 | 87 | | for (int i = 0; i < sceneData.parcels.Length; i++) |
| | 88 | | { |
| 573 | 89 | | parcels.Add(sceneData.parcels[i]); |
| | 90 | | } |
| | 91 | |
|
| 557 | 92 | | gameObject.transform.position = |
| | 93 | | PositionUtils.WorldToUnityPosition(Utils.GridToWorldPosition(data.basePosition.x, data.basePosition.y)); |
| | 94 | |
|
| 557 | 95 | | DataStore.i.sceneWorldObjects.AddScene(sceneData.id); |
| | 96 | |
|
| 557 | 97 | | metricsCounter.Configure(sceneData.id, sceneData.basePosition, sceneData.parcels.Length); |
| 557 | 98 | | metricsCounter.Enable(); |
| | 99 | |
|
| 557 | 100 | | OnSetData?.Invoke(data); |
| 557 | 101 | | } |
| | 102 | |
|
| | 103 | | void OnWorldReposition(Vector3 current, Vector3 previous) |
| | 104 | | { |
| 2 | 105 | | Vector3 sceneWorldPos = Utils.GridToWorldPosition(sceneData.basePosition.x, sceneData.basePosition.y); |
| 2 | 106 | | gameObject.transform.position = PositionUtils.WorldToUnityPosition(sceneWorldPos); |
| 2 | 107 | | } |
| | 108 | |
|
| | 109 | | public virtual void SetUpdateData(LoadParcelScenesMessage.UnityParcelScene data) |
| | 110 | | { |
| 15 | 111 | | contentProvider = new ContentProvider(); |
| 15 | 112 | | contentProvider.baseUrl = data.baseUrl; |
| 15 | 113 | | contentProvider.contents = data.contents; |
| 15 | 114 | | contentProvider.BakeHashes(); |
| 15 | 115 | | } |
| | 116 | |
|
| | 117 | | public void InitializeDebugPlane() |
| | 118 | | { |
| 403 | 119 | | if (EnvironmentSettings.DEBUG && sceneData.parcels != null && sceneDebugPlane == null) |
| | 120 | | { |
| 401 | 121 | | sceneDebugPlane = new SceneDebugPlane(sceneData, gameObject.transform); |
| | 122 | | } |
| 403 | 123 | | } |
| | 124 | |
|
| | 125 | | public void RemoveDebugPlane() |
| | 126 | | { |
| 0 | 127 | | if (sceneDebugPlane != null) |
| | 128 | | { |
| 0 | 129 | | sceneDebugPlane.Dispose(); |
| 0 | 130 | | sceneDebugPlane = null; |
| | 131 | | } |
| 0 | 132 | | } |
| | 133 | |
|
| | 134 | | public void Cleanup(bool immediate) |
| | 135 | | { |
| 458 | 136 | | if (isReleased || gameObject == null) |
| 0 | 137 | | return; |
| | 138 | |
|
| 458 | 139 | | if (sceneDebugPlane != null) |
| | 140 | | { |
| 401 | 141 | | sceneDebugPlane.Dispose(); |
| 401 | 142 | | sceneDebugPlane = null; |
| | 143 | | } |
| | 144 | |
|
| 458 | 145 | | componentsManagerLegacy.DisposeAllSceneComponents(); |
| | 146 | |
|
| 458 | 147 | | if (immediate) //!CommonScriptableObjects.rendererState.Get()) |
| | 148 | | { |
| 420 | 149 | | RemoveAllEntitiesImmediate(); |
| 420 | 150 | | PoolManager.i.Cleanup(true, true); |
| 420 | 151 | | DataStore.i.sceneWorldObjects.RemoveScene(sceneData.id); |
| 420 | 152 | | } |
| | 153 | | else |
| | 154 | | { |
| 38 | 155 | | if (entities.Count > 0) |
| | 156 | | { |
| 20 | 157 | | this.gameObject.transform.position = EnvironmentSettings.MORDOR; |
| 20 | 158 | | this.gameObject.SetActive(false); |
| | 159 | |
|
| 20 | 160 | | RemoveAllEntities(); |
| 20 | 161 | | } |
| | 162 | | else |
| | 163 | | { |
| 18 | 164 | | Destroy(this.gameObject); |
| 18 | 165 | | DataStore.i.sceneWorldObjects.RemoveScene(sceneData.id); |
| | 166 | | } |
| | 167 | | } |
| | 168 | |
|
| 458 | 169 | | isReleased = true; |
| 458 | 170 | | } |
| | 171 | |
|
| 0 | 172 | | public override string ToString() { return "Parcel Scene: " + base.ToString() + "\n" + sceneData; } |
| | 173 | |
|
| | 174 | | public string GetSceneName() |
| | 175 | | { |
| 0 | 176 | | return string.IsNullOrEmpty(sceneName) ? "Unnamed" : sceneName; |
| | 177 | | } |
| | 178 | |
|
| | 179 | | public bool IsInsideSceneBoundaries(Bounds objectBounds) |
| | 180 | | { |
| 150 | 181 | | if (!IsInsideSceneBoundaries(objectBounds.min + CommonScriptableObjects.worldOffset, objectBounds.max.y)) |
| 112 | 182 | | return false; |
| 38 | 183 | | if (!IsInsideSceneBoundaries(objectBounds.max + CommonScriptableObjects.worldOffset, objectBounds.max.y)) |
| 14 | 184 | | return false; |
| | 185 | |
|
| 24 | 186 | | return true; |
| | 187 | | } |
| | 188 | |
|
| | 189 | | public virtual bool IsInsideSceneBoundaries(Vector2Int gridPosition, float height = 0f) |
| | 190 | | { |
| 155 | 191 | | if (parcels.Count == 0) |
| 0 | 192 | | return false; |
| | 193 | |
|
| 155 | 194 | | float heightLimit = metricsCounter.maxCount.sceneHeight; |
| | 195 | |
|
| 155 | 196 | | if (height > heightLimit) |
| 0 | 197 | | return false; |
| | 198 | |
|
| 155 | 199 | | return parcels.Contains(gridPosition); |
| | 200 | | } |
| | 201 | |
|
| | 202 | | public virtual bool IsInsideSceneBoundaries(Vector3 worldPosition, float height = 0f) |
| | 203 | | { |
| 226 | 204 | | if (parcels.Count == 0) |
| 0 | 205 | | return false; |
| | 206 | |
|
| 226 | 207 | | float heightLimit = metricsCounter.maxCount.sceneHeight; |
| 226 | 208 | | if (height > heightLimit) |
| 22 | 209 | | return false; |
| | 210 | |
|
| 204 | 211 | | int noThresholdZCoordinate = Mathf.FloorToInt(worldPosition.z / ParcelSettings.PARCEL_SIZE); |
| 204 | 212 | | int noThresholdXCoordinate = Mathf.FloorToInt(worldPosition.x / ParcelSettings.PARCEL_SIZE); |
| | 213 | |
|
| | 214 | | // We check the target world position |
| 204 | 215 | | Vector2Int targetCoordinate = new Vector2Int(noThresholdXCoordinate, noThresholdZCoordinate); |
| 204 | 216 | | if (parcels.Contains(targetCoordinate)) |
| 87 | 217 | | return true; |
| | 218 | |
|
| | 219 | | // We need to check using a threshold from the target point, in order to cover correctly the parcel "border/ |
| 117 | 220 | | Vector2Int coordinateMin = new Vector2Int(); |
| 117 | 221 | | coordinateMin.x = Mathf.FloorToInt((worldPosition.x - ParcelSettings.PARCEL_BOUNDARIES_THRESHOLD) / ParcelSe |
| 117 | 222 | | coordinateMin.y = Mathf.FloorToInt((worldPosition.z - ParcelSettings.PARCEL_BOUNDARIES_THRESHOLD) / ParcelSe |
| | 223 | |
|
| 117 | 224 | | Vector2Int coordinateMax = new Vector2Int(); |
| 117 | 225 | | coordinateMax.x = Mathf.FloorToInt((worldPosition.x + ParcelSettings.PARCEL_BOUNDARIES_THRESHOLD) / ParcelSe |
| 117 | 226 | | coordinateMax.y = Mathf.FloorToInt((worldPosition.z + ParcelSettings.PARCEL_BOUNDARIES_THRESHOLD) / ParcelSe |
| | 227 | |
|
| | 228 | | // We check the east/north-threshold position |
| 117 | 229 | | targetCoordinate.Set(coordinateMax.x, coordinateMax.y); |
| 117 | 230 | | if (parcels.Contains(targetCoordinate)) |
| 0 | 231 | | return true; |
| | 232 | |
|
| | 233 | | // We check the east/south-threshold position |
| 117 | 234 | | targetCoordinate.Set(coordinateMax.x, coordinateMin.y); |
| 117 | 235 | | if (parcels.Contains(targetCoordinate)) |
| 0 | 236 | | return true; |
| | 237 | |
|
| | 238 | | // We check the west/north-threshold position |
| 117 | 239 | | targetCoordinate.Set(coordinateMin.x, coordinateMax.y); |
| 117 | 240 | | if (parcels.Contains(targetCoordinate)) |
| 0 | 241 | | return true; |
| | 242 | |
|
| | 243 | | // We check the west/south-threshold position |
| 117 | 244 | | targetCoordinate.Set(coordinateMin.x, coordinateMin.y); |
| 117 | 245 | | if (parcels.Contains(targetCoordinate)) |
| 0 | 246 | | return true; |
| | 247 | |
|
| 117 | 248 | | return false; |
| | 249 | | } |
| | 250 | |
|
| 0 | 251 | | public IDCLEntity GetEntityById(string entityId) { throw new System.NotImplementedException(); } |
| 58 | 252 | | public Transform GetSceneTransform() { return transform; } |
| | 253 | |
|
| | 254 | | public IDCLEntity CreateEntity(long id) |
| | 255 | | { |
| 601 | 256 | | if (entities.ContainsKey(id)) |
| | 257 | | { |
| 2 | 258 | | return entities[id]; |
| | 259 | | } |
| | 260 | |
|
| 599 | 261 | | var newEntity = new DecentralandEntity(); |
| 599 | 262 | | newEntity.entityId = id; |
| | 263 | |
|
| 599 | 264 | | PoolManagerFactory.EnsureEntityPool(false); |
| | 265 | |
|
| | 266 | | // As we know that the pool already exists, we just get one gameobject from it |
| 599 | 267 | | PoolableObject po = PoolManager.i.Get(PoolManagerFactory.EMPTY_GO_POOL_NAME); |
| | 268 | |
|
| 599 | 269 | | newEntity.meshesInfo.innerGameObject = po.gameObject; |
| 599 | 270 | | newEntity.gameObject = po.gameObject; |
| | 271 | |
|
| | 272 | | #if UNITY_EDITOR |
| 599 | 273 | | newEntity.gameObject.name = "ENTITY_" + id; |
| | 274 | | #endif |
| 599 | 275 | | newEntity.gameObject.transform.SetParent(gameObject.transform, false); |
| 599 | 276 | | newEntity.gameObject.SetActive(true); |
| 599 | 277 | | newEntity.scene = this; |
| | 278 | |
|
| 599 | 279 | | newEntity.OnCleanupEvent += po.OnCleanup; |
| | 280 | |
|
| 599 | 281 | | if (Environment.i.world.sceneBoundsChecker.enabled) |
| 446 | 282 | | newEntity.OnShapeUpdated += Environment.i.world.sceneBoundsChecker.AddEntityToBeChecked; |
| | 283 | |
|
| 599 | 284 | | entities.Add(id, newEntity); |
| | 285 | |
|
| 599 | 286 | | DataStore.i.sceneWorldObjects.sceneData[sceneData.id].owners.Add(id); |
| | 287 | |
|
| 599 | 288 | | OnEntityAdded?.Invoke(newEntity); |
| | 289 | |
|
| 599 | 290 | | return newEntity; |
| | 291 | | } |
| | 292 | |
|
| | 293 | | public void RemoveEntity(long id, bool removeImmediatelyFromEntitiesList = true) |
| | 294 | | { |
| 520 | 295 | | if (entities.ContainsKey(id)) |
| | 296 | | { |
| 520 | 297 | | IDCLEntity entity = entities[id]; |
| | 298 | |
|
| 520 | 299 | | if (!entity.markedForCleanup) |
| | 300 | | { |
| | 301 | | // This will also cleanup its children |
| 520 | 302 | | CleanUpEntityRecursively(entity, removeImmediatelyFromEntitiesList); |
| | 303 | | } |
| | 304 | |
|
| 520 | 305 | | entities.Remove(id); |
| | 306 | |
|
| 520 | 307 | | var data = DataStore.i.sceneWorldObjects.sceneData; |
| | 308 | |
|
| 520 | 309 | | if (data.ContainsKey(sceneData.id)) |
| | 310 | | { |
| 74 | 311 | | data[sceneData.id].owners.Remove(id); |
| | 312 | | } |
| 74 | 313 | | } |
| | 314 | | #if UNITY_EDITOR || DEVELOPMENT_BUILD |
| | 315 | | else |
| | 316 | | { |
| 0 | 317 | | Debug.LogWarning($"Couldn't remove entity with ID: {id} as it doesn't exist."); |
| | 318 | | } |
| | 319 | | #endif |
| 446 | 320 | | } |
| | 321 | |
|
| | 322 | | void CleanUpEntityRecursively(IDCLEntity entity, bool removeImmediatelyFromEntitiesList) |
| | 323 | | { |
| | 324 | | // Iterate through all entity children |
| 528 | 325 | | using (var iterator = entity.children.GetEnumerator()) |
| | 326 | | { |
| 536 | 327 | | while (iterator.MoveNext()) |
| | 328 | | { |
| 8 | 329 | | CleanUpEntityRecursively(iterator.Current.Value, removeImmediatelyFromEntitiesList); |
| | 330 | | } |
| 528 | 331 | | } |
| | 332 | |
|
| 528 | 333 | | OnEntityRemoved?.Invoke(entity); |
| | 334 | |
|
| 528 | 335 | | if (Environment.i.world.sceneBoundsChecker.enabled) |
| | 336 | | { |
| 443 | 337 | | entity.OnShapeUpdated -= Environment.i.world.sceneBoundsChecker.AddEntityToBeChecked; |
| 443 | 338 | | Environment.i.world.sceneBoundsChecker.RemoveEntityToBeChecked(entity); |
| | 339 | | } |
| | 340 | |
|
| 528 | 341 | | if (removeImmediatelyFromEntitiesList) |
| | 342 | | { |
| | 343 | | // Every entity ends up being removed through here |
| 527 | 344 | | entity.Cleanup(); |
| 527 | 345 | | entities.Remove(entity.entityId); |
| 527 | 346 | | } |
| | 347 | | else |
| | 348 | | { |
| 1 | 349 | | Environment.i.platform.parcelScenesCleaner.MarkForCleanup(entity); |
| | 350 | | } |
| 1 | 351 | | } |
| | 352 | |
|
| | 353 | | void RemoveAllEntities(bool instant = false) |
| | 354 | | { |
| | 355 | | //NOTE(Brian): We need to remove only the rootEntities. |
| | 356 | | // If we don't, duplicated entities will get removed when destroying |
| | 357 | | // recursively, making this more complicated than it should. |
| 440 | 358 | | List<IDCLEntity> rootEntities = new List<IDCLEntity>(); |
| | 359 | |
|
| 440 | 360 | | using (var iterator = entities.GetEnumerator()) |
| | 361 | | { |
| 949 | 362 | | while (iterator.MoveNext()) |
| | 363 | | { |
| 509 | 364 | | if (iterator.Current.Value.parent == null) |
| | 365 | | { |
| 502 | 366 | | if (instant) |
| 482 | 367 | | rootEntities.Add(iterator.Current.Value); |
| | 368 | | else |
| 20 | 369 | | Environment.i.platform.parcelScenesCleaner.MarkRootEntityForCleanup(this, iterator.Current.V |
| | 370 | | } |
| | 371 | | } |
| 440 | 372 | | } |
| | 373 | |
|
| 440 | 374 | | if (instant) |
| | 375 | | { |
| 420 | 376 | | int rootEntitiesCount = rootEntities.Count; |
| 1804 | 377 | | for (int i = 0; i < rootEntitiesCount; i++) |
| | 378 | | { |
| 482 | 379 | | IDCLEntity entity = rootEntities[i]; |
| 482 | 380 | | RemoveEntity(entity.entityId, instant); |
| | 381 | | } |
| | 382 | |
|
| 420 | 383 | | entities.Clear(); |
| | 384 | |
|
| 420 | 385 | | if (gameObject != null) |
| 420 | 386 | | Destroy(gameObject); |
| | 387 | | } |
| 440 | 388 | | } |
| | 389 | |
|
| 840 | 390 | | private void RemoveAllEntitiesImmediate() { RemoveAllEntities(instant: true); } |
| | 391 | |
|
| | 392 | | public void SetEntityParent(long entityId, long parentId) |
| | 393 | | { |
| 16 | 394 | | if (entityId == parentId) |
| | 395 | | { |
| 0 | 396 | | return; |
| | 397 | | } |
| | 398 | |
|
| 16 | 399 | | IDCLEntity me = GetEntityById(entityId); |
| | 400 | |
|
| 16 | 401 | | if (me == null) |
| 0 | 402 | | return; |
| | 403 | |
|
| 16 | 404 | | Environment.i.platform.cullingController.MarkDirty(); |
| 16 | 405 | | Environment.i.platform.physicsSyncController.MarkDirty(); |
| | 406 | |
|
| 16 | 407 | | DataStore_World worldData = DataStore.i.Get<DataStore_World>(); |
| 16 | 408 | | Transform avatarTransform = worldData.avatarTransform.Get(); |
| 16 | 409 | | Transform firstPersonCameraTransform = worldData.fpsTransform.Get(); |
| | 410 | |
|
| | 411 | | // CONST_THIRD_PERSON_CAMERA_ENTITY_REFERENCE is for compatibility purposes |
| 16 | 412 | | if (parentId == (long) SpecialEntityId.FIRST_PERSON_CAMERA_ENTITY_REFERENCE || |
| | 413 | | parentId == (long) SpecialEntityId.THIRD_PERSON_CAMERA_ENTITY_REFERENCE) |
| | 414 | | { |
| | 415 | |
|
| 1 | 416 | | if (firstPersonCameraTransform == null) |
| | 417 | | { |
| 0 | 418 | | Debug.LogError("FPS transform is null when trying to set parent! " + sceneData.id); |
| 0 | 419 | | return; |
| | 420 | | } |
| | 421 | |
|
| | 422 | |
|
| | 423 | | // In this case, the entity will attached to the first person camera |
| | 424 | | // On first person mode, the entity will rotate with the camera. On third person mode, the entity will r |
| 1 | 425 | | me.SetParent(null); |
| 1 | 426 | | me.gameObject.transform.SetParent(firstPersonCameraTransform, false); |
| 1 | 427 | | Environment.i.world.sceneBoundsChecker.RemoveEntityToBeChecked(me); |
| 1 | 428 | | Environment.i.world.sceneBoundsChecker.AddPersistent(me); |
| 1 | 429 | | return; |
| | 430 | | } |
| | 431 | |
|
| 15 | 432 | | if (parentId == (long) SpecialEntityId.AVATAR_ENTITY_REFERENCE || |
| | 433 | | parentId == (long) SpecialEntityId |
| | 434 | | .AVATAR_POSITION_REFERENCE) // AvatarPositionEntityReference is for compatibility purposes |
| | 435 | | { |
| 1 | 436 | | if (avatarTransform == null) |
| | 437 | | { |
| 0 | 438 | | Debug.LogError("Avatar transform is null when trying to set parent! " + sceneData.id); |
| 0 | 439 | | return; |
| | 440 | | } |
| | 441 | |
|
| | 442 | | // In this case, the entity will be attached to the avatar |
| | 443 | | // It will simply rotate with the avatar, regardless of where the camera is pointing |
| 1 | 444 | | me.SetParent(null); |
| 1 | 445 | | me.gameObject.transform.SetParent(avatarTransform, false); |
| 1 | 446 | | Environment.i.world.sceneBoundsChecker.RemoveEntityToBeChecked(me); |
| 1 | 447 | | Environment.i.world.sceneBoundsChecker.AddPersistent(me); |
| 1 | 448 | | return; |
| | 449 | | } |
| | 450 | |
|
| | 451 | | // Remove from persistent checks if it was formerly added as child of avatarTransform or fpsTransform |
| 14 | 452 | | if (me.gameObject.transform.parent == avatarTransform || |
| | 453 | | me.gameObject.transform.parent == firstPersonCameraTransform) |
| | 454 | | { |
| 2 | 455 | | if (Environment.i.world.sceneBoundsChecker.WasAddedAsPersistent(me)) |
| 2 | 456 | | Environment.i.world.sceneBoundsChecker.RemovePersistent(me); |
| | 457 | | } |
| | 458 | |
|
| 14 | 459 | | if (parentId == (long) SpecialEntityId.SCENE_ROOT_ENTITY) |
| | 460 | | { |
| | 461 | | // The entity will be child of the scene directly |
| 2 | 462 | | me.SetParent(null); |
| 2 | 463 | | me.gameObject.transform.SetParent(gameObject.transform, false); |
| 2 | 464 | | } |
| | 465 | | else |
| | 466 | | { |
| 12 | 467 | | IDCLEntity myParent = GetEntityById(parentId); |
| | 468 | |
|
| 12 | 469 | | if (myParent != null) |
| | 470 | | { |
| 12 | 471 | | me.SetParent(myParent); |
| | 472 | | } |
| | 473 | | } |
| | 474 | |
|
| 12 | 475 | | } |
| | 476 | |
|
| | 477 | | protected virtual void SendMetricsEvent() |
| | 478 | | { |
| 14151 | 479 | | if (Time.frameCount % 10 == 0) |
| 1394 | 480 | | metricsCounter.SendEvent(); |
| 14151 | 481 | | } |
| | 482 | |
|
| | 483 | | public IDCLEntity GetEntityById(long entityId) |
| | 484 | | { |
| 948 | 485 | | if (!entities.TryGetValue(entityId, out IDCLEntity entity)) |
| | 486 | | { |
| 2 | 487 | | return null; |
| | 488 | | } |
| | 489 | |
|
| | 490 | | //NOTE(Brian): This is for removing stray null references? This should never happen. |
| | 491 | | // Maybe move to a different 'clean-up' method to make this method have a single responsibility? |
| 946 | 492 | | if (entity == null || entity.gameObject == null) |
| | 493 | | { |
| 0 | 494 | | entities.Remove(entityId); |
| 0 | 495 | | return null; |
| | 496 | | } |
| | 497 | |
|
| 946 | 498 | | return entity; |
| | 499 | | } |
| | 500 | |
|
| | 501 | | public string GetStateString() |
| | 502 | | { |
| 1082 | 503 | | string baseState = isPersistent ? "global-scene" : "scene"; |
| 1082 | 504 | | switch (sceneLifecycleHandler.state) |
| | 505 | | { |
| | 506 | | case SceneLifecycleHandler.State.NOT_READY: |
| 0 | 507 | | return $"{baseState}:{prettyName} - not ready..."; |
| | 508 | | case SceneLifecycleHandler.State.WAITING_FOR_INIT_MESSAGES: |
| 557 | 509 | | return $"{baseState}:{prettyName} - waiting for init messages..."; |
| | 510 | | case SceneLifecycleHandler.State.WAITING_FOR_COMPONENTS: |
| 0 | 511 | | return $"{baseState}:{prettyName} - {sceneLifecycleHandler.sceneResourcesLoadTracker.GetStateString( |
| | 512 | | case SceneLifecycleHandler.State.READY: |
| 525 | 513 | | return $"{baseState}:{prettyName} - ready!"; |
| | 514 | | } |
| | 515 | |
|
| 0 | 516 | | return $"scene:{prettyName} - no state?"; |
| | 517 | | } |
| | 518 | |
|
| | 519 | | public void RefreshLoadingState() |
| | 520 | | { |
| | 521 | | #if UNITY_STANDALONE || UNITY_EDITOR |
| 1082 | 522 | | if (DataStore.i.common.isApplicationQuitting.Get()) |
| 0 | 523 | | return; |
| | 524 | | #endif |
| | 525 | |
|
| 1082 | 526 | | CalculateSceneLoadingState(); |
| | 527 | |
|
| | 528 | | #if UNITY_EDITOR |
| 1082 | 529 | | gameObject.name = GetStateString(); |
| | 530 | | #endif |
| 1082 | 531 | | } |
| | 532 | |
|
| | 533 | | [ContextMenu("Get Waiting Components Debug Info")] |
| | 534 | | public void GetWaitingComponentsDebugInfo() |
| | 535 | | { |
| 0 | 536 | | switch (sceneLifecycleHandler.state) |
| | 537 | | { |
| | 538 | | case SceneLifecycleHandler.State.WAITING_FOR_COMPONENTS: |
| 0 | 539 | | sceneLifecycleHandler.sceneResourcesLoadTracker.PrintWaitingResourcesDebugInfo(); |
| 0 | 540 | | break; |
| | 541 | |
|
| | 542 | | default: |
| 0 | 543 | | Debug.Log("This scene is not waiting for any components. Its current state is " + sceneLifecycleHand |
| | 544 | | break; |
| | 545 | | } |
| 0 | 546 | | } |
| | 547 | |
|
| | 548 | | /// <summary> |
| | 549 | | /// Calculates the current loading progress of the scene and raise the event OnLoadingStateUpdated with the perc |
| | 550 | | /// </summary> |
| | 551 | | public void CalculateSceneLoadingState() |
| | 552 | | { |
| 1082 | 553 | | loadingProgress = 0f; |
| | 554 | |
|
| 1082 | 555 | | if (sceneLifecycleHandler.state == SceneLifecycleHandler.State.WAITING_FOR_COMPONENTS || |
| | 556 | | sceneLifecycleHandler.state == SceneLifecycleHandler.State.READY) |
| | 557 | | { |
| 525 | 558 | | loadingProgress = sceneLifecycleHandler.loadingProgress; |
| | 559 | | } |
| | 560 | |
|
| 1082 | 561 | | OnLoadingStateUpdated?.Invoke(loadingProgress); |
| 0 | 562 | | } |
| | 563 | | } |
| | 564 | | } |