| | 1 | | using System.Collections.Generic; |
| | 2 | | using DCL.Controllers; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using UnityEngine; |
| | 5 | |
|
| | 6 | | namespace DCL |
| | 7 | | { |
| | 8 | | public static class WorldStateUtils |
| | 9 | | { |
| | 10 | | public static bool IsGlobalScene(string sceneId) |
| | 11 | | { |
| 0 | 12 | | var worldState = Environment.i.world.state; |
| | 13 | |
|
| 0 | 14 | | if (worldState.TryGetScene(sceneId, out IParcelScene scene)) |
| | 15 | | { |
| 0 | 16 | | return scene is GlobalScene; |
| | 17 | | } |
| | 18 | |
|
| 0 | 19 | | return false; |
| | 20 | | } |
| | 21 | |
|
| | 22 | | public static string TryToGetSceneCoordsID(string id) |
| | 23 | | { |
| 0 | 24 | | var worldState = Environment.i.world.state; |
| | 25 | |
|
| 0 | 26 | | if (worldState.TryGetScene(id, out IParcelScene parcelScene)) |
| | 27 | | { |
| 0 | 28 | | return parcelScene.sceneData.basePosition.ToString(); |
| | 29 | | } |
| | 30 | |
|
| 0 | 31 | | return id; |
| | 32 | | } |
| | 33 | |
|
| | 34 | | public static Vector3 ConvertUnityToScenePosition(Vector3 pos, IParcelScene scene = null) |
| | 35 | | { |
| 65 | 36 | | if (scene == null) |
| | 37 | | { |
| 4 | 38 | | scene = GetCurrentScene(); |
| | 39 | |
|
| 4 | 40 | | if (scene == null) |
| 4 | 41 | | return pos; |
| | 42 | | } |
| | 43 | |
|
| 61 | 44 | | Vector3 worldPosition = PositionUtils.UnityToWorldPosition(pos); |
| 61 | 45 | | return worldPosition - Utils.GridToWorldPosition(scene.sceneData.basePosition.x, scene.sceneData.basePositio |
| | 46 | | } |
| | 47 | |
|
| 2 | 48 | | public static Vector3 ConvertSceneToUnityPosition(Vector3 pos, IParcelScene scene = null) { return ConvertPointI |
| | 49 | |
|
| 109 | 50 | | public static Vector3 ConvertScenePositionToUnityPosition(IParcelScene scene = null) { return ConvertPointInScen |
| | 51 | |
|
| | 52 | | public static Vector3 ConvertPointInSceneToUnityPosition(Vector3 pos, IParcelScene scene = null) |
| | 53 | | { |
| 111 | 54 | | if (scene == null) |
| | 55 | | { |
| 0 | 56 | | scene = GetCurrentScene(); |
| | 57 | |
|
| 0 | 58 | | if (scene == null) |
| 0 | 59 | | return pos; |
| | 60 | | } |
| | 61 | |
|
| 111 | 62 | | return ConvertPointInSceneToUnityPosition(pos, new Vector2Int(scene.sceneData.basePosition.x, scene.sceneDat |
| | 63 | | } |
| | 64 | |
|
| | 65 | | public static Vector3 ConvertPointInSceneToUnityPosition(Vector3 pos, Vector2Int scenePoint) |
| | 66 | | { |
| 118 | 67 | | Vector3 scenePosition = Utils.GridToWorldPosition(scenePoint.x, scenePoint.y) + pos; |
| 118 | 68 | | Vector3 worldPosition = PositionUtils.WorldToUnityPosition(scenePosition); |
| | 69 | |
|
| 118 | 70 | | return worldPosition; |
| | 71 | | } |
| | 72 | |
|
| | 73 | | public static bool IsCharacterInsideScene(IParcelScene scene) |
| | 74 | | { |
| 802 | 75 | | Vector2Int gridPosition = Utils.WorldToGridPosition(CommonScriptableObjects.playerWorldPosition.Get()); |
| 802 | 76 | | return scene.IsInsideSceneBoundaries(gridPosition); |
| | 77 | | } |
| | 78 | |
|
| | 79 | | public static List<GlobalScene> GetActivePortableExperienceScenes() |
| | 80 | | { |
| 1 | 81 | | List<GlobalScene> activePortableExperienceScenes = new List<GlobalScene>(); |
| 1 | 82 | | IWorldState worldState = Environment.i.world.state; |
| | 83 | |
|
| 2 | 84 | | foreach (var globalSceneId in worldState.globalSceneIds) |
| | 85 | | { |
| 0 | 86 | | if (worldState.TryGetScene(globalSceneId, out GlobalScene scene)) |
| | 87 | | { |
| 0 | 88 | | if (scene.isPortableExperience) |
| | 89 | | { |
| 0 | 90 | | activePortableExperienceScenes.Add(scene); |
| | 91 | | } |
| | 92 | | } |
| | 93 | | } |
| | 94 | |
|
| 1 | 95 | | return activePortableExperienceScenes; |
| | 96 | | } |
| | 97 | |
|
| | 98 | | public static List<string> GetActivePortableExperienceIds() |
| | 99 | | { |
| 14 | 100 | | List<string> currentSceneAndPortableExperiencesIds = new List<string>(); |
| 14 | 101 | | IWorldState worldState = Environment.i.world.state; |
| | 102 | |
|
| 28 | 103 | | foreach (var globalSceneId in worldState.globalSceneIds) |
| | 104 | | { |
| 0 | 105 | | if (worldState.TryGetScene(globalSceneId, out GlobalScene scene)) |
| | 106 | | { |
| 0 | 107 | | if (scene.isPortableExperience) |
| | 108 | | { |
| 0 | 109 | | currentSceneAndPortableExperiencesIds.Add(globalSceneId); |
| | 110 | | } |
| | 111 | | } |
| | 112 | | } |
| | 113 | |
|
| 14 | 114 | | return currentSceneAndPortableExperiencesIds; |
| | 115 | | } |
| | 116 | |
|
| | 117 | | static IParcelScene GetCurrentScene() |
| | 118 | | { |
| 4 | 119 | | var worldState = Environment.i.world.state; |
| 4 | 120 | | string currentSceneId = worldState.currentSceneId; |
| | 121 | |
|
| 4 | 122 | | if (string.IsNullOrEmpty(currentSceneId)) |
| 4 | 123 | | return null; |
| | 124 | |
|
| 0 | 125 | | bool foundCurrentScene = worldState.loadedScenes.TryGetValue(currentSceneId, out IParcelScene scene); |
| | 126 | |
|
| 0 | 127 | | if (!foundCurrentScene) |
| 0 | 128 | | return null; |
| | 129 | |
|
| 0 | 130 | | return scene; |
| | 131 | | } |
| | 132 | | } |
| | 133 | | } |