Files
tidewall/src/app/client_session.gd
T
claudeandClaude Opus 5.5 e8414fde9a A7: Visual effects, hit marker and hurt flash
EffectsView (src/client/effects/) draws muzzle flashes at the held
weapon's muzzle, tracer ribbons to each pellet's impact (and streaks
behind projectiles in flight), hit bursts on players, chips and dust by
material or ground, keg blasts with fireball, debris, smoke and a light,
build dust, collapse clouds for broken pieces, splashes and surge spray.

Particles are simulated on the CPU in packed arrays and drawn by two
MultiMeshes (blended and added) through a small billboard shader that
reads each particle's size from custom data (the stock billboard modes
drop the instance scale) and converts Palette's sRGB colours. Pools are
allocated once at the preset's budget (256/512/1024 particles, 64
tracers, 4 lights): three draw calls whatever happens.

The HUD gains a reticle, four ticks round it when your shot strikes a
player (HitFeedback), and a rust flash at the screen's edge when hurt.

Tests: test_effects_view (12), including a 24-shooter stress scene that
stays inside each budget and never adds nodes. Captures: make
effects-captures (fight, blast, work, stress), checked by eye, and the
blast in the Compatibility renderer.

Closes #47

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 11:45:19 -05:00

288 lines
10 KiB
GDScript

## 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
## The in-game HUD, once the session is in a scene tree.
var hud: Hud = null
## The chart (A11): the minimap in the corner while playing, and the full chart when
## the map key opens it. Children of the HUD.
var minimap: ChartMap = null
var chart: ChartMap = null
## The match's sound (A8), once the session is in a scene tree.
var sound: SoundDirector = null
## The player's settings (see use_settings()).
var settings: GameSettings = GameSettings.new()
## Escape asks for the pause menu: whoever runs the session shows it (AppShell). With
## nobody listening, Escape only lets go of the mouse.
signal pause_requested
## True while a menu is over the match: the keys and mouse don't play.
var paused: bool = false
var _viewed: Simulation = null
var _status: Label = null
## ClientGame.fx_received when the figures last heard the FX.
var _fx_seen: int = 0
## The hit marker and hurt flash (A7).
var feedback: HitFeedback = HitFeedback.new()
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, difficulty: int = -1) -> void:
local = LocalServer.new(match_seed, bots, rules, difficulty)
## Takes the player's settings: their keys and mouse now, and graphics for each
## match's view.
func use_settings(with: GameSettings) -> void:
settings = with
input.settings = with.mouse
input.bindings = with.bindings
if view != null:
view.apply_settings(with)
## The handle this client plays as: the server's name for it in the feed.
func own_handle() -> int:
return LocalServer.HANDLE if local != null else (link.handle if link != null else 0)
## 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
if paused:
# Nothing held while a menu is up, so the player stops rather than runs on.
input.move = Vector2.ZERO
input.buttons = 0
else:
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", Palette.OUTLINE)
_status.add_theme_constant_override("outline_size", 6)
add_child(_status)
sound = SoundDirector.new()
sound.name = "Sound"
add_child(sound)
hud = Hud.new()
add_child(hud)
minimap = ChartMap.new(ChartMap.Mode.MINI)
minimap.position = Vector2(18.0, 18.0)
minimap.size = Vector2(170.0, 170.0)
minimap.visible = false
hud.add_child(minimap)
chart = ChartMap.new(ChartMap.Mode.FULL)
chart.set_anchors_and_offsets_preset(Control.PRESET_CENTER)
chart.size = Vector2(600.0, 600.0)
chart.position = Vector2(-300.0, -300.0)
chart.visible = false
hud.add_child(chart)
if DisplayServer.get_name() != "headless" and not input.autoplay:
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
func _process(delta: float) -> void:
pump(delta)
if sound != null:
if view != null:
sound.rain = view.weather.weather.rain()
sound.gale = view.weather.weather.wind()
sound.update(game, mirror.own_id, view.camera.global_position if view != null else Vector3.ZERO, delta)
if _status != null:
_status.text = status_text()
if hud != null:
var state: HudState = hud_state()
feedback.update(state.health if state.playing else -1, delta)
state.hit = feedback.hit()
state.hurt = feedback.hurt()
hud.show_state(state)
_show_charts(delta)
## What the HUD should show now.
func hud_state() -> HudState:
var state: HudState = HudState.from_client(game, input, own_handle())
state.hit = feedback.hit()
state.hurt = feedback.hurt()
return state
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 not key.echo and key.physical_keycode == KEY_ESCAPE:
# Escape lets go of the mouse and asks for the pause menu; a click takes the
# mouse back when there is no menu.
Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
pause_requested.emit()
get_viewport().set_input_as_handled()
elif click != null and click.pressed and not captured and not input.autoplay and not paused:
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
elif not paused:
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)
view.apply_settings(settings)
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()
var fresh: int = mini(game.fx_received - _fx_seen, game.fx.size())
_fx_seen = game.fx_received
if fresh > 0:
var records: Array[Dictionary] = game.fx.slice(game.fx.size() - fresh)
view.entities.hear(records)
view.effects.hear(records)
feedback.hear(records, mirror.own_id)
_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 minimap while this client plays (the drop included), and the full chart while
## the map key has it open and there's a world.
func _show_charts(delta: float) -> void:
if minimap == null:
return
var world: Simulation = game.world
if world == null:
minimap.visible = false
chart.visible = false
return
if minimap.world != world:
var drop: Drop = Drop.plan(world.ground, SimRng.new(game.world_seed), 0) if world.island != null else null
minimap.setup(world, game.world_seed, drop)
chart.setup(world, game.world_seed, drop, minimap)
var playing: bool = game.playing()
var me: SimEntity = game.prediction.player
var at: Vector3 = me.position if playing else Vector3(Tuning.WORLD_SIZE_METRES * 0.5, 0.0, Tuning.WORLD_SIZE_METRES * 0.5)
minimap.visible = playing and not input.map_open
chart.visible = input.map_open
if minimap.visible:
minimap.refresh(at, input.yaw, true, delta)
if chart.visible:
chart.refresh(at, input.yaw, playing, delta)
## 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