2024-03-09 04:48:31 +00:00
|
|
|
extends Node
|
|
|
|
class_name PeerNodeDirectory
|
|
|
|
|
|
|
|
|
|
|
|
## The scene to instantiate on creation of a new [PeerNode].
|
|
|
|
@export var peernode_scene: PackedScene = preload("res://scenes/peernode.tscn")
|
|
|
|
|
|
|
|
|
|
|
|
## Cache dictionary mapping peer_ids to their respective [PeerNode].
|
|
|
|
##
|
|
|
|
## This script is responsible for keeping it updated.
|
|
|
|
var peernodes_by_id: Dictionary = {}
|
|
|
|
|
|
|
|
|
|
|
|
## Find the subordinate [PeerNode] with the given peer_id, and return it if found, otherwise return null.
|
|
|
|
func get_peernode(peer_id: int) -> PeerNode:
|
|
|
|
return peernodes_by_id.get(peer_id) as PeerNode
|
|
|
|
|
|
|
|
|
|
|
|
## Create a new [PeerNode] for the given peer_id, or do nothing if it already exists.
|
|
|
|
@rpc("authority", "call_local", "reliable")
|
|
|
|
func rpc_create_peernode(peer_id: int):
|
|
|
|
# Make sure the peernode does not exist
|
|
|
|
var peernode: PeerNode = get_peernode(peer_id)
|
|
|
|
if peernode != null:
|
|
|
|
return
|
|
|
|
# Create a new peernode
|
|
|
|
peernode = peernode_scene.instantiate()
|
|
|
|
peernodes_by_id[peer_id] = peernode
|
|
|
|
peernode.set_multiplayer_authority(peer_id)
|
|
|
|
peernode.identified.connect(_on_peernode_identified.bind(peernode))
|
2024-03-10 04:51:36 +00:00
|
|
|
add_child(peernode, true)
|
2024-03-09 04:48:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
## Destroy the [PeerNode] for the given peer_id, or do nothing if it does not exist.
|
|
|
|
@rpc("authority", "call_local", "reliable")
|
|
|
|
func rpc_destroy_peernode(peer_id: int):
|
|
|
|
# Make sure the peernode exists
|
|
|
|
var peernode: PeerNode = get_peernode(peer_id)
|
|
|
|
if peernode == null:
|
|
|
|
return
|
|
|
|
# Destroy the peernode
|
|
|
|
peernode.queue_free()
|
|
|
|
|
|
|
|
|
|
|
|
## Called on the server when a [PeerNode] calls [method rpc_identify] for itself.
|
2024-03-10 04:51:36 +00:00
|
|
|
func _on_peernode_identified(player_name: String, peernode: PeerNode):
|
|
|
|
peernode_identified.emit(player_name, peernode)
|
2024-03-09 04:48:31 +00:00
|
|
|
|
|
|
|
## Emitted on the server when a [PeerNode] calls [method rpc_identify] for itself.
|
2024-03-10 04:51:36 +00:00
|
|
|
signal peernode_identified(player_name: String, peernode: PeerNode)
|