< Summary

Class:DCL.Skybox.SkyboxSatelliteElements
Assembly:ProceduralSkybox
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Rendering/ProceduralSkybox/ToolProceduralSkybox/Scripts/Skybox3DElements/SkyboxSatelliteElements.cs
Covered lines:0
Uncovered lines:75
Coverable lines:75
Total lines:184
Line coverage:0% (0 of 75)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
SkyboxSatelliteElements(...)0%2100%
Initialize()0%6200%
ApplySatelliteConfigurations(...)0%42600%
ResetObjects()0%20400%
GetSatelliteAllActiveSatelliteRefs(...)0%6200%
GetSatelliteObject(...)0%20400%
InstantiateNewSatelliteReference(...)0%6200%
ResolveCameraDependency(...)0%6200%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Rendering/ProceduralSkybox/ToolProceduralSkybox/Scripts/Skybox3DElements/SkyboxSatelliteElements.cs

#LineLine coverage
 1using System.Collections;
 2using System.Collections.Generic;
 3using DCL.Helpers;
 4using UnityEngine;
 5
 6namespace DCL.Skybox
 7{
 8    public class SatelliteReferences
 9    {
 10        public GameObject satelliteParent;
 11        public GameObject orbitGO;
 12        public GameObject satelliteGO;
 13        public GameObject satellitePrefab;
 14        public SatelliteLayerBehaviour satelliteBehavior;
 15    }
 16
 17    public class SkyboxSatelliteElements
 18    {
 19        const string satelliteParentResourcePath = "SkyboxPrefabs/Satellite Parent";
 20
 21        private readonly GameObject satelliteElements;
 22        private GameObject satelliteParentPrefab;
 23        private FollowBehaviour followBehavior;
 24
 025        Dictionary<GameObject, Queue<SatelliteReferences>> satelliteReferences = new Dictionary<GameObject, Queue<Satell
 026        List<SatelliteReferences> usedSatellites = new List<SatelliteReferences>();
 27
 028        public SkyboxSatelliteElements(GameObject satelliteElements)
 29        {
 030            this.satelliteElements = satelliteElements;
 031            Initialize();
 032        }
 33
 34        private void Initialize()
 35        {
 036            if (satelliteElements == null)
 37            {
 038                Debug.LogError("Satellite Elements Container is null");
 039                return;
 40            }
 41
 042            followBehavior = satelliteElements.GetOrCreateComponent<FollowBehaviour>();
 043            followBehavior.followPos = true;
 044            followBehavior.ignoreYAxis = true;
 045        }
 46
 47        internal void ApplySatelliteConfigurations(SkyboxConfiguration config, float dayTime, float normalizedDayTime, L
 48        {
 049            ResetObjects();
 050            List<SatelliteReferences> satelliteRefs = GetSatelliteAllActiveSatelliteRefs(config.satelliteLayers);
 51
 052            if (satelliteRefs.Count != config.satelliteLayers.Count)
 53            {
 054                Debug.LogWarning("Satellite not working!, cause prefab is not assigned");
 055                return;
 56            }
 57
 058            for (int i = 0; i < satelliteRefs.Count; i++)
 59            {
 60                // If satellite is disabled, disable the 3D object too.
 061                if (!config.satelliteLayers[i].enabled)
 62                {
 063                    if (satelliteRefs[i] != null)
 64                    {
 065                        satelliteRefs[i].satelliteParent.SetActive(false);
 066                        satelliteRefs[i].satelliteBehavior.ChangeRenderType(LayerRenderType.NotRendering);
 67                    }
 068                    continue;
 69                }
 70
 071                if (satelliteRefs[i] == null)
 72                {
 73#if UNITY_EDITOR
 074                    Debug.LogWarning(config.satelliteLayers[i].nameInEditor + " Satellite not working!, prefab not assig
 75#endif
 076                    continue;
 77                }
 78
 079                satelliteRefs[i].satelliteBehavior.AssignValues(config.satelliteLayers[i], dayTime, cycleTime);
 80            }
 081        }
 82
 83        private void ResetObjects()
 84        {
 085            if (usedSatellites != null)
 86            {
 087                for (int i = 0; i < usedSatellites.Count; i++)
 88                {
 089                    SatelliteReferences sat = usedSatellites[i];
 090                    if (sat == null)
 91                    {
 92                        continue;
 93                    }
 094                    sat.satelliteParent.SetActive(false);
 095                    satelliteReferences[sat.satellitePrefab].Enqueue(sat);
 96                }
 097                usedSatellites.Clear();
 98            }
 099        }
 100
 101        private List<SatelliteReferences> GetSatelliteAllActiveSatelliteRefs(List<Config3DSatellite> satelliteLayers)
 102        {
 0103            for (int i = 0; i < satelliteLayers.Count; i++)
 104            {
 0105                GetSatelliteObject(satelliteLayers[i]);
 106            }
 0107            return usedSatellites;
 108        }
 109
 110        private SatelliteReferences GetSatelliteObject(Config3DSatellite config)
 111        {
 0112            SatelliteReferences tempSatellite = null;
 113
 0114            if (config.satellite == null)
 115            {
 0116                usedSatellites.Add(tempSatellite);
 0117                return tempSatellite;
 118            }
 119
 120            // Check if GO for this prefab is already in scene, else create new
 0121            if (satelliteReferences.ContainsKey(config.satellite))
 122            {
 123                // Check if there is any unused GO for the given prefab
 0124                if (satelliteReferences[config.satellite].Count > 0)
 125                {
 0126                    tempSatellite = satelliteReferences[config.satellite].Dequeue();
 127                }
 128                else
 129                {
 0130                    tempSatellite = InstantiateNewSatelliteReference(config);
 131                }
 132            }
 133            else
 134            {
 0135                satelliteReferences.Add(config.satellite, new Queue<SatelliteReferences>());
 0136                tempSatellite = InstantiateNewSatelliteReference(config);
 137            }
 138
 0139            usedSatellites.Add(tempSatellite);
 0140            tempSatellite.satelliteParent.SetActive(true);
 141
 0142            return tempSatellite;
 143        }
 144
 145        private SatelliteReferences InstantiateNewSatelliteReference(Config3DSatellite config)
 146        {
 0147            if (satelliteParentPrefab == null)
 148            {
 0149                satelliteParentPrefab = Resources.Load<GameObject>(satelliteParentResourcePath);
 150            }
 151
 0152            GameObject obj = GameObject.Instantiate<GameObject>(satelliteParentPrefab);
 0153            obj.layer = LayerMask.NameToLayer("Skybox");
 0154            obj.name = "Satellite Parent";
 0155            obj.transform.parent = satelliteElements.transform;
 0156            obj.transform.localPosition = Vector3.zero;
 157
 0158            GameObject orbit = obj.transform.GetChild(0).gameObject;
 0159            GameObject satelliteObj = GameObject.Instantiate(config.satellite);
 0160            satelliteObj.transform.parent = obj.transform;
 161
 162            // Get satellite behavior and assign satellite
 0163            SatelliteLayerBehaviour satelliteBehavior = obj.GetComponent<SatelliteLayerBehaviour>();
 0164            satelliteBehavior.satellite = satelliteObj;
 165
 0166            SatelliteReferences satellite = new SatelliteReferences();
 0167            satellite.satelliteParent = obj;
 0168            satellite.orbitGO = orbit;
 0169            satellite.satelliteGO = satelliteObj;
 0170            satellite.satellitePrefab = config.satellite;
 0171            satellite.satelliteBehavior = satelliteBehavior;
 172
 0173            return satellite;
 174        }
 175
 176        public void ResolveCameraDependency(Transform currentTransform)
 177        {
 0178            if (followBehavior != null)
 179            {
 0180                followBehavior.target = currentTransform.gameObject;
 181            }
 0182        }
 183    }
 184}