Created
January 28, 2017 18:34
-
-
Save paullj/3a12dd0ac477cb81f9b1aabaf7df9701 to your computer and use it in GitHub Desktop.
A Coordinate data type for Unity. This is essentially an int Vector2.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
[System.Serializable] | |
public struct Coordinate | |
{ | |
public static Coordinate zero = new Coordinate (0, 0); | |
public static Coordinate one = new Coordinate (1, 1); | |
public static Coordinate north = new Coordinate (0, 1); | |
public static Coordinate south = new Coordinate (0, -1); | |
public static Coordinate east = new Coordinate (1, 0); | |
public static Coordinate west = new Coordinate (-1, 0); | |
public int x; | |
public int y; | |
public Coordinate (Vector2 v) | |
{ | |
this.x = Mathf.RoundToInt (v.x); | |
this.y = Mathf.RoundToInt (v.y); | |
} | |
public Coordinate (Vector3 v) | |
{ | |
this.x = Mathf.RoundToInt (v.x); | |
this.y = Mathf.RoundToInt (v.y); | |
} | |
public Coordinate (Coordinate p) | |
{ | |
this.x = p.x; | |
this.y = p.y; | |
} | |
public Coordinate (int x, int y) | |
{ | |
this.x = x; | |
this.y = y; | |
} | |
public Coordinate Up { get { return this + north; } } | |
public Coordinate Down { get { return this + south; } } | |
public Coordinate Left { get { return this + west; } } | |
public Coordinate Right { get { return this + east; } } | |
public override string ToString () | |
{ | |
return "[" + x + "," + y + "]"; | |
} | |
public override bool Equals (object o) | |
{ | |
Coordinate c = (Coordinate)o; | |
return o is Coordinate && this.x == c.x && this.y == c.y; | |
} | |
public override int GetHashCode () | |
{ | |
return x.GetHashCode () ^ y.GetHashCode (); | |
} | |
public static explicit operator Coordinate (Vector2 v) | |
{ | |
return new Coordinate (Mathf.FloorToInt (v.x), Mathf.FloorToInt (v.y)); | |
} | |
public static explicit operator Coordinate (Vector3 v) | |
{ | |
return new Coordinate (Mathf.FloorToInt (v.x), Mathf.FloorToInt (v.y)); | |
} | |
public static explicit operator Vector2 (Coordinate c) | |
{ | |
return new Vector2 (c.x, c.y); | |
} | |
public static explicit operator Vector3 (Coordinate c) | |
{ | |
return new Vector3 (c.x, 0, c.y); | |
} | |
public static Coordinate operator + (Coordinate a, Coordinate b) | |
{ | |
return new Coordinate (a.x + b.x, a.y + b.y); | |
} | |
public static Coordinate operator - (Coordinate a, Coordinate b) | |
{ | |
return new Coordinate (a.x - b.x, a.y - b.y); | |
} | |
public static Coordinate operator * (Coordinate a, int b) | |
{ | |
return new Coordinate (a.x * b, a.y * b); | |
} | |
public static Coordinate operator / (Coordinate a, int b) | |
{ | |
return new Coordinate (a.x / b, a.y / b); | |
} | |
public static Vector2 operator / (Coordinate a, float b) | |
{ | |
return new Vector2 ((float)a.x / b, (float)a.y / b); | |
} | |
public static bool operator == (Coordinate a, Coordinate b) | |
{ | |
return a.x == b.x && a.y == b.y; | |
} | |
public static bool operator != (Coordinate a, Coordinate b) | |
{ | |
return a.x != b.x || a.y != b.y; ; | |
} | |
public static bool operator < (Coordinate a, Coordinate b) | |
{ | |
return a.x < b.x && a.y < b.y; | |
} | |
public static bool operator > (Coordinate a, Coordinate b) | |
{ | |
return a.x > b.x && a.y > b.y; | |
} | |
public static bool operator <= (Coordinate a, Coordinate b) | |
{ | |
return a.x <= b.x && a.y <= b.y; | |
} | |
public static bool operator >= (Coordinate a, Coordinate b) | |
{ | |
return a.x >= b.x && a.y >= b.y; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment