12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- class_name Hub
- extends Node2D
- # User interface to create liners
- signal hub_finished
- signal input_list_updated
- const Liner = preload("res://Scenes/Liner.tscn")
- 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 linerColors = [
- Color.red,
- Color.blue,
- Color.green,
- Color.yellow,
- Color.white,
- Color.cyan,
- Color.magenta,
- Color.orange,
- ]
- 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("liners").size() > 0:
- get_tree().set_input_as_handled()
- _hub_enabled = false
- emit_signal("hub_finished")
- elif event.is_action_type():
- for player_action in _playerActionsLeft:
- if event.is_action(player_action):
- # Create liner player
- var id = _playerActionsLeft.find(player_action)
- var player := Liner.instance()
- player.turn_left_action = _playerActionsLeft[id]
- player.turn_right_action = _playerActionsRight[id]
- player.position = Vector2(id * 100.0, 0.0)
- player.modulate = linerColors[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(", ")
|