@@ -14,21 +14,49 @@ 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
}
30
+ ]
31
+
32
+ const PLAYER_JOYSTICK_BUTTONS = {
33
+ "up" : JOY_BUTTON_DPAD_UP ,
34
+ "down" : JOY_BUTTON_DPAD_DOWN ,
35
+ "left" : JOY_BUTTON_DPAD_LEFT ,
36
+ "right" : JOY_BUTTON_DPAD_RIGHT ,
37
+ }
38
+
39
+ const PLAYER_JOYSTICK_MOTION = {
40
+ "up" :
41
+ {
42
+ "axis" : JOY_AXIS_LEFT_Y ,
43
+ "axis_value" : - 1 ,
44
+ },
45
+ "down" :
46
+ {
47
+ "axis" : JOY_AXIS_LEFT_Y ,
48
+ "axis_value" : 1 ,
49
+ },
50
+ "left" :
51
+ {
52
+ "axis" : JOY_AXIS_LEFT_X ,
53
+ "axis_value" : - 1 ,
54
+ },
55
+ "right" :
56
+ {
57
+ "axis" : JOY_AXIS_LEFT_X ,
58
+ "axis_value" : 1 ,
59
+ },
32
60
}
33
61
34
62
var sprite : Sprite2D
@@ -83,24 +111,45 @@ func _ready():
83
111
84
112
func simple_setup ():
85
113
add_to_group ("affected_by_gravity" , true )
114
+ _setup_actions ()
86
115
_texture_updated ()
87
116
88
117
89
- func get_custom_class ():
90
- return "SimpleCharacter"
118
+ func _setup_actions ():
119
+ if Engine .is_editor_hint () or InputMap .has_action ("player_1_left" ):
120
+ return
121
+
122
+ for i in PLAYER_KEYS .size ():
123
+ for action in PLAYER_KEYS [i ]:
124
+ var player = "player_%d " % [i + 1 ]
125
+ var action_name = player + "_" + action
126
+ InputMap .add_action (action_name )
91
127
128
+ # keyboard event
129
+ var e = InputEventKey .new ()
130
+ e .physical_keycode = PLAYER_KEYS [i ][action ]
131
+ InputMap .action_add_event (action_name , e )
92
132
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
133
+ # controller d-pad event
134
+ var ej = InputEventJoypadButton .new ()
135
+ ej .device = i
136
+ ej .button_index = PLAYER_JOYSTICK_BUTTONS [action ]
137
+ InputMap .action_add_event (action_name , ej )
138
+
139
+ # controller left stick event
140
+ var ejm = InputEventJoypadMotion .new ()
141
+ ejm .device = i
142
+ ejm .axis = PLAYER_JOYSTICK_MOTION [action ]["axis" ]
143
+ ejm .axis_value = PLAYER_JOYSTICK_MOTION [action ]["axis_value" ]
144
+ InputMap .action_add_event (action_name , ejm )
145
+
146
+
147
+ func get_custom_class ():
148
+ return "SimpleCharacter"
100
149
101
150
102
151
func move_with_player_buttons (player : String , kind : String , delta : float ):
103
- var direction = _player_input_to_direction (player )
152
+ var direction = Input . get_vector (player + "_left" , player + "_right" , player + "_up" , player + "_down" )
104
153
direction_x = direction .x
105
154
106
155
if kind == "top-down" :
@@ -111,7 +160,7 @@ func move_with_player_buttons(player: String, kind: String, delta: float):
111
160
if not is_on_floor ():
112
161
velocity .y += gravity * delta
113
162
else :
114
- if not _jumping and Input . is_physical_key_pressed ( PLAYER_KEYS [ player ][ "up" ]) :
163
+ if not _jumping and direction . y < 0 :
115
164
_jumping = true
116
165
velocity .y -= speed .y
117
166
else :
0 commit comments