phs-galaxy/DeadzoneInput.gd
2021-06-24 13:34:25 +02:00

41 lines
1.0 KiB
GDScript

extends Object
class_name DeadzoneInput
static func get_input(type, outer_deadzone, inner_deadzone, min_length = 0.0, normalized = true):
var input = Vector2(Input.get_action_strength(type + "_up") -
Input.get_action_strength(type + "_down"),
Input.get_action_strength(type + "_right") -
Input.get_action_strength(type + "_left"))
# 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))
if input.length() < min_length:
return Vector2.ZERO
# 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 normalized and input.length() > 1.0:
input /= input.length()
return input