| | 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 | | { |
| 11712 | 11 | | private Dictionary<int, IParcelScene> loadedScenes { get; } = new Dictionary<int, IParcelScene>(); |
| 1091 | 12 | | private Dictionary<string, IParcelScene> loadedPortableExperienceScenes { get; } = new Dictionary<string, IParce |
| 1855 | 13 | | private Dictionary<Vector2Int, int> loadedScenesByCoordinate { get; } = new Dictionary<Vector2Int, int>(); |
| 3586 | 14 | | private List<IParcelScene> scenesSortedByDistance { get; } = new List<IParcelScene>(); |
| 1716 | 15 | | private List<int> globalSceneNumbers { get; } = new List<int>(); |
| 542 | 16 | | private Vector2Int sortAuxiliaryVector = new Vector2Int(EnvironmentSettings.MORDOR_SCALAR, EnvironmentSettings.M |
| 542 | 17 | | private readonly List<IParcelScene> globalScenes = new List<IParcelScene>(); |
| | 18 | | private int currentSceneNumber; |
| | 19 | | private string currentSceneHash; |
| | 20 | |
|
| 7901 | 21 | | public int GetCurrentSceneNumber() => currentSceneNumber; |
| 0 | 22 | | public string GetCurrentSceneHash() => currentSceneHash; |
| | 23 | |
|
| | 24 | |
|
| 451 | 25 | | public IEnumerable<KeyValuePair<int, IParcelScene>> GetLoadedScenes() => loadedScenes; |
| | 26 | |
|
| 0 | 27 | | public List<IParcelScene> GetGlobalScenes() => globalScenes; |
| | 28 | |
|
| 918 | 29 | | public List<IParcelScene> GetScenesSortedByDistance() => scenesSortedByDistance; |
| | 30 | |
|
| | 31 | | public IParcelScene GetScene(int sceneNumber) |
| | 32 | | { |
| 349 | 33 | | if (!ContainsScene(sceneNumber)) |
| 64 | 34 | | return null; |
| | 35 | |
|
| 285 | 36 | | return loadedScenes[sceneNumber]; |
| | 37 | | } |
| | 38 | |
|
| | 39 | | public IParcelScene GetScene(Vector2Int coords) |
| | 40 | | { |
| 64 | 41 | | return GetScene(GetSceneNumberByCoords(coords)); |
| | 42 | | } |
| | 43 | |
|
| | 44 | | public IParcelScene GetPortableExperienceScene(string sceneId) |
| | 45 | | { |
| 0 | 46 | | if (string.IsNullOrEmpty(sceneId) || !loadedPortableExperienceScenes.ContainsKey(sceneId)) |
| 0 | 47 | | return null; |
| | 48 | |
|
| 0 | 49 | | return loadedPortableExperienceScenes[sceneId]; |
| | 50 | | } |
| | 51 | |
|
| | 52 | | public bool ContainsScene(int sceneNumber) |
| | 53 | | { |
| 7318 | 54 | | if (sceneNumber <= 0 || !loadedScenes.ContainsKey(sceneNumber)) |
| 500 | 55 | | return false; |
| | 56 | |
|
| 6818 | 57 | | return true; |
| | 58 | | } |
| | 59 | |
|
| | 60 | | public bool TryGetScene(int sceneNumber, out IParcelScene scene) |
| | 61 | | { |
| 403 | 62 | | scene = null; |
| | 63 | |
|
| 403 | 64 | | if (sceneNumber <= 0 || !loadedScenes.ContainsKey(sceneNumber)) |
| 42 | 65 | | return false; |
| | 66 | |
|
| 361 | 67 | | scene = loadedScenes[sceneNumber]; |
| 361 | 68 | | return true; |
| | 69 | | } |
| | 70 | |
|
| | 71 | | public bool TryGetScene<T>(int sceneNumber, out T scene) |
| | 72 | | where T : class, IParcelScene |
| | 73 | | { |
| 267 | 74 | | scene = default(T); |
| 267 | 75 | | bool result = TryGetScene(sceneNumber, out IParcelScene baseScene); |
| | 76 | |
|
| 267 | 77 | | if (result) |
| 267 | 78 | | scene = baseScene as T; |
| | 79 | |
|
| 267 | 80 | | if (scene == null) |
| 0 | 81 | | result = false; |
| | 82 | |
|
| 267 | 83 | | return result; |
| | 84 | | } |
| | 85 | |
|
| | 86 | | public HashSet<Vector2Int> GetAllLoadedScenesCoords() |
| | 87 | | { |
| 494 | 88 | | HashSet<Vector2Int> allLoadedParcelCoords = new HashSet<Vector2Int>(); |
| | 89 | |
|
| | 90 | | // Create fast (hashset) collection of loaded parcels coords |
| 1260 | 91 | | foreach (var element in loadedScenes) |
| | 92 | | { |
| 136 | 93 | | ParcelScene scene = element.Value as ParcelScene; |
| | 94 | |
|
| 136 | 95 | | if (!scene.sceneLifecycleHandler.isReady) |
| | 96 | | continue; |
| | 97 | |
|
| 8 | 98 | | allLoadedParcelCoords.UnionWith(scene.parcels); |
| | 99 | | } |
| | 100 | |
|
| 494 | 101 | | return allLoadedParcelCoords; |
| | 102 | | } |
| | 103 | |
|
| 542 | 104 | | private readonly Dictionary<GameObject, LoadWrapper> |
| | 105 | | attachedLoaders = new Dictionary<GameObject, LoadWrapper>(); |
| | 106 | |
|
| | 107 | | public LoadWrapper GetLoaderForEntity(IDCLEntity entity) |
| | 108 | | { |
| 393 | 109 | | if (entity == null) |
| | 110 | | { |
| 0 | 111 | | Debug.LogWarning("NULL entity at GetLoaderForEntity()"); |
| 0 | 112 | | return null; |
| | 113 | | } |
| 393 | 114 | | if (entity.meshRootGameObject == null) |
| | 115 | | { |
| 0 | 116 | | Debug.LogWarning("NULL meshRootGameObject at GetLoaderForEntity()"); |
| 0 | 117 | | return null; |
| | 118 | | } |
| | 119 | |
|
| 393 | 120 | | attachedLoaders.TryGetValue(entity.meshRootGameObject, out LoadWrapper result); |
| 393 | 121 | | return result; |
| | 122 | | } |
| | 123 | |
|
| | 124 | | public T GetOrAddLoaderForEntity<T>(IDCLEntity entity) |
| | 125 | | where T : LoadWrapper, new() |
| | 126 | | { |
| 67 | 127 | | if (!attachedLoaders.TryGetValue(entity.meshRootGameObject, out LoadWrapper result)) |
| | 128 | | { |
| 65 | 129 | | result = new T(); |
| 65 | 130 | | attachedLoaders.Add(entity.meshRootGameObject, result); |
| | 131 | | } |
| | 132 | |
|
| 67 | 133 | | return result as T; |
| | 134 | | } |
| | 135 | |
|
| | 136 | | public void RemoveLoaderForEntity(IDCLEntity entity) |
| | 137 | | { |
| 49 | 138 | | if (entity == null || entity.meshRootGameObject == null) |
| 7 | 139 | | return; |
| | 140 | |
|
| 42 | 141 | | attachedLoaders.Remove(entity.meshRootGameObject); |
| 42 | 142 | | } |
| | 143 | |
|
| | 144 | | public int GetSceneNumberByCoords(Vector2Int coords) |
| | 145 | | { |
| 65 | 146 | | if (loadedScenesByCoordinate.ContainsKey(coords)) |
| 0 | 147 | | return loadedScenesByCoordinate[coords]; |
| | 148 | |
|
| 65 | 149 | | return -1; |
| | 150 | | } |
| | 151 | |
|
| | 152 | | public void SortScenesByDistance(Vector2Int position) |
| | 153 | | { |
| 444 | 154 | | currentSceneNumber = -1; |
| 444 | 155 | | currentSceneHash = ""; |
| | 156 | |
|
| 444 | 157 | | scenesSortedByDistance.Sort((sceneA, sceneB) => |
| | 158 | | { |
| 62 | 159 | | sortAuxiliaryVector = sceneA.sceneData.basePosition - position; |
| 62 | 160 | | int dist1 = sortAuxiliaryVector.sqrMagnitude; |
| | 161 | |
|
| 62 | 162 | | sortAuxiliaryVector = sceneB.sceneData.basePosition - position; |
| 62 | 163 | | int dist2 = sortAuxiliaryVector.sqrMagnitude; |
| | 164 | |
|
| 62 | 165 | | return dist1 - dist2; |
| | 166 | | }); |
| | 167 | |
|
| 444 | 168 | | using var iterator = scenesSortedByDistance.GetEnumerator(); |
| | 169 | |
|
| 474 | 170 | | while (iterator.MoveNext()) |
| | 171 | | { |
| 39 | 172 | | IParcelScene scene = iterator.Current; |
| | 173 | |
|
| 39 | 174 | | if (scene == null) |
| | 175 | | continue; |
| | 176 | |
|
| 39 | 177 | | bool characterIsInsideScene = WorldStateUtils.IsCharacterInsideScene(scene); |
| 39 | 178 | | bool isGlobalScene = globalSceneNumbers.Contains(scene.sceneData.sceneNumber); |
| | 179 | |
|
| 39 | 180 | | if (isGlobalScene || !characterIsInsideScene) |
| | 181 | | continue; |
| | 182 | |
|
| 9 | 183 | | currentSceneNumber = scene.sceneData.sceneNumber; |
| 9 | 184 | | currentSceneHash = scene.sceneData.id; |
| | 185 | |
|
| 9 | 186 | | break; |
| | 187 | | } |
| | 188 | |
|
| 879 | 189 | | } |
| | 190 | |
|
| | 191 | | public void ForceCurrentScene(int sceneNumber, string sceneHash) |
| | 192 | | { |
| 334 | 193 | | currentSceneNumber = sceneNumber; |
| 334 | 194 | | currentSceneHash = sceneHash; |
| 334 | 195 | | } |
| | 196 | |
|
| | 197 | | public void AddScene(IParcelScene newScene) |
| | 198 | | { |
| 429 | 199 | | int sceneNumber = newScene.sceneData.sceneNumber; |
| | 200 | |
|
| 429 | 201 | | if (loadedScenes.ContainsKey(sceneNumber)) |
| | 202 | | { |
| 0 | 203 | | Debug.LogWarning($"The scene {newScene.sceneData.id} already exists! scene number: {sceneNumber}"); |
| 0 | 204 | | return; |
| | 205 | | } |
| | 206 | |
|
| 429 | 207 | | if (newScene.isPersistent) |
| | 208 | | { |
| 326 | 209 | | globalSceneNumbers.Add(sceneNumber); |
| 326 | 210 | | globalScenes.Add(newScene); |
| | 211 | |
|
| 326 | 212 | | if (newScene.isPortableExperience) |
| 2 | 213 | | loadedPortableExperienceScenes.Add(newScene.sceneData.id, newScene); |
| | 214 | | } |
| | 215 | |
|
| 429 | 216 | | loadedScenes.Add(sceneNumber, newScene); |
| | 217 | |
|
| 1726 | 218 | | foreach (Vector2Int parcelPosition in newScene.GetParcels()) |
| | 219 | | { |
| 434 | 220 | | loadedScenesByCoordinate[parcelPosition] = sceneNumber; |
| | 221 | | } |
| | 222 | |
|
| 429 | 223 | | scenesSortedByDistance.Add(newScene); |
| | 224 | |
|
| 429 | 225 | | if (currentSceneNumber <= 0) |
| | 226 | | { |
| 349 | 227 | | currentSceneNumber = sceneNumber; |
| 349 | 228 | | currentSceneHash = newScene.sceneData.id; |
| | 229 | | } |
| 429 | 230 | | } |
| | 231 | |
|
| | 232 | | public void RemoveScene(int sceneNumber) |
| | 233 | | { |
| 267 | 234 | | IParcelScene loadedScene = loadedScenes[sceneNumber]; |
| | 235 | |
|
| 1078 | 236 | | foreach (Vector2Int sceneParcel in loadedScene.GetParcels()) |
| | 237 | | { |
| 272 | 238 | | loadedScenesByCoordinate.Remove(sceneParcel); |
| | 239 | | } |
| | 240 | |
|
| 267 | 241 | | scenesSortedByDistance.Remove(loadedScene); |
| | 242 | |
|
| 267 | 243 | | loadedScenes.Remove(sceneNumber); |
| 267 | 244 | | globalSceneNumbers.Remove(sceneNumber); |
| | 245 | |
|
| 267 | 246 | | if (globalScenes.Contains(loadedScene)) |
| | 247 | | { |
| 233 | 248 | | globalScenes.Remove(loadedScene); |
| | 249 | |
|
| 233 | 250 | | if (!string.IsNullOrEmpty(loadedScene.sceneData.id) && loadedPortableExperienceScenes.ContainsKey(loaded |
| 2 | 251 | | loadedPortableExperienceScenes.Remove(loadedScene.sceneData.id); |
| | 252 | | } |
| 267 | 253 | | } |
| | 254 | |
|
| | 255 | | public void Dispose() |
| | 256 | | { |
| 542 | 257 | | loadedScenes.Clear(); |
| 542 | 258 | | loadedScenesByCoordinate.Clear(); |
| 542 | 259 | | scenesSortedByDistance.Clear(); |
| 542 | 260 | | globalScenes.Clear(); |
| 542 | 261 | | globalSceneNumbers.Clear(); |
| 542 | 262 | | loadedPortableExperienceScenes.Clear(); |
| 542 | 263 | | currentSceneNumber = -1; |
| 542 | 264 | | currentSceneHash = ""; |
| 542 | 265 | | } |
| | 266 | |
|
| 542 | 267 | | public void Initialize() { } |
| | 268 | | } |
| | 269 | | } |