From de3b42c13c3e162050b512a891788d95757283fc Mon Sep 17 00:00:00 2001 From: urbit-pilled Date: Mon, 16 Dec 2024 22:31:29 +1300 Subject: [PATCH] added controller support for move block --- .../simple_character/simple_character.gd | 61 ++++++++++++++++--- 1 file changed, 51 insertions(+), 10 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..7a5b5df0 100644 --- a/addons/block_code/simple_nodes/simple_character/simple_character.gd +++ b/addons/block_code/simple_nodes/simple_character/simple_character.gd @@ -31,6 +31,27 @@ const PLAYER_KEYS = { } } +const PLAYER_JOYSTICK_BUTTONS = { + "player_1": { + 0: { + "up": JOY_BUTTON_DPAD_UP, + "down": JOY_BUTTON_DPAD_DOWN, + "left": JOY_BUTTON_DPAD_LEFT, + "right": JOY_BUTTON_DPAD_RIGHT, + }, + }, + + "player_2": + { + 1: { + "up": JOY_BUTTON_DPAD_UP, + "down": JOY_BUTTON_DPAD_DOWN, + "left": JOY_BUTTON_DPAD_LEFT, + "right": JOY_BUTTON_DPAD_RIGHT, + } + } +} + var sprite: Sprite2D var collision: CollisionShape2D @@ -83,24 +104,44 @@ 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(InputMap.has_action("player_1_left")): + return + + InputMap.add_action("player_1_left") + InputMap.add_action("player_1_right") + InputMap.add_action("player_1_up") + InputMap.add_action("player_1_down") + InputMap.add_action("player_2_left") + InputMap.add_action("player_2_right") + InputMap.add_action("player_2_up") + InputMap.add_action("player_2_down") + + for player in PLAYER_KEYS: + for action in PLAYER_KEYS[player]: + var e = InputEventKey.new() + e.keycode = PLAYER_KEYS[player][action] + InputMap.action_add_event(player+"_"+action, e) + + for player in PLAYER_JOYSTICK_BUTTONS: + for device in PLAYER_JOYSTICK_BUTTONS[player]: + for action in PLAYER_JOYSTICK_BUTTONS[player][device]: + var e = InputEventJoypadButton.new() + e.device = device + e.button_index = PLAYER_JOYSTICK_BUTTONS[player][device][action] + InputMap.action_add_event(player+"_"+action, 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":