bugfix, desync, and add UI function

This commit is contained in:
2026-01-14 00:20:20 +08:00
parent 6948a4aed1
commit bee9c30f0e
14 changed files with 362 additions and 112 deletions
+26 -6
View File
@@ -78,6 +78,8 @@ var spawn_locations = [
var finish_locations: Array:
get: return race_manager.finish_locations if race_manager else []
var target_visual_position: Vector3 = Vector3.ZERO # For client-side smoothing
var spawn_point_selected = false
# Action for hilighter
@@ -145,8 +147,8 @@ func _ready():
if is_multiplayer_authority():
rpc("sync_display_name", display_name)
# Wait briefly to ensure proper scene setup
await get_tree().create_timer(0.1).timeout
# Wait briefly to ensure proper scene setup and server recognition
await get_tree().create_timer(0.5).timeout
# More robust way to get the main scene
var main_scene = get_tree().get_root().get_node_or_null("Main")
@@ -226,8 +228,11 @@ func _ready():
1.0,
current_position.y * cell_size.z + cell_size.z * 0.5
)
target_visual_position = global_position
if is_multiplayer_authority():
rpc("sync_position", current_position)
else:
target_visual_position = global_position
func _init_managers():
movement_manager = load("res://scripts/managers/player_movement_manager.gd").new()
@@ -609,6 +614,11 @@ func _process(delta):
if _verify_timer >= 3.0:
_verify_timer = 0.0
rpc("ping_existence")
else:
# Client-side visual smoothing
# Interpolate towards the target position received from authority
if global_position.distance_squared_to(target_visual_position) > 0.001:
global_position = global_position.lerp(target_visual_position, delta * 15.0)
# Delegate rotation to movement manager
if movement_manager:
@@ -942,7 +952,8 @@ static func reset_race_stats():
@rpc("any_peer", "call_local", "unreliable")
func remote_set_position(authority_position):
global_position = authority_position
# Don't snap directly, update target for interpolation
target_visual_position = authority_position
@rpc("any_peer", "call_local")
func display_message(message, type: int = 0):
@@ -1371,11 +1382,14 @@ func bot_arrange_item(from_slot: int, to_slot: int):
func update_visual_position():
# Ensure proper grid-aligned positioning
global_position = Vector3(
var new_pos = Vector3(
current_position.x * cell_size.x + cell_size.x * 0.5,
1.0,
current_position.y * cell_size.z + cell_size.z * 0.5
)
global_position = new_pos
target_visual_position = new_pos # Snap target too
if is_multiplayer_authority():
rpc("sync_position", current_position)
@@ -1383,11 +1397,14 @@ func update_visual_position():
func sync_position(pos: Vector2i):
current_position = pos
# Always update the visual position after position sync
global_position = Vector3(
var new_pos = Vector3(
current_position.x * cell_size.x + cell_size.x * 0.5,
cell_size.y,
current_position.y * cell_size.z + cell_size.z * 0.5
) + cell_offset
global_position = new_pos
target_visual_position = new_pos # Reset smoothing target to prevent fighting
@rpc("any_peer", "call_local", "reliable")
func set_spawn_position(pos: Vector2i):
@@ -1397,11 +1414,14 @@ func set_spawn_position(pos: Vector2i):
# Clear any spawn highlights
clear_spawn_highlights()
# Update visual position
global_position = Vector3(
var new_pos = Vector3(
current_position.x * cell_size.x + cell_size.x * 0.5,
cell_size.y,
current_position.y * cell_size.z + cell_size.z * 0.5
) + cell_offset
global_position = new_pos
target_visual_position = new_pos
@rpc("any_peer", "call_local", "reliable")