Game.gd 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. extends Node2D
  2. onready var cam = $MainCamera
  3. onready var timer = $StartTimer
  4. var levelNames = ["res://Levels/Focus.tscn", "res://Levels/Lab.tscn", "res://Levels/Square.tscn"]
  5. var levelScenes = []
  6. var levelCurrentNode
  7. var levelIndex = -1
  8. var playerScene = preload("res://Player.tscn")
  9. var playerActionsLeft = ["p1_left", "p2_left", "p3_left"]
  10. var playerActionsRight = ["p1_right", "p2_right", "p3_right"]
  11. func _ready():
  12. for k in range(levelNames.size()):
  13. levelScenes.append(null)
  14. func _unhandled_input(event):
  15. if event.is_action("ui_accept") and event.is_pressed():
  16. if get_tree().has_group("living"):
  17. var living = get_tree().get_nodes_in_group("living")
  18. if !living.empty():
  19. return
  20. create_game()
  21. func create_game():
  22. # Clear if required
  23. if (get_tree().has_group("players")):
  24. var players = get_tree().get_nodes_in_group("players")
  25. for p in players:
  26. p.queue_free()
  27. if (levelCurrentNode):
  28. remove_child(levelCurrentNode)
  29. levelCurrentNode.queue_free()
  30. # Select level
  31. levelIndex += 1
  32. if (levelIndex >= levelScenes.size()):
  33. levelIndex = 0
  34. if !levelScenes[levelIndex]:
  35. levelScenes[levelIndex] = load(levelNames[levelIndex])
  36. # Load level
  37. levelCurrentNode = levelScenes[levelIndex].instance()
  38. add_child(levelCurrentNode)
  39. # Retrieve player spawn points
  40. var spawners = get_tree().get_nodes_in_group("spawn")
  41. # Create players
  42. var player
  43. for k in 2:
  44. player = playerScene.instance()
  45. player.grid = levelCurrentNode
  46. player.turn_left_action = playerActionsLeft[k]
  47. player.turn_right_action = playerActionsRight[k]
  48. player.position = spawners[k].position
  49. add_child(player)
  50. # Delay start
  51. timer.start()
  52. func _on_game_start():
  53. get_tree().call_group("players", "_on_game_start")