Hub.gd 2.4 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 ACTION_PLAYER_PREFIX = "p"
  7. const ACTION_LEFT_SUFFIX = "_left"
  8. const ACTION_RIGHT_SUFFIX = "_right"
  9. const MAX_PLAYERS = 8
  10. export var format_player_control = "Player %d > left : %s | right : %s"
  11. export var format_mouse_button = "MB %s"
  12. export (Array, Color) var playerColors = [
  13. Color.red,
  14. Color.blue,
  15. Color.green,
  16. Color.yellow,
  17. Color.white,
  18. Color.cyan,
  19. Color.magenta,
  20. Color.orange,
  21. ]
  22. var _playerScene = preload("res://Scenes/Player.tscn")
  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("players").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 pal in _playerActionsLeft:
  39. if event.is_action(pal):
  40. # Create player
  41. var id = _playerActionsLeft.find(pal)
  42. var player
  43. player = _playerScene.instance()
  44. player.turn_left_action = _playerActionsLeft[id]
  45. player.turn_right_action = _playerActionsRight[id]
  46. player.position = Vector2(id * 100.0, 0.0)
  47. player.modulate = playerColors[id]
  48. add_child(player)
  49. get_tree().set_input_as_handled()
  50. func _fill_action_arrays():
  51. _playerActionsLeft = []
  52. _playerActionsRight = []
  53. for k in MAX_PLAYERS:
  54. _playerActionsLeft.append(ACTION_PLAYER_PREFIX + str(k) + ACTION_LEFT_SUFFIX)
  55. _playerActionsRight.append(ACTION_PLAYER_PREFIX + str(k) + ACTION_RIGHT_SUFFIX)
  56. func _update_input_list_text():
  57. var input_text_array = PoolStringArray()
  58. for k in _playerActionsLeft.size():
  59. var values = [k]
  60. values.append(_get_label_from_action_name(_playerActionsLeft[k]))
  61. values.append(_get_label_from_action_name(_playerActionsRight[k]))
  62. input_text_array.append(format_player_control % values)
  63. _input_list_text = input_text_array.join("\n")
  64. emit_signal("input_list_updated", _input_list_text)
  65. func _get_label_from_action_name(action:String):
  66. var input_label_array = PoolStringArray()
  67. for event in InputMap.get_action_list(action):
  68. if event is InputEventMouseButton:
  69. input_label_array.append(format_mouse_button % event.button_index)
  70. else:
  71. input_label_array.append(event.as_text())
  72. return input_label_array.join(", ")