## 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 ## Where a piece would go, in build mode while playing. A child of `view`. var ghost: BuildGhost = 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) # Make each new island off the main thread, so the connection keeps being serviced. game.build_in_background = true 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 ghost = null if _viewed != null: view = MatchView.new() view.setup(_viewed, 0) add_child(view) move_child(view, 0) ghost = BuildGhost.new() ghost.visible = false view.add_child(ghost) 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() _show_ghost() 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() ## The build ghost, in build mode while this client plays: the piece the next build ## would place, asked of the client's own world and predicted player, as the server ## will ask its own. Its look is the newest the mouse has given. func _show_ghost() -> void: if ghost == null: return var player: SimEntity = game.prediction.player if not (input.build_mode and game.playing() and mirror.own_id != 0 and Building.is_allowed(player)): ghost.visible = false return var yaw: int = player.yaw var pitch: int = player.pitch var look: PlayerCommand = PlayerCommand.from_input(0, Vector2.ZERO, input.yaw, input.pitch) player.yaw = look.yaw player.pitch = look.pitch ghost.update(game.world, player, input.build_kind, input.material) player.yaw = yaw player.pitch = pitch