Ship mostly works #10
@ -13,7 +13,7 @@ var piloting_player: Player = null
|
||||
var ship_id: int = 0
|
||||
var ship_is_piloted: bool = false
|
||||
var current_level
|
||||
var network_uuid: String = ""
|
||||
var network_uuid: String = ""
|
||||
|
||||
var old_global_rotation_cache: Vector3
|
||||
var old_global_position_cache: Vector3
|
||||
@ -32,17 +32,20 @@ var sync_interval: float = 0.1 # Sync every 0.1 seconds
|
||||
var predicted_position: Vector3
|
||||
var predicted_rotation: Quaternion
|
||||
|
||||
|
||||
func _ready():
|
||||
network_uuid = NetworkManager.register_node(self)
|
||||
NetworkManager.property_update_received.connect(_on_property_update)
|
||||
|
||||
#connect signals
|
||||
# Connect to NetworkManager signals
|
||||
NetworkManager.on_property_update.connect(_on_property_update)
|
||||
|
||||
# Connect signals
|
||||
player_detection_area.body_entered.connect(_on_area_3d_body_entered)
|
||||
player_detection_area.body_exited.connect(_on_area_3d_body_exited)
|
||||
|
||||
_add_ship_helm(ship_helm_scene) #TEMPORARY TEST
|
||||
_add_ship_helm(ship_helm_scene) # TEMPORARY TEST
|
||||
|
||||
ship_id = randi_range(1000, 9999) #assign a random id to the ship as a unique identifier
|
||||
ship_id = randi_range(1000, 9999) # Assign a random ID to the ship
|
||||
GameConsole.log_debug("Ship ID: " + str(ship_id))
|
||||
|
||||
# Initialize predicted values
|
||||
@ -52,6 +55,7 @@ func _ready():
|
||||
func _process(delta: float) -> void:
|
||||
delta_time = delta
|
||||
|
||||
|
||||
func _physics_process(_delta) -> void:
|
||||
# Predict movement only for non-authority clients
|
||||
if not piloting_player or piloting_player.is_network_authority:
|
||||
@ -71,6 +75,7 @@ func _physics_process(_delta) -> void:
|
||||
|
||||
_send_ship_sync()
|
||||
|
||||
|
||||
func _predict_movement(_delta):
|
||||
# Simple linear prediction
|
||||
var predicted_velocity = linear_velocity
|
||||
@ -82,6 +87,7 @@ func _predict_movement(_delta):
|
||||
var predicted_rotation_delta = Quaternion(predicted_angular_velocity, _delta)
|
||||
predicted_rotation = predicted_rotation * predicted_rotation_delta
|
||||
|
||||
|
||||
func _apply_local_input(_delta):
|
||||
if piloting_player == null or not piloting_player.is_network_authority:
|
||||
return
|
||||
@ -113,26 +119,32 @@ func _apply_local_input(_delta):
|
||||
|
||||
angular_velocity.y = clamp(angular_velocity.y, -max_turn_speed, max_turn_speed)
|
||||
|
||||
|
||||
func _on_area_3d_body_entered(body: Node3D) -> void:
|
||||
if body is Player and body.get_parent() != self:
|
||||
body.ship_entered(self)
|
||||
piloting_player = body
|
||||
body.call_deferred("reparent", self, true) #reparents player onto self (RigidBody3D)
|
||||
body.call_deferred("reparent", self, true) # Reparents player onto self (RigidBody3D)
|
||||
print("ENTERED")
|
||||
_sync_piloting_state() # Update piloting state on the network
|
||||
|
||||
|
||||
func _on_area_3d_body_exited(body: Node3D) -> void:
|
||||
if body is Player and body.get_parent() != get_node("/root/DevLevel/"):
|
||||
body.ship_exited()
|
||||
ship_is_piloted = false
|
||||
piloting_player = null
|
||||
body.call_deferred("reparent", get_node("/root/DevLevel/"), true) #reparents player back onto world node
|
||||
body.call_deferred("reparent", get_node("/root/DevLevel/"), true) # Reparents player back onto world node
|
||||
print("EXITED")
|
||||
_sync_piloting_state() # Update piloting state on the network
|
||||
|
||||
|
||||
func _add_ship_helm(_ship_helm_scene: PackedScene):
|
||||
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]
|
||||
@ -141,11 +153,22 @@ func _on_property_update(uuid: String, property_name: String, value: Variant):
|
||||
_handle_ship_sync_position(value)
|
||||
elif property_name == "global_rotation":
|
||||
_handle_ship_sync_rotation(value)
|
||||
elif property_name == "ship_is_piloted":
|
||||
node.ship_is_piloted = value
|
||||
elif property_name == "piloting_player":
|
||||
# Handle piloting player updates (e.g., update references)
|
||||
if value != "":
|
||||
# Find the player by network UUID
|
||||
var player_node = NetworkManager.node_map[value]
|
||||
node.piloting_player = player_node
|
||||
else:
|
||||
node.piloting_player = null
|
||||
else:
|
||||
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(network_uuid, "ship_is_piloted", ship_is_piloted)
|
||||
if piloting_player != null:
|
||||
@ -153,14 +176,13 @@ func _sync_piloting_state():
|
||||
else:
|
||||
NetworkManager.sync_property(network_uuid, "piloting_player", "")
|
||||
|
||||
|
||||
func _send_ship_sync():
|
||||
if Time.get_ticks_msec() - last_sync_time > sync_interval * 1000:
|
||||
last_sync_time = Time.get_ticks_msec()
|
||||
NetworkManager.sync_property(network_uuid, "global_position", global_position)
|
||||
NetworkManager.sync_property(network_uuid, "global_rotation", global_rotation)
|
||||
NetworkManager.sync_property(network_uuid, "linear_velocity", linear_velocity)
|
||||
NetworkManager.sync_property(network_uuid, "angular_velocity", angular_velocity)
|
||||
|
||||
|
||||
func _handle_ship_sync_position(pos: Vector3):
|
||||
if piloting_player and not piloting_player.is_network_authority:
|
||||
# Correct prediction with server data only if the difference is significant
|
||||
@ -168,6 +190,7 @@ func _handle_ship_sync_position(pos: Vector3):
|
||||
predicted_position = pos
|
||||
linear_velocity = (pos - global_position) / delta_time
|
||||
|
||||
|
||||
func _handle_ship_sync_rotation(rot: Vector3):
|
||||
if piloting_player and not piloting_player.is_network_authority:
|
||||
# Correct prediction with server data only if the difference is significant
|
||||
@ -176,6 +199,6 @@ func _handle_ship_sync_rotation(rot: Vector3):
|
||||
if delta_rot.length() > 0.1:
|
||||
predicted_rotation = received_rotation
|
||||
angular_velocity = delta_rot.get_euler() / delta_time
|
||||
|
||||
|
||||
# Apply server rotation directly
|
||||
global_rotation = Quaternion.from_euler(rot).get_euler()
|
Loading…
Reference in New Issue
Block a user