1
Fork 0
mirror of https://github.com/Steffo99/nanogolf.git synced 2024-11-28 02:54:19 +00:00
algodist-steffo-nanogolf/scenes/playernode_directory.gd

99 lines
4.6 KiB
GDScript3
Raw Normal View History

2024-03-09 04:48:31 +00:00
extends Node
class_name PlayerNodeDirectory
## The scene to instantiate on creation of a new [PlayerNode].
2024-03-18 05:49:25 +00:00
const playernode_scene: PackedScene = preload("res://scenes/playernode.tscn")
2024-03-09 04:48:31 +00:00
## Find the subordinate [PlayerNode] with the given player_name, and return it if found, otherwise return null.
func get_playernode(player_name: String) -> PlayerNode:
var sanitized_player_name = PlayerNode.sanitize_player_name(player_name)
return get_node_or_null(sanitized_player_name)
2024-03-09 04:48:31 +00:00
## Create a new [PlayerNode] for the given [param player_name], giving control of it to [param peer_id].
##
## If a node with the given name already exists, only its multiplayer authority is changed, leaving the rest intact.
##
## If both node and multiplayer authority match the requested values, nothing is done at all.
@rpc("authority", "call_local", "reliable")
func rpc_possess_playernode(player_name: String, peer_id: int):
var playernode: PlayerNode = get_playernode(player_name)
# If the playernode does not exist, create it
if playernode == null:
playernode = playernode_scene.instantiate()
2024-03-10 04:51:36 +00:00
playernode.name_changed.connect(_on_playernode_name_changed.bind(playernode))
playernode.color_changed.connect(_on_playernode_color_changed.bind(playernode))
playernode.possessed.connect(_on_playernode_possessed.bind(playernode))
2024-03-16 04:25:23 +00:00
playernode.score_reported.connect(_on_playernode_score_reported.bind(playernode))
playernode.scores_changed.connect(_on_playernode_scores_changed.bind(playernode))
2024-03-18 05:08:31 +00:00
playernode.putt_performed.connect(_on_playernode_putt_performed.bind(playernode))
var sanitized_player_name = PlayerNode.sanitize_player_name(player_name)
playernode.player_name = sanitized_player_name
playernode.name = sanitized_player_name
2024-03-18 05:49:25 +00:00
# Determine the number of holes that this player has skipped
var played_holes = 0
for othernode in get_children():
var othernode_played_holes = len(othernode.hole_scores)
if othernode_played_holes > played_holes:
played_holes = othernode_played_holes
# Fill the empty scores with -1
for _index in range(played_holes):
playernode.hole_scores.push_back(-1)
# Add the playernode to the SceneTree
add_child(playernode)
2024-03-09 04:48:31 +00:00
# If the multiplayer authority does not match the requested one, make it match
2024-03-10 04:51:36 +00:00
playernode.possess(peer_id)
2024-03-18 05:49:25 +00:00
## Push the [field reported_score] of all children to the [field hole_scores] array, and reset its value to -1.
@rpc("authority", "call_local", "reliable")
func rpc_push_reported_scores():
for playernode in get_children():
playernode.hole_scores.push_back(playernode.reported_score)
playernode.reported_score = -1
2024-03-10 04:51:36 +00:00
2024-03-16 04:25:23 +00:00
func _on_playernode_name_changed(old: String, new: String, playernode: PlayerNode) -> void:
2024-03-10 04:51:36 +00:00
playernode_name_changed.emit(old, new, playernode)
2024-03-16 04:25:23 +00:00
func _on_playernode_color_changed(old: Color, new: Color, playernode: PlayerNode) -> void:
2024-03-10 04:51:36 +00:00
playernode_color_changed.emit(old, new, playernode)
2024-03-16 04:25:23 +00:00
func _on_playernode_possessed(old: int, new: int, playernode: PlayerNode) -> void:
2024-03-10 04:51:36 +00:00
playernode_possessed.emit(old, new, playernode)
2024-03-18 05:08:31 +00:00
if playernode.is_multiplayer_authority() and not multiplayer.is_server():
local_playernode_possessed.emit(old, new, playernode)
2024-03-10 04:51:36 +00:00
2024-03-16 04:25:23 +00:00
func _on_playernode_score_reported(strokes: int, playernode: PlayerNode) -> void:
playernode_score_reported.emit(strokes, playernode)
func _on_playernode_scores_changed(old: Array, new: Array, playernode: PlayerNode) -> void:
playernode_scores_changed.emit(old, new, playernode)
2024-03-18 05:08:31 +00:00
func _on_playernode_putt_performed(ball: GolfBall, playernode: PlayerNode) -> void:
playernode_putt_performed.emit(ball, playernode)
2024-03-10 04:51:36 +00:00
## Emitted when the name of one of the children [PlayerNode]s changes on the local scene.
signal playernode_name_changed(old: String, new: String, playernode: PlayerNode)
2024-03-16 04:25:23 +00:00
## Emitted when the color of one of the children [PlayerNode]s changes on the local scene.
2024-03-10 04:51:36 +00:00
signal playernode_color_changed(old: Color, new: Color, playernode: PlayerNode)
2024-03-09 04:48:31 +00:00
## Emitted everywhere when one of the children [PlayerNode]s has changed multiplayer authority.
2024-03-10 04:51:36 +00:00
signal playernode_possessed(old: int, new: int, playernode: PlayerNode)
2024-03-16 04:25:23 +00:00
2024-03-18 05:08:31 +00:00
## Emitted on a client when it becomes authority of a [PlayerNode].
signal local_playernode_possessed(old: int, new: int, playernode: PlayerNode)
2024-03-16 04:25:23 +00:00
## Emitted when a [PlayerNode] reports a score.
signal playernode_score_reported(strokes: int, playernode: PlayerNode)
## Emitted when the scores of one of the children [PlayerNode]s change on the local scene.
signal playernode_scores_changed(old: Array, new: Array, playernode: PlayerNode)
2024-03-18 05:08:31 +00:00
## Emitted when a [PlayerNode] performs a putt on its controlled [GolfBall].
signal playernode_putt_performed(ball: GolfBall, playernode: PlayerNode)