12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- extends Node2D
- onready var cam = $MainCamera
- onready var start_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)
- # 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 = Vector2()
- add_child(player)
- start_timer.connect("timeout", player, "_on_round_start")
- 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("running"):
- var running = get_tree().get_nodes_in_group("running")
- if !running.empty():
- return
- create_game()
- func create_game():
- # Clear if required
- if levelCurrentNode:
- remove_child(levelCurrentNode)
- levelCurrentNode.queue_free()
- # Create level node
- if levelToLoop:
- levelCurrentNode = levelSceneToLoop.instance()
- else:
- # Select next level
- levelIndex += 1
- if (levelIndex >= levelScenes.size()):
- levelIndex = 0
- levelScenes.shuffle()
- if !levelScenes[levelIndex]:
- levelScenes[levelIndex] = load_level(levelNames[levelIndex])
- levelCurrentNode = levelScenes[levelIndex].instance()
- add_child(levelCurrentNode)
- # Allocate spawn points to players
- var spawners = get_tree().get_nodes_in_group("spawn")
- spawners.shuffle()
- var p = get_tree().get_nodes_in_group("players")
- for k in p.size():
- p[k].spawn(levelCurrentNode, spawners[k].position, spawners[k].rotation_degrees)
- # Delay start
- start_timer.start()
- func _on_round_end():
- get_tree().call_group("players", "_on_round_end")
|