Ship mostly works #10
@ -25,6 +25,7 @@ var previous_rotation: Quaternion
|
||||
|
||||
func _ready():
|
||||
network_uuid = NetworkManager.register_node(self)
|
||||
# Preserve current position and rotation at the start
|
||||
target_position = global_position
|
||||
target_rotation = global_transform.basis.get_rotation_quaternion()
|
||||
previous_position = global_position
|
||||
@ -36,10 +37,16 @@ func _ready():
|
||||
player_detection_area.body_exited.connect(_on_player_exited)
|
||||
|
||||
func _process(delta: float):
|
||||
# Only the piloting player or the host when no one is piloting should send updates
|
||||
if piloting_player and piloting_player.is_network_authority:
|
||||
handle_input(delta)
|
||||
sync_ship_state() # Piloting player sends updates to all players
|
||||
elif not piloting_player and NetworkManager.is_host:
|
||||
# If no one is piloting, the host should send updates
|
||||
sync_ship_state()
|
||||
else:
|
||||
|
||||
# Interpolation for non-piloting players
|
||||
if not piloting_player or not piloting_player.is_network_authority:
|
||||
interpolate_position_and_rotation(delta)
|
||||
|
||||
func handle_input(_delta: float):
|
||||
@ -69,45 +76,52 @@ func handle_input(_delta: float):
|
||||
if turn_input != 0:
|
||||
var torque = Vector3.UP * turn_input * base_turn_speed
|
||||
apply_torque(torque)
|
||||
print("Torque:", torque)
|
||||
|
||||
# Apply force for forward and backward movement
|
||||
if forward_input != 0:
|
||||
var forward_direction = -global_transform.basis.z.normalized()
|
||||
var forward_force = forward_direction * forward_input * move_speed
|
||||
apply_central_force(forward_force)
|
||||
print("Forward Force:", forward_force)
|
||||
|
||||
# Apply force for vertical movement
|
||||
if vertical_input != 0:
|
||||
var vertical_force = Vector3.UP * vertical_input * base_lift_speed
|
||||
apply_central_force(vertical_force)
|
||||
print("Vertical Force:", vertical_force)
|
||||
|
||||
|
||||
# Sync the ship state (send position and rotation only if we are the piloting player or host when no one is piloting)
|
||||
func sync_ship_state():
|
||||
if global_position != previous_position:
|
||||
NetworkManager.sync_property(network_uuid, "global_position", global_position)
|
||||
previous_position = global_position
|
||||
# Only the piloting player or the host (when no one is piloting) sends the update
|
||||
if piloting_player and piloting_player.is_network_authority or NetworkManager.is_host:
|
||||
# Send updates only if the position has changed
|
||||
if global_position != previous_position:
|
||||
NetworkManager.sync_property(network_uuid, "global_position", global_position)
|
||||
previous_position = global_position
|
||||
|
||||
var current_rotation = global_transform.basis.get_rotation_quaternion()
|
||||
if current_rotation != previous_rotation:
|
||||
NetworkManager.sync_property(network_uuid, "global_rotation", current_rotation)
|
||||
previous_rotation = current_rotation
|
||||
var current_rotation = global_transform.basis.get_rotation_quaternion()
|
||||
if current_rotation != previous_rotation:
|
||||
NetworkManager.sync_property(network_uuid, "global_rotation", current_rotation)
|
||||
previous_rotation = current_rotation
|
||||
|
||||
# Interpolation for non-piloting players (who only receive the updates)
|
||||
func interpolate_position_and_rotation(delta: float):
|
||||
# Non-piloting players should smoothly interpolate between positions and rotations
|
||||
global_position = lerp(global_position, target_position, acceleration * delta)
|
||||
var current_rotation = global_transform.basis.get_rotation_quaternion()
|
||||
var interpolated_rotation = current_rotation.slerp(target_rotation, acceleration * delta)
|
||||
global_transform.basis = Basis(interpolated_rotation)
|
||||
|
||||
# Callback to handle receiving updates from the network
|
||||
func _on_property_update_received(node_id: String, property_name: String, value: Variant):
|
||||
if node_id == network_uuid:
|
||||
match property_name:
|
||||
"global_position":
|
||||
target_position = value
|
||||
# Only non-piloting players should receive this update
|
||||
if not piloting_player or not piloting_player.is_network_authority:
|
||||
target_position = value
|
||||
"global_rotation":
|
||||
target_rotation = value
|
||||
# Only non-piloting players should receive this update
|
||||
if not piloting_player or not piloting_player.is_network_authority:
|
||||
target_rotation = value
|
||||
|
||||
func set_piloting_player(player: Player):
|
||||
piloting_player = player
|
||||
@ -122,12 +136,14 @@ func remove_piloting_player():
|
||||
ship_is_piloted = false
|
||||
print("The ship is no longer piloted.")
|
||||
|
||||
# When a player enters the ship's detection area
|
||||
func _on_player_entered(body):
|
||||
if body is Player and body.get_parent() != self:
|
||||
body.call_deferred("reparent", self, true)
|
||||
body.ship_entered(self)
|
||||
print("Player entered the ship area.")
|
||||
|
||||
# When a player exits the ship's detection area
|
||||
func _on_player_exited(body):
|
||||
if body is Player and body.get_parent() != get_node("/root/DevLevel/"):
|
||||
body.call_deferred("reparent", get_node("/root/DevLevel/"), true)
|
||||
|
Loading…
Reference in New Issue
Block a user