@@ -3,21 +3,6 @@ class_name Player
33extends CharacterBody2D
44## A player's character, which can walk, jump, and stomp on enemies.
55
6- const _PLAYER_ACTIONS = {
7- Global .Player .ONE :
8- {
9- "jump" : "player_1_jump" ,
10- "left" : "player_1_left" ,
11- "right" : "player_1_right" ,
12- },
13- Global .Player .TWO :
14- {
15- "jump" : "player_2_jump" ,
16- "left" : "player_2_left" ,
17- "right" : "player_2_right" ,
18- },
19- }
20-
216## Which player controls this character?
227@export var player : Global .Player = Global .Player .ONE
238
@@ -118,43 +103,6 @@ func stomp():
118103 _jump ()
119104
120105
121- func _player_just_pressed (action ):
122- if player == Global .Player .BOTH :
123- return (
124- Input .is_action_just_pressed (_PLAYER_ACTIONS [Global .Player .ONE ][action ])
125- or Input .is_action_just_pressed (_PLAYER_ACTIONS [Global .Player .TWO ][action ])
126- )
127- return Input .is_action_just_pressed (_PLAYER_ACTIONS [player ][action ])
128-
129-
130- func _player_just_released (action ):
131- if player == Global .Player .BOTH :
132- return (
133- Input .is_action_just_released (_PLAYER_ACTIONS [Global .Player .ONE ][action ])
134- or Input .is_action_just_released (_PLAYER_ACTIONS [Global .Player .TWO ][action ])
135- )
136- return Input .is_action_just_released (_PLAYER_ACTIONS [player ][action ])
137-
138-
139- func _get_player_axis (action_a , action_b ):
140- if player == Global .Player .BOTH :
141- return clamp (
142- (
143- Input .get_axis (
144- _PLAYER_ACTIONS [Global .Player .ONE ][action_a ],
145- _PLAYER_ACTIONS [Global .Player .ONE ][action_b ]
146- )
147- + Input .get_axis (
148- _PLAYER_ACTIONS [Global .Player .TWO ][action_a ],
149- _PLAYER_ACTIONS [Global .Player .TWO ][action_b ]
150- )
151- ),
152- - 1 ,
153- 1
154- )
155- return Input .get_axis (_PLAYER_ACTIONS [player ][action_a ], _PLAYER_ACTIONS [player ][action_b ])
156-
157-
158106func _physics_process (delta ):
159107 # Don't move if there are no lives left.
160108 if Global .lives <= 0 :
@@ -165,24 +113,23 @@ func _physics_process(delta):
165113 coyote_timer = (coyote_time + delta )
166114 double_jump_armed = false
167115
168- if _player_just_pressed ( "jump" ):
116+ if Input . is_action_just_pressed ( Actions . lookup ( player , "jump" ) ):
169117 jump_buffer_timer = (jump_buffer + delta )
170118
171119 if jump_buffer_timer > 0 and (double_jump_armed or coyote_timer > 0 ):
172120 _jump ()
173121
174122 # Reduce velocity if the player lets go of the jump key before the apex.
175123 # This allows controlling the height of the jump.
176- if _player_just_released ( "jump" ) and velocity .y < 0 :
124+ if Input . is_action_just_released ( Actions . lookup ( player , "jump" ) ) and velocity .y < 0 :
177125 velocity .y *= (1 - (jump_cut_factor / 100.00 ))
178126
179127 # Add the gravity.
180128 if coyote_timer <= 0 :
181129 velocity .y += gravity * delta
182130
183131 # Get the input direction and handle the movement/deceleration.
184- # As good practice, you should replace UI actions with custom gameplay actions.
185- var direction = _get_player_axis ("left" , "right" )
132+ var direction = Input .get_axis (Actions .lookup (player , "left" ), Actions .lookup (player , "right" ))
186133 if direction :
187134 velocity .x = move_toward (
188135 velocity .x ,
0 commit comments