Game.gd 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. 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 connect_players():
  31. if _players_connected:
  32. push_error("Players are already connected!")
  33. return
  34. for player in get_tree().get_nodes_in_group("players"):
  35. start_timer.connect("timeout", player, "_on_round_start")
  36. self.connect("round_won", player, "_on_round_won")
  37. self.connect("arena_removed", player, "_on_arena_removed")
  38. player.connect("crash", self, "_on_player_crash")
  39. _players_connected = true
  40. func load_level(name):
  41. var fullName = "res://Levels/" + name + ".tscn"
  42. return load(fullName)
  43. func _unhandled_input(event):
  44. if event.is_action("ui_accept") and event.is_pressed():
  45. if is_round_won:
  46. is_round_won = false
  47. create_game()
  48. get_tree().set_input_as_handled()
  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 players for the first game
  70. if levelOldNode == null:
  71. add_child(levelCurrentNode)
  72. spawn_players()
  73. levelSwap.swap_in(levelCurrentNode)
  74. func spawn_players():
  75. var spawners = get_tree().get_nodes_in_group("spawn")
  76. spawners.shuffle()
  77. var p = get_tree().get_nodes_in_group("players")
  78. for k in p.size():
  79. p[k].spawn(levelCurrentNode, spawners[k].position, spawners[k].rotation_degrees)
  80. func _on_player_crash():
  81. if get_tree().get_nodes_in_group("running").size() <= 1 and !is_round_won:
  82. emit_signal("round_won")
  83. is_round_won = true
  84. func _on_start_game():
  85. is_round_won = false
  86. func _on_swap_completed(object, _k):
  87. if object == levelOldNode:
  88. remove_child(levelOldNode)
  89. levelOldNode.queue_free()
  90. levelOldNode = null
  91. add_child(levelCurrentNode)
  92. spawn_players()
  93. levelSwap.swap_in(levelCurrentNode)
  94. elif object == levelCurrentNode:
  95. start_timer.start()
  96. func _on_hub_finished():
  97. connect_players()
  98. # hub.remove_and_skip()
  99. hub = null # TODO : add an argument in connect and remove temporary attribute
  100. create_game()