PC3 Phase 2.3: networked play path in the game

- Session autoload: carries {world_id, host, port} from the world browser
  to the game scene.
- world_list.gd "Enter World": POST /api/worlds/{id}/join -> Session.begin
  -> load game.tscn. On 503 (backend has no orchestration) falls back to
  local single-player; other errors surface on the card.
- api_client.gd: join_world().
- game.gd (new, on game.tscn root): local single-player by default; when
  Session.active, connects a NetworkClient to the world's dedicated server
  and runs networked -- forwards the local Player's input each physics
  frame, snaps to the server position past a generous drift threshold
  (the Player keeps predicting locally with real collision), renders other
  players from snapshots (labelled capsules, lerped -- avatar appearance
  sync is a follow-up, the server doesn't replicate loadouts yet), and
  returns to the world list on disconnect.
- player.gd: exposes net_input (the input frame it applied) and
  apply_server_correction().
- network_client.gd: send_input() + get_own_server_state() for callers that
  do their own collision-aware prediction.
- scripts/mp_game_flow_client.gd + run_game_multiplayer_test.sh: two real
  clients each register -> join via the API -> game.tscn -> networked;
  A sees B and moves. PASS.

Verified end to end against an orchestration-enabled backend. Non-networked
smokes green (enter_world 9/9, world_list 5/5, player 15/15,
avatar_in_world 10/10, main_menu_full_flow 12/12).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015jtxAiu5dAW4WxCNdGkrkJ
This commit is contained in:
2026-09-05 22:07:06 +00:00
parent 260b7a38da
commit dba865ae4b
10 changed files with 369 additions and 9 deletions
+141
View File
@@ -0,0 +1,141 @@
extends Node3D
## Root controller for scenes/game.tscn.
##
## Local single-player by default (walk around the test level). When
## Session.active (set by world_list.gd after POST /api/worlds/{id}/join),
## connects a NetworkClient to the world's dedicated server and runs
## networked: the local Player keeps predicting with its own collision-aware
## movement, its input is forwarded to the server each physics frame, the
## server's authoritative position is a correction backstop, and other
## players are rendered from snapshots.
##
## Appearance sync is a follow-up -- remote players show as labelled capsules,
## not their own avatars (the server doesn't replicate loadouts yet).
const REMOTE_LERP := 12.0
## Snap the local player to the server position past this drift (metres).
## Generous -- only catches real desync / teleport / a wall the client walked
## through, not normal prediction noise.
const CORRECTION_THRESHOLD := 1.5
@onready var _player: Player = $Player
var _net: NetworkClient = null
var _networked := false
var _remotes: Dictionary = {} # peer_id -> Node3D
var _status_label: Label = null
func _ready() -> void:
if Session.active:
_setup_networked()
func _setup_networked() -> void:
_show_status("Connecting to %s…" % Session.host)
_net = NetworkClient.new()
_net.name = "NetworkClient"
add_child(_net)
_net.server_disconnected.connect(_on_server_lost)
_net.connection_failed.connect(_on_server_lost)
var ok: bool = await _net.connect_to_server(Session.host, Session.port)
if not ok:
_on_server_lost()
return
_networked = true
_hide_status()
# Physics-process AFTER the player so net_input reflects this frame.
process_physics_priority = 10
func _physics_process(_delta: float) -> void:
if not _networked or _net == null or not _net.is_connected_to_server():
return
# 1. Forward the local player's input to the server.
var inp: Dictionary = _player.net_input
_net.send_input(inp["move_x"], inp["move_z"], inp["jump"], inp["look_yaw"])
# 2. Server-authoritative correction backstop.
var own: Dictionary = _net.get_own_server_state()
if own.has("position"):
var server_pos: Vector3 = own["position"]
if _player.global_position.distance_to(server_pos) > CORRECTION_THRESHOLD:
_player.apply_server_correction(server_pos)
# 3. Remote players.
_sync_remotes(_net.get_remote_states(), _delta)
func _sync_remotes(states: Dictionary, delta: float) -> void:
for pid in states:
var st: Dictionary = states[pid]
var node: Node3D = _remotes.get(pid)
if node == null:
node = _make_remote(pid)
_remotes[pid] = node
add_child(node)
node.global_position = st.get("position", Vector3.ZERO)
var target: Vector3 = st.get("position", node.global_position)
node.global_position = node.global_position.lerp(target, clampf(REMOTE_LERP * delta, 0.0, 1.0))
node.rotation.y = st.get("rotation_y", node.rotation.y)
for pid in _remotes.keys():
if not states.has(pid):
_remotes[pid].queue_free()
_remotes.erase(pid)
func _make_remote(pid: int) -> Node3D:
var root := Node3D.new()
root.name = "Remote_%d" % pid
var body := MeshInstance3D.new()
var capsule := CapsuleMesh.new()
capsule.radius = 0.35
capsule.height = 1.7
body.mesh = capsule
body.position = Vector3(0, 0.85, 0)
var mat := StandardMaterial3D.new()
mat.albedo_color = Color.from_hsv(fmod(float(pid) * 0.61803, 1.0), 0.6, 0.9)
body.material_override = mat
root.add_child(body)
var tag := Label3D.new()
tag.text = "Player %d" % pid
tag.position = Vector3(0, 2.0, 0)
tag.billboard = BaseMaterial3D.BILLBOARD_ENABLED
tag.no_depth_test = true
tag.pixel_size = 0.004
root.add_child(tag)
return root
func _on_server_lost() -> void:
_networked = false
_show_status("Disconnected from the server. Returning to worlds…")
await get_tree().create_timer(1.5).timeout
Session.clear()
get_tree().change_scene_to_file("res://scenes/world_list.tscn")
# ─── tiny on-screen status ────────────────────────────────────────────────────
func _show_status(text: String) -> void:
if _status_label == null:
var layer := CanvasLayer.new()
layer.name = "StatusLayer"
add_child(layer)
_status_label = Label.new()
_status_label.position = Vector2(16, 16)
_status_label.add_theme_font_size_override("font_size", 18)
layer.add_child(_status_label)
_status_label.text = text
_status_label.visible = true
func _hide_status() -> void:
if _status_label:
_status_label.visible = false