using System;
using JetBrains.Annotations;
using Robust.Shared.Maths;
namespace Robust.Shared.Map
{
///
/// All of the information needed to reference a tile in the game.
///
[PublicAPI]
public readonly struct TileRef : IEquatable
{
public static TileRef Zero => new(MapId.Nullspace, GridId.Invalid, Vector2i.Zero, Tile.Empty);
///
/// Identifier of the this Tile belongs to.
///
public readonly MapId MapIndex;
///
/// Identifier of the this Tile belongs to.
///
public readonly GridId GridIndex;
///
/// 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 map this tile belongs to.
/// Identifier of the grid 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(MapId mapId, GridId gridId, int xIndex, int yIndex, Tile tile)
: this(mapId, gridId, new Vector2i(xIndex, yIndex), tile) { }
///
/// Constructs a new instance of TileRef.
///
/// Identifier of the map this tile belongs to.
/// Identifier of the grid this tile belongs to.
/// Positional indices of this tile on the grid.
/// Actual data of this tile.
internal TileRef(MapId mapId, GridId gridId, Vector2i gridIndices, Tile tile)
{
MapIndex = mapId;
GridIndex = gridId;
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 bool Equals(TileRef other)
{
return MapIndex.Equals(other.MapIndex)
&& GridIndex.Equals(other.GridIndex)
&& 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 = MapIndex.GetHashCode();
hashCode = (hashCode * 397) ^ GridIndex.GetHashCode();
hashCode = (hashCode * 397) ^ GridIndices.GetHashCode();
hashCode = (hashCode * 397) ^ Tile.GetHashCode();
return hashCode;
}
}
}
}