| | 1 | | using DCL.Controllers; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using UnityEngine; |
| | 5 | |
|
| | 6 | | namespace DCL |
| | 7 | | { |
| | 8 | | public class WorldState : IWorldState |
| | 9 | | { |
| 1375 | 10 | | public HashSet<string> readyScenes { get; set; } = new HashSet<string>(); |
| 7137 | 11 | | public Dictionary<string, IParcelScene> loadedScenes { get; set; } = new Dictionary<string, IParcelScene>(); |
| 6031 | 12 | | public List<IParcelScene> scenesSortedByDistance { get; set; } = new List<IParcelScene>(); |
| | 13 | |
|
| 1561 | 14 | | public List<string> globalSceneIds { get; set; } |
| 17630 | 15 | | public string currentSceneId { get; set; } |
| | 16 | |
|
| | 17 | | public void Initialize() |
| | 18 | | { |
| 689 | 19 | | globalSceneIds = new List<string>(); |
| 689 | 20 | | currentSceneId = null; |
| 689 | 21 | | readyScenes = new HashSet<string>(); |
| 689 | 22 | | loadedScenes = new Dictionary<string, IParcelScene>(); |
| 689 | 23 | | scenesSortedByDistance = new List<IParcelScene>(); |
| 689 | 24 | | } |
| | 25 | |
|
| | 26 | | public IParcelScene GetScene(string id) |
| | 27 | | { |
| 17 | 28 | | if (!Contains(id)) |
| 0 | 29 | | return null; |
| | 30 | |
|
| 17 | 31 | | return loadedScenes[id]; |
| | 32 | | } |
| | 33 | |
|
| | 34 | | public bool Contains(string id) |
| | 35 | | { |
| 744 | 36 | | if (string.IsNullOrEmpty(id) || !loadedScenes.ContainsKey(id)) |
| 0 | 37 | | return false; |
| | 38 | |
|
| 744 | 39 | | return true; |
| | 40 | | } |
| | 41 | |
|
| | 42 | | public bool TryGetScene(string id, out IParcelScene scene) |
| | 43 | | { |
| 4038 | 44 | | scene = null; |
| | 45 | |
|
| 4038 | 46 | | if (string.IsNullOrEmpty(id) || !loadedScenes.ContainsKey(id)) |
| 2012 | 47 | | return false; |
| | 48 | |
|
| 2026 | 49 | | scene = loadedScenes[id]; |
| 2026 | 50 | | return true; |
| | 51 | | } |
| | 52 | |
|
| | 53 | | public bool TryGetScene<T>(string id, out T scene) |
| | 54 | | where T : class, IParcelScene |
| | 55 | | { |
| 0 | 56 | | scene = default(T); |
| 0 | 57 | | bool result = TryGetScene(id, out IParcelScene baseScene); |
| | 58 | |
|
| 0 | 59 | | if (result) |
| 0 | 60 | | scene = baseScene as T; |
| | 61 | |
|
| 0 | 62 | | if (scene == null) |
| 0 | 63 | | result = false; |
| | 64 | |
|
| 0 | 65 | | return result; |
| | 66 | | } |
| | 67 | |
|
| | 68 | | public HashSet<Vector2Int> GetAllLoadedScenesCoords() |
| | 69 | | { |
| 1316 | 70 | | HashSet<Vector2Int> allLoadedParcelCoords = new HashSet<Vector2Int>(); |
| | 71 | |
|
| | 72 | | // Create fast (hashset) collection of loaded parcels coords |
| 2898 | 73 | | foreach (var element in loadedScenes) |
| | 74 | | { |
| 133 | 75 | | ParcelScene scene = element.Value as ParcelScene; |
| | 76 | |
|
| 133 | 77 | | if (!scene.sceneLifecycleHandler.isReady) |
| | 78 | | continue; |
| | 79 | |
|
| 3 | 80 | | allLoadedParcelCoords.UnionWith(scene.parcels); |
| | 81 | | } |
| | 82 | |
|
| 1316 | 83 | | return allLoadedParcelCoords; |
| | 84 | | } |
| | 85 | | } |
| | 86 | | } |