About to big refactor multiplayer... again

This commit is contained in:
Daniel Flanagan 2021-12-10 11:25:06 -06:00
parent 2aea604aba
commit 438ba9682f
Signed by: lytedev
GPG key ID: 5B2020A0F9921EF4
4 changed files with 23 additions and 2 deletions

View file

@ -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

View file

@ -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()

View file

@ -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()

View file

@ -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):