phs-galaxy/InputConverter.gd

36 lines
811 B
GDScript3
Raw Permalink Normal View History

2021-03-18 10:41:54 +01:00
extends Node
export(float) var inner_deadzone = 0.2
export(float) var outer_deadzone = 0.8
func _process(delta):
var input = Vector2(Input.get_joy_axis(0, 0), Input.get_joy_axis(0, 1))
# Remove signs to reduce the number of cases
var signs = Vector2(sign(input.x), sign(input.y))
input = Vector2(abs(input.x), abs(input.y))
# Deazones for each axis
if input.x > outer_deadzone:
input.x = 1.0
elif input.x < inner_deadzone:
input.x = 0.0
else:
input.x = inverse_lerp(inner_deadzone, outer_deadzone, input.x)
if input.y > outer_deadzone:
input.y = 1.0
elif input.y < inner_deadzone:
input.y = 0.0
else:
input.y = inverse_lerp(inner_deadzone, outer_deadzone, input.y)
# Re-apply signs
input *= signs
# Limit at length 1
if input.length() > 1.0:
input /= input.length()