godot-webrtc-mplayer-testing/scripts/screens/game.gd

49 lines
1.4 KiB
GDScript

extends Node2D
onready var player_obj = preload("res://objects/player.tscn")
onready var camera = $camera
onready var zoom_levels = [0.25, 0.5, 1, 1.5, 2, 2.5, 3]
onready var current_zoom_level_index = 2
onready var player = null
onready var players = null
func _ready():
# TODO: probably have to wait for all peers to be ready before we add players
if Global.client.is_host():
for id in get_tree().get_network_connected_peers():
Global.client.peers[id].ready = false
ready(1)
else:
rpc("ready", get_tree().get_network_unique_id())
remotesync func ready(id):
print("Ready %d" % id)
Global.client.peers[id].ready = true
var all_ready = true
for id in Global.client.peers: all_ready = all_ready and Global.client.peers[id].ready
if all_ready: start()
func start():
print("Starting Game...")
# for each peer
for id in get_tree().get_network_connected_peers():
print("Creating player for peer %d" % id)
rpc("add_player", id)
rpc("add_player", 1)
func _process(_delta):
if player: camera.offset = player.position
remotesync func add_player(id):
var new_player = player_obj.instance()
if id == get_tree().get_network_unique_id(): player = new_player
new_player.name = Global.client.peers[id].name
new_player.set_network_master(id)
add_child(new_player)
new_player.global_position = Vector2(-100 + (id % 200), -100 + (id % 200))
print("Added player: %s for peer %s" % [new_player, id])
func _on_Button_pressed():
Global.main_menu()