move to apply_input_velocity on the subject

This commit is contained in:
2025-02-23 18:25:57 +09:00
parent ca63d4594c
commit 151a757e3e
13 changed files with 142 additions and 62 deletions
+4 -1
View File
@@ -15,7 +15,10 @@ func spawn_player(player_id: int, runner: bool) -> void:
player.player_id = player_id
player.name = str(player_id)
_runners_node.add_child(player) if runner else _chasers_node.add_child(player)
if runner:
_runners_node.add_child(player)
else:
_chasers_node.add_child(player)
func despawn_player(player_id: int) -> void:
if not multiplayer.is_server(): return
+9
View File
@@ -13,7 +13,16 @@ func _ready() -> void:
set_physics_process(false)
func _physics_process(_delta: float) -> void:
if get_multiplayer_authority() != multiplayer.get_unique_id(): return
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()
walk = Input.is_action_pressed("walk")
func _input(_event: InputEvent) -> void:
if get_multiplayer_authority() != multiplayer.get_unique_id(): return
if Input.get_mouse_mode() != Input.MOUSE_MODE_CAPTURED: return
primary_interact = Input.is_action_pressed("primary_interact")
secondary_interact = Input.is_action_pressed("secondary_interact")
+21 -8
View File
@@ -33,7 +33,7 @@ extends CharacterBody3D
@onready var _camera_pivot: Node3D = %CameraPivot
@onready var _camera_platform: Node3D = %CameraPlatform
@onready var _camera: Node3D = %Camera
@onready var skin: Node3D = %Skin
@onready var rotation_base: Node3D = %RotationBase
var camera_input_direction := Vector2.ZERO
var last_direction := Vector3.FORWARD
@@ -46,9 +46,9 @@ func _ready() -> void:
if multiplayer.is_server(): return
if multiplayer.get_unique_id() == player_id:
%Camera.make_current()
_camera.make_current()
else:
%Camera.clear_current(false)
_camera.clear_current(false)
func _input(event: InputEvent) -> void:
if multiplayer.get_unique_id() != player_id: return
@@ -86,21 +86,34 @@ func _physics_process(delta: float) -> void:
if not multiplayer.is_server():
if multiplayer.get_unique_id() == player_id and client_prediction:
predicted_position = position
predicted_rotation = skin.global_rotation
predicted_rotation = rotation_base.global_rotation
if multiplayer.get_unique_id() != player_id or not client_prediction:
if client_smoothing:
position = lerp(position, server_position, client_smoothing_speed * delta)
skin.global_rotation.y = lerp_angle(skin.global_rotation.y, server_rotation.y, client_smoothing_rotation_speed * delta)
rotation_base.global_rotation.x = server_rotation.x
rotation_base.global_rotation.y = lerp_angle(rotation_base.global_rotation.y, server_rotation.y, client_smoothing_rotation_speed * delta)
rotation_base.global_rotation.z = server_rotation.z
else:
position = server_position
skin.global_rotation.y = server_rotation.y
rotation_base.global_rotation = server_rotation
#
if multiplayer.is_server():
server_position = position
server_rotation = skin.global_rotation
server_rotation = rotation_base.global_rotation
func _on_sync_delta_synchronized() -> void:
if client_prediction and server_position.distance_to(predicted_position) > client_prediction_tolerance:
print("%v VS %v" % [server_position, predicted_position])
position = server_position
func apply_input_velocity(delta: float, input_direction: Vector2, speed: float, acceleration: float) -> void:
var target_velocity = Vector3(input_direction.x * speed, velocity.y, input_direction.y * speed)
velocity = velocity.move_toward(target_velocity, acceleration * delta) + Main.gravity_velocity
move_and_slide()
if input_direction.length() >= 0.1:
last_direction = Vector3(input_direction.x, 0, input_direction.y)
var target_angle := Vector3.FORWARD.signed_angle_to(last_direction, Vector3.UP)
rotation_base.global_rotation.y = lerp_angle(rotation_base.rotation.y, target_angle, rotation_speed * delta)
+12
View File
@@ -0,0 +1,12 @@
extends State
func enter() -> void:
subject.rotation_base.global_rotation.z = PI / 2
func exit() -> void:
subject.rotation_base.global_rotation.z = 0
func process_physics(_delta: float) -> State:
if not multiplayer.is_server(): return
return
+2 -5
View File
@@ -3,12 +3,9 @@ extends State
func process_physics(delta: float) -> State:
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()
subject.apply_input_velocity(delta, Vector2.ZERO, 0.0, subject.air_deceleration)
if not subject.is_on_floor(): return
if %Input.direction.length() < 0.05: return %Idle
if %Input.walk: return %Walk
if has_node("%Walk") and %Input.walk: return %Walk
return %Run
+3 -5
View File
@@ -3,14 +3,12 @@ extends State
func process_physics(delta: float) -> State:
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()
subject.apply_input_velocity(delta, Vector2.ZERO, 0.0, subject.acceleration)
if not subject.is_on_floor(): return %Fall
if has_node("%Lunge") and %Input.primary_interact: return %Lunge # TODO: Lunge attacks currently do nothing so Chasers can't move when they click in
var input_direction = %Input.direction
if input_direction.length() < 0.05: return
if has_node("%Walk") and %Input.walk: return %Walk
return %Run
+11
View File
@@ -0,0 +1,11 @@
extends State
func process_physics(delta: float) -> State:
if not Main.is_server_or_predicting(subject.player_id, subject.client_prediction): return
var input_direction = %Input.direction if subject.is_on_floor() else Vector2.ZERO
subject.apply_input_velocity(delta, input_direction, subject.run_speed, subject.acceleration if subject.is_on_floor() else subject.air_deceleration)
if has_node("%Walk") and %Input.walk: return %Walk
return
+2 -11
View File
@@ -6,17 +6,8 @@ func process_physics(delta: float) -> State:
var input_direction = %Input.direction
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)
subject.velocity = subject.velocity.move_toward(target_velocity, subject.acceleration * delta)
subject.velocity += Main.gravity_velocity
subject.move_and_slide()
subject.apply_input_velocity(delta, input_direction, subject.run_speed, subject.acceleration)
if move_direction.length() >= 0.1:
subject.last_direction = move_direction
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.walk: return %Walk
if has_node("%Walk") and %Input.walk: return %Walk
if not subject.is_on_floor(): return %Fall
return
+1
View File
@@ -4,6 +4,7 @@ extends Node
var subject
func enter() -> void:
print("Entering state: %s" % name)
pass
func exit() -> void:
+1 -10
View File
@@ -6,16 +6,7 @@ func process_physics(delta: float) -> State:
var input_direction = %Input.direction
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)
subject.velocity = subject.velocity.move_toward(target_velocity, subject.acceleration * delta)
subject.velocity += Main.gravity_velocity
subject.move_and_slide()
if move_direction.length() >= 0.1:
subject.last_direction = move_direction
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)
subject.apply_input_velocity(delta, input_direction, subject.walk_speed, subject.acceleration)
if not %Input.walk: return %Run
if not subject.is_on_floor(): return %Fall