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