-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHexUtilities.gd
47 lines (34 loc) · 1 KB
/
HexUtilities.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
extends Node
class_name HexUtilities
static func from_offset_coordinates(X : int, Z : int) -> HexCoordinates:
return HexCoordinates.new(X - Z / 2, Z)
static func from_position(position : Vector3) -> HexCoordinates:
var x = position.x / (HexConstants.inner_radius * 2.0)
var y = -x
var offset = position.z / (HexConstants.outer_radius * 3.0)
x -= offset
y -= offset
var iX = round_even(x)
var iY = round_even(y)
var iZ = round_even(-x - y)
if iX + iY + iZ != 0:
var dX = abs(x - iX)
var dY = abs(y - iY)
var dZ = abs(-x - y - iZ)
if dX > dY && dX > dZ:
iX = -iY - iZ
elif dZ > dY:
iZ = -iX - iY
return HexCoordinates.new(iX, iZ)
static func round_even(value : float) -> float:
if fmod(value, 2) == 0.5:
return floor(value)
else:
return round(value)
static func get_edge_type(e1 : int, e2 : int) -> int:
if e1 == e2:
return HexConstants.HexEdgeType.FLAT
var delta := abs(e1 - e2)
if delta == 1:
return HexConstants.HexEdgeType.SLOPE
return HexConstants.HexEdgeType.CLIFF