| | | 1 | | //======================================================================= |
| | | 2 | | // Blocker.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 FixedMathSharp; |
| | | 9 | | using GridForge.Grids; |
| | | 10 | | using GridForge.Spatial; |
| | | 11 | | using GridForge.Utility; |
| | | 12 | | using SwiftCollections; |
| | | 13 | | using SwiftCollections.Pool; |
| | | 14 | | using System; |
| | | 15 | | |
| | | 16 | | namespace GridForge.Blockers; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Base class for all grid blockers that handles applying and removing obstacles. |
| | | 20 | | /// </summary> |
| | | 21 | | public abstract class Blocker : IBlocker |
| | | 22 | | { |
| | 148 | 23 | | private readonly object _gridWatcherLock = new(); |
| | | 24 | | private bool _isWatchingWorldEvents; |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// The world this blocker is bound to. |
| | | 28 | | /// </summary> |
| | | 29 | | public GridWorld World { get; } |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Unique token representing this blockage instance. |
| | | 33 | | /// </summary> |
| | | 34 | | public BoundsKey BlockageToken { get; protected set; } = default; |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Indicates whether the blocker is currently active. |
| | | 38 | | /// </summary> |
| | | 39 | | public bool IsActive { get; protected set; } |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// The cached minimum bounds of the blockage area. |
| | | 43 | | /// </summary> |
| | | 44 | | public Vector3d CacheMin { get; protected set; } |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// The cached maximum bounds of the blockage area. |
| | | 48 | | /// </summary> |
| | | 49 | | public Vector3d CacheMax { get; private set; } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Tracks whether the blocker is currently blocking voxels. |
| | | 53 | | /// </summary> |
| | | 54 | | public bool IsBlocking { get; protected set; } |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Flags whether or not to hold onto a reference of the voxels this blocker covers. |
| | | 58 | | /// </summary> |
| | | 59 | | public bool CacheCoveredVoxels { get; protected set; } |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Stable voxel identifiers cached for safe blocker removal when <see cref="CacheCoveredVoxels"/> is true. |
| | | 63 | | /// </summary> |
| | | 64 | | protected SwiftList<WorldVoxelIndex>? _cachedCoveredVoxels; |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Grid indices currently covered by this blocker. |
| | | 68 | | /// </summary> |
| | 148 | 69 | | private readonly SwiftHashSet<ushort> _watchedGridIndices = new(); |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Event triggered when a blocker is applied. |
| | | 73 | | /// </summary> |
| | | 74 | | private static Action<BlockageEventInfo>? _onBlockageApplied; |
| | | 75 | | |
| | | 76 | | /// <inheritdoc cref="_onBlockageApplied"/> |
| | | 77 | | public static event Action<BlockageEventInfo> OnBlockageApplied |
| | | 78 | | { |
| | 3 | 79 | | add => _onBlockageApplied += value; |
| | 3 | 80 | | remove => _onBlockageApplied -= value; |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// Event triggered when a blocker is removed. |
| | | 85 | | /// </summary> |
| | | 86 | | private static Action<BlockageEventInfo>? _onBlockageRemoved; |
| | | 87 | | |
| | | 88 | | /// <inheritdoc cref="_onBlockageRemoved"/> |
| | | 89 | | public static event Action<BlockageEventInfo> OnBlockageRemoved |
| | | 90 | | { |
| | 3 | 91 | | add => _onBlockageRemoved += value; |
| | 3 | 92 | | remove => _onBlockageRemoved -= value; |
| | | 93 | | } |
| | | 94 | | |
| | | 95 | | /// <summary> |
| | | 96 | | /// Initializes a new blocker instance bound to the supplied world. |
| | | 97 | | /// </summary> |
| | | 98 | | /// <param name="world">The world whose grids this blocker should affect.</param> |
| | | 99 | | /// <param name="active">Flag whether or not this blocker will block on update.</param> |
| | | 100 | | /// <param name="cacheCoveredVoxels">Flag whether or not to cache covered voxels that are blocked.</param> |
| | 148 | 101 | | protected Blocker(GridWorld world, bool active = true, bool cacheCoveredVoxels = false) |
| | | 102 | | { |
| | 148 | 103 | | SwiftThrowHelper.ThrowIfNull(world, nameof(world)); |
| | 148 | 104 | | World = world; |
| | 148 | 105 | | IsActive = active; |
| | 148 | 106 | | CacheCoveredVoxels = cacheCoveredVoxels; |
| | 148 | 107 | | } |
| | | 108 | | |
| | | 109 | | /// <summary> |
| | | 110 | | /// Toggles the blocker from inactive to active or active to inactive state |
| | | 111 | | /// If object is currently blocking, the blocker will be removed. |
| | | 112 | | /// If object is not active and not blocking, the blocker will be applied. |
| | | 113 | | /// </summary> |
| | | 114 | | public virtual void ToggleStatus(bool status) |
| | | 115 | | { |
| | 3 | 116 | | if (!status) |
| | | 117 | | { |
| | 1 | 118 | | RemoveBlockageCore(keepWatching: false); |
| | 1 | 119 | | IsActive = false; |
| | 1 | 120 | | return; |
| | | 121 | | } |
| | | 122 | | |
| | 2 | 123 | | if (!IsBlocking) |
| | | 124 | | { |
| | 1 | 125 | | IsActive = true; |
| | 1 | 126 | | ApplyBlockage(); |
| | | 127 | | } |
| | 2 | 128 | | } |
| | | 129 | | |
| | | 130 | | /// <summary> |
| | | 131 | | /// Applies the blockage by marking voxels as obstacles. |
| | | 132 | | /// </summary> |
| | | 133 | | public virtual void ApplyBlockage() |
| | | 134 | | { |
| | 156 | 135 | | if (!IsActive || IsBlocking || !World.IsActive) |
| | 2 | 136 | | return; |
| | | 137 | | |
| | 154 | 138 | | PrepareBlockageApplication(); |
| | 154 | 139 | | bool foundCoverage = ApplyBlockageToCoveredVoxels(out bool hasCoverage); |
| | | 140 | | |
| | 154 | 141 | | IsBlocking = foundCoverage && hasCoverage; |
| | | 142 | | |
| | 154 | 143 | | if (IsBlocking) |
| | | 144 | | { |
| | 147 | 145 | | NotifyBlockageApplied(); |
| | 147 | 146 | | return; |
| | | 147 | | } |
| | | 148 | | |
| | 7 | 149 | | BlockageToken = default; |
| | 7 | 150 | | } |
| | | 151 | | |
| | | 152 | | /// <summary> |
| | | 153 | | /// Removes the blockage by clearing obstacle markers from voxels. |
| | | 154 | | /// </summary> |
| | | 155 | | public virtual void RemoveBlockage() |
| | | 156 | | { |
| | 20 | 157 | | RemoveBlockageCore(keepWatching: false); |
| | 20 | 158 | | } |
| | | 159 | | |
| | | 160 | | private void RemoveBlockageCore(bool keepWatching) |
| | | 161 | | { |
| | 30 | 162 | | if (!IsBlocking) |
| | | 163 | | { |
| | 6 | 164 | | ClearCoverageTracking(); |
| | 6 | 165 | | UnregisterGridWatcherIfNeeded(keepWatching); |
| | 6 | 166 | | return; |
| | | 167 | | } |
| | | 168 | | |
| | 24 | 169 | | BlockageEventInfo removalEventInfo = CreateBlockageEventInfo(); |
| | | 170 | | |
| | 24 | 171 | | RemoveAppliedBlockage(); |
| | | 172 | | |
| | 24 | 173 | | BlockageToken = default; |
| | 24 | 174 | | IsBlocking = false; |
| | 24 | 175 | | ClearCoverageTracking(); |
| | 24 | 176 | | UnregisterGridWatcherIfNeeded(keepWatching); |
| | | 177 | | |
| | 24 | 178 | | NotifyBlockageRemoved(removalEventInfo); |
| | 24 | 179 | | } |
| | | 180 | | |
| | | 181 | | private void PrepareBlockageApplication() |
| | | 182 | | { |
| | 154 | 183 | | CacheMin = GetBoundsMin(); |
| | 154 | 184 | | CacheMax = GetBoundsMax(); |
| | 154 | 185 | | BlockageToken = new BoundsKey(CacheMin, CacheMax); |
| | | 186 | | |
| | 154 | 187 | | if (CacheCoveredVoxels) |
| | 22 | 188 | | _cachedCoveredVoxels ??= new SwiftList<WorldVoxelIndex>(); |
| | | 189 | | |
| | 154 | 190 | | RegisterGridWatcher(); |
| | 154 | 191 | | ClearCoverageTracking(); |
| | 154 | 192 | | } |
| | | 193 | | |
| | | 194 | | private bool ApplyBlockageToCoveredVoxels(out bool hasCoverage) |
| | | 195 | | { |
| | 154 | 196 | | hasCoverage = true; |
| | 154 | 197 | | bool foundCoverage = false; |
| | 154 | 198 | | SwiftList<WorldVoxelIndex>? appliedVoxels = CacheCoveredVoxels |
| | 154 | 199 | | ? _cachedCoveredVoxels |
| | 154 | 200 | | : SwiftListPool<WorldVoxelIndex>.Shared.Rent(); |
| | | 201 | | |
| | | 202 | | try |
| | | 203 | | { |
| | 614 | 204 | | foreach (GridVoxelSet covered in GridTracer.GetCoveredVoxels(World, CacheMin, CacheMax)) |
| | | 205 | | { |
| | 153 | 206 | | foundCoverage = true; |
| | 153 | 207 | | _watchedGridIndices.Add(covered.Grid.GridIndex); |
| | 153 | 208 | | ApplyBlockageToVoxels(covered, appliedVoxels!, ref hasCoverage); |
| | | 209 | | } |
| | | 210 | | |
| | 154 | 211 | | if (!hasCoverage) |
| | 2 | 212 | | RollbackAppliedBlockage(appliedVoxels!); |
| | | 213 | | |
| | 154 | 214 | | return foundCoverage; |
| | | 215 | | } |
| | | 216 | | finally |
| | | 217 | | { |
| | 154 | 218 | | if (!CacheCoveredVoxels) |
| | 132 | 219 | | SwiftListPool<WorldVoxelIndex>.Shared.Release(appliedVoxels!); |
| | 154 | 220 | | } |
| | 154 | 221 | | } |
| | | 222 | | |
| | | 223 | | private void ApplyBlockageToVoxels( |
| | | 224 | | GridVoxelSet covered, |
| | | 225 | | SwiftList<WorldVoxelIndex> appliedVoxels, |
| | | 226 | | ref bool hasCoverage) |
| | | 227 | | { |
| | 1750 | 228 | | foreach (Voxel voxel in covered.Voxels) |
| | | 229 | | { |
| | 722 | 230 | | if (!covered.Grid.TryAddObstacle(voxel, BlockageToken)) |
| | | 231 | | { |
| | 2 | 232 | | hasCoverage = false; |
| | 2 | 233 | | continue; |
| | | 234 | | } |
| | | 235 | | |
| | 720 | 236 | | appliedVoxels.Add(voxel.WorldIndex); |
| | | 237 | | } |
| | 153 | 238 | | } |
| | | 239 | | |
| | | 240 | | private void RollbackAppliedBlockage(SwiftList<WorldVoxelIndex> appliedVoxels) |
| | | 241 | | { |
| | 6 | 242 | | foreach (WorldVoxelIndex voxelIndex in appliedVoxels) |
| | 1 | 243 | | GridObstacleManager.TryRemoveObstacle(World, voxelIndex, BlockageToken); |
| | | 244 | | |
| | 2 | 245 | | appliedVoxels.Clear(); |
| | 2 | 246 | | } |
| | | 247 | | |
| | | 248 | | private void RemoveAppliedBlockage() |
| | | 249 | | { |
| | 24 | 250 | | if (CacheCoveredVoxels && _cachedCoveredVoxels?.Count > 0) |
| | | 251 | | { |
| | 11 | 252 | | RemoveCachedBlockage(); |
| | 11 | 253 | | return; |
| | | 254 | | } |
| | | 255 | | |
| | 13 | 256 | | RemoveTracedBlockage(); |
| | 13 | 257 | | } |
| | | 258 | | |
| | | 259 | | private void RemoveCachedBlockage() |
| | | 260 | | { |
| | 86 | 261 | | foreach (WorldVoxelIndex voxelIndex in _cachedCoveredVoxels!) |
| | 32 | 262 | | GridObstacleManager.TryRemoveObstacle(World, voxelIndex, BlockageToken); |
| | 11 | 263 | | } |
| | | 264 | | |
| | | 265 | | private void RemoveTracedBlockage() |
| | | 266 | | { |
| | 54 | 267 | | foreach (GridVoxelSet covered in GridTracer.GetCoveredVoxels(World, CacheMin, CacheMax)) |
| | | 268 | | { |
| | 86 | 269 | | foreach (Voxel voxel in covered.Voxels) |
| | 29 | 270 | | covered.Grid.TryRemoveObstacle(voxel, BlockageToken); |
| | | 271 | | } |
| | 13 | 272 | | } |
| | | 273 | | |
| | | 274 | | private void ClearCoverageTracking() |
| | | 275 | | { |
| | 184 | 276 | | _cachedCoveredVoxels?.Clear(); |
| | 184 | 277 | | _watchedGridIndices.Clear(); |
| | 184 | 278 | | } |
| | | 279 | | |
| | | 280 | | private void UnregisterGridWatcherIfNeeded(bool keepWatching) |
| | | 281 | | { |
| | 30 | 282 | | if (!keepWatching || !IsActive) |
| | 22 | 283 | | UnregisterGridWatcher(); |
| | 30 | 284 | | } |
| | | 285 | | |
| | | 286 | | /// <summary> |
| | | 287 | | /// Creates a snapshot describing the current blocker coverage. |
| | | 288 | | /// </summary> |
| | | 289 | | protected BlockageEventInfo CreateBlockageEventInfo() |
| | | 290 | | { |
| | 27 | 291 | | return new BlockageEventInfo(World.SpawnToken, BlockageToken, CacheMin, CacheMax); |
| | | 292 | | } |
| | | 293 | | |
| | | 294 | | /// <summary> |
| | | 295 | | /// Notifies subscribers that blockage has been applied. |
| | | 296 | | /// </summary> |
| | | 297 | | protected virtual void NotifyBlockageApplied() |
| | | 298 | | { |
| | 147 | 299 | | Action<BlockageEventInfo>? handlers = _onBlockageApplied; |
| | 147 | 300 | | if (handlers == null) |
| | 144 | 301 | | return; |
| | | 302 | | |
| | 3 | 303 | | BlockageEventInfo eventInfo = CreateBlockageEventInfo(); |
| | | 304 | | |
| | 3 | 305 | | var handlerDelegates = handlers.GetInvocationList(); |
| | 14 | 306 | | for (int i = 0; i < handlerDelegates.Length; i++) |
| | | 307 | | { |
| | | 308 | | try |
| | | 309 | | { |
| | 4 | 310 | | ((Action<BlockageEventInfo>)handlerDelegates[i])(eventInfo); |
| | 3 | 311 | | } |
| | 1 | 312 | | catch (Exception ex) |
| | | 313 | | { |
| | 1 | 314 | | GridForgeLogger.Channel.Error( |
| | 1 | 315 | | $"Blockage apply notification: {ex.Message} | Bounds: {eventInfo.BoundsMin} -> {eventInfo.BoundsMax} |
| | 1 | 316 | | } |
| | | 317 | | } |
| | 3 | 318 | | } |
| | | 319 | | |
| | | 320 | | /// <summary> |
| | | 321 | | /// Notifies subscribers that blockage has been removed. |
| | | 322 | | /// </summary> |
| | | 323 | | protected virtual void NotifyBlockageRemoved(BlockageEventInfo eventInfo) |
| | | 324 | | { |
| | 24 | 325 | | Action<BlockageEventInfo>? handlers = _onBlockageRemoved; |
| | 24 | 326 | | if (handlers == null) |
| | 21 | 327 | | return; |
| | | 328 | | |
| | 3 | 329 | | var handlerDelegates = handlers.GetInvocationList(); |
| | 14 | 330 | | for (int i = 0; i < handlerDelegates.Length; i++) |
| | | 331 | | { |
| | | 332 | | try |
| | | 333 | | { |
| | 4 | 334 | | ((Action<BlockageEventInfo>)handlerDelegates[i])(eventInfo); |
| | 3 | 335 | | } |
| | 1 | 336 | | catch (Exception ex) |
| | | 337 | | { |
| | 1 | 338 | | GridForgeLogger.Channel.Error( |
| | 1 | 339 | | $"Blockage remove notification: {ex.Message} | Bounds: {eventInfo.BoundsMin} -> {eventInfo.BoundsMax |
| | 1 | 340 | | } |
| | | 341 | | } |
| | 3 | 342 | | } |
| | | 343 | | |
| | | 344 | | /// <summary> |
| | | 345 | | /// Gets the min bounds of the area to block. Must be implemented by subclasses. |
| | | 346 | | /// </summary> |
| | | 347 | | protected abstract Vector3d GetBoundsMin(); |
| | | 348 | | |
| | | 349 | | /// <summary> |
| | | 350 | | /// Gets the max bounds of the area to block. Must be implemented by subclasses. |
| | | 351 | | /// </summary> |
| | | 352 | | protected abstract Vector3d GetBoundsMax(); |
| | | 353 | | |
| | | 354 | | /// <summary> |
| | | 355 | | /// Sets whether or not to cache covered voxels for this blocker. |
| | | 356 | | /// If enabled, the blocker will store references to the voxels it covers when applying blockage, |
| | | 357 | | /// which can improve performance when removing blockage at the cost of increased memory usage. |
| | | 358 | | /// </summary> |
| | | 359 | | public virtual void SetCacheCoveredVoxels(bool cache) |
| | | 360 | | { |
| | 5 | 361 | | if (CacheCoveredVoxels == cache) |
| | 2 | 362 | | return; |
| | | 363 | | |
| | 3 | 364 | | CacheCoveredVoxels = cache; |
| | 3 | 365 | | if (cache) |
| | | 366 | | { |
| | 2 | 367 | | _cachedCoveredVoxels = new SwiftList<WorldVoxelIndex>(); |
| | 2 | 368 | | return; |
| | | 369 | | } |
| | | 370 | | |
| | 1 | 371 | | _cachedCoveredVoxels = null; |
| | 1 | 372 | | } |
| | | 373 | | |
| | | 374 | | /// <summary> |
| | | 375 | | /// Resets the blocker to its default state, removing any active blockage and clearing cached data. |
| | | 376 | | /// </summary> |
| | | 377 | | public virtual void Reset() |
| | | 378 | | { |
| | 1 | 379 | | RemoveBlockageCore(keepWatching: false); |
| | | 380 | | |
| | 1 | 381 | | CacheCoveredVoxels = false; |
| | 1 | 382 | | _cachedCoveredVoxels = null; |
| | 1 | 383 | | _watchedGridIndices.Clear(); |
| | | 384 | | |
| | 1 | 385 | | IsActive = false; |
| | 1 | 386 | | CacheMin = Vector3d.Zero; |
| | 1 | 387 | | CacheMax = Vector3d.Zero; |
| | 1 | 388 | | BlockageToken = default; |
| | 1 | 389 | | } |
| | | 390 | | |
| | | 391 | | private void ReapplyBlockage() |
| | | 392 | | { |
| | 9 | 393 | | if (!IsActive) |
| | 1 | 394 | | return; |
| | | 395 | | |
| | 8 | 396 | | RemoveBlockageCore(keepWatching: true); |
| | 8 | 397 | | ApplyBlockage(); |
| | 8 | 398 | | } |
| | | 399 | | |
| | | 400 | | private void RegisterGridWatcher() |
| | | 401 | | { |
| | 155 | 402 | | lock (_gridWatcherLock) |
| | | 403 | | { |
| | 155 | 404 | | if (_isWatchingWorldEvents) |
| | 8 | 405 | | return; |
| | | 406 | | |
| | 147 | 407 | | World.OnActiveGridAdded += HandleActiveGridAdded; |
| | 147 | 408 | | World.OnActiveGridRemoved += HandleActiveGridRemoved; |
| | 147 | 409 | | World.OnActiveGridChange += HandleActiveGridChanged; |
| | 147 | 410 | | World.OnReset += HandleWorldReset; |
| | 147 | 411 | | _isWatchingWorldEvents = true; |
| | 147 | 412 | | } |
| | 155 | 413 | | } |
| | | 414 | | |
| | | 415 | | private void UnregisterGridWatcher() |
| | | 416 | | { |
| | 148 | 417 | | lock (_gridWatcherLock) |
| | | 418 | | { |
| | 148 | 419 | | if (!_isWatchingWorldEvents) |
| | 1 | 420 | | return; |
| | | 421 | | |
| | 147 | 422 | | World.OnActiveGridAdded -= HandleActiveGridAdded; |
| | 147 | 423 | | World.OnActiveGridRemoved -= HandleActiveGridRemoved; |
| | 147 | 424 | | World.OnActiveGridChange -= HandleActiveGridChanged; |
| | 147 | 425 | | World.OnReset -= HandleWorldReset; |
| | 147 | 426 | | _isWatchingWorldEvents = false; |
| | 147 | 427 | | } |
| | 148 | 428 | | } |
| | | 429 | | |
| | | 430 | | private bool ShouldReactToGridAdded(GridEventInfo eventInfo) |
| | | 431 | | { |
| | 6 | 432 | | return IsActive && BoundsOverlap(CacheMin, CacheMax, eventInfo.BoundsMin, eventInfo.BoundsMax); |
| | | 433 | | } |
| | | 434 | | |
| | | 435 | | private static bool BoundsOverlap( |
| | | 436 | | Vector3d firstMin, |
| | | 437 | | Vector3d firstMax, |
| | | 438 | | Vector3d secondMin, |
| | | 439 | | Vector3d secondMax) |
| | | 440 | | { |
| | 7 | 441 | | return AxisOverlaps(firstMin.X, firstMax.X, secondMin.X, secondMax.X) |
| | 7 | 442 | | && AxisOverlaps(firstMin.Y, firstMax.Y, secondMin.Y, secondMax.Y) |
| | 7 | 443 | | && AxisOverlaps(firstMin.Z, firstMax.Z, secondMin.Z, secondMax.Z); |
| | | 444 | | } |
| | | 445 | | |
| | | 446 | | private static bool AxisOverlaps(Fixed64 firstMin, Fixed64 firstMax, Fixed64 secondMin, Fixed64 secondMax) |
| | | 447 | | { |
| | 19 | 448 | | return firstMax >= secondMin && firstMin <= secondMax; |
| | | 449 | | } |
| | | 450 | | |
| | | 451 | | private bool ShouldReactToGridRemoved(GridEventInfo eventInfo) |
| | | 452 | | { |
| | 4 | 453 | | return IsActive && _watchedGridIndices.Contains(eventInfo.GridIndex); |
| | | 454 | | } |
| | | 455 | | |
| | | 456 | | private bool ShouldReactToGridChanged(GridEventInfo eventInfo) |
| | | 457 | | { |
| | 21285 | 458 | | return IsActive |
| | 21285 | 459 | | && IsSparseVoxelMutation(eventInfo.ChangeKind) |
| | 21285 | 460 | | && BoundsOverlap(CacheMin, CacheMax, eventInfo.AffectedBoundsMin, eventInfo.AffectedBoundsMax); |
| | | 461 | | } |
| | | 462 | | |
| | | 463 | | private static bool IsSparseVoxelMutation(GridEventKind changeKind) => |
| | 21285 | 464 | | changeKind == GridEventKind.SparseVoxelAdded |
| | 21285 | 465 | | || changeKind == GridEventKind.SparseVoxelRemoved; |
| | | 466 | | |
| | | 467 | | private void HandleActiveGridAdded(GridEventInfo eventInfo) |
| | | 468 | | { |
| | 6 | 469 | | if (eventInfo.WorldSpawnToken != World.SpawnToken || !ShouldReactToGridAdded(eventInfo)) |
| | 2 | 470 | | return; |
| | | 471 | | |
| | 4 | 472 | | ReapplyBlockage(); |
| | 4 | 473 | | } |
| | | 474 | | |
| | | 475 | | private void HandleActiveGridRemoved(GridEventInfo eventInfo) |
| | | 476 | | { |
| | 4 | 477 | | if (eventInfo.WorldSpawnToken != World.SpawnToken || !ShouldReactToGridRemoved(eventInfo)) |
| | 2 | 478 | | return; |
| | | 479 | | |
| | 2 | 480 | | ReapplyBlockage(); |
| | 2 | 481 | | } |
| | | 482 | | |
| | | 483 | | private void HandleActiveGridChanged(GridEventInfo eventInfo) |
| | | 484 | | { |
| | 21286 | 485 | | if (eventInfo.WorldSpawnToken != World.SpawnToken || !ShouldReactToGridChanged(eventInfo)) |
| | 21284 | 486 | | return; |
| | | 487 | | |
| | 2 | 488 | | ReapplyBlockage(); |
| | 2 | 489 | | } |
| | | 490 | | |
| | | 491 | | private void HandleWorldReset() |
| | | 492 | | { |
| | 126 | 493 | | IsBlocking = false; |
| | 126 | 494 | | BlockageToken = default; |
| | 126 | 495 | | _cachedCoveredVoxels?.Clear(); |
| | 126 | 496 | | _watchedGridIndices.Clear(); |
| | 126 | 497 | | UnregisterGridWatcher(); |
| | 126 | 498 | | } |
| | | 499 | | } |