| | | 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 | | { |
| | 775 | 11 | | private Dictionary<string, IParcelScene> loadedScenes { get; } = new Dictionary<string, IParcelScene>(); |
| | 775 | 12 | | private Dictionary<Vector2Int, string> loadedScenesByCoordinate { get; } = new Dictionary<Vector2Int, string>(); |
| | 775 | 13 | | private List<IParcelScene> scenesSortedByDistance { get; } = new List<IParcelScene>(); |
| | 775 | 14 | | private List<string> globalSceneIds { get; } = new List<string>(); |
| | 775 | 15 | | private Vector2Int sortAuxiliaryVector = new Vector2Int(EnvironmentSettings.MORDOR_SCALAR, EnvironmentSettings.M |
| | 775 | 16 | | private readonly List<IParcelScene> globalScenes = new List<IParcelScene>(); |
| | | 17 | | private string currentSceneId; |
| | | 18 | | |
| | 3446 | 19 | | public string GetCurrentSceneId() => currentSceneId; |
| | | 20 | | |
| | 695 | 21 | | public IEnumerable<KeyValuePair<string, IParcelScene>> GetLoadedScenes() => loadedScenes; |
| | | 22 | | |
| | 0 | 23 | | public List<IParcelScene> GetGlobalScenes() => globalScenes; |
| | | 24 | | |
| | 1423 | 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 | | { |
| | 343 | 39 | | if (!ContainsScene(id)) |
| | 0 | 40 | | return null; |
| | | 41 | | |
| | 343 | 42 | | return loadedScenes[id]; |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | public bool ContainsScene(string id) |
| | | 46 | | { |
| | 2239 | 47 | | if (string.IsNullOrEmpty(id) || !loadedScenes.ContainsKey(id)) |
| | 527 | 48 | | return false; |
| | | 49 | | |
| | 1712 | 50 | | return true; |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | public bool TryGetScene(string id, out IParcelScene scene) |
| | | 54 | | { |
| | 526 | 55 | | scene = null; |
| | | 56 | | |
| | 526 | 57 | | if (string.IsNullOrEmpty(id) || !loadedScenes.ContainsKey(id)) |
| | 12 | 58 | | return false; |
| | | 59 | | |
| | 514 | 60 | | scene = loadedScenes[id]; |
| | 514 | 61 | | return true; |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | public bool TryGetScene<T>(string id, out T scene) |
| | | 65 | | where T : class, IParcelScene |
| | | 66 | | { |
| | 419 | 67 | | scene = default(T); |
| | 419 | 68 | | bool result = TryGetScene(id, out IParcelScene baseScene); |
| | | 69 | | |
| | 419 | 70 | | if (result) |
| | 419 | 71 | | scene = baseScene as T; |
| | | 72 | | |
| | 419 | 73 | | if (scene == null) |
| | 0 | 74 | | result = false; |
| | | 75 | | |
| | 419 | 76 | | return result; |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | public HashSet<Vector2Int> GetAllLoadedScenesCoords() |
| | | 80 | | { |
| | 803 | 81 | | HashSet<Vector2Int> allLoadedParcelCoords = new HashSet<Vector2Int>(); |
| | | 82 | | |
| | | 83 | | // Create fast (hashset) collection of loaded parcels coords |
| | 1868 | 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 | | |
| | 803 | 94 | | return allLoadedParcelCoords; |
| | | 95 | | } |
| | | 96 | | |
| | 775 | 97 | | private readonly Dictionary<GameObject, LoadWrapper> |
| | | 98 | | attachedLoaders = new Dictionary<GameObject, LoadWrapper>(); |
| | | 99 | | |
| | | 100 | | public LoadWrapper GetLoaderForEntity(IDCLEntity entity) |
| | | 101 | | { |
| | 408 | 102 | | if (entity.meshRootGameObject == null) |
| | | 103 | | { |
| | 0 | 104 | | Debug.LogWarning("NULL meshRootGameObject at GetLoaderForEntity()"); |
| | 0 | 105 | | return null; |
| | | 106 | | } |
| | | 107 | | |
| | 408 | 108 | | attachedLoaders.TryGetValue(entity.meshRootGameObject, out LoadWrapper result); |
| | 408 | 109 | | return result; |
| | | 110 | | } |
| | | 111 | | |
| | | 112 | | public T GetOrAddLoaderForEntity<T>(IDCLEntity entity) |
| | | 113 | | where T : LoadWrapper, new() |
| | | 114 | | { |
| | 104 | 115 | | if (!attachedLoaders.TryGetValue(entity.meshRootGameObject, out LoadWrapper result)) |
| | | 116 | | { |
| | 102 | 117 | | result = new T(); |
| | 102 | 118 | | attachedLoaders.Add(entity.meshRootGameObject, result); |
| | | 119 | | } |
| | | 120 | | |
| | 104 | 121 | | return result as T; |
| | | 122 | | } |
| | | 123 | | |
| | | 124 | | public void RemoveLoaderForEntity(IDCLEntity entity) |
| | | 125 | | { |
| | 81 | 126 | | if (entity == null || entity.meshRootGameObject == null) |
| | 7 | 127 | | return; |
| | | 128 | | |
| | 74 | 129 | | attachedLoaders.Remove(entity.meshRootGameObject); |
| | 74 | 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 | | { |
| | 680 | 142 | | currentSceneId = null; |
| | 680 | 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 | | |
| | 680 | 154 | | using var iterator = scenesSortedByDistance.GetEnumerator(); |
| | | 155 | | |
| | 694 | 156 | | while (iterator.MoveNext()) |
| | | 157 | | { |
| | 31 | 158 | | IParcelScene scene = iterator.Current; |
| | | 159 | | |
| | 31 | 160 | | if (scene == null) |
| | | 161 | | continue; |
| | | 162 | | |
| | 31 | 163 | | bool characterIsInsideScene = WorldStateUtils.IsCharacterInsideScene(scene); |
| | 31 | 164 | | bool isGlobalScene = globalSceneIds.Contains(scene.sceneData.id); |
| | | 165 | | |
| | 31 | 166 | | if (isGlobalScene || !characterIsInsideScene) |
| | | 167 | | continue; |
| | | 168 | | |
| | 17 | 169 | | currentSceneId = scene.sceneData.id; |
| | | 170 | | |
| | 17 | 171 | | break; |
| | | 172 | | } |
| | | 173 | | |
| | 1343 | 174 | | } |
| | | 175 | | public void ForceCurrentScene(string id) |
| | | 176 | | { |
| | 513 | 177 | | currentSceneId = id; |
| | 513 | 178 | | } |
| | | 179 | | |
| | | 180 | | public void AddScene(string id, IParcelScene newScene) |
| | | 181 | | { |
| | 531 | 182 | | if (loadedScenes.ContainsKey(id)) |
| | | 183 | | { |
| | 0 | 184 | | Debug.LogWarning($"This scene already exists! {id}"); |
| | 0 | 185 | | return; |
| | | 186 | | } |
| | | 187 | | |
| | 531 | 188 | | loadedScenes.Add(id, newScene); |
| | | 189 | | |
| | 2116 | 190 | | foreach (Vector2Int parcelPosition in newScene.GetParcels()) |
| | | 191 | | { |
| | 527 | 192 | | loadedScenesByCoordinate[parcelPosition] = id; |
| | | 193 | | } |
| | | 194 | | |
| | 531 | 195 | | scenesSortedByDistance.Add(newScene); |
| | | 196 | | |
| | 531 | 197 | | if (currentSceneId == null) |
| | | 198 | | { |
| | 501 | 199 | | currentSceneId = id; |
| | | 200 | | } |
| | 531 | 201 | | } |
| | | 202 | | |
| | | 203 | | public void RemoveScene(string id) |
| | | 204 | | { |
| | 419 | 205 | | IParcelScene loadedScene = loadedScenes[id]; |
| | | 206 | | |
| | 1682 | 207 | | foreach (Vector2Int sceneParcel in loadedScene.GetParcels()) |
| | | 208 | | { |
| | 422 | 209 | | loadedScenesByCoordinate.Remove(sceneParcel); |
| | | 210 | | } |
| | | 211 | | |
| | 419 | 212 | | scenesSortedByDistance.Remove(loadedScene); |
| | | 213 | | |
| | 419 | 214 | | loadedScenes.Remove(id); |
| | 419 | 215 | | globalSceneIds.Remove(id); |
| | | 216 | | |
| | 419 | 217 | | if (globalScenes.Contains(loadedScene)) |
| | | 218 | | { |
| | 5 | 219 | | globalScenes.Remove(loadedScene); |
| | | 220 | | } |
| | 419 | 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 | | { |
| | 768 | 239 | | loadedScenes.Clear(); |
| | 768 | 240 | | loadedScenesByCoordinate.Clear(); |
| | 768 | 241 | | scenesSortedByDistance.Clear(); |
| | 768 | 242 | | globalScenes.Clear(); |
| | 768 | 243 | | globalScenes.Clear(); |
| | 768 | 244 | | currentSceneId = null; |
| | 768 | 245 | | } |
| | | 246 | | |
| | | 247 | | public void Initialize() |
| | | 248 | | { |
| | 768 | 249 | | } |
| | | 250 | | } |
| | | 251 | | } |