Skip to content

Commit 0e24cbc

Browse files
authored
Merge pull request #41 from endlessm/push-vqxtzoklvtxt
Simplify handling both players' inputs
2 parents 679bf92 + 4ff43a8 commit 0e24cbc

4 files changed

Lines changed: 60 additions & 56 deletions

File tree

project.godot

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ config/icon="res://icon.svg"
1919
[autoload]
2020

2121
Global="*res://scripts/global.gd"
22+
Actions="*res://scripts/actions.gd"
2223

2324
[display]
2425

scripts/actions.gd

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
extends Node
2+
## Registry of action names for each player
3+
##
4+
## This game has actions preconfigured for two players. The [class Player] scene can be configured
5+
## to respond to inputs for player one or two, or for both players.
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]
42+
43+
var deadzone := maxf(InputMap.action_get_deadzone(p1), InputMap.action_get_deadzone(p2))
44+
InputMap.add_action(both, deadzone)
45+
46+
for event: InputEvent in InputMap.action_get_events(p1) + InputMap.action_get_events(p2):
47+
InputMap.action_add_event(both, event)
48+
49+
50+
## Looks up the full action name for [param player] and [param action].
51+
## [br][br]
52+
## For example, [code]Actions.lookup(Global.Player.TWO, "jump")[/code] returns
53+
## [code]"player_2_jump"[/code].
54+
func lookup(player: Global.Player, action: StringName) -> StringName:
55+
return _PLAYER_ACTIONS[player][action]

scripts/actions.gd.uid

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
uid://vybbwarmer0p

scripts/player.gd

Lines changed: 3 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,6 @@ class_name Player
33
extends 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-
158106
func _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

Comments
 (0)