using System; using JetBrains.Annotations; using Robust.Shared.GameObjects; using Robust.Shared.Maths; using Robust.Shared.Utility; #pragma warning disable CS0618 namespace Robust.Shared.Map { /// /// All of the information needed to reference a tile in the game. /// [PublicAPI] public readonly struct TileRef : IEquatable, ISpanFormattable { public static TileRef Zero => new(EntityUid.Invalid, Vector2i.Zero, Tile.Empty); /// /// Grid Entity this Tile belongs to. /// public readonly EntityUid GridUid; /// /// Positional indices of this tile on the grid. /// public readonly Vector2i GridIndices; /// /// Actual data of this Tile. /// public readonly Tile Tile; /// /// Constructs a new instance of TileRef. /// /// Identifier of the grid this tile belongs to. /// Identifier of the grid entity this tile belongs to. /// Positional X index of this tile on the grid. /// Positional Y index of this tile on the grid. /// Actual data of this tile. internal TileRef(EntityUid gridUid, int xIndex, int yIndex, Tile tile) : this(gridUid, new Vector2i(xIndex, yIndex), tile) { } /// /// Constructs a new instance of TileRef. /// /// Identifier of the grid this tile belongs to. /// Identifier of the grid entity this tile belongs to. /// Positional indices of this tile on the grid. /// Actual data of this tile. internal TileRef(EntityUid gridUid, Vector2i gridIndices, Tile tile) { GridUid = gridUid; GridIndices = gridIndices; Tile = tile; } /// /// Grid index on the X axis. /// public int X => GridIndices.X; /// /// Grid index on the Y axis. /// public int Y => GridIndices.Y; /// public override string ToString() { return $"TileRef: {X},{Y} ({Tile})"; } public string ToString(string? format, IFormatProvider? formatProvider) { return ToString(); } public bool TryFormat( Span destination, out int charsWritten, ReadOnlySpan format, IFormatProvider? provider) { return FormatHelpers.TryFormatInto( destination, out charsWritten, $"TileRef: {X},{Y} ({Tile})"); } /// public bool Equals(TileRef other) { return GridUid.Equals(other.GridUid) && GridIndices.Equals(other.GridIndices) && Tile.Equals(other.Tile); } /// public override bool Equals(object? obj) { if (ReferenceEquals(null, obj)) return false; return obj is TileRef other && Equals(other); } /// /// Check for equality by value between two objects. /// public static bool operator ==(TileRef a, TileRef b) { return a.Equals(b); } /// /// Check for inequality by value between two objects. /// public static bool operator !=(TileRef a, TileRef b) { return !a.Equals(b); } /// public override int GetHashCode() { unchecked { var hashCode = GridUid.GetHashCode(); hashCode = (hashCode * 397) ^ GridIndices.GetHashCode(); hashCode = (hashCode * 397) ^ Tile.GetHashCode(); return hashCode; } } } }