123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- extends Node2D
- # Game orchestrator
- signal round_won
- signal arena_removed
- export (String) var levelToLoop
- export (Array, String) var levelNames = ["Square"]
- var levelSceneToLoop
- var levelScenes = []
- var levelOldNode
- var levelCurrentNode
- var levelIndex = -1
- var is_round_won
- var _liners_connected = false
- onready var cam = $MainCamera
- onready var start_timer = $StartTimer
- onready var levelSwap = $LevelSwap
- onready var hub = $Hub
- onready var ui = $Ui
- func _ready():
- # Prepare levels
- for _k in range(levelNames.size()):
- levelScenes.append(null)
- if levelToLoop:
- levelSceneToLoop = load_level(levelToLoop)
- if !levelSceneToLoop:
- push_error("Cant't load level " + levelToLoop)
- # Connections
- levelSwap.connect("tween_completed", self, "_on_swap_completed")
- hub.connect("hub_finished", self, "_on_hub_finished")
- func _unhandled_input(event):
- if event.is_action("ui_accept") and event.is_pressed():
- if is_round_won:
- is_round_won = false
- create_game()
- get_tree().set_input_as_handled()
- func connect_liners():
- if _liners_connected:
- push_error("Liners are already connected!")
- return
- for liner in get_tree().get_nodes_in_group("liners"):
- start_timer.connect("timeout", liner, "_on_round_start")
- self.connect("round_won", liner, "_on_round_won")
- self.connect("arena_removed", liner, "_on_arena_removed")
- liner.connect("crash", self, "_on_liner_crash")
- _liners_connected = true
- func load_level(name):
- var fullName = "res://Levels/" + name + ".tscn"
- return load(fullName)
- func create_game():
- # Clear if required
- if levelCurrentNode:
- levelOldNode = levelCurrentNode
- levelSwap.swap_out(levelOldNode)
- emit_signal("arena_removed")
- # 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()
- # Put the level far away, the tween will bring it back
- levelCurrentNode.position.y = 2000
- # Manually spawn liners for the first game
- if levelOldNode == null:
- # TODO : remove duplicated code with func _on_swap_completed
- add_child(levelCurrentNode)
- spawn_liners()
- levelSwap.swap_in(levelCurrentNode)
- func spawn_liners():
- var spawners = get_tree().get_nodes_in_group("spawn")
- spawners.shuffle()
- var p = get_tree().get_nodes_in_group("liners")
- for k in p.size():
- p[k].spawn(levelCurrentNode, spawners[k].position, spawners[k].rotation_degrees)
- func _on_liner_crash():
- if get_tree().get_nodes_in_group("running").size() <= 1 and !is_round_won:
- emit_signal("round_won")
- is_round_won = true
- func _on_start_game():
- is_round_won = false
- func _on_swap_completed(object, _k):
- if object == levelOldNode:
- remove_child(levelOldNode)
- levelOldNode.queue_free()
- levelOldNode = null
- add_child(levelCurrentNode)
- spawn_liners()
- levelSwap.swap_in(levelCurrentNode)
- elif object == levelCurrentNode:
- start_timer.start()
- func _on_hub_finished():
- connect_liners()
- create_game()
|