< Summary

Information
Class: Trailblazer.Navigation.Motor.TransitState
Assembly: Trailblazer
File(s): /home/runner/work/Trailblazer/Trailblazer/src/Trailblazer/Navigation/Motor/Climbing/TransitState.cs
Line coverage
100%
Covered lines: 27
Uncovered lines: 0
Coverable lines: 27
Total lines: 149
Line coverage: 100%
Branch coverage
100%
Covered branches: 12
Total branches: 12
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_PreviousMedium()100%22100%
.ctor(...)100%11100%
.ctor(...)100%11100%
Update(...)100%44100%
GetSignedSlopeAngle(...)100%66100%
ToTrekCondition()100%11100%

File(s)

/home/runner/work/Trailblazer/Trailblazer/src/Trailblazer/Navigation/Motor/Climbing/TransitState.cs

#LineLine coverage
 1//=======================================================================
 2// TransitState.cs
 3//=======================================================================
 4// MIT License, Copyright (c) 2024-present David Oravsky (mrdav30)
 5// See LICENSE file in the project root for full license information.
 6//=======================================================================
 7
 8using FixedMathSharp;
 9
 10namespace Trailblazer.Navigation.Motor;
 11
 12/// <summary>
 13/// Represents the traversal state for a object while using a <see cref="NavMotor"/>
 14///  and provides synchronization with <see cref="TrekCondition"/>.
 15/// </summary>
 16public class TransitState
 17{
 18    #region State Properties
 19
 20    /// <summary>
 21    /// The current traversal medium.
 22    /// </summary>
 23    public TraversalMedium Medium { get; private set; }
 24
 25    /// <summary>
 26    /// The height of the surface the entity is interacting with.
 27    /// </summary>
 28    public Fixed64 SurfaceLevel { get; private set; }
 29
 30    /// <summary>
 31    /// The ceiling height above the entity.
 32    /// </summary>
 33    public Fixed64 CeilingLevel { get; private set; } = Fixed64.MaxValue;
 34
 35    /// <summary>
 36    /// The ground state, if applicable.
 37    /// </summary>
 38    public GroundCondition? GroundState { get; private set; }
 39
 40    /// <summary>
 41    /// The normal of the ground surface.
 42    /// </summary>
 43    public Vector3d SurfaceNormal { get; private set; }
 44
 45    /// <summary>
 46    /// Gets the angle of the slope at the current position, in fixed-point degrees.
 47    /// </summary>
 48    public Fixed64 SlopeAngle { get; private set; }
 49
 50    /// <summary>
 51    /// The previous traversal state (for comparison and transition detection).
 52    /// </summary>
 53    public TrekCondition? PreviousState { get; private set; }
 54
 55    /// <summary>
 56    /// The previous traversal medium, treating a missing previous sample as <see cref="TraversalMedium.Unknown"/>.
 57    /// </summary>
 659158    public TraversalMedium PreviousMedium => PreviousState?.Medium ?? TraversalMedium.Unknown;
 59
 60    #endregion
 61
 62    #region Constructors
 63
 64    /// <summary>
 65    /// Initializes a new instance of the TransitState class using the specified trek condition.
 66    /// </summary>
 67    /// <param name="condition">The trek condition to use for initializing the state. Cannot be null.</param>
 107868    public TransitState(TrekCondition condition) => Update(condition, null);
 69
 70    /// <summary>
 71    /// Initializes a new instance of the TransitState class with the specified current and previous trek conditions.
 72    /// </summary>
 73    /// <param name="condition">The current trek condition to represent. Cannot be null.</param>
 74    /// <param name="previous">The previous trek condition, or null if there is no previous condition.</param>
 275    public TransitState(TrekCondition condition, TrekCondition? previous) => Update(condition, previous);
 76
 77    #endregion
 78
 79    #region State Update
 80
 81    /// <summary>
 82    /// Updates the traversal state and retains the previous state for transition tracking.
 83    /// </summary>
 84    public void Update(TrekCondition condition, TrekCondition? previous)
 85    {
 305086        PreviousState = previous;
 305087        Medium = condition.Medium;
 305088        SurfaceLevel = condition.SurfaceLevel;
 305089        GroundState = condition.GroundState;
 90
 305091        if (Medium == TraversalMedium.Solid)
 92        {
 84893            SurfaceNormal = GroundState?.GroundNormal ?? Vector3d.Zero;
 84894            SlopeAngle = Vector3d.Angle(Vector3d.Up, SurfaceNormal);
 95        }
 96        else
 97        {
 220298            SurfaceNormal = Vector3d.Up;
 220299            SlopeAngle = Fixed64.Zero;
 100        }
 101
 3050102        CeilingLevel = condition.CeilingLevel;
 3050103    }
 104
 105    #endregion
 106
 107    #region Utility Methods
 108
 109    /// <summary>
 110    /// Calculates the signed angle of the current surface slope relative to the specified movement direction.
 111    /// </summary>
 112    /// <param name="moveDirection">
 113    /// The movement direction vector used to determine the orientation relative to the slope.
 114    /// If this value is <see cref="Vector3d.Zero"/>, the method treats the movement as oriented downhill.
 115    /// </param>
 116    /// <returns>
 117    /// The signed slope angle in degrees.
 118    /// A negative value indicates movement downhill, and a positive value indicates movement uphill.
 119    /// Returns zero if the surface is flat.
 120    /// </returns>
 121    public Fixed64 GetSignedSlopeAngle(Vector3d moveDirection)
 122    {
 2184123        if (SlopeAngle == Fixed64.Zero)
 2100124            return Fixed64.Zero;
 125
 126        // Treat downhill as gravity-biased when idle
 84127        if (moveDirection == Vector3d.Zero)
 30128            moveDirection = Vector3d.Backward;
 129
 84130        bool isDownhill = Vector3d.Dot(moveDirection.Normalized, SurfaceNormal) < Fixed64.Zero;
 84131        return isDownhill ? -SlopeAngle : SlopeAngle;
 132    }
 133
 134    /// <summary>
 135    /// Returns a new TraversalCondition instance reflecting the current state.
 136    /// </summary>
 137    public TrekCondition ToTrekCondition()
 138    {
 2362139        return new TrekCondition()
 2362140        {
 2362141            Medium = Medium,
 2362142            SurfaceLevel = SurfaceLevel,
 2362143            GroundState = GroundState,
 2362144            CeilingLevel = CeilingLevel
 2362145        };
 146    }
 147
 148    #endregion
 149}