< Summary

Class:DCLServices.MapRendererV2.ConsumerUtils.MapRendererTrackPlayerPosition
Assembly:MapRendererConsumerUtils
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLServices/MapRendererV2/ConsumerUtils/MapRendererTrackPlayerPosition.cs
Covered lines:16
Uncovered lines:0
Coverable lines:16
Total lines:57
Line coverage:100% (16 of 16)
Covered branches:0
Total branches:0
Covered methods:5
Total methods:5
Method coverage:100% (5 of 5)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
MapRendererTrackPlayerPosition(...)0%110100%
OnPlayerPositionChanged(...)0%220100%
GetPlayerCentricCoords(...)0%110100%
GetPlayerCentricCoords(...)0%110100%
Dispose()0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLServices/MapRendererV2/ConsumerUtils/MapRendererTrackPlayerPosition.cs

#LineLine coverage
 1using DCLServices.MapRendererV2.MapCameraController;
 2using System;
 3using UnityEngine;
 4
 5namespace DCLServices.MapRendererV2.ConsumerUtils
 6{
 7    /// <summary>
 8    /// Updates Map Camera Controller's position accordingly to the player position
 9    /// </summary>
 10    public struct MapRendererTrackPlayerPosition : IDisposable
 11    {
 12        private const float SQR_DISTANCE_TOLERANCE = 0.01f;
 13
 14        private readonly IMapCameraController cameraController;
 15        private readonly BaseVariable<Vector3> playerWorldPosition;
 16
 17        private Vector3 lastPlayerPosition;
 18
 19        public MapRendererTrackPlayerPosition(IMapCameraController cameraController, BaseVariable<Vector3> playerWorldPo
 20        {
 1021            this.cameraController = cameraController;
 1022            this.playerWorldPosition = playerWorldPosition;
 23
 1024            lastPlayerPosition = Vector3.positiveInfinity;
 25
 1026            OnPlayerPositionChanged(Vector3.positiveInfinity, playerWorldPosition.Get());
 27
 1028            playerWorldPosition.OnChange += OnPlayerPositionChanged;
 1029        }
 30
 31        private void OnPlayerPositionChanged(Vector3 oldPos, Vector3 newPos)
 32        {
 48933            if (Vector3.SqrMagnitude(newPos - lastPlayerPosition) < SQR_DISTANCE_TOLERANCE)
 19334                return;
 35
 29636            lastPlayerPosition = newPos;
 29637            cameraController.SetPosition(GetPlayerCentricCoords(newPos));
 29638        }
 39
 40        public static Vector2 GetPlayerCentricCoords(Vector3 playerPosition)
 41        {
 29642            var newCoords = DCL.Helpers.Utils.WorldToGridPositionUnclamped(playerPosition);
 29643            return GetPlayerCentricCoords(newCoords);
 44        }
 45
 46        public static Vector2 GetPlayerCentricCoords(Vector2 playerCoords)
 47        {
 48            // quick hack to align with `CoordsToPositionWithOffset` and the pivot
 30649            return playerCoords - Vector2.one;
 50        }
 51
 52        public void Dispose()
 53        {
 1054            playerWorldPosition.OnChange -= OnPlayerPositionChanged;
 1055        }
 56    }
 57}