Game.gd 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 levelCurrentNode
  13. var levelIndex = -1
  14. var playerScene = preload("res://Player.tscn")
  15. var playerActionsLeft = ["p1_left", "p2_left", "p3_left"]
  16. var playerActionsRight = ["p1_right", "p2_right", "p3_right"]
  17. export (Array, Color) var playerColors = [Color.red, Color.blue, Color.green, Color.yellow, Color.white, Color.cyan, Color.magenta, Color.orange]
  18. func _ready():
  19. for _k in range(levelNames.size()):
  20. levelScenes.append(null)
  21. if levelToLoop:
  22. levelSceneToLoop = load_level(levelToLoop)
  23. if !levelSceneToLoop:
  24. push_error("Cant't load level " + levelToLoop)
  25. # Create players
  26. var player
  27. for k in 3:
  28. player = playerScene.instance()
  29. player.grid = levelCurrentNode
  30. player.turn_left_action = playerActionsLeft[k]
  31. player.turn_right_action = playerActionsRight[k]
  32. player.position = Vector2()
  33. player.modulate = playerColors[k]
  34. add_child(player)
  35. start_timer.connect("timeout", player, "_on_round_start")
  36. player.connect("crash", self, "_on_player_crash")
  37. self.connect("round_won", player, "_on_round_won")
  38. self.connect("arena_removed", player, "_on_arena_removed")
  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 get_tree().has_group("running"):
  45. var running = get_tree().get_nodes_in_group("running")
  46. if !running.empty():
  47. return
  48. create_game()
  49. func create_game():
  50. # Clear if required
  51. if levelCurrentNode:
  52. remove_child(levelCurrentNode)
  53. levelCurrentNode.queue_free()
  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. add_child(levelCurrentNode)
  68. # Allocate spawn points to players
  69. var spawners = get_tree().get_nodes_in_group("spawn")
  70. spawners.shuffle()
  71. var p = get_tree().get_nodes_in_group("players")
  72. for k in p.size():
  73. p[k].spawn(levelCurrentNode, spawners[k].position, spawners[k].rotation_degrees)
  74. # Delay start
  75. start_timer.start()
  76. func _on_player_crash():
  77. if get_tree().get_nodes_in_group("running").size() <= 1 and !is_round_won:
  78. emit_signal("round_won")
  79. is_round_won = true
  80. func _on_start_game():
  81. is_round_won = false