Skip to content

Commit 52c0f06

Browse files
authored
Merge pull request #338 from urbit-pilled/add_controller_support
Added controller support for move block
2 parents 28f102e + 34301a1 commit 52c0f06

File tree

1 file changed

+63
-14
lines changed

1 file changed

+63
-14
lines changed

addons/block_code/simple_nodes/simple_character/simple_character.gd

+63-14
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,49 @@ const Types = preload("res://addons/block_code/types/types.gd")
1414

1515
@export var speed: Vector2 = Vector2(300, 300)
1616

17-
const PLAYER_KEYS = {
18-
"player_1":
17+
const PLAYER_KEYS = [
1918
{
2019
"up": KEY_W,
2120
"down": KEY_S,
2221
"left": KEY_A,
2322
"right": KEY_D,
2423
},
25-
"player_2":
2624
{
2725
"up": KEY_UP,
2826
"down": KEY_DOWN,
2927
"left": KEY_LEFT,
3028
"right": KEY_RIGHT,
3129
}
30+
]
31+
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+
},
3260
}
3361

3462
var sprite: Sprite2D
@@ -83,24 +111,45 @@ func _ready():
83111

84112
func simple_setup():
85113
add_to_group("affected_by_gravity", true)
114+
_setup_actions()
86115
_texture_updated()
87116

88117

89-
func get_custom_class():
90-
return "SimpleCharacter"
118+
func _setup_actions():
119+
if Engine.is_editor_hint() or InputMap.has_action("player_1_left"):
120+
return
121+
122+
for i in PLAYER_KEYS.size():
123+
for action in PLAYER_KEYS[i]:
124+
var player = "player_%d" % [i + 1]
125+
var action_name = player + "_" + action
126+
InputMap.add_action(action_name)
91127

128+
#keyboard event
129+
var e = InputEventKey.new()
130+
e.physical_keycode = PLAYER_KEYS[i][action]
131+
InputMap.action_add_event(action_name, e)
92132

93-
func _player_input_to_direction(player: String):
94-
var direction = Vector2()
95-
direction.x += float(Input.is_physical_key_pressed(PLAYER_KEYS[player]["right"]))
96-
direction.x -= float(Input.is_physical_key_pressed(PLAYER_KEYS[player]["left"]))
97-
direction.y += float(Input.is_physical_key_pressed(PLAYER_KEYS[player]["down"]))
98-
direction.y -= float(Input.is_physical_key_pressed(PLAYER_KEYS[player]["up"]))
99-
return direction
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+
146+
147+
func get_custom_class():
148+
return "SimpleCharacter"
100149

101150

102151
func move_with_player_buttons(player: String, kind: String, delta: float):
103-
var direction = _player_input_to_direction(player)
152+
var direction = Input.get_vector(player + "_left", player + "_right", player + "_up", player + "_down")
104153
direction_x = direction.x
105154

106155
if kind == "top-down":
@@ -111,7 +160,7 @@ func move_with_player_buttons(player: String, kind: String, delta: float):
111160
if not is_on_floor():
112161
velocity.y += gravity * delta
113162
else:
114-
if not _jumping and Input.is_physical_key_pressed(PLAYER_KEYS[player]["up"]):
163+
if not _jumping and direction.y < 0:
115164
_jumping = true
116165
velocity.y -= speed.y
117166
else:

0 commit comments

Comments
 (0)