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