using System; using System.Numerics; using JetBrains.Annotations; using Robust.Shared.GameObjects; using Robust.Shared.Serialization; using Robust.Shared.Utility; namespace Robust.Shared.Map; /// /// A networked version of /// [PublicAPI] [Serializable, NetSerializable] public readonly struct NetCoordinates : IEquatable, ISpanFormattable { public static readonly NetCoordinates Invalid = new(NetEntity.Invalid, Vector2.Zero); /// /// Networked ID of the entity that this position is relative to. /// public readonly NetEntity NetEntity; /// /// Position in the entity's local space. /// public readonly Vector2 Position; /// /// Location of the X axis local to the entity. /// public float X => Position.X; /// /// Location of the Y axis local to the entity. /// public float Y => Position.Y; public NetCoordinates(NetEntity netEntity, Vector2 position) { NetEntity = netEntity; Position = position; } public NetCoordinates(NetEntity netEntity, float x, float y) { NetEntity = netEntity; Position = new Vector2(x, y); } #region IEquatable /// public bool Equals(NetCoordinates other) { return NetEntity.Equals(other.NetEntity) && Position.Equals(other.Position); } /// public override bool Equals(object? obj) { return obj is NetCoordinates other && Equals(other); } /// public override int GetHashCode() { return HashCode.Combine(NetEntity, Position); } #endregion /// /// Deconstructs the object into it's fields. /// /// ID of the entity that this position is relative to. /// Position in the entity's local space. public void Deconstruct(out NetEntity entId, out Vector2 localPos) { entId = NetEntity; localPos = Position; } /// public override string ToString() { return $"NetEntity={NetEntity}, X={Position.X:N2}, Y={Position.Y:N2}"; } public string ToString(string? format, IFormatProvider? formatProvider) => ToString(); public bool TryFormat( Span destination, out int charsWritten, ReadOnlySpan format, IFormatProvider? provider) { return FormatHelpers.TryFormatInto( destination, out charsWritten, $"NetEntity={NetEntity}, X={Position.X:N2}, Y={Position.Y:N2}"); } }