extends Node2D onready var cam = $MainCamera onready var timer = $StartTimer var levelNames = ["res://Levels/Focus.tscn", "res://Levels/Lab.tscn", "res://Levels/Square.tscn"] var levelScenes = [] var levelCurrentNode var levelIndex = -1 var playerScene = preload("res://Player.tscn") var playerActionsLeft = ["p1_left", "p2_left", "p3_left"] var playerActionsRight = ["p1_right", "p2_right", "p3_right"] func _ready(): for k in range(levelNames.size()): levelScenes.append(null) func _unhandled_input(event): if event.is_action("ui_accept") and event.is_pressed(): if get_tree().has_group("living"): var living = get_tree().get_nodes_in_group("living") if !living.empty(): return create_game() func create_game(): # Clear if required if (get_tree().has_group("players")): var players = get_tree().get_nodes_in_group("players") for p in players: p.queue_free() if (levelCurrentNode): remove_child(levelCurrentNode) levelCurrentNode.queue_free() # Select level levelIndex += 1 if (levelIndex >= levelScenes.size()): levelIndex = 0 if !levelScenes[levelIndex]: levelScenes[levelIndex] = load(levelNames[levelIndex]) # Load level levelCurrentNode = levelScenes[levelIndex].instance() add_child(levelCurrentNode) # Retrieve player spawn points var spawners = get_tree().get_nodes_in_group("spawn") # Create players var player for k in 2: player = playerScene.instance() player.grid = levelCurrentNode player.turn_left_action = playerActionsLeft[k] player.turn_right_action = playerActionsRight[k] player.position = spawners[k].position add_child(player) # Delay start timer.start() func _on_game_start(): get_tree().call_group("players", "_on_game_start")