Skip to content

Commit

Permalink
Merge pull request #143 from nezvers/gamepad_direction
Browse files Browse the repository at this point in the history
add deadzone for movement and aim inputs
  • Loading branch information
nezvers authored Nov 20, 2024
2 parents 2edec36 + 273933d commit 48670f5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
2 changes: 1 addition & 1 deletion addons/top_down/scripts/actor/player/PlayerInput.gd
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func _physics_process(_delta:float)->void:
# Walking direction
var _axis:Vector2 = Vector2(Input.get_axis(action_resource.left_action, action_resource.right_action), Input.get_axis(action_resource.up_action, action_resource.down_action))
# Analog sticks sucks for diagonals
_axis = Vector2(ceil(abs(_axis.x)) * sign(_axis.x), ceil(abs(_axis.y)) * sign(_axis.y) )
#_axis = Vector2(ceil(abs(_axis.x)) * sign(_axis.x), ceil(abs(_axis.y)) * sign(_axis.y) )

var _length:float = _axis.length()
if _length > 0.01:
Expand Down
21 changes: 12 additions & 9 deletions addons/top_down/scripts/input/ActionResource.gd
Original file line number Diff line number Diff line change
Expand Up @@ -69,25 +69,28 @@ func _initialize()->void:

_init_action(pause_action, [pause_kb, pause_gp].filter(_filter_empty))

_init_action(right_action, [right_kb, right_gp].filter(_filter_empty))
_init_action(left_action, [left_kb, left_gp].filter(_filter_empty))
_init_action(up_action, [up_kb, up_gp].filter(_filter_empty))
_init_action(down_action, [down_kb, down_gp].filter(_filter_empty))
_init_action(right_action, [right_kb, right_gp].filter(_filter_empty), 0.2)
_init_action(left_action, [left_kb, left_gp].filter(_filter_empty), 0.2)
_init_action(up_action, [up_kb, up_gp].filter(_filter_empty), 0.2)
_init_action(down_action, [down_kb, down_gp].filter(_filter_empty), 0.2)

_init_action(aim_right_action, [aim_right_kb, aim_right_gp].filter(_filter_empty), 0.1)
_init_action(aim_left_action, [aim_left_kb, aim_left_gp].filter(_filter_empty), 0.1)
_init_action(aim_up_action, [aim_up_kb, aim_up_gp].filter(_filter_empty), 0.1)
_init_action(aim_down_action, [aim_down_kb, aim_down_gp].filter(_filter_empty), 0.1)

_init_action(aim_right_action, [aim_right_kb, aim_right_gp].filter(_filter_empty))
_init_action(aim_left_action, [aim_left_kb, aim_left_gp].filter(_filter_empty))
_init_action(aim_up_action, [aim_up_kb, aim_up_gp].filter(_filter_empty))
_init_action(aim_down_action, [aim_down_kb, aim_down_gp].filter(_filter_empty))
_init_action(action_1_action, [action_1_kb, action_1_gp].filter(_filter_empty))
_init_action(action_2_action, [action_2_kb, action_2_gp].filter(_filter_empty))
_init_action(next_action, [next_kb, next_gp].filter(_filter_empty))
_init_action(previous_action, [previous_kb, previous_gp].filter(_filter_empty))

updated.emit()

func _init_action(action_name:StringName, event_list:Array)->void:
func _init_action(action_name:StringName, event_list:Array, deadzone:float = 0.5)->void:
if !InputMap.has_action(action_name):
InputMap.add_action(action_name)
InputMap.action_set_deadzone(action_name, deadzone)

for event:InputEvent in event_list:
if InputMap.action_has_event(action_name, event):
continue
Expand Down

0 comments on commit 48670f5

Please sign in to comment.