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