Game.gd 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. extends Node2D
  2. onready var cam = $MainCamera
  3. onready var start_timer = $StartTimer
  4. var is_round_won
  5. signal round_won
  6. signal arena_removed
  7. # Used to test a level in creation
  8. export (String) var levelToLoop
  9. var levelSceneToLoop
  10. export (Array, String) var levelNames = ["Square"]
  11. var levelScenes = []
  12. var levelOldNode
  13. var levelCurrentNode
  14. var levelIndex = -1
  15. var _players_connected = false
  16. onready var levelSwap = $LevelSwap
  17. onready var hub = $Hub
  18. func _ready():
  19. # Prepare levels
  20. for _k in range(levelNames.size()):
  21. levelScenes.append(null)
  22. if levelToLoop:
  23. levelSceneToLoop = load_level(levelToLoop)
  24. if !levelSceneToLoop:
  25. push_error("Cant't load level " + levelToLoop)
  26. # Connections
  27. levelSwap.connect("tween_completed", self, "_on_swap_completed")
  28. hub.connect("hub_finished", self, "_on_hub_finished")
  29. func connect_players():
  30. if _players_connected:
  31. push_error("Players are already connected!")
  32. return
  33. for player in get_tree().get_nodes_in_group("players"):
  34. start_timer.connect("timeout", player, "_on_round_start")
  35. self.connect("round_won", player, "_on_round_won")
  36. self.connect("arena_removed", player, "_on_arena_removed")
  37. player.connect("crash", self, "_on_player_crash")
  38. _players_connected = true
  39. func load_level(name):
  40. var fullName = "res://Levels/" + name + ".tscn"
  41. return load(fullName)
  42. func _unhandled_input(event):
  43. if event.is_action("ui_accept") and event.is_pressed():
  44. if is_round_won:
  45. is_round_won = false
  46. create_game()
  47. get_tree().set_input_as_handled()
  48. func create_game():
  49. # Clear if required
  50. if levelCurrentNode:
  51. levelOldNode = levelCurrentNode
  52. levelSwap.swap_out(levelOldNode)
  53. emit_signal("arena_removed")
  54. # Create level node
  55. if levelToLoop:
  56. levelCurrentNode = levelSceneToLoop.instance()
  57. else:
  58. # Select next level
  59. levelIndex += 1
  60. if (levelIndex >= levelScenes.size()):
  61. levelIndex = 0
  62. levelScenes.shuffle()
  63. if !levelScenes[levelIndex]:
  64. levelScenes[levelIndex] = load_level(levelNames[levelIndex])
  65. levelCurrentNode = levelScenes[levelIndex].instance()
  66. # Put the level far away, the tween will bring it back
  67. levelCurrentNode.position.y = 2000
  68. # Manually spawn players for the first game
  69. if levelOldNode == null:
  70. add_child(levelCurrentNode)
  71. spawn_players()
  72. levelSwap.swap_in(levelCurrentNode)
  73. func spawn_players():
  74. var spawners = get_tree().get_nodes_in_group("spawn")
  75. spawners.shuffle()
  76. var p = get_tree().get_nodes_in_group("players")
  77. for k in p.size():
  78. p[k].spawn(levelCurrentNode, spawners[k].position, spawners[k].rotation_degrees)
  79. func _on_player_crash():
  80. if get_tree().get_nodes_in_group("running").size() <= 1 and !is_round_won:
  81. emit_signal("round_won")
  82. is_round_won = true
  83. func _on_start_game():
  84. is_round_won = false
  85. func _on_swap_completed(object, _k):
  86. if object == levelOldNode:
  87. remove_child(levelOldNode)
  88. levelOldNode.queue_free()
  89. levelOldNode = null
  90. add_child(levelCurrentNode)
  91. spawn_players()
  92. levelSwap.swap_in(levelCurrentNode)
  93. elif object == levelCurrentNode:
  94. start_timer.start()
  95. func _on_hub_finished():
  96. connect_players()
  97. # hub.remove_and_skip()
  98. hub = null # TODO : add an argument in connect and remove temporary attribute
  99. create_game()