Full ship rework
This commit is contained in:
parent
c84681fdfa
commit
4bf6eaae0b
@ -416,5 +416,3 @@ func sync_property_to_all(node_id: String, property_name: String, value: Variant
|
||||
func sync_property_to_all_except_host(node_id: String, property_name: String, value: Variant):
|
||||
var packet_data = {"message":"property_update", "node_id": node_id, "property_name":property_name, "value":value}
|
||||
send_p2p_packet(1, packet_data)
|
||||
|
||||
|
||||
|
@ -4,10 +4,6 @@ extends RigidBody3D
|
||||
|
||||
@export var player_detection_area: Area3D
|
||||
@export var helm_location_marker: Marker3D
|
||||
|
||||
var piloting_player: Player = null
|
||||
var ship_is_piloted: bool = false
|
||||
|
||||
@export var base_turn_speed: float = 50.0
|
||||
@export var max_turn_speed: float = 50.0
|
||||
@export var base_lift_speed: float = 50.0
|
||||
@ -15,38 +11,38 @@ var ship_is_piloted: bool = false
|
||||
@export var move_speed: float = 50.0
|
||||
@export var acceleration: float = 50.0
|
||||
|
||||
# Networking
|
||||
var network_uuid: String = ""
|
||||
var piloting_player: Player = null
|
||||
var ship_is_piloted: bool = false
|
||||
var target_position: Vector3 = Vector3.ZERO
|
||||
var target_rotation: Quaternion = Quaternion()
|
||||
var previous_position: Vector3
|
||||
var previous_rotation: Quaternion
|
||||
|
||||
var network_uuid: String = ""
|
||||
|
||||
func _ready():
|
||||
|
||||
network_uuid = NetworkManager.register_node(self)
|
||||
NetworkManager.property_update_received.connect(_on_property_update)
|
||||
|
||||
# Preserve current position and rotation at the start
|
||||
target_position = global_position
|
||||
target_rotation = global_transform.basis.get_rotation_quaternion()
|
||||
previous_position = global_position
|
||||
previous_rotation = global_transform.basis.get_rotation_quaternion()
|
||||
|
||||
NetworkManager.property_update_received.connect(_on_property_update_received)
|
||||
|
||||
player_detection_area.body_entered.connect(_on_player_entered)
|
||||
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:
|
||||
if NetworkManager.is_host:
|
||||
if ship_is_piloted and piloting_player.steam_id == NetworkManager.steam_id:
|
||||
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()
|
||||
|
||||
# Interpolation for non-piloting players
|
||||
if not piloting_player or not piloting_player.is_network_authority:
|
||||
send_network_update()
|
||||
elif ship_is_piloted and piloting_player.steam_id == NetworkManager.steam_id:
|
||||
handle_input(delta)
|
||||
send_network_update()
|
||||
else:
|
||||
interpolate_position_and_rotation(delta)
|
||||
|
||||
func handle_input(_delta: float):
|
||||
@ -88,21 +84,7 @@ func handle_input(_delta: float):
|
||||
var vertical_force = Vector3.UP * vertical_input * base_lift_speed
|
||||
apply_central_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():
|
||||
# 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_to_all(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_to_all(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)
|
||||
@ -110,19 +92,6 @@ func interpolate_position_and_rotation(delta: float):
|
||||
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":
|
||||
# Only non-piloting players should receive this update
|
||||
if not piloting_player or not piloting_player.is_network_authority:
|
||||
target_position = value
|
||||
"global_rotation":
|
||||
# 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
|
||||
ship_is_piloted = piloting_player != null
|
||||
@ -149,3 +118,36 @@ func _on_player_exited(body):
|
||||
body.call_deferred("reparent", get_node("/root/DevLevel/"), true)
|
||||
body.ship_exited()
|
||||
print("Player exited the ship area.")
|
||||
|
||||
|
||||
func send_network_update():
|
||||
if NetworkManager.is_host:
|
||||
if ship_is_piloted and piloting_player.steam_id == NetworkManager.steam_id or !ship_is_piloted:
|
||||
NetworkManager.sync_property_to_all_except_host(network_uuid, "global_position", global_position)
|
||||
NetworkManager.sync_property_to_all_except_host(network_uuid, "global_rotation", global_rotation)
|
||||
elif ship_is_piloted and piloting_player.steam_id == NetworkManager.steam_id:
|
||||
NetworkManager.sync_property_to_all(network_uuid, "global_position", global_position)
|
||||
NetworkManager.sync_property_to_all(network_uuid, "global_rotation", global_rotation)
|
||||
else:
|
||||
GameConsole.log_warning("This should never be hit. If it is, something is wrong with send network logic on the ship.")
|
||||
|
||||
|
||||
func receive_global_position_update(value: Vector3):
|
||||
target_position = value
|
||||
|
||||
|
||||
func receive_global_rotation_update(value: Quaternion):
|
||||
target_rotation = value
|
||||
|
||||
|
||||
func _on_property_update(node_id: String, property_name: String, value: Variant) -> void:
|
||||
if node_id == network_uuid:
|
||||
if property_name == "global_position":
|
||||
receive_global_position_update(value)
|
||||
elif property_name == "global_rotation":
|
||||
receive_global_rotation_update(value)
|
||||
else:
|
||||
var node = NetworkManager.node_map[node_id]
|
||||
node.set(property_name, value)
|
||||
else:
|
||||
GameConsole.log_warning("Received a property update for a node that doesn't match the network UUID of this ship.")
|
||||
|
33
deleteme.txt
Normal file
33
deleteme.txt
Normal file
@ -0,0 +1,33 @@
|
||||
Player 1: Host
|
||||
Player 2: Pilot
|
||||
Player 3: Neither
|
||||
|
||||
|
||||
FOR SENDING UPDATES:
|
||||
if host is pilot or no pilot, host sends ship state
|
||||
if pilot, pilot sends ship state not the host
|
||||
|
||||
FOR RECEIVING UPDATES:
|
||||
if no pilot and not host, receive updates
|
||||
if no pilot and is host, do not receive updates - host is sending updates
|
||||
if pilot, do not receive updates
|
||||
if host and there is a pilot, receive updates from pilot
|
||||
|
||||
|
||||
func send():
|
||||
if (host == pilot or no_pilot) {
|
||||
// host sends ship state
|
||||
} else if (pilot) {
|
||||
// pilot sends ship state
|
||||
}
|
||||
|
||||
func receive():
|
||||
if (no_pilot and not host) {
|
||||
// receive updates from host
|
||||
} else if (no_pilot and is_host) {
|
||||
// do not receive updates - host is sending updates
|
||||
} else if (pilot) {
|
||||
// do not receive updates - sending updates as pilot
|
||||
} else if (host and there is a pilot) {
|
||||
// receive updates from pilot - receiving updates as host from pilot
|
||||
}
|
Loading…
Reference in New Issue
Block a user