From 34301a18df9974da9ac1671ea1bd950f9aba78d6 Mon Sep 17 00:00:00 2001 From: urbit-pilled Date: Mon, 16 Dec 2024 22:31:29 +1300 Subject: [PATCH] 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: https://github.com/endlessm/godot-block-coding/issues/257 --- .../simple_character/simple_character.gd | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/addons/block_code/simple_nodes/simple_character/simple_character.gd b/addons/block_code/simple_nodes/simple_character/simple_character.gd index 9787fd2c..674095f2 100644 --- a/addons/block_code/simple_nodes/simple_character/simple_character.gd +++ b/addons/block_code/simple_nodes/simple_character/simple_character.gd @@ -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 @@ -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"