feat(client): tokens, BitmapText labels, statuses and lists (#71)
- slices/tokens.ts: token upsert, token_gone, hello reset; status names only. - slices/characters.ts + slices/encountered.ts: own characters, encountered list with grey entries; the GM's per-player copies are ignored. - render/tokenBoard.ts (pure): event -> entered/updated/left patches, so one changed token costs one container; seeds from the reducer on mount. - render/modules/tokens.ts: pooled world-coordinate nodes on the tokens layer, one shared unit-circle context per kind, BitmapText from one pre-installed BitmapFont assigned only on a name change, stuck/squeezed badges, canvas dataset instrumentation for the "only changed entities" criterion. - panels/lists.ts + render/modules/lists.ts: #lists under #panels, textContent only, greyed encountered rows; mounted by a render module so no shared bootstrap file changed.
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
// The token renderer's pure half (T8, #71). No Pixi import: it turns the store's event stream into
|
||||
// the exact nodes a renderer must create, update or remove, so the "only changed entities" rule of
|
||||
// DESIGN 7.2 is observable and unit-tested instead of hidden inside a WebGL module.
|
||||
//
|
||||
// It mirrors the token slice's parsing (one wire shape, one parser) but keeps its own view of what
|
||||
// is currently on screen, which is what a renderer actually needs: a `token_gone` is only a removal
|
||||
// if a node exists, and a `token` frame for an existing node is an update, not an entry.
|
||||
|
||||
import type { ReducerEvent } from '../reducer.ts'
|
||||
import { isHello } from '../protocol.ts'
|
||||
import type { TokenView } from '../slices/tokens.ts'
|
||||
import { tokenViewFromFrame } from '../slices/tokens.ts'
|
||||
|
||||
export interface TokenPatch {
|
||||
/** Everything on screen must go (a `hello` is a full reset, 6.3). */
|
||||
reset: boolean
|
||||
entered: TokenView[]
|
||||
updated: TokenView[]
|
||||
left: string[]
|
||||
}
|
||||
|
||||
export class TokenBoard {
|
||||
#views = new Map<string, TokenView>()
|
||||
#reset = false
|
||||
#entered = new Map<string, TokenView>()
|
||||
#updated = new Map<string, TokenView>()
|
||||
#left = new Set<string>()
|
||||
|
||||
get size(): number {
|
||||
return this.#views.size
|
||||
}
|
||||
|
||||
/** What is on screen now, in entry order. Read-only for the renderer's dataset/teardown. */
|
||||
get views(): ReadonlyMap<string, TokenView> {
|
||||
return this.#views
|
||||
}
|
||||
|
||||
/**
|
||||
* Start from the reducer's current view. The stage is created asynchronously, so the resync may
|
||||
* have run before a render module mounts; without this a token that arrived during `app.init`
|
||||
* would stay invisible until it changed. Every seeded token is an entry.
|
||||
*/
|
||||
seed(views: readonly TokenView[]): void {
|
||||
this.#views.clear()
|
||||
this.#entered.clear()
|
||||
this.#updated.clear()
|
||||
this.#left.clear()
|
||||
this.#reset = false
|
||||
for (const view of views) {
|
||||
this.#views.set(view.id, view)
|
||||
this.#entered.set(view.id, view)
|
||||
}
|
||||
}
|
||||
|
||||
/** Fold one store event into the pending patch. Nothing is drawn here. */
|
||||
apply(event: ReducerEvent): void { if (event.type !== 'frame') return
|
||||
const frame = event.frame
|
||||
if (isHello(frame)) {
|
||||
// A `hello` means "start clean" (6.3): the nodes all die, and anything still pending for the
|
||||
// previous connection is dropped rather than replayed onto the new view.
|
||||
this.#views.clear()
|
||||
this.#entered.clear()
|
||||
this.#updated.clear()
|
||||
this.#left.clear()
|
||||
this.#reset = true
|
||||
return
|
||||
}
|
||||
if (frame.t === 'token') {
|
||||
const view = tokenViewFromFrame(frame)
|
||||
if (!view) return
|
||||
const existing = this.#views.has(view.id)
|
||||
this.#views.set(view.id, view)
|
||||
if (existing) {
|
||||
// A token that entered and changed between two drains is still one entry, drawn once.
|
||||
if (this.#entered.has(view.id)) {
|
||||
this.#entered.set(view.id, view)
|
||||
return
|
||||
}
|
||||
this.#updated.set(view.id, view)
|
||||
this.#left.delete(view.id)
|
||||
} else {
|
||||
this.#updated.delete(view.id)
|
||||
this.#entered.set(view.id, view)
|
||||
}
|
||||
return
|
||||
}
|
||||
if (frame.t === 'token_gone') {
|
||||
const id = frame['id']
|
||||
if (typeof id !== 'string' || !this.#views.has(id)) return
|
||||
this.#views.delete(id)
|
||||
this.#entered.delete(id)
|
||||
this.#updated.delete(id)
|
||||
this.#left.add(id)
|
||||
}
|
||||
}
|
||||
|
||||
/** Take the pending patch: the renderer touches exactly these entities, then waits. */
|
||||
drain(): TokenPatch {
|
||||
const patch: TokenPatch = {
|
||||
reset: this.#reset,
|
||||
entered: [...this.#entered.values()],
|
||||
updated: [...this.#updated.values()],
|
||||
left: [...this.#left],
|
||||
}
|
||||
this.#reset = false
|
||||
this.#entered.clear()
|
||||
this.#updated.clear()
|
||||
this.#left.clear()
|
||||
return patch
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user