Game.gd 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. extends Node2D
  2. onready var cam = $MainCamera
  3. onready var start_timer = $StartTimer
  4. # Used to test a level in creation
  5. export (String) var levelToLoop
  6. var levelSceneToLoop
  7. export (Array, String) var levelNames = ["Square"]
  8. var levelScenes = []
  9. var levelCurrentNode
  10. var levelIndex = -1
  11. var playerScene = preload("res://Player.tscn")
  12. var playerActionsLeft = ["p1_left", "p2_left", "p3_left"]
  13. var playerActionsRight = ["p1_right", "p2_right", "p3_right"]
  14. func _ready():
  15. for _k in range(levelNames.size()):
  16. levelScenes.append(null)
  17. if levelToLoop:
  18. levelSceneToLoop = load_level(levelToLoop)
  19. if !levelSceneToLoop:
  20. push_error("Cant't load level " + levelToLoop)
  21. # Create players
  22. var player
  23. for k in 2:
  24. player = playerScene.instance()
  25. player.grid = levelCurrentNode
  26. player.turn_left_action = playerActionsLeft[k]
  27. player.turn_right_action = playerActionsRight[k]
  28. player.position = Vector2()
  29. add_child(player)
  30. start_timer.connect("timeout", player, "_on_round_start")
  31. func load_level(name):
  32. var fullName = "res://Levels/" + name + ".tscn"
  33. return load(fullName)
  34. func _unhandled_input(event):
  35. if event.is_action("ui_accept") and event.is_pressed():
  36. if get_tree().has_group("running"):
  37. var running = get_tree().get_nodes_in_group("running")
  38. if !running.empty():
  39. return
  40. create_game()
  41. func create_game():
  42. # Clear if required
  43. if levelCurrentNode:
  44. remove_child(levelCurrentNode)
  45. levelCurrentNode.queue_free()
  46. # Create level node
  47. if levelToLoop:
  48. levelCurrentNode = levelSceneToLoop.instance()
  49. else:
  50. # Select next level
  51. levelIndex += 1
  52. if (levelIndex >= levelScenes.size()):
  53. levelIndex = 0
  54. levelScenes.shuffle()
  55. if !levelScenes[levelIndex]:
  56. levelScenes[levelIndex] = load_level(levelNames[levelIndex])
  57. levelCurrentNode = levelScenes[levelIndex].instance()
  58. add_child(levelCurrentNode)
  59. # Allocate spawn points to players
  60. var spawners = get_tree().get_nodes_in_group("spawn")
  61. spawners.shuffle()
  62. var p = get_tree().get_nodes_in_group("players")
  63. for k in p.size():
  64. p[k].spawn(levelCurrentNode, spawners[k].position, spawners[k].rotation_degrees)
  65. # Delay start
  66. start_timer.start()
  67. func _on_round_end():
  68. get_tree().call_group("players", "_on_round_end")