From 438ba9682f1d4a1488304386e747c09494021ed6 Mon Sep 17 00:00:00 2001 From: Daniel Flanagan Date: Fri, 10 Dec 2021 11:25:06 -0600 Subject: [PATCH] About to big refactor multiplayer... again --- screens/game.tscn | 6 +++++- scripts/global/webrtc_negotiator.gd | 9 +++++++++ scripts/screens/game.gd | 8 +++++++- scripts/screens/multiplayer_lobby.gd | 2 ++ 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/screens/game.tscn b/screens/game.tscn index 15f0f45..e3ae6b0 100644 --- a/screens/game.tscn +++ b/screens/game.tscn @@ -2,9 +2,13 @@ [ext_resource path="res://scripts/screens/game.gd" type="Script" id=1] -[node name="Node2D" type="Node2D"] +[node name="game" type="Node2D"] script = ExtResource( 1 ) +[node name="camera" type="Camera2D" parent="."] +current = true +zoom = Vector2( 0.5, 0.5 ) + [node name="Label" type="Label" parent="."] margin_left = 93.0 margin_top = 109.0 diff --git a/scripts/global/webrtc_negotiator.gd b/scripts/global/webrtc_negotiator.gd index 32d7f35..68df280 100644 --- a/scripts/global/webrtc_negotiator.gd +++ b/scripts/global/webrtc_negotiator.gd @@ -13,6 +13,8 @@ onready var webrtc = WebRTCMultiplayer.new() var signaller_client = null var ice_servers = null +var peers = {} +var peers_id_mappings = {} func _init(_ice_servers, _signaller_client): ice_servers = _ice_servers @@ -60,6 +62,13 @@ func close(): func connect_to_signaller(): signaller_client.connect_websocket() +func peer_by_peer_id(peer_id): + if peers_id_mappings.has(peer_id): + var p = peers_id_mappings[peer_id] + if peers.has(p): + return peers[p] + return null + func _create_peer(peer_id): # if peer_id == signaller_client.peer_id: return var peer: WebRTCPeerConnection = WebRTCPeerConnection.new() diff --git a/scripts/screens/game.gd b/scripts/screens/game.gd index 807c3e0..c5df8ea 100644 --- a/scripts/screens/game.gd +++ b/scripts/screens/game.gd @@ -2,6 +2,11 @@ extends Node2D onready var player = preload("res://objects/player.tscn") +onready var camera = $camera +onready var camera_target = null +onready var zoom_levels = [0.25, 0.5, 1, 1.5, 2, 2.5, 3] +onready var current_zoom_level_index = 2 + func _ready(): # TODO: probably have to wait for all peers to be ready before we add players rpc("add_player", get_tree().get_network_unique_id()) @@ -11,10 +16,11 @@ func _process(delta): remotesync func add_player(peer_id): var new_player = player.instance() + new_player.name = Global.negotiator.peer_by_peer_id(peer_id).name new_player.set_network_master(peer_id) add_child(new_player) new_player.global_position = Vector2(100, 100) - print("Added player: %s" % new_player) + print("Added player: %s for peer %s" % [new_player, peer_id]) func _on_Button_pressed(): Global.main_menu() diff --git a/scripts/screens/multiplayer_lobby.gd b/scripts/screens/multiplayer_lobby.gd index 6f2aa1f..aa86eeb 100644 --- a/scripts/screens/multiplayer_lobby.gd +++ b/scripts/screens/multiplayer_lobby.gd @@ -135,6 +135,8 @@ func _on_start_pressed(): if is_host: rpc("start_game") remotesync func start_game(): + Global.negotiator.peers = peers + Global.negotiator.peers_id_mappings = peers_id_mappings Global.goto_game() remotesync func set_ready(ready):