Swarm/Multiplayer/Multiplayer.gd

64 lines
1.9 KiB
GDScript3
Raw Normal View History

extends Node
var has_opponent: bool = false
@onready var start_game_button = $LobbyInfo/PanelContainer/VBoxContainer/StartGameButton
@onready var game = $Game
2024-03-16 19:56:22 +01:00
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
GameData.peer_id = 1
start_game_button.disabled = true
2024-03-16 19:56:22 +01:00
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?
2024-03-23 16:18:30 +01:00
#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():
2024-03-23 16:18:30 +01:00
pass
#_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():
2024-03-23 16:18:30 +01:00
pass
#_log("[Multiplayer] Server disconnected (I am %d)" % WSClient.rtc_mp.get_unique_id())
2024-03-16 19:56:22 +01:00
#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
GameData.peer_id = id
start_game_button.disabled = false
@onready var lobby_info = $LobbyInfo
@rpc("any_peer", "call_local")
func load_game() -> void:
# Hide menu
# load game scene
2024-03-16 19:56:22 +01:00
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()