Sync fixes for RigidBody3D.

This commit is contained in:
Patrick_Pluto 2024-12-19 14:01:18 +01:00
parent c0d3c68fb3
commit 8c90c4e744
4 changed files with 63 additions and 11 deletions

View file

@ -47,7 +47,7 @@ script = ExtResource("2_qrp84")
[node name="NPCCarryables" type="Node3D" parent="."] [node name="NPCCarryables" type="Node3D" parent="."]
[node name="Closet" parent="NPCCarryables" instance=ExtResource("2_yvpvm")] [node name="Closet" parent="NPCCarryables" instance=ExtResource("2_yvpvm")]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 29, 10, -30) transform = Transform3D(0.642788, -0.766044, 0, 0.766044, 0.642788, 0, 0, 0, 1, 29, 10, -30)
[node name="DirectionalLight3D" type="DirectionalLight3D" parent="."] [node name="DirectionalLight3D" type="DirectionalLight3D" parent="."]
transform = Transform3D(-0.899519, 0.375, 0.224144, -0.224144, -0.836516, 0.5, 0.375, 0.399519, 0.836516, 0, 0, 0) transform = Transform3D(-0.899519, 0.375, 0.224144, -0.224144, -0.836516, 0.5, 0.375, 0.399519, 0.836516, 0, 0, 0)

View file

@ -32,13 +32,16 @@ func _timer_done() -> void:
if angry_meter > 0: if angry_meter > 0:
angry_meter -= 1 angry_meter -= 1
if target is Objective: if target is Objective:
($Label3D as Label3D).text = "%s %ds left" % [(target as Objective).displayed, angry_meter] Networking.npc_text_sync_call("%s %ds left" % [(target as Objective).displayed, angry_meter], self)
else: else:
($Label3D as Label3D).text = "" Networking.npc_text_sync_call("", self)
(target as Objective).occupied = false (target as Objective).occupied = false
timer.stop() timer.stop()
phase = 2 phase = 2
func set_text(text: String) -> void:
($Label3D as Label3D).text = text
# determines the closest resting place and navigates to it. # determines the closest resting place and navigates to it.
func find_objective() -> void: func find_objective() -> void:
var lowest_distance: float = 99999999999999 var lowest_distance: float = 99999999999999
@ -70,6 +73,7 @@ func pick_up(node: Carryable) -> void:
node.get_parent().remove_child(node) node.get_parent().remove_child(node)
node.carry() node.carry()
node.position = Vector3(0, 1, -1) node.position = Vector3(0, 1, -1)
node.rotation = Vector3(0, 0, 0)
add_child(node) add_child(node)
find_player(99999999999) find_player(99999999999)
phase = 3 phase = 3
@ -78,7 +82,8 @@ func set_down(node: Carryable) -> void:
carrying = false carrying = false
node.get_parent().remove_child(node) node.get_parent().remove_child(node)
node.uncarry() node.uncarry()
node.position = position + Vector3(0,0,1) node.rotation = rotation
node.position += position
get_tree().root.get_node("/root/"+Game.mapname+"/NPCCarryables").add_child(node) get_tree().root.get_node("/root/"+Game.mapname+"/NPCCarryables").add_child(node)
find_player(99999999999) find_player(99999999999)
phase = 0 phase = 0

View file

@ -24,6 +24,10 @@ func _on_body_entered(body: Node) -> void:
carried_by = body carried_by = body
Networking.pick_up_sync_call(carried_by, self) Networking.pick_up_sync_call(carried_by, self)
if body is Player and carried: if body is Player and carried:
Networking.set_down_sync_call(carried_by, self) Networking.set_down_sync_call(carried_by, self)
(body as Player).incapacitate() (body as Player).incapacitate()
func _physics_process(_delta: float) -> void:
if Networking.isManager and sleeping and !carried:
Networking.object_sync_call(position, rotation, name)

View file

@ -103,6 +103,7 @@ func _on_peer_disconnected(id: int) -> void:
Log.warning("Failed to send disconnect message to %d!" % found_peer) Log.warning("Failed to send disconnect message to %d!" % found_peer)
mutex.unlock() mutex.unlock()
return return
mutex.unlock() mutex.unlock()
# The following function sends the roomname selected by the client. # The following function sends the roomname selected by the client.
@ -237,6 +238,8 @@ func send_playerlist(status: int, newPeers: Dictionary) -> void:
Log.warning("Name %s is already taken in the room!" % Game.username) Log.warning("Name %s is already taken in the room!" % Game.username)
3: 3:
Log.warning("Sent a playerlist request to a regular peer, not the host!") Log.warning("Sent a playerlist request to a regular peer, not the host!")
else:
Log.warning("Non-manager peer tried to send a manager only request: send_playerlist")
## General Game RPCs ## General Game RPCs
@ -263,6 +266,8 @@ func start_game() -> void:
if multiplayer.get_remote_sender_id() == managerID: if multiplayer.get_remote_sender_id() == managerID:
if get_tree().change_scene_to_file("res://scenes/maps/testmap.tscn") != OK: if get_tree().change_scene_to_file("res://scenes/maps/testmap.tscn") != OK:
Log.warning("Couldn't load the map!") Log.warning("Couldn't load the map!")
else:
Log.warning("Non-manager peer tried to send a manager only request: start_game")
func player_sync_call(position: Vector3, rotation: Vector3) -> void: func player_sync_call(position: Vector3, rotation: Vector3) -> void:
for peer: int in Networking.get_ids(): for peer: int in Networking.get_ids():
@ -271,14 +276,16 @@ func player_sync_call(position: Vector3, rotation: Vector3) -> void:
Log.warning("Couldn't send RPC to %d!" % peer) Log.warning("Couldn't send RPC to %d!" % peer)
# Synchronizes the player state. # Synchronizes the player state.
@rpc("any_peer", "call_remote", "reliable") @rpc("any_peer", "call_remote", "unreliable")
func player_sync(position: Vector3, rotation: Vector3) -> void: func player_sync(position: Vector3, rotation: Vector3) -> void:
if verify_id(multiplayer.get_remote_sender_id()): if verify_id(multiplayer.get_remote_sender_id()):
for peer: String in peers: for peer: String in peers:
if peers[peer] == multiplayer.get_remote_sender_id() and has_node("/root/"+Game.mapname+"/"+peer): if peers[peer] == multiplayer.get_remote_sender_id() and has_node("/root/"+Game.mapname+"/"+peer):
var player: CharacterBody3D = get_node("/root/"+Game.mapname+"/"+peer) var player: Player = get_node("/root/"+Game.mapname+"/"+peer)
player.position = position player.position = position
player.rotation = rotation player.rotation = rotation
else:
Log.warning("A non affiliated peer tried to send: player_sync")
func npc_sync_call(position: Vector3, rotation: Vector3, node_name: String) -> void: func npc_sync_call(position: Vector3, rotation: Vector3, node_name: String) -> void:
for peer: int in Networking.get_ids(): for peer: int in Networking.get_ids():
@ -287,12 +294,30 @@ func npc_sync_call(position: Vector3, rotation: Vector3, node_name: String) -> v
Log.warning("Couldn't send RPC to %d!" % peer) Log.warning("Couldn't send RPC to %d!" % peer)
# Synchronizes the npc state. # Synchronizes the npc state.
@rpc("any_peer", "call_remote", "reliable") @rpc("any_peer", "call_remote", "unreliable")
func npc_sync(position: Vector3, rotation: Vector3, node_name: String) -> void: func npc_sync(position: Vector3, rotation: Vector3, node_name: String) -> void:
if managerID == multiplayer.get_remote_sender_id(): if managerID == multiplayer.get_remote_sender_id():
var npc: CharacterBody3D = get_node("/root/"+Game.mapname+"/"+node_name) var npc: NPC = get_node("/root/"+Game.mapname+"/"+node_name)
npc.position = position npc.position = position
npc.rotation = rotation npc.rotation = rotation
else:
Log.warning("Non-manager peer tried to send a manager only request: npc_sync")
func object_sync_call(position: Vector3, rotation: Vector3, node_name: String) -> void:
for peer: int in Networking.get_ids():
if peer != multiplayer.get_unique_id():
if rpc_id(peer, "object_sync", position, rotation, node_name) != OK:
Log.warning("Couldn't send RPC to %d!" % peer)
# Synchronizes the npc state.
@rpc("any_peer", "call_remote", "unreliable")
func object_sync(position: Vector3, rotation: Vector3, node_name: String) -> void:
if managerID == multiplayer.get_remote_sender_id():
var carryable: Carryable = get_node("/root/"+Game.mapname+"/NPCCarryables/"+node_name)
carryable.position = position
carryable.rotation = rotation
else:
Log.warning("Non-manager peer tried to send a manager only request: npc_sync")
func pick_up_sync_call(npc: NPC, carryable: Carryable) -> void: func pick_up_sync_call(npc: NPC, carryable: Carryable) -> void:
var npc_path: NodePath = npc.get_path() var npc_path: NodePath = npc.get_path()
@ -301,11 +326,27 @@ func pick_up_sync_call(npc: NPC, carryable: Carryable) -> void:
if rpc_id(peer, "pick_up_sync", npc_path, carryable_path) != OK: if rpc_id(peer, "pick_up_sync", npc_path, carryable_path) != OK:
Log.warning("Couldn't send RPC to %d!" % peer) Log.warning("Couldn't send RPC to %d!" % peer)
# Synchronizes the text above the npc.
@rpc("any_peer", "call_local", "reliable")
func npc_text_sync(text: String, npc: NodePath) -> void:
if managerID == multiplayer.get_remote_sender_id():
(get_node(npc) as NPC).set_text(text)
else:
Log.warning("Non-manager peer tried to send a manager only request: npc_text_sync")
func npc_text_sync_call(text: String, npc: NPC) -> void:
var npc_path: NodePath = npc.get_path()
for peer: int in Networking.get_ids():
if rpc_id(peer, "npc_text_sync", text, npc_path) != OK:
Log.warning("Couldn't send RPC to %d!" % peer)
# Synchronizes the object that is picked up by the npc. # Synchronizes the object that is picked up by the npc.
@rpc("any_peer", "call_local", "reliable") @rpc("any_peer", "call_local", "reliable")
func pick_up_sync(npc: NodePath, carryable: NodePath) -> void: func pick_up_sync(npc: NodePath, carryable: NodePath) -> void:
if managerID == multiplayer.get_remote_sender_id(): if managerID == multiplayer.get_remote_sender_id():
(get_node(npc) as NPC).pick_up(get_node(carryable) as Carryable) (get_node(npc) as NPC).pick_up(get_node(carryable) as Carryable)
else:
Log.warning("Non-manager peer tried to send a manager only request: pick_up_sync")
func set_down_sync_call(npc: NPC, carryable: Carryable) -> void: func set_down_sync_call(npc: NPC, carryable: Carryable) -> void:
var npc_path: NodePath = npc.get_path() var npc_path: NodePath = npc.get_path()
@ -319,3 +360,5 @@ func set_down_sync_call(npc: NPC, carryable: Carryable) -> void:
func set_down_sync(npc: NodePath, carryable: NodePath) -> void: func set_down_sync(npc: NodePath, carryable: NodePath) -> void:
if managerID == multiplayer.get_remote_sender_id(): if managerID == multiplayer.get_remote_sender_id():
(get_node(npc) as NPC).set_down(get_node(carryable) as Carryable) (get_node(npc) as NPC).set_down(get_node(carryable) as Carryable)
else:
Log.warning("Non-manager peer tried to send a manager only request: set_down_sync")