extends PathFollow2D

export (int) var threshold = 5  # Range to snap around axis direction, in degrees.

func _ready():
	set_unit_offset(0.0)

func snap_rotation(r):
	""" 
	Snap rotation to multiples of 45°
		Also snaps within a certain range of direction axis
	"""
	if r >= 0 - threshold and r <= 0 + threshold: # Right
		r = 0
	elif r >= 180 - threshold or r <= -180 + threshold: # Left
		r = 180
	elif r >= 90 - threshold and r <= 90 + threshold: # Down
		r = 90
	elif r >= -90 - threshold and r <= -90 + threshold: # Up
		r = -90
	elif r > 0 + threshold and r < 90 - threshold:
		r = 45
	elif r > 90 + threshold and r < 180 - threshold:
		r = 135
	elif r > -90 + threshold and r < 0 - threshold:
		r = -45
	elif r > -180 + threshold and r < -90 - threshold:
		r = -135
	return r

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta):
	set_rotation_degrees( snap_rotation(get_rotation_degrees()) )
