despawn runner or chaser on disconnect
This commit is contained in:
+15
-7
@@ -9,16 +9,24 @@ var _ping := 0.0
|
||||
@onready var _runners_node: Node3D = %Runners
|
||||
|
||||
func spawn_player(player_id: int, runner: bool) -> void:
|
||||
if multiplayer.is_server():
|
||||
var player = runner_scene.instantiate() if runner else chaser_scene.instantiate()
|
||||
player.player_id = player_id
|
||||
player.name = str(player_id)
|
||||
if not multiplayer.is_server(): return
|
||||
|
||||
_runners_node.add_child(player) if runner else _chasers_node.add_child(player)
|
||||
var player = runner_scene.instantiate() if runner else chaser_scene.instantiate()
|
||||
player.player_id = player_id
|
||||
player.name = str(player_id)
|
||||
|
||||
_runners_node.add_child(player) if runner else _chasers_node.add_child(player)
|
||||
|
||||
func despawn_player(player_id: int) -> void:
|
||||
if not multiplayer.is_server(): return
|
||||
|
||||
var player_id_str := str(player_id)
|
||||
for runner_or_chaser in _runners_node.get_children() + _chasers_node.get_children():
|
||||
if runner_or_chaser.name == player_id_str:
|
||||
runner_or_chaser.queue_free()
|
||||
|
||||
func _process(_delta: float) -> void:
|
||||
if DisplayServer.get_name() == "headless":
|
||||
return
|
||||
if DisplayServer.get_name() == "headless": return
|
||||
|
||||
%StatsLabel.text = statsTemplate % [
|
||||
Engine.get_frames_per_second(),
|
||||
|
||||
@@ -5,3 +5,6 @@ var gravity_vector: Vector3 = ProjectSettings.get_setting("physics/3d/default_gr
|
||||
var physics_tickrate = Engine.get_physics_ticks_per_second()
|
||||
var terminal_velocity: float = 150.0
|
||||
var gravity_velocity: Vector3 = (gravity_vector * gravity) / (physics_tickrate as float)
|
||||
|
||||
func is_server_or_predicting(player_id: int, client_prediction: bool = false):
|
||||
return multiplayer.is_server() or (multiplayer.get_unique_id() == player_id and client_prediction)
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
extends MultiplayerSynchronizer
|
||||
|
||||
@export var direction: Vector2 = Vector2.ZERO
|
||||
@export var walking: bool = false
|
||||
@export var walk: bool = false
|
||||
@export var primary_interact: bool = false
|
||||
@export var secondary_interact: bool = false
|
||||
|
||||
@onready var _camera_pivot: Node3D = %CameraPivot
|
||||
|
||||
@@ -9,10 +11,9 @@ func _ready() -> void:
|
||||
if multiplayer.is_server() or get_multiplayer_authority() != multiplayer.get_unique_id():
|
||||
set_process(false)
|
||||
set_physics_process(false)
|
||||
return
|
||||
|
||||
func _physics_process(_delta: float) -> void:
|
||||
var directional_input := Input.get_vector("move_right", "move_left", "move_forward", "move_back")
|
||||
var camera_adjusted: Vector3 = (directional_input.x * _camera_pivot.global_basis.z) + (directional_input.y * _camera_pivot.global_basis.x)
|
||||
direction = Vector2(camera_adjusted.x, camera_adjusted.z).rotated(PI / 2.0).normalized()
|
||||
walking = Input.is_action_pressed("walk")
|
||||
walk = Input.is_action_pressed("walk")
|
||||
|
||||
@@ -26,9 +26,8 @@ func _ready() -> void:
|
||||
get_tree().change_scene_to_packed.call_deferred(main_menu_scene)
|
||||
|
||||
func connect_to_ip(runner: bool = true, ip: String = "") -> void:
|
||||
if ip.is_empty():
|
||||
ip = IP_ADDRESS
|
||||
|
||||
if ip.is_empty(): ip = IP_ADDRESS
|
||||
|
||||
runner_client_selection = runner
|
||||
|
||||
print("Connecting to: %s" % ip)
|
||||
@@ -46,25 +45,23 @@ func _on_connect(id: int) -> void:
|
||||
|
||||
func _on_disconnect(id: int) -> void:
|
||||
print("Client ID #%s disconnected" % id)
|
||||
pass
|
||||
if not multiplayer.is_server(): return
|
||||
|
||||
get_tree().get_current_scene().despawn_player(id)
|
||||
|
||||
func _on_connect_client() -> void:
|
||||
request_is_runner.rpc_id(1, runner_client_selection)
|
||||
print("[%s] Connected to server" % multiplayer.get_unique_id())
|
||||
pass
|
||||
|
||||
func _on_disconnect_client() -> void:
|
||||
print("[%s] Disconnected" % multiplayer.get_unique_id())
|
||||
pass
|
||||
|
||||
func _on_server_closed_client() -> void:
|
||||
print("[%s] Server closed" % multiplayer.get_unique_id())
|
||||
pass
|
||||
|
||||
@rpc("any_peer", "call_remote", "reliable")
|
||||
func request_is_runner(runner: bool) -> void:
|
||||
if not multiplayer.is_server():
|
||||
return
|
||||
if not multiplayer.is_server(): return
|
||||
|
||||
var id := multiplayer.get_remote_sender_id()
|
||||
runner_dict[id] = runner
|
||||
|
||||
+9
-11
@@ -43,8 +43,7 @@ var predicted_rotation := Vector3.ZERO
|
||||
|
||||
func _ready() -> void:
|
||||
state_machine.set_subject(self)
|
||||
if multiplayer.is_server():
|
||||
return
|
||||
if multiplayer.is_server(): return
|
||||
|
||||
if multiplayer.get_unique_id() == player_id:
|
||||
%Camera.make_current()
|
||||
@@ -52,8 +51,7 @@ func _ready() -> void:
|
||||
%Camera.clear_current(false)
|
||||
|
||||
func _input(event: InputEvent) -> void:
|
||||
if multiplayer.get_unique_id() != player_id:
|
||||
return
|
||||
if multiplayer.get_unique_id() != player_id: return
|
||||
|
||||
if event.is_action_pressed("mouse_capture"):
|
||||
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
|
||||
@@ -65,13 +63,14 @@ func _input(event: InputEvent) -> void:
|
||||
camera_input_direction += event.screen_relative * mouse_sensitivity
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
if multiplayer.get_unique_id() == player_id:
|
||||
_camera_platform.global_rotation = _camera_pivot.global_rotation
|
||||
if multiplayer.get_unique_id() != player_id: return
|
||||
|
||||
if smooth_camera:
|
||||
_camera_platform.global_position = lerp(_camera_platform.global_position, _camera_pivot.global_position, camera_follow_speed * delta)
|
||||
else:
|
||||
_camera_platform.global_position = _camera_pivot.global_position
|
||||
_camera_platform.global_rotation = _camera_pivot.global_rotation
|
||||
|
||||
if smooth_camera:
|
||||
_camera_platform.global_position = lerp(_camera_platform.global_position, _camera_pivot.global_position, camera_follow_speed * delta)
|
||||
else:
|
||||
_camera_platform.global_position = _camera_pivot.global_position
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
state_machine.process_physics(delta)
|
||||
@@ -100,7 +99,6 @@ func _physics_process(delta: float) -> void:
|
||||
if multiplayer.is_server():
|
||||
server_position = position
|
||||
server_rotation = skin.global_rotation
|
||||
pass
|
||||
|
||||
func _on_sync_delta_synchronized() -> void:
|
||||
if client_prediction and server_position.distance_to(predicted_position) > client_prediction_tolerance:
|
||||
|
||||
+5
-13
@@ -1,22 +1,14 @@
|
||||
extends State
|
||||
|
||||
func process_physics(delta: float) -> State:
|
||||
if not multiplayer.is_server() and (multiplayer.get_unique_id() != subject.player_id or not subject.client_prediction):
|
||||
return
|
||||
if not Main.is_server_or_predicting(subject.player_id, subject.client_prediction): return
|
||||
|
||||
var target_velocity = Vector3(0, subject.velocity.y, 0)
|
||||
subject.velocity = subject.velocity.move_toward(target_velocity, subject.air_deceleration * delta)
|
||||
subject.velocity += Main.gravity_velocity
|
||||
subject.move_and_slide()
|
||||
|
||||
if subject.is_on_floor():
|
||||
var input_direction = %Input.direction
|
||||
if input_direction.length() < 0.05:
|
||||
return %Idle
|
||||
|
||||
if %Input.walking:
|
||||
return %Walk
|
||||
|
||||
return %Run
|
||||
|
||||
return
|
||||
if not subject.is_on_floor(): return
|
||||
if %Input.direction.length() < 0.05: return %Idle
|
||||
if %Input.walk: return %Walk
|
||||
return %Run
|
||||
|
||||
@@ -1,19 +1,16 @@
|
||||
extends State
|
||||
|
||||
func process_physics(delta: float) -> State:
|
||||
if not multiplayer.is_server() and (multiplayer.get_unique_id() != subject.player_id or not subject.client_prediction):
|
||||
return
|
||||
if not Main.is_server_or_predicting(subject.player_id, subject.client_prediction): return
|
||||
|
||||
var target_velocity = Vector3(0, subject.velocity.y, 0)
|
||||
subject.velocity = subject.velocity.move_toward(target_velocity, subject.acceleration * delta)
|
||||
subject.velocity += Main.gravity_velocity
|
||||
subject.move_and_slide()
|
||||
|
||||
if not subject.is_on_floor():
|
||||
return %Fall
|
||||
if not subject.is_on_floor(): return %Fall
|
||||
|
||||
var input_direction = %Input.direction
|
||||
if input_direction.length() < 0.05:
|
||||
return
|
||||
if input_direction.length() < 0.05: return
|
||||
|
||||
return %Run
|
||||
|
||||
+4
-10
@@ -1,12 +1,10 @@
|
||||
extends State
|
||||
|
||||
func process_physics(delta: float) -> State:
|
||||
if not multiplayer.is_server() and (multiplayer.get_unique_id() != subject.player_id or not subject.client_prediction):
|
||||
return
|
||||
if not Main.is_server_or_predicting(subject.player_id, subject.client_prediction): return
|
||||
|
||||
var input_direction = %Input.direction
|
||||
if input_direction.length() < 0.05:
|
||||
return %Idle
|
||||
if input_direction.length() < 0.05: return %Idle
|
||||
|
||||
var move_direction = Vector3(input_direction.x, 0, input_direction.y)
|
||||
var target_velocity = Vector3(move_direction.x * subject.run_speed, subject.velocity.y, move_direction.z * subject.run_speed)
|
||||
@@ -19,10 +17,6 @@ func process_physics(delta: float) -> State:
|
||||
var target_angle := Vector3.FORWARD.signed_angle_to(subject.last_direction, Vector3.UP)
|
||||
subject.skin.global_rotation.y = lerp_angle(subject.skin.rotation.y, target_angle, subject.rotation_speed * delta)
|
||||
|
||||
if %Input.walking:
|
||||
return %Walk
|
||||
|
||||
if not subject.is_on_floor():
|
||||
return %Fall
|
||||
|
||||
if %Input.walk: return %Walk
|
||||
if not subject.is_on_floor(): return %Fall
|
||||
return
|
||||
|
||||
@@ -9,42 +9,27 @@ func set_subject(new_subject: Node) -> void:
|
||||
state.subject = subject
|
||||
|
||||
func _change_state(new_state: State) -> void:
|
||||
if current_state:
|
||||
#print("Exiting: %s" % current_state.name)
|
||||
current_state.exit()
|
||||
if current_state: current_state.exit()
|
||||
|
||||
current_state = new_state
|
||||
#print("Entering: %s" % current_state.name)
|
||||
current_state.enter()
|
||||
|
||||
func process_physics(delta: float) -> void:
|
||||
if not current_state:
|
||||
return
|
||||
|
||||
if not current_state.subject:
|
||||
return
|
||||
if not current_state or not current_state.subject: return
|
||||
|
||||
var new_state = current_state.process_physics(delta)
|
||||
if new_state:
|
||||
_change_state(new_state)
|
||||
|
||||
func process_input(event: InputEvent) -> void:
|
||||
if not current_state:
|
||||
return
|
||||
|
||||
if not current_state.subject:
|
||||
return
|
||||
if not current_state or not current_state.subject: return
|
||||
|
||||
var new_state = current_state.process_input(event)
|
||||
if new_state:
|
||||
_change_state(new_state)
|
||||
|
||||
func process_frame(delta: float) -> void:
|
||||
if not current_state:
|
||||
return
|
||||
|
||||
if not current_state.subject:
|
||||
return
|
||||
if not current_state or not current_state.subject: return
|
||||
|
||||
var new_state = current_state.process_frame(delta)
|
||||
if new_state:
|
||||
|
||||
+4
-10
@@ -1,12 +1,10 @@
|
||||
extends State
|
||||
|
||||
func process_physics(delta: float) -> State:
|
||||
if not multiplayer.is_server() and (multiplayer.get_unique_id() != subject.player_id or not subject.client_prediction):
|
||||
return
|
||||
if not Main.is_server_or_predicting(subject.player_id, subject.client_prediction): return
|
||||
|
||||
var input_direction = %Input.direction
|
||||
if input_direction.length() < 0.05:
|
||||
return %Idle
|
||||
if input_direction.length() < 0.05: return %Idle
|
||||
|
||||
var move_direction = Vector3(input_direction.x, 0, input_direction.y)
|
||||
var target_velocity = Vector3(move_direction.x * subject.walk_speed, subject.velocity.y, move_direction.z * subject.walk_speed)
|
||||
@@ -19,10 +17,6 @@ func process_physics(delta: float) -> State:
|
||||
var target_angle := Vector3.FORWARD.signed_angle_to(subject.last_direction, Vector3.UP)
|
||||
subject.skin.global_rotation.y = lerp_angle(subject.skin.rotation.y, target_angle, subject.rotation_speed * delta)
|
||||
|
||||
if not %Input.walking:
|
||||
return %Run
|
||||
|
||||
if not subject.is_on_floor():
|
||||
return %Fall
|
||||
|
||||
if not %Input.walk: return %Run
|
||||
if not subject.is_on_floor(): return %Fall
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user