Skip to content

Commit 34301a1

Browse files
urbit-pilledwjt
authored andcommitted
SimpleCharacter: Add controller support
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
1 parent 5541231 commit 34301a1

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

addons/block_code/simple_nodes/simple_character/simple_character.gd

+43
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,36 @@ const PLAYER_KEYS = [
2929
}
3030
]
3131

32+
const PLAYER_JOYSTICK_BUTTONS = {
33+
"up": JOY_BUTTON_DPAD_UP,
34+
"down": JOY_BUTTON_DPAD_DOWN,
35+
"left": JOY_BUTTON_DPAD_LEFT,
36+
"right": JOY_BUTTON_DPAD_RIGHT,
37+
}
38+
39+
const PLAYER_JOYSTICK_MOTION = {
40+
"up":
41+
{
42+
"axis": JOY_AXIS_LEFT_Y,
43+
"axis_value": -1,
44+
},
45+
"down":
46+
{
47+
"axis": JOY_AXIS_LEFT_Y,
48+
"axis_value": 1,
49+
},
50+
"left":
51+
{
52+
"axis": JOY_AXIS_LEFT_X,
53+
"axis_value": -1,
54+
},
55+
"right":
56+
{
57+
"axis": JOY_AXIS_LEFT_X,
58+
"axis_value": 1,
59+
},
60+
}
61+
3262
var sprite: Sprite2D
3363
var collision: CollisionShape2D
3464

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

133+
#controller d-pad event
134+
var ej = InputEventJoypadButton.new()
135+
ej.device = i
136+
ej.button_index = PLAYER_JOYSTICK_BUTTONS[action]
137+
InputMap.action_add_event(action_name, ej)
138+
139+
#controller left stick event
140+
var ejm = InputEventJoypadMotion.new()
141+
ejm.device = i
142+
ejm.axis = PLAYER_JOYSTICK_MOTION[action]["axis"]
143+
ejm.axis_value = PLAYER_JOYSTICK_MOTION[action]["axis_value"]
144+
InputMap.action_add_event(action_name, ejm)
145+
103146

104147
func get_custom_class():
105148
return "SimpleCharacter"

0 commit comments

Comments
 (0)