| | | 1 | | using Chronicler; |
| | | 2 | | using FixedMathSharp; |
| | | 3 | | using System; |
| | | 4 | | |
| | | 5 | | namespace Trailblazer.Navigation; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Navigator-owned opt-in settings for consuming context-registered heightmap layers. |
| | | 9 | | /// </summary> |
| | | 10 | | public sealed class NavigatorHeightmapGroundingSettings : IRecordable |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Gets the configured heightmap grounding mode. |
| | | 14 | | /// </summary> |
| | | 15 | | public HeightmapGroundingMode Mode { get; private set; } |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Gets the optional configured layer preference used before an active layer has been established. |
| | | 19 | | /// </summary> |
| | | 20 | | public string? LayerName { get; private set; } |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Gets the active layer selected by the most recent successful heightmap grounding sample. |
| | | 24 | | /// </summary> |
| | | 25 | | public string? ActiveLayerName { get; internal set; } |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Gets the extra root offset applied above sampled ground Y. |
| | | 29 | | /// </summary> |
| | | 30 | | public Fixed64 GroundOffset { get; private set; } |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Gets the maximum allowed root-Y correction for positional projection, or null for no limit. |
| | | 34 | | /// </summary> |
| | | 35 | | public Fixed64? SnapTolerance { get; private set; } |
| | | 36 | | |
| | | 37 | | internal void Configure( |
| | | 38 | | HeightmapGroundingMode mode, |
| | | 39 | | string? layerName, |
| | | 40 | | Fixed64? groundOffset, |
| | | 41 | | Fixed64? snapTolerance) |
| | | 42 | | { |
| | 12 | 43 | | ValidateMode(mode); |
| | 12 | 44 | | if (snapTolerance.HasValue && snapTolerance.Value < Fixed64.Zero) |
| | 0 | 45 | | throw new ArgumentOutOfRangeException(nameof(snapTolerance), "Heightmap snap tolerance cannot be negative.") |
| | | 46 | | |
| | 12 | 47 | | Mode = mode; |
| | 12 | 48 | | LayerName = string.IsNullOrWhiteSpace(layerName) ? null : layerName; |
| | 12 | 49 | | GroundOffset = groundOffset ?? Fixed64.Zero; |
| | 12 | 50 | | SnapTolerance = snapTolerance; |
| | 12 | 51 | | ActiveLayerName = mode == HeightmapGroundingMode.Disabled ? null : LayerName; |
| | 12 | 52 | | } |
| | | 53 | | |
| | | 54 | | internal void Reset() |
| | | 55 | | { |
| | 3 | 56 | | Mode = HeightmapGroundingMode.Disabled; |
| | 3 | 57 | | LayerName = null; |
| | 3 | 58 | | ActiveLayerName = null; |
| | 3 | 59 | | GroundOffset = Fixed64.Zero; |
| | 3 | 60 | | SnapTolerance = null; |
| | 3 | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <inheritdoc/> |
| | | 64 | | public void RecordData(IChronicler chronicler) |
| | | 65 | | { |
| | 86 | 66 | | HeightmapGroundingMode mode = Mode; |
| | 86 | 67 | | string? layerName = LayerName; |
| | 86 | 68 | | string? activeLayerName = ActiveLayerName; |
| | 86 | 69 | | Fixed64 groundOffset = GroundOffset; |
| | 86 | 70 | | bool hasSnapTolerance = SnapTolerance.HasValue; |
| | 86 | 71 | | Fixed64 snapTolerance = SnapTolerance ?? Fixed64.Zero; |
| | | 72 | | |
| | 86 | 73 | | RecordValues.Look(chronicler, ref mode, "Mode", HeightmapGroundingMode.Disabled); |
| | 86 | 74 | | RecordValues.Look(chronicler, ref layerName, "LayerName", null); |
| | 86 | 75 | | RecordValues.Look(chronicler, ref activeLayerName, "ActiveLayerName", null); |
| | 86 | 76 | | RecordValues.Look(chronicler, ref groundOffset, "GroundOffset", Fixed64.Zero); |
| | 86 | 77 | | RecordValues.Look(chronicler, ref hasSnapTolerance, "HasSnapTolerance", false); |
| | 86 | 78 | | RecordValues.Look(chronicler, ref snapTolerance, "SnapTolerance", Fixed64.Zero); |
| | | 79 | | |
| | 86 | 80 | | if (chronicler.Mode == SerializationMode.Loading) |
| | | 81 | | { |
| | 44 | 82 | | ValidateMode(mode); |
| | 44 | 83 | | if (hasSnapTolerance && snapTolerance < Fixed64.Zero) |
| | 0 | 84 | | throw new ArgumentOutOfRangeException(nameof(SnapTolerance), "Heightmap snap tolerance cannot be negativ |
| | | 85 | | |
| | 44 | 86 | | Mode = mode; |
| | 44 | 87 | | LayerName = string.IsNullOrWhiteSpace(layerName) ? null : layerName; |
| | 44 | 88 | | ActiveLayerName = string.IsNullOrWhiteSpace(activeLayerName) ? null : activeLayerName; |
| | 44 | 89 | | GroundOffset = groundOffset; |
| | 44 | 90 | | SnapTolerance = hasSnapTolerance ? snapTolerance : null; |
| | | 91 | | } |
| | 86 | 92 | | } |
| | | 93 | | |
| | | 94 | | private static void ValidateMode(HeightmapGroundingMode mode) |
| | | 95 | | { |
| | | 96 | | switch (mode) |
| | | 97 | | { |
| | | 98 | | case HeightmapGroundingMode.Disabled: |
| | | 99 | | case HeightmapGroundingMode.SurfaceLevelOnly: |
| | | 100 | | case HeightmapGroundingMode.SurfaceLevelAndPosition: |
| | 56 | 101 | | return; |
| | | 102 | | default: |
| | 0 | 103 | | throw new ArgumentOutOfRangeException(nameof(mode), "Unknown heightmap grounding mode."); |
| | | 104 | | } |
| | | 105 | | } |
| | | 106 | | } |