Game.gd 3.1 KB

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