2021-12-21 11:39:29 +01:00
using System ;
2019-12-07 13:00:50 +01:00
using System.Runtime.Serialization ;
2020-05-22 20:32:38 +02:00
using MonoGame.Extended.Tiled ;
2019-12-07 13:00:50 +01:00
namespace MLEM.Extended.Tiled {
2020-05-22 20:32:38 +02:00
/// <summary>
2022-06-06 23:50:13 +02:00
/// A struct that represents a position on a <see cref="TiledMap"/> with multiple layers, where the <see cref="X"/> and <see cref="Y"/> coordinates are 32-bit integer numbers.
/// See <see cref="LayerPositionF"/> for a floating point position.
2020-05-22 20:32:38 +02:00
/// </summary>
2019-12-07 13:00:50 +01:00
[DataContract]
2021-12-21 11:39:29 +01:00
public struct LayerPosition : IEquatable < LayerPosition > {
2019-12-07 13:00:50 +01:00
2020-05-22 20:32:38 +02:00
/// <summary>
/// The name of the layer that this position is on
/// </summary>
2019-12-07 13:00:50 +01:00
[DataMember]
public string Layer ;
2020-05-22 20:32:38 +02:00
/// <summary>
/// The x coordinate of this position
/// </summary>
2019-12-07 13:00:50 +01:00
[DataMember]
public int X ;
2020-05-22 20:32:38 +02:00
/// <summary>
/// The y coordinate of this position
/// </summary>
2019-12-07 13:00:50 +01:00
[DataMember]
public int Y ;
2020-05-22 20:32:38 +02:00
/// <summary>
/// Creates a new layer position with the given settings
/// </summary>
/// <param name="layerName">The layer name</param>
/// <param name="x">The x coordinate</param>
/// <param name="y">The y coordinate</param>
2019-12-07 13:00:50 +01:00
public LayerPosition ( string layerName , int x , int y ) {
this . Layer = layerName ;
this . X = x ;
this . Y = y ;
}
2020-05-22 20:32:38 +02:00
/// <inheritdoc cref="Equals(object)"/>
2019-12-07 13:00:50 +01:00
public bool Equals ( LayerPosition other ) {
return this . Layer = = other . Layer & & this . X = = other . X & & this . Y = = other . Y ;
}
2021-11-22 19:25:18 +01:00
/// <summary>Indicates whether this instance and a specified object are equal.</summary>
/// <param name="obj">The object to compare with the current instance.</param>
/// <returns>true if <paramref name="obj">obj</paramref> and this instance are the same type and represent the same value; otherwise, false.</returns>
2019-12-07 13:00:50 +01:00
public override bool Equals ( object obj ) {
return obj is LayerPosition other & & this . Equals ( other ) ;
}
2021-11-22 19:25:18 +01:00
/// <summary>Returns the hash code for this instance.</summary>
/// <returns>A 32-bit signed integer that is the hash code for this instance.</returns>
2019-12-07 13:00:50 +01:00
public override int GetHashCode ( ) {
var hashCode = this . Layer . GetHashCode ( ) ;
2022-06-15 11:38:11 +02:00
hashCode = hashCode * 397 ^ this . X ;
hashCode = hashCode * 397 ^ this . Y ;
2019-12-07 13:00:50 +01:00
return hashCode ;
}
2021-11-30 11:46:06 +01:00
/// <summary>Returns the fully qualified type name of this instance.</summary>
/// <returns>The fully qualified type name.</returns>
2019-12-07 13:00:50 +01:00
public override string ToString ( ) {
return $"{nameof(this.Layer)}: {this.Layer}, {nameof(this.X)}: {this.X}, {nameof(this.Y)}: {this.Y}" ;
}
2021-12-21 11:39:29 +01:00
/// <summary>
/// Adds the given layer positions together, returning a new layer position with the sum of their coordinates.
/// If the two layer positions' <see cref="Layer"/> differ, an <see cref="ArgumentException"/> is thrown.
/// </summary>
/// <param name="left">The left position.</param>
/// <param name="right">The right position.</param>
/// <returns>The sum of the positions.</returns>
/// <exception cref="ArgumentException">Thrown if the two positions' <see cref="Layer"/> are not the same.</exception>
public static LayerPosition Add ( LayerPosition left , LayerPosition right ) {
if ( left . Layer ! = right . Layer )
throw new ArgumentException ( "Cannot add layer positions on different layers" ) ;
return new LayerPosition ( left . Layer , left . X + right . X , left . Y + right . Y ) ;
}
/// <inheritdoc cref="Equals(LayerPosition)"/>
public static bool operator = = ( LayerPosition left , LayerPosition right ) {
return left . Equals ( right ) ;
}
/// <inheritdoc cref="Equals(LayerPosition)"/>
public static bool operator ! = ( LayerPosition left , LayerPosition right ) {
return ! left . Equals ( right ) ;
}
/// <summary>
/// Returns the negative of the given layer position.
/// </summary>
/// <param name="position">The position to negate.</param>
/// <returns>The negative position.</returns>
public static LayerPosition operator - ( LayerPosition position ) {
return new LayerPosition ( position . Layer , - position . X , - position . Y ) ;
}
/// <summary>
/// Returns the sum of the two layer positions using <see cref="Add"/>.
/// </summary>
/// <param name="left">The left position.</param>
/// <param name="right">The right position.</param>
/// <returns>The sum of the positions.</returns>
public static LayerPosition operator + ( LayerPosition left , LayerPosition right ) {
2022-06-15 11:38:11 +02:00
return LayerPosition . Add ( left , right ) ;
2021-12-21 11:39:29 +01:00
}
/// <summary>
/// Subtracts the second from the first position using <see cref="Add"/>.
/// </summary>
/// <param name="left">The left position.</param>
/// <param name="right">The right position.</param>
/// <returns>The difference of the positions.</returns>
public static LayerPosition operator - ( LayerPosition left , LayerPosition right ) {
2022-06-15 11:38:11 +02:00
return LayerPosition . Add ( left , - right ) ;
2021-12-21 11:39:29 +01:00
}
2022-06-06 23:50:13 +02:00
/// <summary>
/// Implicitly converts a <see cref="LayerPosition"/> to a <see cref="LayerPositionF"/>.
/// </summary>
/// <param name="position">The position to convert.</param>
/// <returns>The converted position.</returns>
public static implicit operator LayerPositionF ( LayerPosition position ) {
return new LayerPositionF ( position . Layer , position . X , position . Y ) ;
}
2019-12-07 13:00:50 +01:00
}
2022-06-17 18:23:47 +02:00
}