Skip to content

Commit

Permalink
fixed platformer code and changed PLAYER_KEYS from dict to array
Browse files Browse the repository at this point in the history
  • Loading branch information
urbit-pilled committed Dec 17, 2024
1 parent dafe38e commit fd7db2b
Showing 1 changed file with 9 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,20 @@ const Types = preload("res://addons/block_code/types/types.gd")

@export var speed: Vector2 = Vector2(300, 300)

const PLAYER_KEYS = {
"player_1":
const PLAYER_KEYS = [
{
"up": KEY_W,
"down": KEY_S,
"left": KEY_A,
"right": KEY_D,
},
"player_2":
{
"up": KEY_UP,
"down": KEY_DOWN,
"left": KEY_LEFT,
"right": KEY_RIGHT,
}
}
]

const PLAYER_JOYSTICK_BUTTONS = {
"up": JOY_BUTTON_DPAD_UP,
Expand Down Expand Up @@ -121,27 +119,26 @@ func _setup_actions():
if Engine.is_editor_hint() or InputMap.has_action("player_1_left"):
return

for player in PLAYER_KEYS:
for action in PLAYER_KEYS[player]:
for i in PLAYER_KEYS.size():
for action in PLAYER_KEYS[i]:
var player = "player_%d" % [i + 1]
var action_name = player + "_" + action
InputMap.add_action(action_name)

var device = int(player.split("_")[-1]) - 1

#keyboard event
var e = InputEventKey.new()
e.physical_keycode = PLAYER_KEYS[player][action]
e.physical_keycode = PLAYER_KEYS[i][action]
InputMap.action_add_event(action_name, e)

#controller d-pad event
var ej = InputEventJoypadButton.new()
ej.device = device
ej.device = i
ej.button_index = PLAYER_JOYSTICK_BUTTONS[action]
InputMap.action_add_event(action_name, ej)

#controller left stick event
var ejm = InputEventJoypadMotion.new()
ejm.device = device
ejm.device = i
ejm.axis = PLAYER_JOYSTICK_MOTION[action]["axis"]
ejm.axis_value = PLAYER_JOYSTICK_MOTION[action]["axis_value"]
InputMap.action_add_event(action_name, ejm)
Expand All @@ -163,7 +160,7 @@ func move_with_player_buttons(player: String, kind: String, delta: float):
if not is_on_floor():
velocity.y += gravity * delta
else:
if not _jumping and Input.is_physical_key_pressed(PLAYER_KEYS[player]["up"]):
if not _jumping and direction.y < 0:
_jumping = true
velocity.y -= speed.y
else:
Expand Down

0 comments on commit fd7db2b

Please sign in to comment.