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