2024-03-09 04:48:31 +00:00
|
|
|
extends Node
|
|
|
|
class_name PlayerNode
|
|
|
|
|
|
|
|
|
2024-03-11 04:09:33 +00:00
|
|
|
## Remove problematic characters from the player's name
|
|
|
|
static func sanitize_player_name(s: String) -> String:
|
|
|
|
return s.replace("/", "_").replace("*", "_").replace(" ", "_")
|
|
|
|
|
|
|
|
|
2024-03-10 04:51:36 +00:00
|
|
|
## The name of the player represented by this node.
|
|
|
|
var player_name: String
|
|
|
|
|
|
|
|
## The color of the player represented by this node.
|
|
|
|
var player_color: Color
|
|
|
|
|
|
|
|
## Change the [field player_name] everywhere.
|
|
|
|
@rpc("authority", "call_local", "reliable")
|
|
|
|
func rpc_set_name(value: String):
|
|
|
|
if player_name != value:
|
|
|
|
var old_value: String = player_name
|
2024-03-11 04:09:33 +00:00
|
|
|
player_name = PlayerNode.sanitize_player_name(value)
|
|
|
|
name = PlayerNode.sanitize_player_name(value)
|
2024-03-10 04:51:36 +00:00
|
|
|
name_changed.emit(old_value, value)
|
|
|
|
|
|
|
|
## Change the [field player_color] everywhere.
|
|
|
|
@rpc("authority", "call_local", "reliable")
|
|
|
|
func rpc_set_color(value: Color):
|
|
|
|
if player_color != value:
|
|
|
|
var old_value: Color = player_color
|
|
|
|
player_color = value
|
|
|
|
color_changed.emit(old_value, value)
|
|
|
|
|
|
|
|
## Change the multiplayer authority on the local client.
|
|
|
|
##
|
|
|
|
## Used by [PlayerNodeDirectory], provided here for the purpose of signal emission.
|
|
|
|
func possess(value: int) -> void:
|
|
|
|
var old_value: int = get_multiplayer_authority()
|
|
|
|
if old_value != value:
|
|
|
|
set_multiplayer_authority(value)
|
|
|
|
possessed.emit(old_value, value)
|
|
|
|
|
|
|
|
|
|
|
|
## Prompt the multiplayer authority for this node to call [method rpc_set_name] again with the current value.
|
|
|
|
##
|
|
|
|
## Used to repeat [field player_name] to new peers.
|
|
|
|
@rpc("any_peer", "call_local", "reliable")
|
|
|
|
func rpc_query_name():
|
|
|
|
if is_multiplayer_authority():
|
|
|
|
rpc_set_name.rpc(player_name)
|
|
|
|
|
|
|
|
## Prompt the multiplayer authority for this node to call [method rpc_set_color] again with the current value.
|
|
|
|
##
|
|
|
|
## Used to repeat [field player_color] to new peers.
|
|
|
|
@rpc("any_peer", "call_local", "reliable")
|
|
|
|
func rpc_query_color():
|
|
|
|
if is_multiplayer_authority():
|
|
|
|
rpc_set_color.rpc(player_color)
|
|
|
|
|
|
|
|
|
|
|
|
## Emitted when the name changes on the local scene because of [method rpc_set_name].
|
|
|
|
signal name_changed(old: String, new: String)
|
|
|
|
|
|
|
|
## Emitted when the color changes on the local scene because of [method rpc_set_color].
|
|
|
|
signal color_changed(old: Color, new: Color)
|
|
|
|
|
|
|
|
## Emitted when the multiplayer authority of this [Node] is changed via [method possess].
|
|
|
|
signal possessed(old: int, new: int)
|