Game.gd 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. extends Node2D
  2. onready var cam = $MainCamera
  3. onready var 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. func load_level(name):
  31. var fullName = "res://Levels/" + name + ".tscn"
  32. return load(fullName)
  33. func _unhandled_input(event):
  34. if event.is_action("ui_accept") and event.is_pressed():
  35. if get_tree().has_group("running"):
  36. var running = get_tree().get_nodes_in_group("running")
  37. if !running.empty():
  38. return
  39. create_game()
  40. func create_game():
  41. # Clear if required
  42. if levelCurrentNode:
  43. remove_child(levelCurrentNode)
  44. levelCurrentNode.queue_free()
  45. # Create level node
  46. if levelToLoop:
  47. levelCurrentNode = levelSceneToLoop.instance()
  48. else:
  49. # Select next level
  50. levelIndex += 1
  51. if (levelIndex >= levelScenes.size()):
  52. levelIndex = 0
  53. levelScenes.shuffle()
  54. if !levelScenes[levelIndex]:
  55. levelScenes[levelIndex] = load_level(levelNames[levelIndex])
  56. levelCurrentNode = levelScenes[levelIndex].instance()
  57. add_child(levelCurrentNode)
  58. # Allocate spawn points to players
  59. var spawners = get_tree().get_nodes_in_group("spawn")
  60. spawners.shuffle()
  61. var p = get_tree().get_nodes_in_group("players")
  62. for k in p.size():
  63. p[k].spawn(levelCurrentNode, spawners[k].position, spawners[k].rotation_degrees)
  64. # Delay start
  65. timer.start()
  66. func _on_round_start():
  67. get_tree().call_group("players", "_on_round_start")
  68. func _on_round_end():
  69. get_tree().call_group("players", "_on_round_end")