extends Node2D onready var cam = $MainCamera onready var timer = $StartTimer # Used to test a level in creation export (String) var levelToLoop var levelSceneToLoop export (Array, String) var levelNames = ["Square"] 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) if levelToLoop: levelSceneToLoop = load_level(levelToLoop) if !levelSceneToLoop: push_error("Cant't load level " + levelToLoop) func load_level(name): var fullName = "res://Levels/" + name + ".tscn" return load(fullName) 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() if levelToLoop: levelCurrentNode = levelSceneToLoop.instance() else: # Select next level levelIndex += 1 if (levelIndex >= levelScenes.size()): levelIndex = 0 if !levelScenes[levelIndex]: levelScenes[levelIndex] = load_level(levelNames[levelIndex]) 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 player.rotation = spawners[k].rotation add_child(player) # Delay start timer.start() func _on_game_start(): get_tree().call_group("players", "_on_game_start")