Merge branch 'fix/networking-issues' into 'develop'

🐛 Fix network issues affecting flag and player movement

See merge request open-fpsz/open-fpsz!42
This commit is contained in:
Squinty 2024-04-17 16:07:25 +00:00
commit dbe9545590
9 changed files with 34 additions and 33 deletions

View file

@ -32,18 +32,16 @@ func _process(_delta):
_carried_flag.global_position = attachment.global_position _carried_flag.global_position = attachment.global_position
_carried_flag.global_rotation = attachment.global_rotation _carried_flag.global_rotation = attachment.global_rotation
@rpc("call_local")
func _grab(flag : Flag): func _grab(flag : Flag):
if not _is_carrying(): if not _is_carrying():
flag.grab() flag.grab()
_carried_flag = flag _carried_flag = flag
@rpc("call_local") func _release(inherited_velocity : Vector3, throw_speed : float):
func _drop(throw_speed : float):
if _is_carrying(): if _is_carrying():
_carried_flag.drop() _carried_flag.drop()
_carried_flag.rotation_degrees.x = 0.0 _carried_flag.rotation_degrees.x = 0.0
_carried_flag.linear_velocity = attachment.global_basis.z * throw_speed _carried_flag.linear_velocity = inherited_velocity + (attachment.global_basis.z * throw_speed)
_carried_flag = null _carried_flag = null
func _is_carrying() -> bool: func _is_carrying() -> bool:
@ -51,10 +49,10 @@ func _is_carrying() -> bool:
func _sensor_on_body_entered(collider): func _sensor_on_body_entered(collider):
if collider is Flag: if collider is Flag:
_grab.rpc(collider) _grab(collider)
func drop(): func drop():
_drop.rpc(0.0) _release(Vector3.ZERO, 0.0)
func throw(): func throw(inherited_velocity : Vector3):
_drop.rpc(max_throw_speed) _release(inherited_velocity, max_throw_speed)

View file

@ -1,7 +1,7 @@
[gd_scene load_steps=6 format=3 uid="uid://c88l3h0ph00c7"] [gd_scene load_steps=6 format=3 uid="uid://c88l3h0ph00c7"]
[ext_resource type="Script" path="res://entities/flag/flag.gd" id="1_y7d3d"] [ext_resource type="Script" path="res://entities/flag/flag.gd" id="1_y7d3d"]
[ext_resource type="PackedScene" uid="uid://bftyiy6r0xaa3" path="res://entities/flag/assets/flag.glb" id="2_i78em"] [ext_resource type="PackedScene" uid="uid://d3l7fvbdg6m5g" path="res://entities/flag/assets/flag.glb" id="2_i78em"]
[sub_resource type="PhysicsMaterial" id="PhysicsMaterial_4ymrw"] [sub_resource type="PhysicsMaterial" id="PhysicsMaterial_4ymrw"]
bounce = 0.5 bounce = 0.5

View file

@ -19,6 +19,7 @@ enum PlayerState { PLAYER_ALIVE, PLAYER_DEAD }
@export_category("Parameters") @export_category("Parameters")
@export var ground_speed : float = 48 / 3.6 # m/s @export var ground_speed : float = 48 / 3.6 # m/s
@export var aerial_control_force : int = 400 @export var aerial_control_force : int = 400
@export var jump_height : float = 2.0
@export var max_floor_angle : float = 60 @export var max_floor_angle : float = 60
@export_group("Jetpack") @export_group("Jetpack")
@ -32,7 +33,7 @@ enum PlayerState { PLAYER_ALIVE, PLAYER_DEAD }
@export_group("State") @export_group("State")
@export var player_state = PlayerState.PLAYER_ALIVE @export var player_state = PlayerState.PLAYER_ALIVE
@export var player_id = 1: @export var player_id : int = 1:
set(id): set(id):
player_id = id player_id = id
$PlayerInput.set_multiplayer_authority(id) $PlayerInput.set_multiplayer_authority(id)
@ -60,16 +61,16 @@ var _jumping = false
func _ready(): func _ready():
energy_changed.connect(hud._on_energy_changed) energy_changed.connect(hud._on_energy_changed)
health_component.health_changed.connect(hud._on_health_changed)
health_component.health_changed.connect(iff._on_health_changed)
health_component.health_changed.emit(health_component.health)
health_component.health_zeroed.connect(_die)
if _is_first_person(): if _is_first_person():
health_component.health_changed.connect(hud._on_health_changed)
camera.current = true camera.current = true
remove_child($ThirdPerson) remove_child($ThirdPerson)
else: else:
remove_child($HUD) health_component.health_changed.connect(iff._on_health_changed)
remove_child(hud)
weapon.hide() weapon.hide()
health_component.health_changed.emit(health_component.health)
health_component.health_zeroed.connect(die)
input.fired_primary.connect(_fire_primary) input.fired_primary.connect(_fire_primary)
input.jumped.connect(_jump) input.jumped.connect(_jump)
input.throwed_flag.connect(_throw_flag) input.throwed_flag.connect(_throw_flag)
@ -96,7 +97,7 @@ func _jump():
_jumping = true _jumping = true
func _throw_flag(): func _throw_flag():
flag_carry_component.throw() flag_carry_component.throw(linear_velocity)
func is_on_floor() -> bool: func is_on_floor() -> bool:
if shape_cast.is_colliding(): if shape_cast.is_colliding():
@ -176,8 +177,10 @@ func _physics_process(delta):
linear_velocity = lerp(linear_velocity, _direction * ground_speed, .1) linear_velocity = lerp(linear_velocity, _direction * ground_speed, .1)
if _jumping: func _integrate_forces(_state):
linear_velocity.y = sqrt(2 * abs((mass * gravity * delta).y) * 1) if is_on_floor() and _jumping:
var v = sqrt(2 * g * jump_height)
apply_central_impulse(Vector3(0, mass * v, 0))
_jumping = false _jumping = false
@ -202,7 +205,7 @@ func _update_third_person_animations():
func _is_player_dead(): func _is_player_dead():
return player_state == PlayerState.PLAYER_DEAD return player_state == PlayerState.PLAYER_DEAD
func _die(): func die():
player_state = PlayerState.PLAYER_DEAD player_state = PlayerState.PLAYER_DEAD
if _is_first_person(): if _is_first_person():
animation_player.stop() animation_player.stop()

View file

@ -207,6 +207,7 @@ mass = 75.0
physics_material_override = SubResource("PhysicsMaterial_clur0") physics_material_override = SubResource("PhysicsMaterial_clur0")
continuous_cd = true continuous_cd = true
script = ExtResource("1_mk68k") script = ExtResource("1_mk68k")
jump_height = 1.5
[node name="MeshInstance3D" type="MeshInstance3D" parent="."] [node name="MeshInstance3D" type="MeshInstance3D" parent="."]
visible = false visible = false

View file

@ -41,7 +41,7 @@ func _unhandled_input(event: InputEvent) -> void:
# retrieve mouse position relative to last frame # retrieve mouse position relative to last frame
_mouse_position = event.relative * MOUSE_SENSITIVITY _mouse_position = event.relative * MOUSE_SENSITIVITY
func _process(_delta): func _process(delta):
direction = Input.get_vector("left", "right", "forward", "backward") direction = Input.get_vector("left", "right", "forward", "backward")
if Input.is_action_just_pressed("jump_and_jet"): if Input.is_action_just_pressed("jump_and_jet"):
_jump.rpc() _jump.rpc()
@ -51,8 +51,6 @@ func _process(_delta):
_throw_flag.rpc() _throw_flag.rpc()
jetting = Input.is_action_pressed("jump_and_jet") jetting = Input.is_action_pressed("jump_and_jet")
skiing = Input.is_action_pressed("ski") skiing = Input.is_action_pressed("ski")
func _physics_process(delta):
_update_camera(delta) _update_camera(delta)
@rpc("call_local") @rpc("call_local")

View file

@ -31,7 +31,6 @@ func start_server(port, nickname):
load_map.call_deferred(MAP) load_map.call_deferred(MAP)
multiplayer.peer_connected.connect(add_player)
multiplayer.peer_disconnected.connect(remove_player) multiplayer.peer_disconnected.connect(remove_player)
if DisplayServer.get_name() != \"headless\": if DisplayServer.get_name() != \"headless\":
@ -69,7 +68,9 @@ func add_player(peer_id : int, nickname : String):
func remove_player(peer_id : int): func remove_player(peer_id : int):
var node_name = str(peer_id) var node_name = str(peer_id)
if players.has_node(node_name): if players.has_node(node_name):
players.get_node(node_name).queue_free() var player : Player = players.get_node(node_name)
player.die()
player.queue_free()
print(\"Peer `%s` disconnected\" % node_name) print(\"Peer `%s` disconnected\" % node_name)
@rpc(\"any_peer\") @rpc(\"any_peer\")