< Summary

Class:GDX.Space
Assembly:GDX
File(s):./Packages/com.dotbunny.gdx/GDX/Space.cs
Covered lines:0
Uncovered lines:55
Coverable lines:55
Total lines:146
Line coverage:0% (0 of 55)
Covered branches:0
Total branches:0
Covered methods:0
Total methods:4
Method coverage:0% (0 of 4)

Coverage History

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Space()0%2100%
GetDirection(...)0%1821300%
ToRotation(...)0%72800%
ToRotation(...)0%1321100%

File(s)

./Packages/com.dotbunny.gdx/GDX/Space.cs

#LineLine coverage
 1// Copyright (c) 2020-2024 dotBunny Inc.
 2// dotBunny licenses this file to you under the BSL-1.0 license.
 3// See the LICENSE file in the project root for more information.
 4
 5using UnityEngine;
 6
 7namespace GDX
 8{
 9    public static class Space
 10    {
 11        public enum Axis
 12        {
 13            Undefined,
 14            X,
 15            Y,
 16            Z
 17        }
 18
 19        public enum Direction
 20        {
 21            /// <summary>
 22            ///     An undetermined direction.
 23            /// </summary>
 24            Undefined,
 25
 26            /// <summary>
 27            ///     Z axis.
 28            /// </summary>
 29            Forward,
 30
 31            /// <summary>
 32            ///     -Z axis.
 33            /// </summary>
 34            Backward,
 35
 36            /// <summary>
 37            ///     -X axis.
 38            /// </summary>
 39            Left,
 40
 41            /// <summary>
 42            ///     X axis.
 43            /// </summary>
 44            Right,
 45
 46            /// <summary>
 47            ///     Y axis.
 48            /// </summary>
 49            Up,
 50
 51            /// <summary>
 52            ///     -Y axis.
 53            /// </summary>
 54            Down
 55        }
 56
 57#if !UNITY_DOTSRUNTIME
 058        static readonly Quaternion k_RotationLeft = Quaternion.Euler(-90, 0, 0);
 059        static readonly Quaternion k_RotationRight = Quaternion.Euler(90, 0, 0);
 060        static readonly Quaternion k_RotationUp = Quaternion.Euler(0, 90, 0);
 061        static readonly Quaternion k_RotationDown = Quaternion.Euler(0, -90, 0);
 062        static readonly Quaternion k_RotationForward = Quaternion.Euler(0, 0, 90);
 063        static readonly Quaternion k_RotationBackward = Quaternion.Euler(0, 0, -90);
 64
 65        /// <summary>
 66        ///     Get the corresponding <see cref="Direction" /> for a <see cref="Vector3" /> provided direction.
 67        /// </summary>
 68        /// <remarks>Only works with deterministic directions builtin to Unity.</remarks>
 69        /// <param name="direction">The given direction.</param>
 70        /// <returns>A qualified <see cref="Direction" /> equivalent to the <paramref name="direction" />.</returns>
 71        public static Direction GetDirection(Vector3 direction)
 072        {
 73            // Left/Right
 074            if (direction.y == 0 && direction.z == 0)
 075            {
 076                if (direction.x > 0)
 077                {
 078                    return Direction.Right;
 79                }
 80
 081                if (direction.x < 0)
 082                {
 083                    return Direction.Left;
 84                }
 085            }
 86
 87            // Forward / Backward
 088            if (direction.x == 0 && direction.y == 0)
 089            {
 090                if (direction.z > 0)
 091                {
 092                    return Direction.Forward;
 93                }
 94
 095                if (direction.z < 0)
 096                {
 097                    return Direction.Backward;
 98                }
 099            }
 100
 101            // Up / Down
 0102            if (direction.x == 0 && direction.z == 0)
 0103            {
 0104                if (direction.y > 0)
 0105                {
 0106                    return Direction.Up;
 107                }
 108
 0109                if (direction.y < 0)
 0110                {
 0111                    return Direction.Down;
 112                }
 0113            }
 114
 0115            return Direction.Undefined;
 0116        }
 117
 118        public static Quaternion ToRotation(this Axis axis)
 0119        {
 0120            return axis switch
 121            {
 0122                Axis.Undefined => Quaternion.identity,
 0123                Axis.X => k_RotationRight,
 0124                Axis.Y => k_RotationUp,
 0125                Axis.Z => k_RotationForward,
 0126                _ => Quaternion.identity
 127            };
 0128        }
 129
 130        public static Quaternion ToRotation(this Direction direction)
 0131        {
 0132            return direction switch
 133            {
 0134                Direction.Undefined => Quaternion.identity,
 0135                Direction.Left => k_RotationLeft,
 0136                Direction.Right => k_RotationRight,
 0137                Direction.Up => k_RotationUp,
 0138                Direction.Down => k_RotationDown,
 0139                Direction.Backward => k_RotationBackward,
 0140                Direction.Forward => k_RotationForward,
 0141                _ => Quaternion.identity
 142            };
 0143        }
 144#endif // !UNITY_DOTSRUNTIME
 145    }
 146}