class_name Hub extends Node2D # User interface to create liners signal hub_finished signal input_list_updated const ACTION_PLAYER_PREFIX = "p" const ACTION_LEFT_SUFFIX = "_left" const ACTION_RIGHT_SUFFIX = "_right" const MAX_PLAYERS = 8 export var format_player_control = "Player %d > left : %s | right : %s" export var format_mouse_button = "MB %s" export (Array, Color) var playerColors = [ Color.red, Color.blue, Color.green, Color.yellow, Color.white, Color.cyan, Color.magenta, Color.orange, ] var _playerScene = preload("res://Scenes/Player.tscn") var _playerActionsLeft var _playerActionsRight var _hub_enabled = true var _input_list_text = ""; func _ready(): _fill_action_arrays() _update_input_list_text() func _unhandled_input(event): if event.is_pressed() and _hub_enabled: if event.is_action("ui_accept"): if get_tree().get_nodes_in_group("players").size() > 0: get_tree().set_input_as_handled() _hub_enabled = false emit_signal("hub_finished") elif event.is_action_type(): for pal in _playerActionsLeft: if event.is_action(pal): # Create player var id = _playerActionsLeft.find(pal) var player player = _playerScene.instance() player.turn_left_action = _playerActionsLeft[id] player.turn_right_action = _playerActionsRight[id] player.position = Vector2(id * 100.0, 0.0) player.modulate = playerColors[id] add_child(player) get_tree().set_input_as_handled() func _fill_action_arrays(): _playerActionsLeft = [] _playerActionsRight = [] for k in MAX_PLAYERS: _playerActionsLeft.append(ACTION_PLAYER_PREFIX + str(k) + ACTION_LEFT_SUFFIX) _playerActionsRight.append(ACTION_PLAYER_PREFIX + str(k) + ACTION_RIGHT_SUFFIX) func _update_input_list_text(): var input_text_array = PoolStringArray() for k in _playerActionsLeft.size(): var values = [k] values.append(_get_label_from_action_name(_playerActionsLeft[k])) values.append(_get_label_from_action_name(_playerActionsRight[k])) input_text_array.append(format_player_control % values) _input_list_text = input_text_array.join("\n") emit_signal("input_list_updated", _input_list_text) func _get_label_from_action_name(action:String): var input_label_array = PoolStringArray() for event in InputMap.get_action_list(action): if event is InputEventMouseButton: input_label_array.append(format_mouse_button % event.button_index) else: input_label_array.append(event.as_text()) return input_label_array.join(", ")