using System;
using UnityEngine;
namespace Core.Utilities
{
///
/// A 2-dimensional vector with integer components
///
[Serializable]
public struct IntVector2 : IEquatable
{
///
/// Vector with both components being 1
///
public static readonly IntVector2 one = new IntVector2(1, 1);
///
/// Vector with both components being 0
///
public static readonly IntVector2 zero = new IntVector2(0, 0);
///
/// The x component of this vector
///
public int x;
///
/// The y component of this vector
///
public int y;
///
/// Gets the squared magnitude of this vector
///
public int sqrMagnitude
{
get { return (x * x) + (y * y); }
}
///
/// Returns the magnitude of this vector
///
public float magnitude
{
get { return Mathf.Sqrt(sqrMagnitude); }
}
///
/// Gets the manhattan distance of this vector
///
public int manhattanDistance
{
get { return Mathf.Abs(x) + Mathf.Abs(y); }
}
///
/// Initialize a new vector
///
public IntVector2(int x, int y)
{
this.x = x;
this.y = y;
}
public bool Equals(IntVector2 other)
{
return other.x == x && other.y == y;
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
{
return false;
}
return obj is IntVector2 && Equals((IntVector2) obj);
}
///
/// Simple hash multiplying by two primes
///
public override int GetHashCode()
{
unchecked
{
return (x.GetHashCode() * 92821) ^ (y.GetHashCode() * 31);
}
}
public override string ToString()
{
return string.Format("X: {0}, Y: {1}", x, y);
}
public static bool operator ==(IntVector2 left, IntVector2 right)
{
return left.Equals(right);
}
public static bool operator !=(IntVector2 left, IntVector2 right)
{
return !left.Equals(right);
}
public static implicit operator Vector2(IntVector2 vector)
{
return new Vector2(vector.x, vector.y);
}
public static explicit operator IntVector2(Vector2 vector)
{
return new IntVector2((int) vector.x, (int) vector.y);
}
public static IntVector2 operator +(IntVector2 left, IntVector2 right)
{
return new IntVector2(left.x + right.x, left.y + right.y);
}
public static IntVector2 operator -(IntVector2 left, IntVector2 right)
{
return new IntVector2(left.x - right.x, left.y - right.y);
}
public static IntVector2 operator *(int scale, IntVector2 left)
{
return new IntVector2(left.x * scale, left.y * scale);
}
public static IntVector2 operator *(IntVector2 left, int scale)
{
return new IntVector2(left.x * scale, left.y * scale);
}
public static Vector2 operator *(float scale, IntVector2 left)
{
return new Vector2(left.x * scale, left.y * scale);
}
public static Vector2 operator *(IntVector2 left, float scale)
{
return new Vector2(left.x * scale, left.y * scale);
}
public static IntVector2 operator -(IntVector2 left)
{
return new IntVector2(-left.x, -left.y);
}
}
}