| | 1 | | using DCL.Controllers; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using DCL.Models; |
| | 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.isPersistent; |
| | 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 | | { |
| 6 | 38 | | scene = GetCurrentScene(); |
| | 39 | |
|
| 6 | 40 | | if (scene == null) |
| 2 | 41 | | return pos; |
| | 42 | | } |
| | 43 | |
|
| 63 | 44 | | Vector3 worldPosition = PositionUtils.UnityToWorldPosition(pos); |
| 63 | 45 | | return worldPosition - |
| | 46 | | Utils.GridToWorldPosition(scene.sceneData.basePosition.x, scene.sceneData.basePosition.y); |
| | 47 | | } |
| | 48 | |
|
| | 49 | | public static Vector3 ConvertSceneToUnityPosition(Vector3 pos, IParcelScene scene = null) |
| | 50 | | { |
| 2 | 51 | | return ConvertPointInSceneToUnityPosition(pos, scene); |
| | 52 | | } |
| | 53 | |
|
| | 54 | | public static Vector3 ConvertScenePositionToUnityPosition(IParcelScene scene = null) |
| | 55 | | { |
| 0 | 56 | | return ConvertPointInSceneToUnityPosition(Vector3.zero, scene); |
| | 57 | | } |
| | 58 | |
|
| | 59 | | public static Vector3 ConvertPointInSceneToUnityPosition(Vector3 pos, IParcelScene scene = null) |
| | 60 | | { |
| 2 | 61 | | if (scene == null) |
| | 62 | | { |
| 0 | 63 | | scene = GetCurrentScene(); |
| | 64 | |
|
| 0 | 65 | | if (scene == null) |
| 0 | 66 | | return pos; |
| | 67 | | } |
| | 68 | |
|
| 2 | 69 | | return ConvertPointInSceneToUnityPosition(pos, |
| | 70 | | new Vector2Int(scene.sceneData.basePosition.x, scene.sceneData.basePosition.y)); |
| | 71 | | } |
| | 72 | |
|
| | 73 | | public static Vector3 ConvertPointInSceneToUnityPosition(Vector3 pos, Vector2Int scenePoint) |
| | 74 | | { |
| 9 | 75 | | Vector3 scenePosition = Utils.GridToWorldPosition(scenePoint.x, scenePoint.y) + pos; |
| 9 | 76 | | Vector3 worldPosition = PositionUtils.WorldToUnityPosition(scenePosition); |
| | 77 | |
|
| 9 | 78 | | return worldPosition; |
| | 79 | | } |
| | 80 | |
|
| | 81 | | public static bool IsCharacterInsideScene(IParcelScene scene) |
| | 82 | | { |
| 34 | 83 | | return scene.IsInsideSceneBoundaries(DataStore.i.player.playerGridPosition.Get()); |
| | 84 | | } |
| | 85 | |
|
| | 86 | | public static IParcelScene GetCurrentScene() |
| | 87 | | { |
| 6 | 88 | | var worldState = Environment.i.world.state; |
| 6 | 89 | | string currentSceneId = worldState.GetCurrentSceneId(); |
| | 90 | |
|
| 6 | 91 | | if (string.IsNullOrEmpty(currentSceneId)) |
| 0 | 92 | | return null; |
| | 93 | |
|
| 6 | 94 | | bool foundCurrentScene = worldState.TryGetScene(currentSceneId, out IParcelScene scene); |
| | 95 | |
|
| 6 | 96 | | if (!foundCurrentScene) |
| 2 | 97 | | return null; |
| | 98 | |
|
| 4 | 99 | | return scene; |
| | 100 | | } |
| | 101 | | } |
| | 102 | | } |