123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- extends Node2D
- onready var cam = $MainCamera
- onready var start_timer = $StartTimer
- var is_round_won
- signal round_won
- signal arena_removed
- # Used to test a level in creation
- export (String) var levelToLoop
- var levelSceneToLoop
- export (Array, String) var levelNames = ["Square"]
- var levelScenes = []
- var levelOldNode
- var levelCurrentNode
- var levelIndex = -1
- onready var levelSwap = $LevelSwap
- var playerScene = preload("res://Player.tscn")
- var playerActionsLeft = ["p1_left", "p2_left", "p3_left"]
- var playerActionsRight = ["p1_right", "p2_right", "p3_right"]
- export (Array, Color) var playerColors = [Color.red, Color.blue, Color.green, Color.yellow, Color.white, Color.cyan, Color.magenta, Color.orange]
- func _ready():
- # Prepare levels
- for _k in range(levelNames.size()):
- levelScenes.append(null)
- levelSwap.connect("tween_completed", self, "_on_swap_completed")
- if levelToLoop:
- levelSceneToLoop = load_level(levelToLoop)
- if !levelSceneToLoop:
- push_error("Cant't load level " + levelToLoop)
- # Create players
- var player
- for k in 3:
- player = playerScene.instance()
- player.grid = levelCurrentNode
- player.turn_left_action = playerActionsLeft[k]
- player.turn_right_action = playerActionsRight[k]
- player.position = Vector2()
- player.modulate = playerColors[k]
- add_child(player)
- start_timer.connect("timeout", player, "_on_round_start")
- player.connect("crash", self, "_on_player_crash")
- self.connect("round_won", player, "_on_round_won")
- self.connect("arena_removed", player, "_on_arena_removed")
- 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 is_round_won == null or is_round_won:
- is_round_won = false
- create_game()
- 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 players for the first game
- if levelOldNode == null:
- add_child(levelCurrentNode)
- spawn_players()
- levelSwap.swap_in(levelCurrentNode)
- func spawn_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)
- func _on_player_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_players()
- levelSwap.swap_in(levelCurrentNode)
- elif object == levelCurrentNode:
- start_timer.start()
|