Skip to content

Commit

Permalink
SimpleCharacter: Add controller support
Browse files Browse the repository at this point in the history
Map the d-pad and left stick of controllers 0 and 1 to the directional
actions of players 1 and 2, respectively, in addition to the keyboard
mappings.

Fixes: #257
  • Loading branch information
urbit-pilled authored and wjt committed Dec 17, 2024
1 parent 5541231 commit 34301a1
Showing 1 changed file with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,36 @@ const PLAYER_KEYS = [
}
]

const PLAYER_JOYSTICK_BUTTONS = {
"up": JOY_BUTTON_DPAD_UP,
"down": JOY_BUTTON_DPAD_DOWN,
"left": JOY_BUTTON_DPAD_LEFT,
"right": JOY_BUTTON_DPAD_RIGHT,
}

const PLAYER_JOYSTICK_MOTION = {
"up":
{
"axis": JOY_AXIS_LEFT_Y,
"axis_value": -1,
},
"down":
{
"axis": JOY_AXIS_LEFT_Y,
"axis_value": 1,
},
"left":
{
"axis": JOY_AXIS_LEFT_X,
"axis_value": -1,
},
"right":
{
"axis": JOY_AXIS_LEFT_X,
"axis_value": 1,
},
}

var sprite: Sprite2D
var collision: CollisionShape2D

Expand Down Expand Up @@ -100,6 +130,19 @@ func _setup_actions():
e.physical_keycode = PLAYER_KEYS[i][action]
InputMap.action_add_event(action_name, e)

#controller d-pad event
var ej = InputEventJoypadButton.new()
ej.device = i
ej.button_index = PLAYER_JOYSTICK_BUTTONS[action]
InputMap.action_add_event(action_name, ej)

#controller left stick event
var ejm = InputEventJoypadMotion.new()
ejm.device = i
ejm.axis = PLAYER_JOYSTICK_MOTION[action]["axis"]
ejm.axis_value = PLAYER_JOYSTICK_MOTION[action]["axis_value"]
InputMap.action_add_event(action_name, ejm)


func get_custom_class():
return "SimpleCharacter"
Expand Down

0 comments on commit 34301a1

Please sign in to comment.