< Summary

Class:SpawnPointsDataHandler
Assembly:DebugPlugins.SpawnpointsDisplayerPlugin
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/DebugPlugins/SpawnPointsDisplayerPlugin/SpawnPointsDataHandler.cs
Covered lines:53
Uncovered lines:1
Coverable lines:54
Total lines:135
Line coverage:98.1% (53 of 54)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
SpawnPointsDataHandler(...)0%110100%
CreateSpawnPoints(...)0%220100%
RemoveSpawnPoints(...)0%220100%
Dispose()0%220100%
DisposeIndicatorsOfSceneId(...)0%3.033085.71%
GetName(...)0%880100%
GetSize(...)0%10100100%
GetPosition(...)0%110100%
GetLookAtRotation(...)0%220100%
IsSpawnArea(...)0%330100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/DebugPlugins/SpawnPointsDisplayerPlugin/SpawnPointsDataHandler.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using UnityEngine;
 5using Variables.SpawnPoints;
 6
 7internal interface ISpawnPointsDataHandler : IDisposable
 8{
 9    void CreateSpawnPoints(in string sceneId, in SceneSpawnPoint[] spawnPoints);
 10    void RemoveSpawnPoints(in string sceneId);
 11}
 12
 13internal class SpawnPointsDataHandler : ISpawnPointsDataHandler
 14{
 15    internal const string POINT_TYPE_NAME = "SpawnPoint";
 16    internal const string AREA_TYPE_NAME = "SpawnArea";
 17    internal const string DEFAULT_NAME = "Unnamed";
 18    internal const string NAME_DEFAULT_INDICATOR = "*";
 19    internal const string NAME_FORMAT = "{0}: {1}{2}";
 20
 21    internal const float SIZE_MIN_XZ = 0.5f;
 22    internal const float SIZE_MIN_Y = 2.0f;
 23
 324    internal Dictionary<string, List<ISpawnPointIndicator>> indicatorsBySceneId = new Dictionary<string, List<ISpawnPoin
 25    internal ISpawnPointIndicatorInstantiator instantiator;
 26
 327    public SpawnPointsDataHandler(ISpawnPointIndicatorInstantiator instantiator)
 28    {
 329        this.instantiator = instantiator;
 330    }
 31
 32    void ISpawnPointsDataHandler.CreateSpawnPoints(in string sceneId, in SceneSpawnPoint[] spawnPoints)
 33    {
 234        List<ISpawnPointIndicator> sceneSpawnPoints = new List<ISpawnPointIndicator>(spawnPoints.Length);
 1235        for (int i = 0; i < spawnPoints.Length; i++)
 36        {
 437            var spawnPoint = spawnPoints[i];
 38
 439            var name = GetName(spawnPoint);
 440            var size = GetSize(spawnPoint);
 441            var position = GetPosition(spawnPoint, size);
 442            var rotation = GetLookAtRotation(spawnPoint, position);
 43
 444            var indicator = instantiator.Instantiate();
 445            indicator.SetName(name);
 446            indicator.SetPosition(position);
 447            indicator.SetSize(size);
 448            indicator.SetRotation(rotation);
 49
 450            sceneSpawnPoints.Add(indicator);
 51        }
 252        indicatorsBySceneId.Add(sceneId, sceneSpawnPoints);
 253    }
 54
 55    void ISpawnPointsDataHandler.RemoveSpawnPoints(in string sceneId)
 56    {
 157        if (DisposeIndicatorsOfSceneId(sceneId, indicatorsBySceneId))
 58        {
 159            indicatorsBySceneId.Remove(sceneId);
 60        }
 161    }
 62
 63    void IDisposable.Dispose()
 64    {
 165        var sceneIds = indicatorsBySceneId.Keys.ToArray();
 466        foreach (var sceneId in sceneIds)
 67        {
 168            DisposeIndicatorsOfSceneId(sceneId, indicatorsBySceneId);
 69        }
 170        indicatorsBySceneId.Clear();
 171    }
 72
 73    private static bool DisposeIndicatorsOfSceneId(in string sceneId, in IDictionary<string, List<ISpawnPointIndicator>>
 74    {
 275        if (!indicatorsBySceneId.TryGetValue(sceneId, out List<ISpawnPointIndicator> indicators))
 76        {
 077            return false;
 78        }
 1679        foreach (var indicator in indicators)
 80        {
 681            indicator.Dispose();
 82        }
 283        return true;
 84    }
 85
 86    internal static string GetName(in SceneSpawnPoint spawnPoint)
 87    {
 888        string nameType = IsSpawnArea(spawnPoint) ? AREA_TYPE_NAME : POINT_TYPE_NAME;
 889        string name = string.IsNullOrEmpty(spawnPoint.name) ? DEFAULT_NAME : spawnPoint.name;
 890        string defaultIndicator = spawnPoint.@default.HasValue && spawnPoint.@default.Value ? NAME_DEFAULT_INDICATOR : s
 891        return string.Format(NAME_FORMAT, nameType, name, defaultIndicator);
 92    }
 93
 94    internal static Vector3 GetSize(in SceneSpawnPoint spawnPoint)
 95    {
 796        float x = spawnPoint.position.x.Length > 1 ? (spawnPoint.position.x[1] - spawnPoint.position.x[0]) : 0;
 797        float y = spawnPoint.position.y.Length > 1 ? (spawnPoint.position.y[1] - spawnPoint.position.y[0]) : 0;
 798        float z = spawnPoint.position.z.Length > 1 ? (spawnPoint.position.z[1] - spawnPoint.position.z[0]) : 0;
 99
 7100        if (x < SIZE_MIN_XZ)
 5101            x = SIZE_MIN_XZ;
 102
 7103        if (z < SIZE_MIN_XZ)
 7104            z = SIZE_MIN_XZ;
 105
 7106        if (y < SIZE_MIN_Y)
 6107            y = SIZE_MIN_Y;
 108
 7109        return new Vector3(x, y, z);
 110    }
 111
 112    internal static Vector3 GetPosition(in SceneSpawnPoint spawnPoint, in Vector3 size)
 113    {
 6114        float x = spawnPoint.position.x[0] + size.x * 0.5f;
 6115        float y = spawnPoint.position.y[0] + size.y * 0.5f;
 6116        float z = spawnPoint.position.z[0] + size.z * 0.5f;
 117
 6118        return new Vector3(x, y, z);
 119    }
 120
 121    internal static Quaternion? GetLookAtRotation(in SceneSpawnPoint spawnPoint, in Vector3 position)
 122    {
 7123        if (spawnPoint.cameraTarget == null)
 124        {
 5125            return null;
 126        }
 127
 2128        return Quaternion.LookRotation(spawnPoint.cameraTarget.Value - position);
 129    }
 130
 131    private static bool IsSpawnArea(in SceneSpawnPoint spawnPoint)
 132    {
 8133        return spawnPoint.position.x.Length > 1 || spawnPoint.position.y.Length > 1 || spawnPoint.position.y.Length > 1;
 134    }
 135}