N10: Playable network client
The client program now turns a connection into a match someone can play. - ClientSession: a ClientGame, the keyboard and mouse as commands (ClientInput), and a MatchView of the client's own copy of the world. - ClientMirror writes what the client is showing into that world every frame: other players from the recent past, its own player where it was predicted, items, the barge, the clock and the sea. The existing views draw it unchanged. - LocalServer: offline play is the same ServerLoop and NetGame in the same process, with packets handed across in memory, so there is one path. - main.gd: --connect=HOST[:PORT] joins a server, otherwise an offline match; --autoplay and --client-seconds=N for scripts and checks. - The camera aboard the barge follows the player's own place on the deck. Tests drive a scripted client through a whole match on a real server over loopback UDP, lobby to results, and through an offline match. `make client-check` does it with two real processes, and `make client-captures` renders what the client sees. Closes #77 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,146 @@
|
||||
## The client program while it is in a game: a ClientGame fed by a server (one across
|
||||
## the network, or a LocalServer in this process when playing offline), the keyboard
|
||||
## and mouse turned into its commands, and a MatchView drawing the client's world as
|
||||
## ClientMirror fills it in.
|
||||
##
|
||||
## pump() does a frame's work and needs no scene tree, so tests drive it directly.
|
||||
class_name ClientSession
|
||||
extends Node3D
|
||||
|
||||
var game: ClientGame = ClientGame.new()
|
||||
var mirror: ClientMirror = ClientMirror.new()
|
||||
var input: ClientInput = ClientInput.new()
|
||||
## The connection, when playing on a server across the network.
|
||||
var link: NetClient = null
|
||||
## The server in this process, when playing offline.
|
||||
var local: LocalServer = null
|
||||
## The picture of the current match's world, or null before there is one.
|
||||
var view: MatchView = null
|
||||
## Every match phase this client has been in, in order, without repeats.
|
||||
var phases: Array[int] = []
|
||||
## Frames in which the client's own player was in the match, and frames pumped.
|
||||
var frames_playing: int = 0
|
||||
var frames: int = 0
|
||||
|
||||
var _viewed: Simulation = null
|
||||
var _status: Label = null
|
||||
|
||||
|
||||
func _init() -> void:
|
||||
game.sample_input = _sample
|
||||
|
||||
|
||||
## Starts joining the server at `host`. Returns an error if the socket can't be made.
|
||||
func connect_to(host: String, port: int, player_name: String) -> Error:
|
||||
link = NetClient.new()
|
||||
game.attach(link)
|
||||
return link.connect_to(host, port, player_name)
|
||||
|
||||
|
||||
## Starts an offline match of this player and `bots` bots on a server in this process.
|
||||
func play_offline(match_seed: int, bots: int, rules: MatchRules = null) -> void:
|
||||
local = LocalServer.new(match_seed, bots, rules)
|
||||
|
||||
|
||||
## Leaves the server, if there is one across the network.
|
||||
func leave() -> void:
|
||||
if link != null:
|
||||
link.leave()
|
||||
|
||||
|
||||
## One line saying where things stand, for the corner of the screen and the log.
|
||||
func status_text() -> String:
|
||||
if link != null and link.state != NetClient.JOINED:
|
||||
return link.status_text()
|
||||
if game.world == null:
|
||||
return "Waiting for the server"
|
||||
var phase: String = Match.PHASE_NAMES[game.match_phase]
|
||||
if game.match_phase == Match.LOBBY or game.match_phase == Match.COUNTDOWN:
|
||||
return "In the %s" % phase
|
||||
if game.playing():
|
||||
return ""
|
||||
return "Watching" if game.match_phase != Match.RESULTS else "The match is over"
|
||||
|
||||
|
||||
## One frame: packets in, commands out, and the picture brought up to date.
|
||||
func pump(delta: float) -> void:
|
||||
frames += 1
|
||||
input.poll()
|
||||
if link != null:
|
||||
game.poll()
|
||||
if local != null:
|
||||
local.advance(delta, game)
|
||||
game.advance(delta)
|
||||
if game.world != null and (phases.is_empty() or phases[phases.size() - 1] != game.match_phase):
|
||||
phases.append(game.match_phase)
|
||||
if game.playing():
|
||||
frames_playing += 1
|
||||
mirror.sync(game)
|
||||
_show(delta)
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
_status = Label.new()
|
||||
_status.position = Vector2(24.0, 20.0)
|
||||
_status.add_theme_font_size_override("font_size", 22)
|
||||
_status.add_theme_color_override("font_outline_color", Color(0.08, 0.08, 0.08))
|
||||
_status.add_theme_constant_override("outline_size", 6)
|
||||
add_child(_status)
|
||||
if DisplayServer.get_name() != "headless" and not input.autoplay:
|
||||
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
|
||||
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
pump(delta)
|
||||
if _status != null:
|
||||
_status.text = status_text()
|
||||
|
||||
|
||||
func _unhandled_input(event: InputEvent) -> void:
|
||||
var captured: bool = Input.mouse_mode == Input.MOUSE_MODE_CAPTURED
|
||||
var key: InputEventKey = event as InputEventKey
|
||||
var click: InputEventMouseButton = event as InputEventMouseButton
|
||||
if key != null and key.pressed and key.physical_keycode == KEY_ESCAPE:
|
||||
# Escape lets go of the mouse; a click takes it back.
|
||||
Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
|
||||
elif click != null and click.pressed and not captured and not input.autoplay:
|
||||
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
|
||||
else:
|
||||
input.handle(event, captured)
|
||||
|
||||
|
||||
func _exit_tree() -> void:
|
||||
leave()
|
||||
|
||||
|
||||
func _sample(tick: int) -> PlayerCommand:
|
||||
if input.autoplay and game.world != null:
|
||||
return input.autoplay_command(tick, game.prediction.player, game.world.ground)
|
||||
return input.command(tick)
|
||||
|
||||
|
||||
## Draws the client's world: a new MatchView whenever there is a new world, and the
|
||||
## camera on the watched player's shoulder, or over the island when there is nobody.
|
||||
func _show(delta: float) -> void:
|
||||
if game.world != _viewed:
|
||||
_viewed = game.world
|
||||
if view != null:
|
||||
remove_child(view)
|
||||
view.queue_free()
|
||||
view = null
|
||||
if _viewed != null:
|
||||
view = MatchView.new()
|
||||
view.setup(_viewed, 0)
|
||||
add_child(view)
|
||||
move_child(view, 0)
|
||||
if view == null:
|
||||
return
|
||||
view.entities.local_id = mirror.own_id if mirror.own_id != 0 else view.entities.local_id
|
||||
view.entities.barge_at = mirror.barge_at
|
||||
view.rig.update_aim(input.aiming() and mirror.own_id != 0, delta)
|
||||
view.sync()
|
||||
if mirror.own_id != 0:
|
||||
# The player's own look is newer than any command made from it.
|
||||
view.follow(mirror.own_id, input.pitch, input.yaw)
|
||||
elif mirror.watched_id == 0 or not view.follow(mirror.watched_id):
|
||||
view.overview()
|
||||
Reference in New Issue
Block a user