Skip to content

Instantly share code, notes, and snippets.

@tacoe
Created July 22, 2018 16:47
Show Gist options
  • Save tacoe/eef0cea473f706b83a758e120969179a to your computer and use it in GitHub Desktop.
Save tacoe/eef0cea473f706b83a758e120969179a to your computer and use it in GitHub Desktop.
using UnityEngine;
// a class to work with 2D-points on our map grid
public class GridPoint
{
public readonly int x, y;
public GridPoint(int X, int Y) { x = X; y = Y; }
public GridPoint(float X, float Y) { x = (int)X; y = (int)Y; }
public GridPoint(Vector2 v2) { x = (int)v2.x; y = (int)v2.y; }
public GridPoint(Vector3 v3) { x = (int)v3.x; y = (int)v3.z; }
public Vector2 ToVector2() { return new Vector2(x, y); }
public Vector2 ToVector2(Vector2 offset) { return new Vector2(x, y) + offset; }
public override string ToString() {
return ($"GridPoint[{x},{y}]");
}
public override bool Equals(object obj)
{
if (obj == null) return false;
if (this.GetType() != obj.GetType()) return false;
return Equals((GridPoint)obj);
}
public bool Equals(GridPoint obj) {
if (obj == null) return false;
if (ReferenceEquals(this, obj)) return true;
if (this.GetHashCode() != obj.GetHashCode()) return false;
return (obj.x == x & obj.y == y);
}
public override int GetHashCode()
{
unchecked
{
int hash = 17;
hash = hash * 643 + x.GetHashCode();
hash = hash * 503 + y.GetHashCode();
return hash;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment