| | 1 | | using System.Collections.Generic; |
| | 2 | | using DCL.Controllers; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using DCL.Models; |
| | 5 | | using UnityEngine; |
| | 6 | |
|
| | 7 | | namespace DCL |
| | 8 | | { |
| | 9 | | public static class WorldStateUtils |
| | 10 | | { |
| | 11 | | public static bool IsGlobalScene(string sceneId) |
| | 12 | | { |
| 0 | 13 | | var worldState = Environment.i.world.state; |
| | 14 | |
|
| 0 | 15 | | if (worldState.TryGetScene(sceneId, out IParcelScene scene)) |
| | 16 | | { |
| 0 | 17 | | return scene is GlobalScene; |
| | 18 | | } |
| | 19 | |
|
| 0 | 20 | | return false; |
| | 21 | | } |
| | 22 | |
|
| | 23 | | public static string TryToGetSceneCoordsID(string id) |
| | 24 | | { |
| 0 | 25 | | var worldState = Environment.i.world.state; |
| | 26 | |
|
| 0 | 27 | | if (worldState.TryGetScene(id, out IParcelScene parcelScene)) |
| | 28 | | { |
| 0 | 29 | | return parcelScene.sceneData.basePosition.ToString(); |
| | 30 | | } |
| | 31 | |
|
| 0 | 32 | | return id; |
| | 33 | | } |
| | 34 | |
|
| | 35 | | public static Vector3 ConvertUnityToScenePosition(Vector3 pos, IParcelScene scene = null) |
| | 36 | | { |
| 65 | 37 | | if (scene == null) |
| | 38 | | { |
| 6 | 39 | | scene = GetCurrentScene(); |
| | 40 | |
|
| 6 | 41 | | if (scene == null) |
| 4 | 42 | | return pos; |
| | 43 | | } |
| | 44 | |
|
| 61 | 45 | | Vector3 worldPosition = PositionUtils.UnityToWorldPosition(pos); |
| 61 | 46 | | return worldPosition - Utils.GridToWorldPosition(scene.sceneData.basePosition.x, scene.sceneData.basePositio |
| | 47 | | } |
| | 48 | |
|
| 2 | 49 | | public static Vector3 ConvertSceneToUnityPosition(Vector3 pos, IParcelScene scene = null) { return ConvertPointI |
| | 50 | |
|
| 76 | 51 | | public static Vector3 ConvertScenePositionToUnityPosition(IParcelScene scene = null) { return ConvertPointInScen |
| | 52 | |
|
| | 53 | | public static Vector3 ConvertPointInSceneToUnityPosition(Vector3 pos, IParcelScene scene = null) |
| | 54 | | { |
| 78 | 55 | | if (scene == null) |
| | 56 | | { |
| 0 | 57 | | scene = GetCurrentScene(); |
| | 58 | |
|
| 0 | 59 | | if (scene == null) |
| 0 | 60 | | return pos; |
| | 61 | | } |
| | 62 | |
|
| 78 | 63 | | return ConvertPointInSceneToUnityPosition(pos, new Vector2Int(scene.sceneData.basePosition.x, scene.sceneDat |
| | 64 | | } |
| | 65 | |
|
| | 66 | | public static Vector3 ConvertPointInSceneToUnityPosition(Vector3 pos, Vector2Int scenePoint) |
| | 67 | | { |
| 85 | 68 | | Vector3 scenePosition = Utils.GridToWorldPosition(scenePoint.x, scenePoint.y) + pos; |
| 85 | 69 | | Vector3 worldPosition = PositionUtils.WorldToUnityPosition(scenePosition); |
| | 70 | |
|
| 85 | 71 | | return worldPosition; |
| | 72 | | } |
| | 73 | |
|
| | 74 | | public static bool IsCharacterInsideScene(IParcelScene scene) |
| | 75 | | { |
| 26 | 76 | | Vector2Int gridPosition = Utils.WorldToGridPosition(CommonScriptableObjects.playerWorldPosition.Get()); |
| 26 | 77 | | return scene.IsInsideSceneBoundaries(gridPosition); |
| | 78 | | } |
| | 79 | |
|
| | 80 | | public static List<GlobalScene> GetActivePortableExperienceScenes() |
| | 81 | | { |
| 0 | 82 | | List<GlobalScene> activePortableExperienceScenes = new List<GlobalScene>(); |
| 0 | 83 | | IWorldState worldState = Environment.i.world.state; |
| | 84 | |
|
| 0 | 85 | | foreach (var globalSceneId in worldState.globalSceneIds) |
| | 86 | | { |
| 0 | 87 | | if (worldState.TryGetScene(globalSceneId, out GlobalScene scene)) |
| | 88 | | { |
| 0 | 89 | | if (scene.isPortableExperience) |
| | 90 | | { |
| 0 | 91 | | activePortableExperienceScenes.Add(scene); |
| | 92 | | } |
| | 93 | | } |
| | 94 | | } |
| | 95 | |
|
| 0 | 96 | | return activePortableExperienceScenes; |
| | 97 | | } |
| | 98 | |
|
| | 99 | | public static List<string> GetActivePortableExperienceIds() |
| | 100 | | { |
| 14 | 101 | | List<string> currentSceneAndPortableExperiencesIds = new List<string>(); |
| 14 | 102 | | IWorldState worldState = Environment.i.world.state; |
| | 103 | |
|
| 28 | 104 | | foreach (var globalSceneId in worldState.globalSceneIds) |
| | 105 | | { |
| 0 | 106 | | if (worldState.TryGetScene(globalSceneId, out GlobalScene scene)) |
| | 107 | | { |
| 0 | 108 | | if (scene.isPortableExperience) |
| | 109 | | { |
| 0 | 110 | | currentSceneAndPortableExperiencesIds.Add(globalSceneId); |
| | 111 | | } |
| | 112 | | } |
| | 113 | | } |
| | 114 | |
|
| 14 | 115 | | return currentSceneAndPortableExperiencesIds; |
| | 116 | | } |
| | 117 | |
|
| | 118 | | public static IParcelScene GetCurrentScene() |
| | 119 | | { |
| 6 | 120 | | var worldState = Environment.i.world.state; |
| 6 | 121 | | string currentSceneId = worldState.currentSceneId; |
| | 122 | |
|
| 6 | 123 | | if (string.IsNullOrEmpty(currentSceneId)) |
| 4 | 124 | | return null; |
| | 125 | |
|
| 2 | 126 | | bool foundCurrentScene = worldState.loadedScenes.TryGetValue(currentSceneId, out IParcelScene scene); |
| | 127 | |
|
| 2 | 128 | | if (!foundCurrentScene) |
| 0 | 129 | | return null; |
| | 130 | |
|
| 2 | 131 | | return scene; |
| | 132 | | } |
| | 133 | |
|
| | 134 | | public static IParcelScene CreateTestScene(LoadParcelScenesMessage.UnityParcelScene data = null) |
| | 135 | | { |
| 468 | 136 | | if (data == null) |
| | 137 | | { |
| 468 | 138 | | data = new LoadParcelScenesMessage.UnityParcelScene(); |
| | 139 | | } |
| | 140 | |
|
| 468 | 141 | | if (data.parcels == null) |
| | 142 | | { |
| 468 | 143 | | data.parcels = new Vector2Int[] { data.basePosition }; |
| | 144 | | } |
| | 145 | |
|
| 468 | 146 | | if (string.IsNullOrEmpty(data.id)) |
| | 147 | | { |
| 468 | 148 | | data.id = $"(test):{data.basePosition.x},{data.basePosition.y}"; |
| | 149 | | } |
| | 150 | |
|
| 468 | 151 | | if (Environment.i.world.state.loadedScenes != null) |
| | 152 | | { |
| 468 | 153 | | if (Environment.i.world.state.loadedScenes.ContainsKey(data.id)) |
| | 154 | | { |
| 0 | 155 | | Debug.LogWarning($"Scene {data.id} is already loaded."); |
| 0 | 156 | | return Environment.i.world.state.loadedScenes[data.id]; |
| | 157 | | } |
| | 158 | | } |
| | 159 | |
|
| 468 | 160 | | var go = new GameObject(); |
| 468 | 161 | | var newScene = go.AddComponent<ParcelScene>(); |
| 468 | 162 | | newScene.isTestScene = true; |
| 468 | 163 | | newScene.isPersistent = true; |
| 468 | 164 | | newScene.SetData(data); |
| | 165 | |
|
| 468 | 166 | | if (DCLCharacterController.i != null) |
| 360 | 167 | | newScene.InitializeDebugPlane(); |
| | 168 | |
|
| 468 | 169 | | Environment.i.world.state.scenesSortedByDistance?.Add(newScene); |
| 468 | 170 | | Environment.i.world.state.loadedScenes?.Add(data.id, newScene); |
| | 171 | |
|
| | 172 | |
|
| 468 | 173 | | return newScene; |
| | 174 | | } |
| | 175 | | } |
| | 176 | | } |