From 2a9cbb1fa0268c3be828c0d872180282704abb61 Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Thu, 28 Dec 2023 08:48:41 +0100 Subject: show other player puppets --- Network/Lobby.gd | 9 +++--- Network/Network.gd | 84 +++++++++++++++++++++++++++++++++++++----------------- 2 files changed, 63 insertions(+), 30 deletions(-) (limited to 'Network') diff --git a/Network/Lobby.gd b/Network/Lobby.gd index 80b663b..87a3894 100644 --- a/Network/Lobby.gd +++ b/Network/Lobby.gd @@ -25,7 +25,7 @@ func _ready(): elif OS.has_environment("USER"): ConnectNode.get_node("Name").text = OS.get_environment("USER") else: - var desktop_path = OS.get_system_dir(0).replace("\\", "/").split("/") + var desktop_path = OS.get_system_dir(OS.SYSTEM_DIR_DESKTOP).replace("\\", "/").split("/") ConnectNode.get_node("Name").text = desktop_path[desktop_path.size() - 2] # show lobby if we're already connected somewhere @@ -35,9 +35,10 @@ func _ready(): self.refresh_lobby() -func disable_connect_buttons(is_disabled = true): - ConnectNode.get_node("Host").disabled = is_disabled - ConnectNode.get_node("Join").disabled = is_disabled +func disable_connect_buttons(_is_disabled = true): + pass + #ConnectNode.get_node("Host").disabled = is_disabled + #ConnectNode.get_node("Join").disabled = is_disabled func refresh_lobby(): diff --git a/Network/Network.gd b/Network/Network.gd index 1222b92..ce4952c 100644 --- a/Network/Network.gd +++ b/Network/Network.gd @@ -4,31 +4,37 @@ extends Node signal connection_succeeded() signal connection_failed() signal player_list_changed() +signal player_changed() signal game_error() signal game_started() signal game_ended() const PORT = 10567 - const MAX_PEERS = 2 - - var peer: ENetMultiplayerPeer = null -var player = { - idx = 0, - name = "", - character = "Tux", -} + +var player = null var winning_player = player -# Dictionary { id = { idx: 1, name: "" } } +# Dictionary { id = player } var players = {} -var players_ready = [] + +func create_player(): + return { + name = "", + character = "", + symbol = "", + components = [], + current_level = -1, + position = Vector2(0, 0), + } func _ready(): + self.player = self.create_player() + multiplayer.connect("peer_connected", Callable(self, '_player_connected')) multiplayer.connect("peer_disconnected", Callable(self, "_player_disconnected")) multiplayer.connect("connected_to_server", Callable(self, "_connection_succeeded")) @@ -41,18 +47,28 @@ func _ready(): @rpc("any_peer") func register_player(player_name): var id = multiplayer.get_remote_sender_id() - self.players[id] = { - name = player_name, - ip = self.peer.get_peer(id).get_remote_address(), - } + + var network_player = self.create_player() + network_player.name = player_name + self.players[id] = network_player + emit_signal("player_list_changed") -@rpc("any_peer") func unregister_player(id): +func unregister_player(id): self.players.erase(id) emit_signal("player_list_changed") +func update_player(): + rpc("remote_update_player", self.player) + +@rpc("any_peer") func remote_update_player(network_player): + var id = multiplayer.get_remote_sender_id() + self.players[id] = network_player + emit_signal("player_changed", id) + + func host_game(player_name): self.player.name = player_name self.peer = ENetMultiplayerPeer.new() @@ -70,6 +86,7 @@ func join_game(ip, player_name): func leave_game(): rpc("_leave_game") multiplayer.multiplayer_peer = null + self.players = [] @rpc("any_peer") func _leave_game(): @@ -82,23 +99,34 @@ func get_player_count(): func _on_Global_game_started(): - # preconfigure game and set idx to each player + # set symbols + self.player.symbol = "X" var idx = 1 for id in self.players: - self.players[id].idx = idx - rpc_id(id, "_preconfigure_game", idx, Global.Level_Map) + self.players[id].symbol = ["X", "O", "P"][idx] idx += 1 + # preconfigure game + var all_players = self.players.duplicate() + all_players[1] = self.player + rpc("_preconfigure_game", { + level_map = Global.Level_Map, + players = all_players, + }) + # start game for everyone rpc("_start_game") -@rpc("any_peer") func _preconfigure_game(idx, level_map): - self.player.idx = idx - Global.Level_Map = level_map +@rpc("any_peer") func _preconfigure_game(configuration): + Global.Level_Map = configuration.level_map + self.player = configuration.players[multiplayer.get_unique_id()] + configuration.players.erase(multiplayer.get_unique_id()) + self.players = configuration.players @rpc("any_peer", "call_local") func _start_game(): + rpc("remote_update_player", self.player) emit_signal("game_started") @@ -107,12 +135,12 @@ func _on_Global_game_ended(): rpc("_end_game") -func _on_Global_game_won(winning_player): - rpc("_end_game", winning_player) +func _on_Global_game_won(winning_player_object): + rpc("_end_game", winning_player_object) -@rpc("any_peer", "call_local") func _end_game(winning_player): - self.winning_player = winning_player +@rpc("any_peer", "call_local") func _end_game(winning_player_object): + self.winning_player = winning_player_object emit_signal("game_ended") @@ -125,7 +153,8 @@ func _player_disconnected(id): # self.end_game() ? # else #rpc("unregister_player", id) - rpc("unregister_player", id) + #rpc("unregister_player", id) + self.unregister_player(id) func _connection_succeeded(): @@ -134,9 +163,12 @@ func _connection_succeeded(): func _connection_failed(): multiplayer.multiplayer_peer = null # remove peer + self.players = [] emit_signal("connection_failed") func _server_disconnected(): emit_signal("game_error", "Server disconnected!") + multiplayer.multiplayer_peer = null + self.players = [] #self.end_game("") -- cgit v1.2.3