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