| | 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(int sceneNumber) |
| | 11 | | { |
| 1 | 12 | | var worldState = Environment.i.world.state; |
| | 13 | |
|
| 1 | 14 | | if (worldState.TryGetScene(sceneNumber, out IParcelScene scene)) |
| | 15 | | { |
| 1 | 16 | | return scene.isPersistent; |
| | 17 | | } |
| | 18 | |
|
| 0 | 19 | | return false; |
| | 20 | | } |
| | 21 | |
|
| | 22 | | public static string TryToGetSceneCoordsFromSceneNumber(int sceneNumber) |
| | 23 | | { |
| 0 | 24 | | var worldState = Environment.i.world.state; |
| | 25 | |
|
| 0 | 26 | | if (worldState.TryGetScene(sceneNumber, out IParcelScene parcelScene)) |
| | 27 | | { |
| 0 | 28 | | return parcelScene.sceneData.basePosition.ToString(); |
| | 29 | | } |
| | 30 | |
|
| 0 | 31 | | return $"scene number {sceneNumber}"; |
| | 32 | | } |
| | 33 | |
|
| | 34 | | public static Vector3 ConvertUnityToScenePosition(Vector3 pos, IParcelScene scene = null) |
| | 35 | | { |
| 81 | 36 | | if (scene == null) |
| | 37 | | { |
| 2 | 38 | | scene = GetCurrentScene(); |
| | 39 | |
|
| 2 | 40 | | if (scene == null) |
| 2 | 41 | | return pos; |
| | 42 | | } |
| | 43 | |
|
| 79 | 44 | | Vector3 worldPosition = PositionUtils.UnityToWorldPosition(pos); |
| 79 | 45 | | return worldPosition - Utils.GridToWorldPosition(scene.sceneData.basePosition.x, scene.sceneData.basePositio |
| | 46 | | } |
| | 47 | |
|
| | 48 | | public static Vector3 ConvertSceneToUnityPosition(Vector3 pos, IParcelScene scene = null) |
| | 49 | | { |
| 2 | 50 | | return ConvertPointInSceneToUnityPosition(pos, scene); |
| | 51 | | } |
| | 52 | |
|
| | 53 | | public static Vector3 ConvertScenePositionToUnityPosition(IParcelScene scene = null) |
| | 54 | | { |
| 0 | 55 | | return ConvertPointInSceneToUnityPosition(Vector3.zero, scene); |
| | 56 | | } |
| | 57 | |
|
| | 58 | | public static Vector3 ConvertPointInSceneToUnityPosition(Vector3 pos, IParcelScene scene = null) |
| | 59 | | { |
| 2 | 60 | | if (scene == null) |
| | 61 | | { |
| 0 | 62 | | scene = GetCurrentScene(); |
| | 63 | |
|
| 0 | 64 | | if (scene == null) |
| 0 | 65 | | return pos; |
| | 66 | | } |
| | 67 | |
|
| 2 | 68 | | return ConvertPointInSceneToUnityPosition(pos, |
| | 69 | | new Vector2Int(scene.sceneData.basePosition.x, scene.sceneData.basePosition.y)); |
| | 70 | | } |
| | 71 | |
|
| | 72 | | public static Vector3 ConvertPointInSceneToUnityPosition(Vector3 pos, Vector2Int scenePoint) |
| | 73 | | { |
| 2 | 74 | | Vector3 scenePosition = Utils.GridToWorldPosition(scenePoint.x, scenePoint.y) + pos; |
| 2 | 75 | | Vector3 worldPosition = PositionUtils.WorldToUnityPosition(scenePosition); |
| | 76 | |
|
| 2 | 77 | | return worldPosition; |
| | 78 | | } |
| | 79 | |
|
| | 80 | | public static bool IsCharacterInsideScene(IParcelScene scene) |
| | 81 | | { |
| 39 | 82 | | return scene.IsInsideSceneBoundaries(DataStore.i.player.playerGridPosition.Get()); |
| | 83 | | } |
| | 84 | |
|
| | 85 | | public static IParcelScene GetCurrentScene() |
| | 86 | | { |
| 2 | 87 | | var worldState = Environment.i.world.state; |
| 2 | 88 | | int currentSceneNumber = worldState.GetCurrentSceneNumber(); |
| | 89 | |
|
| 2 | 90 | | if (currentSceneNumber <= 0) |
| 0 | 91 | | return null; |
| | 92 | |
|
| 2 | 93 | | bool foundCurrentScene = worldState.TryGetScene(currentSceneNumber, out IParcelScene scene); |
| | 94 | |
|
| 2 | 95 | | if (!foundCurrentScene) |
| 2 | 96 | | return null; |
| | 97 | |
|
| 0 | 98 | | return scene; |
| | 99 | | } |
| | 100 | | } |
| | 101 | | } |