@@ -14,22 +14,20 @@ const Types = preload("res://addons/block_code/types/types.gd")
14
14
15
15
@export var speed : Vector2 = Vector2 (300 , 300 )
16
16
17
- const PLAYER_KEYS = {
18
- "player_1" :
17
+ const PLAYER_KEYS = [
19
18
{
20
19
"up" : KEY_W ,
21
20
"down" : KEY_S ,
22
21
"left" : KEY_A ,
23
22
"right" : KEY_D ,
24
23
},
25
- "player_2" :
26
24
{
27
25
"up" : KEY_UP ,
28
26
"down" : KEY_DOWN ,
29
27
"left" : KEY_LEFT ,
30
28
"right" : KEY_RIGHT ,
31
29
}
32
- }
30
+ ]
33
31
34
32
var sprite : Sprite2D
35
33
var collision : CollisionShape2D
@@ -83,24 +81,32 @@ func _ready():
83
81
84
82
func simple_setup ():
85
83
add_to_group ("affected_by_gravity" , true )
84
+ _setup_actions ()
86
85
_texture_updated ()
87
86
88
87
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 )
91
97
98
+ # keyboard event
99
+ var e = InputEventKey .new ()
100
+ e .physical_keycode = PLAYER_KEYS [i ][action ]
101
+ InputMap .action_add_event (action_name , e )
92
102
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"
100
106
101
107
102
108
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" )
104
110
direction_x = direction .x
105
111
106
112
if kind == "top-down" :
@@ -111,7 +117,7 @@ func move_with_player_buttons(player: String, kind: String, delta: float):
111
117
if not is_on_floor ():
112
118
velocity .y += gravity * delta
113
119
else :
114
- if not _jumping and Input . is_physical_key_pressed ( PLAYER_KEYS [ player ][ "up" ]) :
120
+ if not _jumping and direction . y < 0 :
115
121
_jumping = true
116
122
velocity .y -= speed .y
117
123
else :
0 commit comments