| | 1 | | using DCL.Controllers; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using DCL.Configuration; |
| | 4 | | using DCL.Models; |
| | 5 | | using UnityEngine; |
| | 6 | |
|
| | 7 | | namespace DCL |
| | 8 | | { |
| | 9 | | public class WorldState : IWorldState |
| | 10 | | { |
| 779 | 11 | | private Dictionary<string, IParcelScene> loadedScenes { get; } = new Dictionary<string, IParcelScene>(); |
| 779 | 12 | | private Dictionary<Vector2Int, string> loadedScenesByCoordinate { get; } = new Dictionary<Vector2Int, string>(); |
| 779 | 13 | | private List<IParcelScene> scenesSortedByDistance { get; } = new List<IParcelScene>(); |
| 779 | 14 | | private List<string> globalSceneIds { get; } = new List<string>(); |
| 779 | 15 | | private Vector2Int sortAuxiliaryVector = new Vector2Int(EnvironmentSettings.MORDOR_SCALAR, EnvironmentSettings.M |
| 779 | 16 | | private readonly List<IParcelScene> globalScenes = new List<IParcelScene>(); |
| | 17 | | private string currentSceneId; |
| | 18 | |
|
| 3475 | 19 | | public string GetCurrentSceneId() => currentSceneId; |
| | 20 | |
|
| 699 | 21 | | public IEnumerable<KeyValuePair<string, IParcelScene>> GetLoadedScenes() => loadedScenes; |
| | 22 | |
|
| 0 | 23 | | public List<IParcelScene> GetGlobalScenes() => globalScenes; |
| | 24 | |
|
| 1432 | 25 | | public List<IParcelScene> GetScenesSortedByDistance() => scenesSortedByDistance; |
| | 26 | |
|
| | 27 | | public IParcelScene GetScene(Vector2Int coords) |
| | 28 | | { |
| 0 | 29 | | var id = GetSceneIdByCoords(coords); |
| | 30 | |
|
| 0 | 31 | | if (!ContainsScene(id)) |
| 0 | 32 | | return null; |
| | 33 | |
|
| 0 | 34 | | return loadedScenes[id]; |
| | 35 | | } |
| | 36 | |
|
| | 37 | | public IParcelScene GetScene(string id) |
| | 38 | | { |
| 347 | 39 | | if (!ContainsScene(id)) |
| 0 | 40 | | return null; |
| | 41 | |
|
| 347 | 42 | | return loadedScenes[id]; |
| | 43 | | } |
| | 44 | |
|
| | 45 | | public bool ContainsScene(string id) |
| | 46 | | { |
| 2261 | 47 | | if (string.IsNullOrEmpty(id) || !loadedScenes.ContainsKey(id)) |
| 530 | 48 | | return false; |
| | 49 | |
|
| 1731 | 50 | | return true; |
| | 51 | | } |
| | 52 | |
|
| | 53 | | public bool TryGetScene(string id, out IParcelScene scene) |
| | 54 | | { |
| 530 | 55 | | scene = null; |
| | 56 | |
|
| 530 | 57 | | if (string.IsNullOrEmpty(id) || !loadedScenes.ContainsKey(id)) |
| 12 | 58 | | return false; |
| | 59 | |
|
| 518 | 60 | | scene = loadedScenes[id]; |
| 518 | 61 | | return true; |
| | 62 | | } |
| | 63 | |
|
| | 64 | | public bool TryGetScene<T>(string id, out T scene) |
| | 65 | | where T : class, IParcelScene |
| | 66 | | { |
| 422 | 67 | | scene = default(T); |
| 422 | 68 | | bool result = TryGetScene(id, out IParcelScene baseScene); |
| | 69 | |
|
| 422 | 70 | | if (result) |
| 422 | 71 | | scene = baseScene as T; |
| | 72 | |
|
| 422 | 73 | | if (scene == null) |
| 0 | 74 | | result = false; |
| | 75 | |
|
| 422 | 76 | | return result; |
| | 77 | | } |
| | 78 | |
|
| | 79 | | public HashSet<Vector2Int> GetAllLoadedScenesCoords() |
| | 80 | | { |
| 809 | 81 | | HashSet<Vector2Int> allLoadedParcelCoords = new HashSet<Vector2Int>(); |
| | 82 | |
|
| | 83 | | // Create fast (hashset) collection of loaded parcels coords |
| 1880 | 84 | | foreach (var element in loadedScenes) |
| | 85 | | { |
| 131 | 86 | | ParcelScene scene = element.Value as ParcelScene; |
| | 87 | |
|
| 131 | 88 | | if (!scene.sceneLifecycleHandler.isReady) |
| | 89 | | continue; |
| | 90 | |
|
| 5 | 91 | | allLoadedParcelCoords.UnionWith(scene.parcels); |
| | 92 | | } |
| | 93 | |
|
| 809 | 94 | | return allLoadedParcelCoords; |
| | 95 | | } |
| | 96 | |
|
| 779 | 97 | | private readonly Dictionary<GameObject, LoadWrapper> |
| | 98 | | attachedLoaders = new Dictionary<GameObject, LoadWrapper>(); |
| | 99 | |
|
| | 100 | | public LoadWrapper GetLoaderForEntity(IDCLEntity entity) |
| | 101 | | { |
| 423 | 102 | | if (entity.meshRootGameObject == null) |
| | 103 | | { |
| 0 | 104 | | Debug.LogWarning("NULL meshRootGameObject at GetLoaderForEntity()"); |
| 0 | 105 | | return null; |
| | 106 | | } |
| | 107 | |
|
| 423 | 108 | | attachedLoaders.TryGetValue(entity.meshRootGameObject, out LoadWrapper result); |
| 423 | 109 | | return result; |
| | 110 | | } |
| | 111 | |
|
| | 112 | | public T GetOrAddLoaderForEntity<T>(IDCLEntity entity) |
| | 113 | | where T : LoadWrapper, new() |
| | 114 | | { |
| 106 | 115 | | if (!attachedLoaders.TryGetValue(entity.meshRootGameObject, out LoadWrapper result)) |
| | 116 | | { |
| 104 | 117 | | result = new T(); |
| 104 | 118 | | attachedLoaders.Add(entity.meshRootGameObject, result); |
| | 119 | | } |
| | 120 | |
|
| 106 | 121 | | return result as T; |
| | 122 | | } |
| | 123 | |
|
| | 124 | | public void RemoveLoaderForEntity(IDCLEntity entity) |
| | 125 | | { |
| 83 | 126 | | if (entity == null || entity.meshRootGameObject == null) |
| 8 | 127 | | return; |
| | 128 | |
|
| 75 | 129 | | attachedLoaders.Remove(entity.meshRootGameObject); |
| 75 | 130 | | } |
| | 131 | |
|
| | 132 | | public string GetSceneIdByCoords(Vector2Int coords) |
| | 133 | | { |
| 0 | 134 | | if (loadedScenesByCoordinate.ContainsKey(coords)) |
| 0 | 135 | | return loadedScenesByCoordinate[coords]; |
| | 136 | |
|
| 0 | 137 | | return null; |
| | 138 | | } |
| | 139 | |
|
| | 140 | | public void SortScenesByDistance(Vector2Int position) |
| | 141 | | { |
| 685 | 142 | | currentSceneId = null; |
| 685 | 143 | | scenesSortedByDistance.Sort((sceneA, sceneB) => |
| | 144 | | { |
| 60 | 145 | | sortAuxiliaryVector = sceneA.sceneData.basePosition - position; |
| 60 | 146 | | int dist1 = sortAuxiliaryVector.sqrMagnitude; |
| | 147 | |
|
| 60 | 148 | | sortAuxiliaryVector = sceneB.sceneData.basePosition - position; |
| 60 | 149 | | int dist2 = sortAuxiliaryVector.sqrMagnitude; |
| | 150 | |
|
| 60 | 151 | | return dist1 - dist2; |
| | 152 | | }); |
| | 153 | |
|
| 685 | 154 | | using var iterator = scenesSortedByDistance.GetEnumerator(); |
| | 155 | |
|
| 699 | 156 | | while (iterator.MoveNext()) |
| | 157 | | { |
| 32 | 158 | | IParcelScene scene = iterator.Current; |
| | 159 | |
|
| 32 | 160 | | if (scene == null) |
| | 161 | | continue; |
| | 162 | |
|
| 32 | 163 | | bool characterIsInsideScene = WorldStateUtils.IsCharacterInsideScene(scene); |
| 32 | 164 | | bool isGlobalScene = globalSceneIds.Contains(scene.sceneData.id); |
| | 165 | |
|
| 32 | 166 | | if (isGlobalScene || !characterIsInsideScene) |
| | 167 | | continue; |
| | 168 | |
|
| 18 | 169 | | currentSceneId = scene.sceneData.id; |
| | 170 | |
|
| 18 | 171 | | break; |
| | 172 | | } |
| | 173 | |
|
| 1352 | 174 | | } |
| | 175 | | public void ForceCurrentScene(string id) |
| | 176 | | { |
| 516 | 177 | | currentSceneId = id; |
| 516 | 178 | | } |
| | 179 | |
|
| | 180 | | public void AddScene(string id, IParcelScene newScene) |
| | 181 | | { |
| 534 | 182 | | if (loadedScenes.ContainsKey(id)) |
| | 183 | | { |
| 0 | 184 | | Debug.LogWarning($"This scene already exists! {id}"); |
| 0 | 185 | | return; |
| | 186 | | } |
| | 187 | |
|
| 534 | 188 | | loadedScenes.Add(id, newScene); |
| | 189 | |
|
| 2128 | 190 | | foreach (Vector2Int parcelPosition in newScene.GetParcels()) |
| | 191 | | { |
| 530 | 192 | | loadedScenesByCoordinate[parcelPosition] = id; |
| | 193 | | } |
| | 194 | |
|
| 534 | 195 | | scenesSortedByDistance.Add(newScene); |
| | 196 | |
|
| 534 | 197 | | if (currentSceneId == null) |
| | 198 | | { |
| 504 | 199 | | currentSceneId = id; |
| | 200 | | } |
| 534 | 201 | | } |
| | 202 | |
|
| | 203 | | public void RemoveScene(string id) |
| | 204 | | { |
| 422 | 205 | | IParcelScene loadedScene = loadedScenes[id]; |
| | 206 | |
|
| 1694 | 207 | | foreach (Vector2Int sceneParcel in loadedScene.GetParcels()) |
| | 208 | | { |
| 425 | 209 | | loadedScenesByCoordinate.Remove(sceneParcel); |
| | 210 | | } |
| | 211 | |
|
| 422 | 212 | | scenesSortedByDistance.Remove(loadedScene); |
| | 213 | |
|
| 422 | 214 | | loadedScenes.Remove(id); |
| 422 | 215 | | globalSceneIds.Remove(id); |
| | 216 | |
|
| 422 | 217 | | if (globalScenes.Contains(loadedScene)) |
| | 218 | | { |
| 5 | 219 | | globalScenes.Remove(loadedScene); |
| | 220 | | } |
| 422 | 221 | | } |
| | 222 | |
|
| | 223 | | public void AddGlobalScene(string sceneId, IParcelScene newScene) |
| | 224 | | { |
| 5 | 225 | | if (globalSceneIds.Contains(sceneId)) |
| | 226 | | { |
| 0 | 227 | | Debug.LogWarning($"This GLOBAL scene already exists! {sceneId}"); |
| 0 | 228 | | return; |
| | 229 | | } |
| | 230 | |
|
| 5 | 231 | | globalSceneIds.Add(sceneId); |
| 5 | 232 | | globalScenes.Add(newScene); |
| | 233 | |
|
| 5 | 234 | | AddScene(sceneId, newScene); |
| 5 | 235 | | } |
| | 236 | |
|
| | 237 | | public void Dispose() |
| | 238 | | { |
| 772 | 239 | | loadedScenes.Clear(); |
| 772 | 240 | | loadedScenesByCoordinate.Clear(); |
| 772 | 241 | | scenesSortedByDistance.Clear(); |
| 772 | 242 | | globalScenes.Clear(); |
| 772 | 243 | | globalScenes.Clear(); |
| 772 | 244 | | currentSceneId = null; |
| 772 | 245 | | } |
| | 246 | |
|
| | 247 | | public void Initialize() |
| | 248 | | { |
| 772 | 249 | | } |
| | 250 | | } |
| | 251 | | } |