From 554123123bcdd91504fa3df873e03fb965ee75d9 Mon Sep 17 00:00:00 2001 From: urbit-pilled Date: Mon, 16 Dec 2024 22:31:29 +1300 Subject: [PATCH 1/2] SimpleCharacter: Use input map for keyboard input Previously, keypresses were detected directly, rather than going through the input map. In preparation for handling multiple input devices for each action, add appropriate entries to the input map at runtime, if not already present. Map each one to the same key as previously. --- .../simple_character/simple_character.gd | 36 +++++++++++-------- 1 file changed, 21 insertions(+), 15 deletions(-) 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 7f3982a7..9787fd2c 100644 --- a/addons/block_code/simple_nodes/simple_character/simple_character.gd +++ b/addons/block_code/simple_nodes/simple_character/simple_character.gd @@ -14,22 +14,20 @@ const Types = preload("res://addons/block_code/types/types.gd") @export var speed: Vector2 = Vector2(300, 300) -const PLAYER_KEYS = { - "player_1": +const PLAYER_KEYS = [ { "up": KEY_W, "down": KEY_S, "left": KEY_A, "right": KEY_D, }, - "player_2": { "up": KEY_UP, "down": KEY_DOWN, "left": KEY_LEFT, "right": KEY_RIGHT, } -} +] var sprite: Sprite2D var collision: CollisionShape2D @@ -83,24 +81,32 @@ func _ready(): func simple_setup(): add_to_group("affected_by_gravity", true) + _setup_actions() _texture_updated() -func get_custom_class(): - return "SimpleCharacter" +func _setup_actions(): + if Engine.is_editor_hint() or InputMap.has_action("player_1_left"): + return + + for i in PLAYER_KEYS.size(): + for action in PLAYER_KEYS[i]: + var player = "player_%d" % [i + 1] + var action_name = player + "_" + action + InputMap.add_action(action_name) + #keyboard event + var e = InputEventKey.new() + e.physical_keycode = PLAYER_KEYS[i][action] + InputMap.action_add_event(action_name, e) -func _player_input_to_direction(player: String): - var direction = Vector2() - direction.x += float(Input.is_physical_key_pressed(PLAYER_KEYS[player]["right"])) - direction.x -= float(Input.is_physical_key_pressed(PLAYER_KEYS[player]["left"])) - direction.y += float(Input.is_physical_key_pressed(PLAYER_KEYS[player]["down"])) - direction.y -= float(Input.is_physical_key_pressed(PLAYER_KEYS[player]["up"])) - return direction + +func get_custom_class(): + return "SimpleCharacter" func move_with_player_buttons(player: String, kind: String, delta: float): - var direction = _player_input_to_direction(player) + var direction = Input.get_vector(player + "_left", player + "_right", player + "_up", player + "_down") direction_x = direction.x if kind == "top-down": @@ -111,7 +117,7 @@ func move_with_player_buttons(player: String, kind: String, delta: float): if not is_on_floor(): velocity.y += gravity * delta else: - if not _jumping and Input.is_physical_key_pressed(PLAYER_KEYS[player]["up"]): + if not _jumping and direction.y < 0: _jumping = true velocity.y -= speed.y else: From 34301a18df9974da9ac1671ea1bd950f9aba78d6 Mon Sep 17 00:00:00 2001 From: urbit-pilled Date: Mon, 16 Dec 2024 22:31:29 +1300 Subject: [PATCH 2/2] 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"