Game.gd 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. onready var levelSwap = $LevelSwap
  16. var playerScene = preload("res://Player.tscn")
  17. var playerActionsLeft = ["p1_left", "p2_left", "p3_left"]
  18. var playerActionsRight = ["p1_right", "p2_right", "p3_right"]
  19. export (Array, Color) var playerColors = [Color.red, Color.blue, Color.green, Color.yellow, Color.white, Color.cyan, Color.magenta, Color.orange]
  20. func _ready():
  21. # Prepare levels
  22. for _k in range(levelNames.size()):
  23. levelScenes.append(null)
  24. levelSwap.connect("tween_completed", self, "_on_swap_completed")
  25. if levelToLoop:
  26. levelSceneToLoop = load_level(levelToLoop)
  27. if !levelSceneToLoop:
  28. push_error("Cant't load level " + levelToLoop)
  29. # Create players
  30. var player
  31. for k in 3:
  32. player = playerScene.instance()
  33. player.grid = levelCurrentNode
  34. player.turn_left_action = playerActionsLeft[k]
  35. player.turn_right_action = playerActionsRight[k]
  36. player.position = Vector2()
  37. player.modulate = playerColors[k]
  38. add_child(player)
  39. start_timer.connect("timeout", player, "_on_round_start")
  40. player.connect("crash", self, "_on_player_crash")
  41. self.connect("round_won", player, "_on_round_won")
  42. self.connect("arena_removed", player, "_on_arena_removed")
  43. func load_level(name):
  44. var fullName = "res://Levels/" + name + ".tscn"
  45. return load(fullName)
  46. func _unhandled_input(event):
  47. if event.is_action("ui_accept") and event.is_pressed():
  48. if is_round_won == null or is_round_won:
  49. is_round_won = false
  50. create_game()
  51. func create_game():
  52. # Clear if required
  53. if levelCurrentNode:
  54. levelOldNode = levelCurrentNode
  55. levelSwap.swap_out(levelOldNode)
  56. emit_signal("arena_removed")
  57. # Create level node
  58. if levelToLoop:
  59. levelCurrentNode = levelSceneToLoop.instance()
  60. else:
  61. # Select next level
  62. levelIndex += 1
  63. if (levelIndex >= levelScenes.size()):
  64. levelIndex = 0
  65. levelScenes.shuffle()
  66. if !levelScenes[levelIndex]:
  67. levelScenes[levelIndex] = load_level(levelNames[levelIndex])
  68. levelCurrentNode = levelScenes[levelIndex].instance()
  69. # Put the level far away, the tween will bring it back
  70. levelCurrentNode.position.y = 2000
  71. # Manually spawn players for the first game
  72. if levelOldNode == null:
  73. add_child(levelCurrentNode)
  74. spawn_players()
  75. levelSwap.swap_in(levelCurrentNode)
  76. func spawn_players():
  77. var spawners = get_tree().get_nodes_in_group("spawn")
  78. spawners.shuffle()
  79. var p = get_tree().get_nodes_in_group("players")
  80. for k in p.size():
  81. p[k].spawn(levelCurrentNode, spawners[k].position, spawners[k].rotation_degrees)
  82. func _on_player_crash():
  83. if get_tree().get_nodes_in_group("running").size() <= 1 and !is_round_won:
  84. emit_signal("round_won")
  85. is_round_won = true
  86. func _on_start_game():
  87. is_round_won = false
  88. func _on_swap_completed(object, _k):
  89. if object == levelOldNode:
  90. remove_child(levelOldNode)
  91. levelOldNode.queue_free()
  92. levelOldNode = null
  93. add_child(levelCurrentNode)
  94. spawn_players()
  95. levelSwap.swap_in(levelCurrentNode)
  96. elif object == levelCurrentNode:
  97. start_timer.start()