< Summary

Class:DCL.WorldStateUtils
Assembly:MainScripts
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/Utils/WorldStateUtils.cs
Covered lines:44
Uncovered lines:24
Coverable lines:68
Total lines:176
Line coverage:64.7% (44 of 68)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
IsGlobalScene(...)0%6200%
TryToGetSceneCoordsID(...)0%6200%
ConvertUnityToScenePosition(...)0%330100%
ConvertSceneToUnityPosition(...)0%110100%
ConvertScenePositionToUnityPosition(...)0%110100%
ConvertPointInSceneToUnityPosition(...)0%4.943040%
ConvertPointInSceneToUnityPosition(...)0%110100%
IsCharacterInsideScene(...)0%110100%
GetActivePortableExperienceScenes()0%20400%
GetActivePortableExperienceIds()0%5.44055.56%
GetCurrentScene()0%3.023087.5%
CreateTestScene(...)0%9.089090%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/Utils/WorldStateUtils.cs

#LineLine coverage
 1using System.Collections.Generic;
 2using DCL.Controllers;
 3using DCL.Helpers;
 4using DCL.Models;
 5using UnityEngine;
 6
 7namespace DCL
 8{
 9    public static class WorldStateUtils
 10    {
 11        public static bool IsGlobalScene(string sceneId)
 12        {
 013            var worldState = Environment.i.world.state;
 14
 015            if (worldState.TryGetScene(sceneId, out IParcelScene scene))
 16            {
 017                return scene is GlobalScene;
 18            }
 19
 020            return false;
 21        }
 22
 23        public static string TryToGetSceneCoordsID(string id)
 24        {
 025            var worldState = Environment.i.world.state;
 26
 027            if (worldState.TryGetScene(id, out IParcelScene parcelScene))
 28            {
 029                return parcelScene.sceneData.basePosition.ToString();
 30            }
 31
 032            return id;
 33        }
 34
 35        public static Vector3 ConvertUnityToScenePosition(Vector3 pos, IParcelScene scene = null)
 36        {
 6537            if (scene == null)
 38            {
 639                scene = GetCurrentScene();
 40
 641                if (scene == null)
 442                    return pos;
 43            }
 44
 6145            Vector3 worldPosition = PositionUtils.UnityToWorldPosition(pos);
 6146            return worldPosition - Utils.GridToWorldPosition(scene.sceneData.basePosition.x, scene.sceneData.basePositio
 47        }
 48
 249        public static Vector3 ConvertSceneToUnityPosition(Vector3 pos, IParcelScene scene = null) { return ConvertPointI
 50
 7651        public static Vector3 ConvertScenePositionToUnityPosition(IParcelScene scene = null) { return ConvertPointInScen
 52
 53        public static Vector3 ConvertPointInSceneToUnityPosition(Vector3 pos, IParcelScene scene = null)
 54        {
 7855            if (scene == null)
 56            {
 057                scene = GetCurrentScene();
 58
 059                if (scene == null)
 060                    return pos;
 61            }
 62
 7863            return ConvertPointInSceneToUnityPosition(pos, new Vector2Int(scene.sceneData.basePosition.x, scene.sceneDat
 64        }
 65
 66        public static Vector3 ConvertPointInSceneToUnityPosition(Vector3 pos, Vector2Int scenePoint)
 67        {
 8568            Vector3 scenePosition = Utils.GridToWorldPosition(scenePoint.x, scenePoint.y) + pos;
 8569            Vector3 worldPosition = PositionUtils.WorldToUnityPosition(scenePosition);
 70
 8571            return worldPosition;
 72        }
 73
 74        public static bool IsCharacterInsideScene(IParcelScene scene)
 75        {
 2676            Vector2Int gridPosition = Utils.WorldToGridPosition(CommonScriptableObjects.playerWorldPosition.Get());
 2677            return scene.IsInsideSceneBoundaries(gridPosition);
 78        }
 79
 80        public static List<GlobalScene> GetActivePortableExperienceScenes()
 81        {
 082            List<GlobalScene> activePortableExperienceScenes = new List<GlobalScene>();
 083            IWorldState worldState = Environment.i.world.state;
 84
 085            foreach (var globalSceneId in worldState.globalSceneIds)
 86            {
 087                if (worldState.TryGetScene(globalSceneId, out GlobalScene scene))
 88                {
 089                    if (scene.isPortableExperience)
 90                    {
 091                        activePortableExperienceScenes.Add(scene);
 92                    }
 93                }
 94            }
 95
 096            return activePortableExperienceScenes;
 97        }
 98
 99        public static List<string> GetActivePortableExperienceIds()
 100        {
 14101            List<string> currentSceneAndPortableExperiencesIds = new List<string>();
 14102            IWorldState worldState = Environment.i.world.state;
 103
 28104            foreach (var globalSceneId in worldState.globalSceneIds)
 105            {
 0106                if (worldState.TryGetScene(globalSceneId, out GlobalScene scene))
 107                {
 0108                    if (scene.isPortableExperience)
 109                    {
 0110                        currentSceneAndPortableExperiencesIds.Add(globalSceneId);
 111                    }
 112                }
 113            }
 114
 14115            return currentSceneAndPortableExperiencesIds;
 116        }
 117
 118        public static IParcelScene GetCurrentScene()
 119        {
 6120            var worldState = Environment.i.world.state;
 6121            string currentSceneId = worldState.currentSceneId;
 122
 6123            if (string.IsNullOrEmpty(currentSceneId))
 4124                return null;
 125
 2126            bool foundCurrentScene = worldState.loadedScenes.TryGetValue(currentSceneId, out IParcelScene scene);
 127
 2128            if (!foundCurrentScene)
 0129                return null;
 130
 2131            return scene;
 132        }
 133
 134        public static IParcelScene CreateTestScene(LoadParcelScenesMessage.UnityParcelScene data = null)
 135        {
 468136            if (data == null)
 137            {
 468138                data = new LoadParcelScenesMessage.UnityParcelScene();
 139            }
 140
 468141            if (data.parcels == null)
 142            {
 468143                data.parcels = new Vector2Int[] { data.basePosition };
 144            }
 145
 468146            if (string.IsNullOrEmpty(data.id))
 147            {
 468148                data.id = $"(test):{data.basePosition.x},{data.basePosition.y}";
 149            }
 150
 468151            if (Environment.i.world.state.loadedScenes != null)
 152            {
 468153                if (Environment.i.world.state.loadedScenes.ContainsKey(data.id))
 154                {
 0155                    Debug.LogWarning($"Scene {data.id} is already loaded.");
 0156                    return Environment.i.world.state.loadedScenes[data.id];
 157                }
 158            }
 159
 468160            var go = new GameObject();
 468161            var newScene = go.AddComponent<ParcelScene>();
 468162            newScene.isTestScene = true;
 468163            newScene.isPersistent = true;
 468164            newScene.SetData(data);
 165
 468166            if (DCLCharacterController.i != null)
 360167                newScene.InitializeDebugPlane();
 168
 468169            Environment.i.world.state.scenesSortedByDistance?.Add(newScene);
 468170            Environment.i.world.state.loadedScenes?.Add(data.id, newScene);
 171
 172
 468173            return newScene;
 174        }
 175    }
 176}