Hub.gd 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. class_name Hub
  2. extends Node2D
  3. # User interface to create liners
  4. signal hub_finished
  5. const ACTION_PLAYER_PREFIX = "p"
  6. const ACTION_LEFT_SUFFIX = "_left"
  7. const ACTION_RIGHT_SUFFIX = "_right"
  8. const MAX_PLAYERS = 8
  9. export (Array, Color) var playerColors = [
  10. Color.red,
  11. Color.blue,
  12. Color.green,
  13. Color.yellow,
  14. Color.white,
  15. Color.cyan,
  16. Color.magenta,
  17. Color.orange,
  18. ]
  19. var _playerScene = preload("res://Scenes/Player.tscn")
  20. var _playerActionsLeft
  21. var _playerActionsRight
  22. var _hub_enabled = true
  23. func _ready():
  24. _fill_action_arrays()
  25. func _unhandled_input(event):
  26. if event.is_pressed() and _hub_enabled:
  27. if event.is_action("ui_accept"):
  28. if get_tree().get_nodes_in_group("players").size() > 0:
  29. get_tree().set_input_as_handled()
  30. _hub_enabled = false
  31. emit_signal("hub_finished")
  32. elif event.is_action_type():
  33. for pal in _playerActionsLeft:
  34. if event.is_action(pal):
  35. # Create player
  36. var id = _playerActionsLeft.find(pal)
  37. var player
  38. player = _playerScene.instance()
  39. player.turn_left_action = _playerActionsLeft[id]
  40. player.turn_right_action = _playerActionsRight[id]
  41. player.position = Vector2(id * 100.0, 0.0)
  42. player.modulate = playerColors[id]
  43. add_child(player)
  44. get_tree().set_input_as_handled()
  45. func _fill_action_arrays():
  46. _playerActionsLeft = []
  47. _playerActionsRight = []
  48. for k in MAX_PLAYERS:
  49. _playerActionsLeft.append(ACTION_PLAYER_PREFIX + str(k) + ACTION_LEFT_SUFFIX)
  50. _playerActionsRight.append(ACTION_PLAYER_PREFIX + str(k) + ACTION_RIGHT_SUFFIX)