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