| | 1 | | using DCL.Controllers; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using DCL.Components; |
| | 5 | | using DCL.Models; |
| | 6 | | using UnityEngine; |
| | 7 | |
|
| | 8 | | namespace DCL |
| | 9 | | { |
| | 10 | | public class WorldState : IWorldState |
| | 11 | | { |
| 1170 | 12 | | public HashSet<string> readyScenes { get; set; } = new HashSet<string>(); |
| 5621 | 13 | | public Dictionary<string, IParcelScene> loadedScenes { get; set; } = new Dictionary<string, IParcelScene>(); |
| 4458 | 14 | | public List<IParcelScene> scenesSortedByDistance { get; set; } = new List<IParcelScene>(); |
| 1229 | 15 | | public List<string> globalSceneIds { get; set; } = new List<string>(); |
| 4244 | 16 | | public string currentSceneId { get; set; } = null; |
| | 17 | |
|
| | 18 | | public IParcelScene GetScene(string id) |
| | 19 | | { |
| 16 | 20 | | if (!Contains(id)) |
| 0 | 21 | | return null; |
| | 22 | |
|
| 16 | 23 | | return loadedScenes[id]; |
| | 24 | | } |
| | 25 | |
|
| | 26 | | public bool Contains(string id) |
| | 27 | | { |
| 450 | 28 | | if (string.IsNullOrEmpty(id) || !loadedScenes.ContainsKey(id)) |
| 1 | 29 | | return false; |
| | 30 | |
|
| 449 | 31 | | return true; |
| | 32 | | } |
| | 33 | |
|
| | 34 | | public bool TryGetScene(string id, out IParcelScene scene) |
| | 35 | | { |
| 24 | 36 | | scene = null; |
| | 37 | |
|
| 24 | 38 | | if (string.IsNullOrEmpty(id) || !loadedScenes.ContainsKey(id)) |
| 10 | 39 | | return false; |
| | 40 | |
|
| 14 | 41 | | scene = loadedScenes[id]; |
| 14 | 42 | | return true; |
| | 43 | | } |
| | 44 | |
|
| | 45 | | public bool TryGetScene<T>(string id, out T scene) |
| | 46 | | where T : class, IParcelScene |
| | 47 | | { |
| 0 | 48 | | scene = default(T); |
| 0 | 49 | | bool result = TryGetScene(id, out IParcelScene baseScene); |
| | 50 | |
|
| 0 | 51 | | if (result) |
| 0 | 52 | | scene = baseScene as T; |
| | 53 | |
|
| 0 | 54 | | if (scene == null) |
| 0 | 55 | | result = false; |
| | 56 | |
|
| 0 | 57 | | return result; |
| | 58 | | } |
| | 59 | |
|
| | 60 | | public HashSet<Vector2Int> GetAllLoadedScenesCoords() |
| | 61 | | { |
| 787 | 62 | | HashSet<Vector2Int> allLoadedParcelCoords = new HashSet<Vector2Int>(); |
| | 63 | |
|
| | 64 | | // Create fast (hashset) collection of loaded parcels coords |
| 1836 | 65 | | foreach (var element in loadedScenes) |
| | 66 | | { |
| 131 | 67 | | ParcelScene scene = element.Value as ParcelScene; |
| | 68 | |
|
| 131 | 69 | | if (!scene.sceneLifecycleHandler.isReady) |
| | 70 | | continue; |
| | 71 | |
|
| 5 | 72 | | allLoadedParcelCoords.UnionWith(scene.parcels); |
| | 73 | | } |
| | 74 | |
|
| 787 | 75 | | return allLoadedParcelCoords; |
| | 76 | | } |
| | 77 | |
|
| 766 | 78 | | private readonly Dictionary<GameObject, LoadWrapper> |
| | 79 | | attachedLoaders = new Dictionary<GameObject, LoadWrapper>(); |
| | 80 | |
|
| | 81 | | public LoadWrapper GetLoaderForEntity(IDCLEntity entity) |
| | 82 | | { |
| 234 | 83 | | if (entity.meshRootGameObject == null) |
| | 84 | | { |
| 0 | 85 | | Debug.LogWarning("NULL meshRootGameObject at GetLoaderForEntity()"); |
| 0 | 86 | | return null; |
| | 87 | | } |
| | 88 | |
|
| 234 | 89 | | attachedLoaders.TryGetValue(entity.meshRootGameObject, out LoadWrapper result); |
| 234 | 90 | | return result; |
| | 91 | | } |
| | 92 | |
|
| | 93 | | public T GetOrAddLoaderForEntity<T>(IDCLEntity entity) |
| | 94 | | where T : LoadWrapper, new() |
| | 95 | | { |
| 99 | 96 | | if (!attachedLoaders.TryGetValue(entity.meshRootGameObject, out LoadWrapper result)) |
| | 97 | | { |
| 97 | 98 | | result = new T(); |
| 97 | 99 | | attachedLoaders.Add(entity.meshRootGameObject, result); |
| | 100 | | } |
| | 101 | |
|
| 99 | 102 | | return result as T; |
| | 103 | | } |
| | 104 | |
|
| | 105 | | public void RemoveLoaderForEntity(IDCLEntity entity) |
| | 106 | | { |
| 76 | 107 | | if (entity == null || entity.meshRootGameObject == null) |
| 7 | 108 | | return; |
| | 109 | |
|
| 69 | 110 | | attachedLoaders.Remove(entity.meshRootGameObject); |
| 69 | 111 | | } |
| | 112 | |
|
| | 113 | | public void Dispose() |
| | 114 | | { |
| 761 | 115 | | } |
| | 116 | |
|
| | 117 | | public void Initialize() |
| | 118 | | { |
| 761 | 119 | | } |
| | 120 | | } |
| | 121 | | } |