10000 SimpleCharacter: Use input map for keyboard input · DoomTas3r/changes-block-coding@5541231 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5541231

Browse files
urbit-pilledwjt
authored andcommitted
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.
1 parent a9c7193 commit 5541231

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

addons/block_code/simple_nodes/simple_character/simple_character.gd

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,20 @@ 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
}
32-
}
30+
]
3331

3432
var sprite: Sprite2D
3533
var collision: CollisionShape2D
@@ -83,24 +81,32 @@ func _ready():
8381

8482
func simple_setup():
8583
add_to_group("affected_by_gravity", true)
84+
_setup_actions()
8685
_texture_updated()
8786

8887

89-
func get_custom_class():
90-
return "SimpleCharacter"
88+
func _setup_actions():
89+
if Engine.is_editor_hint() or InputMap.has_action("player_1_left"):
90+
return
91+
92+
for i in PLAYER_KEYS.size():
93+
for action in PLAYER_KEYS[i]:
94+
var player = "player_%d" % [i + 1]
95+
var action_name = player + "_" + action
96+
InputMap.add_action(action_name)
9197

98+
#keyboard event
99+
var e = InputEventKey.new()
100+
e.physical_keycode = PLAYER_KEYS[i][action]
101+
InputMap.action_add_event(action_name, e)
92102

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
103+
104+
func get_custom_class():
105+
return "SimpleCharacter"
100106

101107

102108
func move_with_player_buttons(player: String, kind: String, delta: float):
103-
var direction = _player_input_to_direction(player)
109+
var direction = Input.get_vector(player + "_left", player + "_right", player + "_up", player + "_down")
104110
direction_x = direction.x
105111

106112
if kind == "top-down":
@@ -111,7 +117,7 @@ func move_with_player_buttons(player: String, kind: String, delta: float):
111117
if not is_on_floor():
112118
velocity.y += gravity * delta
113119
else:
114-
if not _jumping and Input.is_physical_key_pressed(PLAYER_KEYS[player]["up"]):
120+
if not _jumping and direction.y < 0:
115121
_jumping = true
116122
velocity.y -= speed.y
117123
else:

0 commit comments

Comments
 (0)
0