Game.gd 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. func _ready():
  18. for _k in range(levelNames.size()):
  19. levelScenes.append(null)
  20. if levelToLoop:
  21. levelSceneToLoop = load_level(levelToLoop)
  22. if !levelSceneToLoop:
  23. push_error("Cant't load level " + levelToLoop)
  24. # Create players
  25. var player
  26. for k in 3:
  27. player = playerScene.instance()
  28. player.grid = levelCurrentNode
  29. player.turn_left_action = playerActionsLeft[k]
  30. player.turn_right_action = playerActionsRight[k]
  31. player.position = Vector2()
  32. add_child(player)
  33. start_timer.connect("timeout", player, "_on_round_start")
  34. player.connect("crash", self, "_on_player_crash")
  35. self.connect("round_won", player, "_on_round_won")
  36. self.connect("arena_removed", player, "_on_arena_removed")
  37. func load_level(name):
  38. var fullName = "res://Levels/" + name + ".tscn"
  39. return load(fullName)
  40. func _unhandled_input(event):
  41. if event.is_action("ui_accept") and event.is_pressed():
  42. if get_tree().has_group("running"):
  43. var running = get_tree().get_nodes_in_group("running")
  44. if !running.empty():
  45. return
  46. create_game()
  47. func create_game():
  48. # Clear if required
  49. if levelCurrentNode:
  50. remove_child(levelCurrentNode)
  51. levelCurrentNode.queue_free()
  52. emit_signal("arena_removed")
  53. # Create level node
  54. if levelToLoop:
  55. levelCurrentNode = levelSceneToLoop.instance()
  56. else:
  57. # Select next level
  58. levelIndex += 1
  59. if (levelIndex >= levelScenes.size()):
  60. levelIndex = 0
  61. levelScenes.shuffle()
  62. if !levelScenes[levelIndex]:
  63. levelScenes[levelIndex] = load_level(levelNames[levelIndex])
  64. levelCurrentNode = levelScenes[levelIndex].instance()
  65. add_child(levelCurrentNode)
  66. # Allocate spawn points to players
  67. var spawners = get_tree().get_nodes_in_group("spawn")
  68. spawners.shuffle()
  69. var p = get_tree().get_nodes_in_group("players")
  70. for k in p.size():
  71. p[k].spawn(levelCurrentNode, spawners[k].position, spawners[k].rotation_degrees)
  72. # Delay start
  73. start_timer.start()
  74. func _on_player_crash():
  75. if get_tree().get_nodes_in_group("running").size() <= 1 and !is_round_won:
  76. emit_signal("round_won")
  77. is_round_won = true
  78. func _on_start_game():
  79. is_round_won = false