Hub.gd 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. class_name Hub
  2. extends Node2D
  3. # User interface to create liners
  4. signal hub_finished
  5. signal input_list_updated
  6. const Liner = preload("res://Scenes/Liner.tscn")
  7. const ACTION_PLAYER_PREFIX = "p"
  8. const ACTION_LEFT_SUFFIX = "_left"
  9. const ACTION_RIGHT_SUFFIX = "_right"
  10. const MAX_PLAYERS = 8
  11. export var format_player_control = "Player %d > left : %s | right : %s"
  12. export var format_mouse_button = "MB %s"
  13. export (Array, Color) var linerColors = [
  14. Color.red,
  15. Color.blue,
  16. Color.green,
  17. Color.yellow,
  18. Color.white,
  19. Color.cyan,
  20. Color.magenta,
  21. Color.orange,
  22. ]
  23. var _playerActionsLeft
  24. var _playerActionsRight
  25. var _hub_enabled = true
  26. var _input_list_text = "";
  27. func _ready():
  28. _fill_action_arrays()
  29. _update_input_list_text()
  30. func _unhandled_input(event):
  31. if event.is_pressed() and _hub_enabled:
  32. if event.is_action("ui_accept"):
  33. if get_tree().get_nodes_in_group("liners").size() > 0:
  34. get_tree().set_input_as_handled()
  35. _hub_enabled = false
  36. emit_signal("hub_finished")
  37. elif event.is_action_type():
  38. for player_action in _playerActionsLeft:
  39. if event.is_action(player_action):
  40. # Create liner player
  41. var id = _playerActionsLeft.find(player_action)
  42. var player := Liner.instance()
  43. player.turn_left_action = _playerActionsLeft[id]
  44. player.turn_right_action = _playerActionsRight[id]
  45. player.position = Vector2(id * 100.0, 0.0)
  46. player.modulate = linerColors[id]
  47. add_child(player)
  48. get_tree().set_input_as_handled()
  49. func _fill_action_arrays():
  50. _playerActionsLeft = []
  51. _playerActionsRight = []
  52. for k in MAX_PLAYERS:
  53. _playerActionsLeft.append(ACTION_PLAYER_PREFIX + str(k) + ACTION_LEFT_SUFFIX)
  54. _playerActionsRight.append(ACTION_PLAYER_PREFIX + str(k) + ACTION_RIGHT_SUFFIX)
  55. func _update_input_list_text():
  56. var input_text_array = PoolStringArray()
  57. for k in _playerActionsLeft.size():
  58. var values = [k]
  59. values.append(_get_label_from_action_name(_playerActionsLeft[k]))
  60. values.append(_get_label_from_action_name(_playerActionsRight[k]))
  61. input_text_array.append(format_player_control % values)
  62. _input_list_text = input_text_array.join("\n")
  63. emit_signal("input_list_updated", _input_list_text)
  64. func _get_label_from_action_name(action:String):
  65. var input_label_array = PoolStringArray()
  66. for event in InputMap.get_action_list(action):
  67. if event is InputEventMouseButton:
  68. input_label_array.append(format_mouse_button % event.button_index)
  69. else:
  70. input_label_array.append(event.as_text())
  71. return input_label_array.join(", ")