From 636b44793e065c2aa4fcf75f9812753014667d32 Mon Sep 17 00:00:00 2001 From: Chris Bell Date: Thu, 19 Dec 2024 19:37:58 -0600 Subject: [PATCH] Buffer --- assets/core/ships/ship_script.gd | 70 ++++++++++++++++++++------------ 1 file changed, 44 insertions(+), 26 deletions(-) diff --git a/assets/core/ships/ship_script.gd b/assets/core/ships/ship_script.gd index 9c83702..64f64c9 100644 --- a/assets/core/ships/ship_script.gd +++ b/assets/core/ships/ship_script.gd @@ -5,16 +5,16 @@ extends RigidBody3D @export var player_detection_area: Area3D @export var helm_location_marker: Marker3D +var delta_time: float = 0.0 + var ship_helm_scene: PackedScene = load("res://assets/core/interactables/ship-helm/ship-helm.tscn") var ship_helm: ShipHelm var piloting_player: Player = null - var ship_id: int = 0 var ship_is_piloted: bool = false var current_level - -var uuid: String = "" +var network_uuid: String = "" var old_global_rotation_cache: Vector3 var old_global_position_cache: Vector3 @@ -28,7 +28,7 @@ var old_global_position_cache: Vector3 func _ready(): - uuid = NetworkManager.register_node(self) + network_uuid = NetworkManager.register_node(self) NetworkManager.property_update_received.connect(_on_property_update) #connect signals @@ -41,7 +41,11 @@ func _ready(): GameConsole.log_debug("Ship ID: " + str(ship_id)) -func _physics_process(_delta): +func _process(delta: float) -> void: + delta_time = delta + + +func _physics_process(_delta) -> void: if piloting_player != null and !piloting_player.is_network_authority: return if ship_is_piloted: var turn_speed = base_turn_speed / max(mass, 1) @@ -74,16 +78,9 @@ func _physics_process(_delta): # Self level slowly if host global_rotation.x = lerpf(global_rotation.x, 0, 0.1) global_rotation.z = lerpf(global_rotation.z, 0, 0.1) - - # Sync position and rotation for all players except the piloting player - if piloting_player == null or !piloting_player.is_network_authority: - if old_global_position_cache != global_position: - NetworkManager.sync_property(uuid, "global_position", global_position) - old_global_position_cache = global_position - - if old_global_rotation_cache != global_rotation: - NetworkManager.sync_property(uuid, "global_rotation", global_rotation) - old_global_rotation_cache = global_rotation + + _send_ship_sync() + func _on_area_3d_body_entered(body: Node3D) -> void: @@ -104,25 +101,46 @@ func _on_area_3d_body_exited(body: Node3D) -> void: func _add_ship_helm(_ship_helm_scene: PackedScene): - var ship_helm = _ship_helm_scene.instantiate() - helm_location_marker.add_child(ship_helm) - ship_helm = helm_location_marker.get_node("ShipHelm") + var ship_helm_instance = _ship_helm_scene.instantiate() + helm_location_marker.add_child(ship_helm_instance) + ship_helm_instance = helm_location_marker.get_node("ShipHelm") func _on_property_update(uuid: String, property_name: String, value: Variant): if NetworkManager.node_map.has(uuid): var node = NetworkManager.node_map[uuid] - if node != self: - node.set(property_name, value) + + if property_name == "global_position": + _handle_ship_sync_position(value) + elif property_name == "global_rotation": + _handle_ship_sync_rotation(value) else: - if property_name == "global_position" and (global_position - value).length() > 0.01: - global_position = value - elif property_name == "global_rotation" and (global_rotation - value).length() > 0.01: - global_rotation = value + node.set(property_name, value) + else: printerr("Received property update but node_id is wrong? Expected " + str(uuid) + " but got " + str(uuid)) func _sync_piloting_state(): - NetworkManager.sync_property(uuid, "ship_is_piloted", ship_is_piloted) - NetworkManager.sync_property(uuid, "piloting_player", piloting_player) + NetworkManager.sync_property(network_uuid, "ship_is_piloted", ship_is_piloted) + NetworkManager.sync_property(network_uuid, "piloting_player", piloting_player) + + +func _send_ship_sync(): +# if piloting_player == null or !piloting_player.is_network_authority: + var sync_buffer: float = 4 + if old_global_position_cache.distance_squared_to(global_position) > sync_buffer: + NetworkManager.sync_property(network_uuid, "global_position", global_position) + old_global_position_cache = global_position + + if old_global_rotation_cache.distance_squared_to(global_rotation) > sync_buffer: + NetworkManager.sync_property(network_uuid, "global_rotation", global_rotation) + old_global_rotation_cache = global_rotation + + +func _handle_ship_sync_position(pos: Vector3): + lerp(global_position, pos, 0.1 * delta_time) + + +func _handle_ship_sync_rotation(rot: Vector3): + lerp(global_rotation, rot, 0.1 * delta_time) \ No newline at end of file