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