extends Node
class_name AxialCoordinate

func _init(_q : int = 0, _r : int = 0) -> void:
	q = _q
	r = _r

static var d1=	preload("AxialCoordinate.gd").new(1, 0)
static var d2=	preload("AxialCoordinate.gd").new(1, -1)
static var d3=	preload("AxialCoordinate.gd").new(0, -1)
static var d4=	preload("AxialCoordinate.gd").new(-1, 0)
static var d5=	preload("AxialCoordinate.gd").new(-1, 1)
static var d6=	preload("AxialCoordinate.gd").new(0, 1)

static var Directions = [ d1, d2, d3, d4, d5, d6 ]

static var zero = AxialCoordinate.new(0, 0)

var q : int
var r : int

static func Add(a: AxialCoordinate, b : AxialCoordinate) ->AxialCoordinate:
	var result = AxialCoordinate.new(0, 0)
	result.q = a.q + b.q
	result.r = a.r + b.r
	return result;

func DistanceTo(other : AxialCoordinate) -> int:
	return (abs(q - other.q)
			+ abs(q + r - other.q - other.r)
			+ abs(r - other.r)) / 2

func Equals(ac : AxialCoordinate) -> bool:
	return (q == ac.q) && (r == ac.r)

func GetNeighbor(direction : int) -> AxialCoordinate:
	return AxialCoordinate.Add(self, Directions[direction])

func GetHashCode() -> int:
	return q * 0x00010000 + r	

static func round_to_nearest(myq : float, myr : float) -> AxialCoordinate:
	var x = myq
	var z = myr
	var y = -x - z
	var rx = round(x)
	var ry = round(y)
	var rz = round(z)
	var x_diff = abs(rx - x)
	var y_diff = abs(ry - y)
	var z_diff = abs(rz - z)
	if x_diff > y_diff and x_diff > z_diff:
		rx = -ry - rz
	elif y_diff > z_diff:
		ry = -rx - rz
	else:
		rz = -rx - ry
	return AxialCoordinate.new(rx, rz)

func FromCube(cc : CubeCoordinate) -> void:
	q = cc.x
	r = cc.z

func FromOffset(oc : OffsetCoordinate) -> void:
	q = oc.col - (oc.row - (oc.row & 1)) / (2)
	r = oc.row

