Skip to content

Commit

Permalink
added controller support for move block
Browse files Browse the repository at this point in the history
  • Loading branch information
urbit-pilled committed Dec 16, 2024
1 parent a9c7193 commit de3b42c
Showing 1 changed file with 51 additions and 10 deletions.
61 changes: 51 additions & 10 deletions addons/block_code/simple_nodes/simple_character/simple_character.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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":
Expand Down

0 comments on commit de3b42c

Please sign in to comment.