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

67 lines
2.3 KiB
GDScript3
Raw Permalink Normal View History

2021-12-17 17:10:56 -06:00
extends Control
2021-11-15 15:43:33 -06:00
2021-12-21 17:13:02 -06:00
@onready var player_obj = preload("res://objects/player.tscn")
2021-11-15 15:43:33 -06:00
2021-12-21 17:13:02 -06:00
@export @onready var camera = get_node(camera) as Camera2D
@export @onready var menu = get_node(menu) as PopupDialog
@export @onready var players = get_node(players) as YSort
@export @onready var objects = get_node(objects) as YSort
@export @onready var nav = get_node(nav) as Navigation2D
2021-12-17 17:10:56 -06:00
2021-12-21 17:13:02 -06:00
@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
2021-11-15 15:43:33 -06:00
func _ready():
2021-12-21 16:40:58 -06:00
for c in players.get_children(): c.queue_free()
# TODO: probably have to wait for all peers to be ready before we add players
2021-12-17 17:10:56 -06:00
# camera.zoom = Vector2(0.25, 0.25)
Global.client.connect("webrtc_peer_disconnected", self, "_webrtc_peer_disconnected")
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-21 17:13:02 -06:00
@rpc
func ready(id):
2021-12-15 16:14:36 -06:00
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-12-21 16:40:58 -06:00
for c in objects.get_children():
if c is Slime:
c.target_player = Global.client.peers[id].player
c.navigation = nav
2021-11-15 15:43:33 -06:00
2021-12-17 17:10:56 -06:00
func _webrtc_peer_disconnected(id):
if Global.client.peers[id].has("player"):
Global.client.peers[id].player.queue_free()
2021-12-15 16:14:36 -06:00
func start():
print("Starting Game...")
# for each peer
2021-12-17 17:10:56 -06:00
for id in Global.client.peers:
print("Creating player for peer %d (name: %s)" % [id, Global.client.peers[id].name])
rpc("add_player", id, Global.client.peers[id].name)
2021-12-15 16:14:36 -06:00
func _process(_delta):
if player: camera.offset = player.position
2021-12-21 17:13:02 -06:00
@rpc func add_player(id, name):
2021-12-15 16:14:36 -06:00
var new_player = player_obj.instance()
if id == get_tree().get_network_unique_id(): player = new_player
2021-12-17 17:10:56 -06:00
new_player.name = str(id)
2021-12-15 16:14:36 -06:00
new_player.set_network_master(id)
2021-12-21 16:40:58 -06:00
players.add_child(new_player)
2021-12-17 17:10:56 -06:00
new_player.global_position = Vector2(-400 + (id % 800), -400 + (id % 800))
print("Added player: %s for peer %s named %s" % [new_player, id, name])
Global.client.peers[id].player = new_player
new_player.display_name = name
func _on_leave_pressed(): Global.main_menu()
func _on_menu_button_pressed(): menu.popup_centered()
func _on_close_pressed(): menu.hide()