| | | 1 | | using GridForge.Grids; |
| | | 2 | | using System; |
| | | 3 | | |
| | | 4 | | namespace Trailblazer.Pathing; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Subscribes one pathing state owner to one <see cref="GridWorld"/>'s grid lifecycle events. |
| | | 8 | | /// </summary> |
| | | 9 | | internal sealed class PathingWorldGridBridge : IDisposable |
| | | 10 | | { |
| | | 11 | | private readonly PathingWorldState _state; |
| | | 12 | | |
| | | 13 | | private bool _disposed; |
| | | 14 | | |
| | 961 | 15 | | internal PathingWorldGridBridge(PathingWorldState state) |
| | | 16 | | { |
| | 961 | 17 | | _state = state; |
| | 961 | 18 | | GridWorld world = state.World; |
| | 961 | 19 | | world.OnActiveGridAdded += HandleGridAdded; |
| | 961 | 20 | | world.OnActiveGridRemoved += HandleGridRemoved; |
| | 961 | 21 | | world.OnActiveGridChange += HandleGridChanged; |
| | 961 | 22 | | world.OnReset += HandleGridReset; |
| | 961 | 23 | | } |
| | | 24 | | |
| | | 25 | | internal ExternalGridBridgeDiagnosticsSnapshot GetDiagnosticsSnapshot() |
| | | 26 | | { |
| | 2 | 27 | | using (PathManager.EnterState(_state)) |
| | 2 | 28 | | return PathManagerExternalGridBridge.GetDiagnosticsSnapshot(); |
| | 2 | 29 | | } |
| | | 30 | | |
| | | 31 | | internal void ResetDiagnostics() |
| | | 32 | | { |
| | 0 | 33 | | using (PathManager.EnterState(_state)) |
| | 0 | 34 | | PathManagerExternalGridBridge.ResetDiagnostics(); |
| | 0 | 35 | | } |
| | | 36 | | |
| | | 37 | | internal void FlushPendingGridChanges() |
| | | 38 | | { |
| | 2301 | 39 | | using (PathManager.EnterState(_state)) |
| | 2301 | 40 | | PathManagerExternalGridBridge.FlushPendingGridChanges(); |
| | 2301 | 41 | | } |
| | | 42 | | |
| | | 43 | | private void HandleGridAdded(GridEventInfo eventInfo) |
| | | 44 | | { |
| | 721 | 45 | | using (PathManager.EnterState(_state)) |
| | 721 | 46 | | PathManagerExternalGridBridge.HandleGridAdded(eventInfo); |
| | 721 | 47 | | } |
| | | 48 | | |
| | | 49 | | private void HandleGridRemoved(GridEventInfo eventInfo) |
| | | 50 | | { |
| | 6 | 51 | | using (PathManager.EnterState(_state)) |
| | 6 | 52 | | PathManagerExternalGridBridge.HandleGridRemoved(eventInfo); |
| | 6 | 53 | | } |
| | | 54 | | |
| | | 55 | | private void HandleGridChanged(GridEventInfo eventInfo) |
| | | 56 | | { |
| | 1273 | 57 | | using (PathManager.EnterState(_state)) |
| | 1273 | 58 | | PathManagerExternalGridBridge.HandleGridChanged(eventInfo); |
| | 1273 | 59 | | } |
| | | 60 | | |
| | | 61 | | private void HandleGridReset() |
| | | 62 | | { |
| | 3 | 63 | | PathManager.ResetPathingState(_state, resetScopedRegistries: true, flushGuideCache: true); |
| | 3 | 64 | | } |
| | | 65 | | |
| | | 66 | | public void Dispose() |
| | | 67 | | { |
| | 1922 | 68 | | if (_disposed) |
| | 961 | 69 | | return; |
| | | 70 | | |
| | 961 | 71 | | _disposed = true; |
| | 961 | 72 | | GridWorld world = _state.World; |
| | 961 | 73 | | if (!world.IsActive) |
| | 2 | 74 | | return; |
| | | 75 | | |
| | 959 | 76 | | world.OnActiveGridAdded -= HandleGridAdded; |
| | 959 | 77 | | world.OnActiveGridRemoved -= HandleGridRemoved; |
| | 959 | 78 | | world.OnActiveGridChange -= HandleGridChanged; |
| | 959 | 79 | | world.OnReset -= HandleGridReset; |
| | 959 | 80 | | } |
| | | 81 | | } |