| | | 1 | | //======================================================================= |
| | | 2 | | // LocomotionProfileBuilder.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 | | |
| | | 10 | | namespace Trailblazer.Navigation.Motor; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Fluent builder for composing built-in locomotion profiles. |
| | | 14 | | /// </summary> |
| | | 15 | | public sealed class LocomotionProfileBuilder |
| | | 16 | | { |
| | | 17 | | #region Constructor |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Initializes a new builder. |
| | | 21 | | /// </summary> |
| | | 22 | | /// <param name="includeOptionalLocomotions">When true, seeds the builder with the default full locomotion profile.< |
| | 1195 | 23 | | public LocomotionProfileBuilder(bool includeOptionalLocomotions = true) |
| | | 24 | | { |
| | 1195 | 25 | | Move = new MoveLocomotion(); |
| | 1195 | 26 | | Fall = new FallLocomotion(); |
| | 1195 | 27 | | Platform = new PlatformLocomotion(); |
| | | 28 | | |
| | 1195 | 29 | | if (includeOptionalLocomotions) |
| | | 30 | | { |
| | 1071 | 31 | | Jump = new JumpLocomotion(); |
| | 1071 | 32 | | Slide = new SlideLocomotion(); |
| | 1071 | 33 | | Water = new WaterLocomotion(); |
| | 1071 | 34 | | Fly = new FlyLocomotion(); |
| | 1071 | 35 | | Climb = new ClimbLocomotion(); |
| | | 36 | | } |
| | 1195 | 37 | | } |
| | | 38 | | |
| | | 39 | | #endregion |
| | | 40 | | |
| | | 41 | | #region Properties |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// The move locomotion to install. |
| | | 45 | | /// </summary> |
| | | 46 | | public MoveLocomotion Move { get; private set; } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// The fall locomotion to install. |
| | | 50 | | /// </summary> |
| | | 51 | | public FallLocomotion Fall { get; private set; } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// The required platform locomotion to install. |
| | | 55 | | /// </summary> |
| | | 56 | | public PlatformLocomotion Platform { get; private set; } |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// The optional jump locomotion to install. |
| | | 60 | | /// </summary> |
| | | 61 | | public JumpLocomotion? Jump { get; private set; } |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// The optional slide locomotion to install. |
| | | 65 | | /// </summary> |
| | | 66 | | public SlideLocomotion? Slide { get; private set; } |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// The optional water locomotion to install. |
| | | 70 | | /// </summary> |
| | | 71 | | public WaterLocomotion? Water { get; private set; } |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// The optional fly locomotion to install. |
| | | 75 | | /// </summary> |
| | | 76 | | public FlyLocomotion? Fly { get; private set; } |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// The optional climb locomotion to install. |
| | | 80 | | /// </summary> |
| | | 81 | | public ClimbLocomotion? Climb { get; private set; } |
| | | 82 | | |
| | | 83 | | #endregion |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// Replaces the core move locomotion. |
| | | 87 | | /// </summary> |
| | | 88 | | public LocomotionProfileBuilder WithMove(MoveLocomotion move) |
| | | 89 | | { |
| | 4 | 90 | | Move = move ?? throw new ArgumentNullException(nameof(move)); |
| | 3 | 91 | | return this; |
| | | 92 | | } |
| | | 93 | | |
| | | 94 | | /// <summary> |
| | | 95 | | /// Replaces the core fall locomotion. |
| | | 96 | | /// </summary> |
| | | 97 | | public LocomotionProfileBuilder WithFall(FallLocomotion fall) |
| | | 98 | | { |
| | 4 | 99 | | Fall = fall ?? throw new ArgumentNullException(nameof(fall)); |
| | 3 | 100 | | return this; |
| | | 101 | | } |
| | | 102 | | |
| | | 103 | | /// <summary> |
| | | 104 | | /// Installs or replaces platform locomotion. |
| | | 105 | | /// </summary> |
| | | 106 | | public LocomotionProfileBuilder WithPlatform(PlatformLocomotion? platform = null) |
| | | 107 | | { |
| | 2 | 108 | | Platform = platform ?? new PlatformLocomotion(); |
| | 2 | 109 | | return this; |
| | | 110 | | } |
| | | 111 | | |
| | | 112 | | /// <summary> |
| | | 113 | | /// Installs or replaces jump locomotion. |
| | | 114 | | /// </summary> |
| | | 115 | | public LocomotionProfileBuilder WithJump(JumpLocomotion? jump = null) |
| | | 116 | | { |
| | 78 | 117 | | Jump = jump ?? new JumpLocomotion(); |
| | 78 | 118 | | return this; |
| | | 119 | | } |
| | | 120 | | |
| | | 121 | | /// <summary> |
| | | 122 | | /// Removes jump locomotion. |
| | | 123 | | /// </summary> |
| | | 124 | | public LocomotionProfileBuilder WithoutJump() |
| | | 125 | | { |
| | 1 | 126 | | Jump = null; |
| | 1 | 127 | | return this; |
| | | 128 | | } |
| | | 129 | | |
| | | 130 | | /// <summary> |
| | | 131 | | /// Installs or replaces slide locomotion. |
| | | 132 | | /// </summary> |
| | | 133 | | public LocomotionProfileBuilder WithSlide(SlideLocomotion? slide = null) |
| | | 134 | | { |
| | 76 | 135 | | Slide = slide ?? new SlideLocomotion(); |
| | 76 | 136 | | return this; |
| | | 137 | | } |
| | | 138 | | |
| | | 139 | | /// <summary> |
| | | 140 | | /// Removes slide locomotion. |
| | | 141 | | /// </summary> |
| | | 142 | | public LocomotionProfileBuilder WithoutSlide() |
| | | 143 | | { |
| | 1 | 144 | | Slide = null; |
| | 1 | 145 | | return this; |
| | | 146 | | } |
| | | 147 | | |
| | | 148 | | /// <summary> |
| | | 149 | | /// Installs or replaces water locomotion. |
| | | 150 | | /// </summary> |
| | | 151 | | public LocomotionProfileBuilder WithWater(WaterLocomotion? water = null) |
| | | 152 | | { |
| | 76 | 153 | | Water = water ?? new WaterLocomotion(); |
| | 76 | 154 | | return this; |
| | | 155 | | } |
| | | 156 | | |
| | | 157 | | /// <summary> |
| | | 158 | | /// Removes water locomotion. |
| | | 159 | | /// </summary> |
| | | 160 | | public LocomotionProfileBuilder WithoutWater() |
| | | 161 | | { |
| | 1 | 162 | | Water = null; |
| | 1 | 163 | | return this; |
| | | 164 | | } |
| | | 165 | | |
| | | 166 | | /// <summary> |
| | | 167 | | /// Installs or replaces fly locomotion. |
| | | 168 | | /// </summary> |
| | | 169 | | public LocomotionProfileBuilder WithFly(FlyLocomotion? fly = null) |
| | | 170 | | { |
| | 79 | 171 | | Fly = fly ?? new FlyLocomotion(); |
| | 79 | 172 | | return this; |
| | | 173 | | } |
| | | 174 | | |
| | | 175 | | /// <summary> |
| | | 176 | | /// Removes fly locomotion. |
| | | 177 | | /// </summary> |
| | | 178 | | public LocomotionProfileBuilder WithoutFly() |
| | | 179 | | { |
| | 1 | 180 | | Fly = null; |
| | 1 | 181 | | return this; |
| | | 182 | | } |
| | | 183 | | |
| | | 184 | | /// <summary> |
| | | 185 | | /// Installs or replaces climb locomotion. |
| | | 186 | | /// </summary> |
| | | 187 | | public LocomotionProfileBuilder WithClimb(ClimbLocomotion? climb = null) |
| | | 188 | | { |
| | 78 | 189 | | Climb = climb ?? new ClimbLocomotion(); |
| | 78 | 190 | | return this; |
| | | 191 | | } |
| | | 192 | | |
| | | 193 | | /// <summary> |
| | | 194 | | /// Removes climb locomotion. |
| | | 195 | | /// </summary> |
| | | 196 | | public LocomotionProfileBuilder WithoutClimb() |
| | | 197 | | { |
| | 1 | 198 | | Climb = null; |
| | 1 | 199 | | return this; |
| | | 200 | | } |
| | | 201 | | |
| | | 202 | | /// <summary> |
| | | 203 | | /// Builds the composed locomotion profile. |
| | | 204 | | /// </summary> |
| | | 205 | | public LocomotionProfile Build() |
| | | 206 | | { |
| | 1194 | 207 | | return new LocomotionProfile( |
| | 1194 | 208 | | Move, |
| | 1194 | 209 | | Fall, |
| | 1194 | 210 | | Platform, |
| | 1194 | 211 | | Jump, |
| | 1194 | 212 | | Slide, |
| | 1194 | 213 | | Water, |
| | 1194 | 214 | | Fly, |
| | 1194 | 215 | | Climb); |
| | | 216 | | } |
| | | 217 | | |
| | | 218 | | internal static LocomotionProfileBuilder FromHandler(LocomotionHandler handler) |
| | | 219 | | { |
| | 2 | 220 | | SwiftThrowHelper.ThrowIfNull(handler, nameof(handler)); |
| | | 221 | | |
| | 2 | 222 | | return new LocomotionProfileBuilder(includeOptionalLocomotions: false) |
| | 2 | 223 | | .WithMove(handler.Move) |
| | 2 | 224 | | .WithFall(handler.Fall) |
| | 2 | 225 | | .SetPlatform(handler.Platform) |
| | 2 | 226 | | .SetJump(handler.Jump) |
| | 2 | 227 | | .SetSlide(handler.Slide) |
| | 2 | 228 | | .SetWater(handler.Water) |
| | 2 | 229 | | .SetFly(handler.Fly) |
| | 2 | 230 | | .SetClimb(handler.Climb); |
| | | 231 | | } |
| | | 232 | | |
| | | 233 | | private LocomotionProfileBuilder SetPlatform(PlatformLocomotion platform) |
| | | 234 | | { |
| | 2 | 235 | | Platform = platform; |
| | 2 | 236 | | return this; |
| | | 237 | | } |
| | | 238 | | |
| | | 239 | | private LocomotionProfileBuilder SetJump(JumpLocomotion? jump) |
| | | 240 | | { |
| | 2 | 241 | | Jump = jump; |
| | 2 | 242 | | return this; |
| | | 243 | | } |
| | | 244 | | |
| | | 245 | | private LocomotionProfileBuilder SetSlide(SlideLocomotion? slide) |
| | | 246 | | { |
| | 2 | 247 | | Slide = slide; |
| | 2 | 248 | | return this; |
| | | 249 | | } |
| | | 250 | | |
| | | 251 | | private LocomotionProfileBuilder SetWater(WaterLocomotion? water) |
| | | 252 | | { |
| | 2 | 253 | | Water = water; |
| | 2 | 254 | | return this; |
| | | 255 | | } |
| | | 256 | | |
| | | 257 | | private LocomotionProfileBuilder SetFly(FlyLocomotion? fly) |
| | | 258 | | { |
| | 2 | 259 | | Fly = fly; |
| | 2 | 260 | | return this; |
| | | 261 | | } |
| | | 262 | | |
| | | 263 | | private LocomotionProfileBuilder SetClimb(ClimbLocomotion? climb) |
| | | 264 | | { |
| | 2 | 265 | | Climb = climb; |
| | 2 | 266 | | return this; |
| | | 267 | | } |
| | | 268 | | } |