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

49 lines
1.4 KiB
GDScript3
Raw Normal View History

2021-11-15 15:43:33 -06:00
extends Node2D
2021-12-15 16:14:36 -06:00
onready var player_obj = preload("res://objects/player.tscn")
2021-11-15 15:43:33 -06:00
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
2021-12-15 16:14:36 -06:00
onready var player = null
onready var players = null
2021-11-15 15:43:33 -06:00
func _ready():
# TODO: probably have to wait for all peers to be ready before we add players
2021-12-15 16:14:36 -06:00
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())
2021-11-15 15:43:33 -06:00
2021-12-15 16:14:36 -06:00
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()
2021-11-15 15:43:33 -06:00
2021-12-15 16:14:36 -06:00
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)
2021-12-08 16:35:37 -06:00
add_child(new_player)
2021-12-15 16:14:36 -06:00
new_player.global_position = Vector2(-100 + (id % 200), -100 + (id % 200))
print("Added player: %s for peer %s" % [new_player, id])
2021-11-15 15:43:33 -06:00
func _on_Button_pressed():
2021-11-17 08:18:12 -06:00
Global.main_menu()