using System; using System.Collections.Generic; using UnityEngine; namespace BrewMonster.Scripts { public class APoint where T : IComparable { public T x, y; public APoint() { x = default(T); y = default(T); } public APoint(T x, T y) { this.x = x; this.y = y; } public APoint(APoint p) { x = p.x; y = p.y; } // Offset point public void Offset(T ox, T oy) { x = Add(x, ox); y = Add(y, oy); } // Set point public void Set(T _x, T _y) { x = _x; y = _y; } // helpers for int/float only static T Add(T a, T b) { if (typeof(T) == typeof(int)) return (T)(object)((int)(object)a + (int)(object)b); if (typeof(T) == typeof(float)) return (T)(object)((float)(object)a + (float)(object)b); throw new NotSupportedException("APoint supports only int and float"); } static T Sub(T a, T b) { if (typeof(T) == typeof(int)) return (T)(object)((int)(object)a - (int)(object)b); if (typeof(T) == typeof(float)) return (T)(object)((float)(object)a - (float)(object)b); throw new NotSupportedException("APoint supports only int and float"); } static T Neg(T a) { if (typeof(T) == typeof(int)) return (T)(object)(-(int)(object)a); if (typeof(T) == typeof(float)) return (T)(object)(-(float)(object)a); throw new NotSupportedException("APoint supports only int and float"); } // == and != operator public static bool operator !=(APoint p1, APoint p2) { return !(p1 == p2); } public static bool operator ==(APoint p1, APoint p2) { return EqualityComparer.Default.Equals(p1.x, p2.x) && EqualityComparer.Default.Equals(p1.y, p2.y); } // + and - operator public static APoint operator +(APoint p1, APoint p2) { return new APoint( Add(p1.x, p2.x), Add(p1.y, p2.y) ); } public static APoint operator -(APoint p1, APoint p2) { return new APoint( Sub(p1.x, p2.x), Sub(p1.y, p2.y) ); } } }