| | | 1 | | using GridForge.Grids; |
| | | 2 | | using SwiftCollections; |
| | | 3 | | using System; |
| | | 4 | | using System.Runtime.CompilerServices; |
| | | 5 | | |
| | | 6 | | namespace GridForge.Spatial; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Represents the world-scoped runtime identity of a voxel. |
| | | 10 | | /// Used for uniquely identifying voxels across multiple grids and world instances. |
| | | 11 | | /// </summary> |
| | | 12 | | public struct WorldVoxelIndex : IEquatable<WorldVoxelIndex> |
| | | 13 | | { |
| | | 14 | | #region Properties |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// The runtime token of the owning <see cref="GridWorld"/> instance. |
| | | 18 | | /// Used to reject stale or cross-world identities safely. |
| | | 19 | | /// </summary> |
| | | 20 | | public int WorldSpawnToken; |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// The world-local slot index of the grid in <see cref="GridWorld.ActiveGrids"/>. |
| | | 24 | | /// </summary> |
| | | 25 | | public ushort GridIndex; |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// The unique identifier generated by the grid during initialization. |
| | | 29 | | /// Used to ensure the correct instance is at the assigned <see cref="GridIndex"/>. |
| | | 30 | | /// </summary> |
| | | 31 | | public int GridSpawnToken; |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// The local coordinate of a voxel within the grid at <see cref="GridIndex"/>. |
| | | 35 | | /// </summary> |
| | | 36 | | public VoxelIndex VoxelIndex; |
| | | 37 | | |
| | | 38 | | #endregion |
| | | 39 | | |
| | | 40 | | #region Constructors |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Initializes a new instance of <see cref="WorldVoxelIndex"/>, linking a voxel to a specific world and grid instan |
| | | 44 | | /// </summary> |
| | | 45 | | public WorldVoxelIndex( |
| | | 46 | | int worldSpawnToken, |
| | | 47 | | ushort gridIndex, |
| | | 48 | | int gridSpawnToken, |
| | | 49 | | VoxelIndex coord) |
| | | 50 | | { |
| | 78163 | 51 | | WorldSpawnToken = worldSpawnToken; |
| | 78163 | 52 | | GridIndex = gridIndex; |
| | 78163 | 53 | | GridSpawnToken = gridSpawnToken; |
| | 78163 | 54 | | VoxelIndex = coord; |
| | 78163 | 55 | | } |
| | | 56 | | |
| | | 57 | | #endregion |
| | | 58 | | |
| | | 59 | | #region operators |
| | | 60 | | |
| | | 61 | | /// <inheritdoc/> |
| | | 62 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 63 | | public static bool operator ==(WorldVoxelIndex left, WorldVoxelIndex right) |
| | | 64 | | { |
| | 2 | 65 | | return left.Equals(right); |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | /// <inheritdoc/> |
| | | 69 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 70 | | public static bool operator !=(WorldVoxelIndex left, WorldVoxelIndex right) |
| | | 71 | | { |
| | 1 | 72 | | return !(left == right); |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | #endregion |
| | | 76 | | |
| | | 77 | | #region Overrides |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// Returns a string representation of the world-scoped voxel identity. |
| | | 81 | | /// </summary> |
| | | 82 | | public override readonly string ToString() |
| | | 83 | | { |
| | 16 | 84 | | return $"World: {WorldSpawnToken}; Grid: {GridIndex}; Coordinate: {VoxelIndex}"; |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// Computes a hash code for uniquely identifying this world-scoped voxel identity. |
| | | 89 | | /// </summary> |
| | | 90 | | public override readonly int GetHashCode() => |
| | 2635 | 91 | | SwiftHashTools.CombineHashCodes(WorldSpawnToken, GridSpawnToken, GridIndex, VoxelIndex); |
| | | 92 | | |
| | | 93 | | /// <inheritdoc/> |
| | | 94 | | public readonly bool Equals(WorldVoxelIndex other) |
| | | 95 | | { |
| | 1339 | 96 | | return WorldSpawnToken == other.WorldSpawnToken |
| | 1339 | 97 | | && GridIndex == other.GridIndex |
| | 1339 | 98 | | && GridSpawnToken == other.GridSpawnToken |
| | 1339 | 99 | | && VoxelIndex.Equals(other.VoxelIndex); |
| | | 100 | | } |
| | | 101 | | |
| | | 102 | | /// <inheritdoc/> |
| | 2 | 103 | | public override readonly bool Equals(object? obj) => obj is WorldVoxelIndex other && Equals(other); |
| | | 104 | | |
| | | 105 | | #endregion |
| | | 106 | | } |