Skip to content

Commit 0788b06

Browse files
committed
Build player-action mapping dynamically
Previously there was a constant dictionary mapping player enum × short action name to full action name. This is very simple but is tedious to keep updated as more actions are added. Instead, build the mapping dynamically from a constant array of short action names, and improve the actions.gd documentation. As it stands this is a bit longer than writing it out by hand, but future commits which will add new actions become one-liners.
1 parent 92a8e8b commit 0788b06

1 file changed

Lines changed: 37 additions & 38 deletions

File tree

scripts/actions.gd

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,54 @@
11
extends Node
22
## Registry of action names for each player
33
##
4-
## This game has actions preconfigured for two players. The [class Player] scene can be configured
4+
## This game has actions preconfigured for two players. The [Player] scene can be configured
55
## to respond to inputs for player one or two, or for both players.
66
## [br][br]
7-
## Use [member lookup] to find the full action name for a given player and action.
8-
9-
const _PLAYER_ACTIONS = {
10-
Global.Player.ONE:
11-
{
12-
"jump": &"player_1_jump",
13-
"left": &"player_1_left",
14-
"right": &"player_1_right",
15-
},
16-
Global.Player.TWO:
17-
{
18-
"jump": &"player_2_jump",
19-
"left": &"player_2_left",
20-
"right": &"player_2_right",
21-
},
22-
Global.Player.BOTH:
23-
{
24-
"jump": &"player_both_jump",
25-
"left": &"player_both_left",
26-
"right": &"player_both_right",
27-
},
28-
}
29-
30-
31-
func _ready() -> void:
32-
_setup_both_actions()
33-
34-
35-
# Sets up the "both" actions, bound to the corresponding events from both players one and two.
36-
# This is done dynamically so that we don't have to keep them in sync in the project settings.
37-
func _setup_both_actions() -> void:
38-
for action: String in _PLAYER_ACTIONS[Global.Player.BOTH]:
39-
var p1: StringName = _PLAYER_ACTIONS[Global.Player.ONE][action]
40-
var p2: StringName = _PLAYER_ACTIONS[Global.Player.TWO][action]
41-
var both: StringName = _PLAYER_ACTIONS[Global.Player.BOTH][action]
7+
## Use [method lookup] to find the full action name for a given player and action.
8+
9+
## Short names for actions available to each player. Use [method lookup] to find the full action
10+
## name for a given player and action.
11+
const ACTIONS = [
12+
&"jump",
13+
&"left",
14+
&"right",
15+
]
16+
17+
# Dictionary[Global.Player, Dictionary[StringName, StringName]]
18+
# e.g. _player_actions[Global.Player.ONE][&"jump"] == &"player_1_jump"
19+
@onready var _player_actions = _setup_both_actions()
20+
21+
22+
# Creates the mapping from Global.Player enum & elements of ACTIONS to full action name, and creates
23+
# the "both" actions bound to the combination of events for players one and two. This is done
24+
# dynamically so that we don't have to keep them in sync in the project settings.
25+
func _setup_both_actions() -> Dictionary[Global.Player, Dictionary]:
26+
var player_actions: Dictionary[Global.Player, Dictionary] = {
27+
Global.Player.ONE: {}, Global.Player.TWO: {}, Global.Player.BOTH: {}
28+
}
29+
30+
for action: StringName in ACTIONS:
31+
var p1: StringName = "player_1_" + action
32+
var p2: StringName = "player_2_" + action
33+
var both: StringName = "player_both_" + action
34+
35+
player_actions[Global.Player.ONE][action] = p1
36+
player_actions[Global.Player.TWO][action] = p2
37+
player_actions[Global.Player.BOTH][action] = both
4238

4339
var deadzone := maxf(InputMap.action_get_deadzone(p1), InputMap.action_get_deadzone(p2))
4440
InputMap.add_action(both, deadzone)
4541

4642
for event: InputEvent in InputMap.action_get_events(p1) + InputMap.action_get_events(p2):
4743
InputMap.action_add_event(both, event)
4844

45+
return player_actions
4946

50-
## Looks up the full action name for [param player] and [param action].
47+
48+
## Looks up the full action name for [param player] and [param action]. These full names can be
49+
## passed to methods such as [method Input.is_action_pressed].
5150
## [br][br]
5251
## For example, [code]Actions.lookup(Global.Player.TWO, "jump")[/code] returns
5352
## [code]"player_2_jump"[/code].
5453
func lookup(player: Global.Player, action: StringName) -> StringName:
55-
return _PLAYER_ACTIONS[player][action]
54+
return _player_actions[player][action]

0 commit comments

Comments
 (0)