|
1 | 1 | extends Node |
2 | 2 | ## Registry of action names for each player |
3 | 3 | ## |
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 |
5 | 5 | ## to respond to inputs for player one or two, or for both players. |
6 | 6 | ## [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 |
42 | 38 |
|
43 | 39 | var deadzone := maxf(InputMap.action_get_deadzone(p1), InputMap.action_get_deadzone(p2)) |
44 | 40 | InputMap.add_action(both, deadzone) |
45 | 41 |
|
46 | 42 | for event: InputEvent in InputMap.action_get_events(p1) + InputMap.action_get_events(p2): |
47 | 43 | InputMap.action_add_event(both, event) |
48 | 44 |
|
| 45 | + return player_actions |
49 | 46 |
|
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]. |
51 | 50 | ## [br][br] |
52 | 51 | ## For example, [code]Actions.lookup(Global.Player.TWO, "jump")[/code] returns |
53 | 52 | ## [code]"player_2_jump"[/code]. |
54 | 53 | func lookup(player: Global.Player, action: StringName) -> StringName: |
55 | | - return _PLAYER_ACTIONS[player][action] |
| 54 | + return _player_actions[player][action] |
0 commit comments