extends Node

func _ready() -> void:
	SceneTransition.fade_in()

func _on_host_pressed() -> void:
	var peer: ENetMultiplayerPeer = ENetMultiplayerPeer.new()
	var error: Error = peer.create_server(1027)
	if error:
		print(error)
	multiplayer.multiplayer_peer = peer
	start_game()

func _on_join_pressed() -> void:
	var peer: ENetMultiplayerPeer = ENetMultiplayerPeer.new()
	var error: Error = peer.create_client("127.0.0.1", 1027)
	if error:
		print(error)
	multiplayer.multiplayer_peer = peer
	start_game()

func start_game() -> void:
	$UI.hide()
	get_tree().paused = false

	if multiplayer.is_server():
		change_level.call_deferred(load("res://src/scenes/levels/level.tscn"))

func change_level(scene: PackedScene) -> void:
	var level: Node = $Level
	for c in level.get_children():
		level.remove_child(c)
		c.queue_free()

	level.add_child(scene.instantiate())
