65 lines
2 KiB
GDScript
65 lines
2 KiB
GDScript
extends Node
|
|
|
|
var has_opponent: bool = false
|
|
@onready var start_game_button = $LobbyInfo/PanelContainer/VBoxContainer/StartGameButton
|
|
@onready var game = $Game
|
|
|
|
var game_in_progress: bool = false
|
|
|
|
func _ready():
|
|
multiplayer.connected_to_server.connect(_mp_server_connected)
|
|
multiplayer.connection_failed.connect(_mp_server_disconnect)
|
|
multiplayer.server_disconnected.connect(_mp_server_disconnect)
|
|
multiplayer.peer_connected.connect(_mp_peer_connected)
|
|
multiplayer.peer_disconnected.connect(_mp_peer_disconnected)
|
|
|
|
func _mp_peer_disconnected(id: int) -> void:
|
|
has_opponent = false
|
|
start_game_button.disabled = true
|
|
|
|
if game_in_progress or id == 1:
|
|
GameData.disconnect_reason = "Other peer terminated the connection"
|
|
# Display message that other end terminated the connection
|
|
# 1 is always the server
|
|
# So we better get going back to the main menu?
|
|
WSClient.close()
|
|
get_tree().change_scene_to_file("res://main_menu.tscn")
|
|
# Maybe just display a "server close
|
|
return
|
|
|
|
func _log(msg):
|
|
print(msg)
|
|
|
|
func _mp_server_connected():
|
|
_log("[Multiplayer] Server connected (I am %d)" % WSClient.rtc_mp.get_unique_id())
|
|
#pass_initial_playerdata.rpc_id(1, multiplayer.get_unique_id(), GameState.player_name)
|
|
|
|
func _mp_server_disconnect():
|
|
_log("[Multiplayer] Server disconnected (I am %d)" % WSClient.rtc_mp.get_unique_id())
|
|
#get_tree().change_scene_to_file("res://main_menu.tscn")
|
|
|
|
func _mp_peer_connected(id: int):
|
|
_log("[Multiplayer] Peer %d connected" % id)
|
|
has_opponent = true
|
|
start_game_button.disabled = false
|
|
|
|
@onready var lobby_info = $LobbyInfo
|
|
|
|
|
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
|
func _process(delta):
|
|
pass
|
|
|
|
@rpc("any_peer", "call_local")
|
|
func load_game() -> void:
|
|
# Hide menu
|
|
# load game scene
|
|
game_in_progress = true
|
|
lobby_info.visible = false
|
|
var game_scene = preload("res://Game.tscn").instantiate()
|
|
game.add_child(game_scene)
|
|
|
|
func _on_start_game_button_pressed():
|
|
# tell other peer to now load the world via rpc
|
|
load_game.rpc()
|
|
pass # Replace with function body.
|