| | | 1 | | using SwiftCollections; |
| | | 2 | | using System; |
| | | 3 | | using Trailblazer.Navigation.MovementGroups; |
| | | 4 | | |
| | | 5 | | namespace Trailblazer.Navigation; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Context-owned navigation coordination state for navigators, steering, movement groups, and ids. |
| | | 9 | | /// </summary> |
| | | 10 | | public sealed class TrailblazerNavigationService |
| | | 11 | | { |
| | | 12 | | private readonly TrailblazerWorldContext _context; |
| | | 13 | | |
| | 961 | 14 | | internal TrailblazerNavigationService(TrailblazerWorldContext context) |
| | | 15 | | { |
| | 961 | 16 | | _context = context; |
| | 961 | 17 | | MovementGroups = new MovementGroupCoordinatorState(context); |
| | 961 | 18 | | NavigatorIds = new NavigatorGlobalIdAllocatorState(); |
| | 961 | 19 | | } |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Gets the world context that owns this navigation service. |
| | | 23 | | /// </summary> |
| | 1 | 24 | | public TrailblazerWorldContext Context => _context; |
| | | 25 | | |
| | | 26 | | internal MovementGroupCoordinatorState MovementGroups { get; } |
| | | 27 | | |
| | | 28 | | internal NavigatorGlobalIdAllocatorState NavigatorIds { get; } |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Binds an uninitialized navigator to this context. |
| | | 32 | | /// </summary> |
| | | 33 | | public void Bind(Navigator navigator) |
| | | 34 | | { |
| | 2 | 35 | | SwiftThrowHelper.ThrowIfNull(navigator, nameof(navigator)); |
| | 1 | 36 | | navigator.BindContext(_context); |
| | 1 | 37 | | } |
| | | 38 | | |
| | | 39 | | internal Guid CreateNavigatorId() |
| | | 40 | | { |
| | 156 | 41 | | EnsureUsable(); |
| | 154 | 42 | | return NavigatorIds.Create(); |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | internal void Reset() |
| | | 46 | | { |
| | 25 | 47 | | MovementGroups.Reset(); |
| | 25 | 48 | | NavigatorIds.Reset(); |
| | 25 | 49 | | } |
| | | 50 | | |
| | | 51 | | private void EnsureUsable() |
| | | 52 | | { |
| | 156 | 53 | | SwiftThrowHelper.ThrowIfDisposed(_context.IsDisposed, nameof(TrailblazerWorldContext)); |
| | 155 | 54 | | if (!_context.World.IsActive) |
| | 1 | 55 | | throw new InvalidOperationException("TrailblazerNavigationService is bound to an inactive GridWorld."); |
| | 154 | 56 | | } |
| | | 57 | | } |