129 lines
3.2 KiB
GDScript
129 lines
3.2 KiB
GDScript
extends Node
|
|
|
|
var allow_joins = true
|
|
var players: Dictionary[int, PlayerInputData]
|
|
|
|
const DEADZONE = 0.2
|
|
|
|
signal player_join(data: PlayerInputData)
|
|
|
|
func _ready() -> void:
|
|
Input.joy_connection_changed.connect(joy_connection_changed)
|
|
|
|
@warning_ignore("unused_parameter")
|
|
func joy_connection_changed(device: int, connected: bool):
|
|
print("Connected ", device)
|
|
|
|
|
|
func move_input(player: PlayerInputData, event: InputEvent, action: String):
|
|
var strength := 0.0
|
|
if event is InputEventJoypadMotion:
|
|
strength = abs(event.axis_value)
|
|
if strength <= DEADZONE:
|
|
strength = 0.0
|
|
if event.axis_value < 0.0:
|
|
action = action.replace("down", "up").replace("right", "left")
|
|
if action == "move_up" or action == "move_down":
|
|
if event.axis_value < 0.0:
|
|
player.raw_up = strength
|
|
player.raw_down = 0.0
|
|
else:
|
|
player.raw_down = strength
|
|
player.raw_up = 0.0
|
|
elif action == "move_right" or action == "move_left":
|
|
if event.axis_value < 0.0:
|
|
player.raw_left = strength
|
|
player.raw_right = 0.0
|
|
else:
|
|
player.raw_right = strength
|
|
player.raw_left = 0.0
|
|
elif event is InputEventKey:
|
|
strength = float(event.pressed)
|
|
if action == "move_up":
|
|
player.raw_up = strength
|
|
elif action == "move_right":
|
|
player.raw_right = strength
|
|
elif action == "move_down":
|
|
player.raw_down = strength
|
|
elif action == "move_left":
|
|
player.raw_left = strength
|
|
|
|
player.walk_dir = Vector3(
|
|
player.raw_right - player.raw_left,
|
|
0.0,
|
|
player.raw_down - player.raw_up,
|
|
)
|
|
|
|
func game_input(player: PlayerInputData, event: InputEvent):
|
|
if not event.is_action_type():
|
|
return
|
|
var action := get_action_name_all(event)
|
|
if action.contains("move"):
|
|
move_input(player, event, action)
|
|
if action == "jump":
|
|
player.raw_jump = event.is_pressed()
|
|
if action == "dig":
|
|
player.raw_dig = event.is_pressed()
|
|
|
|
player.jumping = player.raw_jump
|
|
player.digging = player.raw_dig
|
|
|
|
|
|
|
|
func join(who: int):
|
|
var new_player := PlayerInputData.new()
|
|
players[who] = new_player
|
|
player_join.emit(new_player)
|
|
|
|
func drop(who: int):
|
|
players[who].drop.emit()
|
|
players.erase(who)
|
|
|
|
|
|
func get_action_name_all(event: InputEvent) -> String:
|
|
return get_action_name(event).replace("_alt", "")
|
|
|
|
func get_action_name(event: InputEvent) -> String:
|
|
if event.is_echo():
|
|
return ""
|
|
if event.is_action("dig"):
|
|
return "dig"
|
|
if event.is_action("jump"):
|
|
return "jump"
|
|
if event.is_action("dig_alt"):
|
|
return "dig_alt"
|
|
if event.is_action("jump_alt"):
|
|
return "jump_alt"
|
|
if event.is_action("move_up"):
|
|
return "move_up"
|
|
if event.is_action("move_right"):
|
|
return "move_right"
|
|
if event.is_action("move_down"):
|
|
return "move_down"
|
|
if event.is_action("move_left"):
|
|
return "move_left"
|
|
if event.is_action("move_up_alt"):
|
|
return "move_up_alt"
|
|
if event.is_action("move_right_alt"):
|
|
return "move_right_alt"
|
|
if event.is_action("move_down_alt"):
|
|
return "move_down_alt"
|
|
if event.is_action("move_left_alt"):
|
|
return "move_left_alt"
|
|
return ""
|
|
|
|
func _input(event: InputEvent) -> void:
|
|
if not event.is_action_type():
|
|
return
|
|
var who = event.device
|
|
if get_action_name(event).contains("_alt"):
|
|
who = -2
|
|
elif event is InputEventKey:
|
|
who = -3
|
|
|
|
|
|
if players.has(who):
|
|
game_input(players[who], event)
|
|
elif allow_joins and get_action_name(event):
|
|
join(who)
|
|
|