wren/val Public Public: anyone can read this; nobody can change it.
forked from benjivers/val
workbenchthis machine
Files wren/42-engine-renderer-colour-bodies-by-side-no
| 1 | /** |
| 2 | * The renderer, run for real against a canvas that writes down what it was |
| 3 | * asked to draw. No browser, but every line of the draw path is executed — |
| 4 | * which is what catches a typo in a frame nobody can see from a unit test. |
| 5 | * |
| 6 | * This is the engine's half: walls, bodies, marks, arrivals. What a game puts |
| 7 | * on the floor besides is drawn through hooks and tested with the game. |
| 8 | */ |
| 9 | |
| 10 | import { expect, test } from "bun:test"; |
| 11 | import { GHOST, ghosts, makePresence, shown, sighted } from "../../static/engine/presence.js"; |
| 12 | import { drawScene, makeScene } from "../../static/engine/scene.js"; |
| 13 | |
| 14 | type Op = { op: string; args: unknown[]; alpha: number; fill: string; stroke: string }; |
| 15 | |
| 16 | /** A canvas that records. Every call the renderer makes has to exist here. */ |
| 17 | function fakeContext() { |
| 18 | const ops: Op[] = []; |
| 19 | const note = |
| 20 | (op: string) => |
| 21 | (...args: unknown[]) => { |
| 22 | // The opacity in force when the call was made: how faded a body |
| 23 | // arriving into sight is, and how far a last-seen mark has dimmed. |
| 24 | ops.push({ op, args, alpha: ctx.globalAlpha, fill: ctx.fillStyle, stroke: ctx.strokeStyle }); |
| 25 | }; |
| 26 | const ctx = { |
| 27 | ops, |
| 28 | fillStyle: "", |
| 29 | strokeStyle: "", |
| 30 | lineWidth: 1, |
| 31 | font: "", |
| 32 | textAlign: "left", |
| 33 | globalAlpha: 1, |
| 34 | clearRect: note("clearRect"), |
| 35 | fillRect: note("fillRect"), |
| 36 | strokeRect: note("strokeRect"), |
| 37 | beginPath: note("beginPath"), |
| 38 | closePath: note("closePath"), |
| 39 | moveTo: note("moveTo"), |
| 40 | lineTo: note("lineTo"), |
| 41 | arc: note("arc"), |
| 42 | ellipse: note("ellipse"), |
| 43 | fill: note("fill"), |
| 44 | stroke: note("stroke"), |
| 45 | clip: note("clip"), |
| 46 | rect: note("rect"), |
| 47 | save: note("save"), |
| 48 | restore: note("restore"), |
| 49 | translate: note("translate"), |
| 50 | rotate: note("rotate"), |
| 51 | fillText: note("fillText"), |
| 52 | measureText: (text: string) => ({ width: text.length * 6 }), |
| 53 | createLinearGradient: () => ({ addColorStop() {} }), |
| 54 | }; |
| 55 | return ctx; |
| 56 | } |
| 57 | |
| 58 | // A room three tiles deep running east, with a pillar standing in it at |
| 59 | // column 5 — enough geometry for a body to hide behind. |
| 60 | const room = (col: number, row: number): boolean => |
| 61 | col < 1 || col > 12 || row < 1 || row > 3 || col === 5; |
| 62 | const TILE = 80; |
| 63 | const world = { solid: room, tile: TILE }; |
| 64 | |
| 65 | // What the engine's renderer needs of a snapshot: you, who you can see, |
| 66 | // and the shots you were let see. Nothing of the game. |
| 67 | const snapshot = (players: unknown[]) => ({ |
| 68 | me: { id: "me", x: 120, y: 200, angle: 0, alive: true, hp: 100 }, |
| 69 | players, |
| 70 | shots: [], |
| 71 | }); |
| 72 | |
| 73 | const body = (over: Record<string, unknown> = {}) => ({ |
| 74 | id: "them", |
| 75 | name: "Sable", |
| 76 | x: 280, |
| 77 | y: 200, |
| 78 | angle: 3.1, |
| 79 | hp: 100, |
| 80 | alive: true, |
| 81 | mate: false, |
| 82 | ...over, |
| 83 | }); |
| 84 | |
| 85 | const view = { width: 900, height: 570, look: (p: { x: number; y: number; angle: number }) => p }; |
| 86 | const eye = { x: 120, y: 200, angle: 0 }; |
| 87 | |
| 88 | test("a frame draws walls in runs: the whole width covered, far fewer fills", () => { |
| 89 | const ctx = fakeContext(); |
| 90 | drawScene(ctx as never, makeScene(), world, eye, snapshot([]), view); |
| 91 | const rects = ctx.ops.filter((o) => o.op === "fillRect"); |
| 92 | // The bands are the full width; the rest are wall runs. |
| 93 | const walls = rects |
| 94 | .map((o) => ({ x: Number(o.args[0]), w: Number(o.args[2]) })) |
| 95 | .filter((r) => r.w < view.width) |
| 96 | .sort((a, b) => a.x - b.x); |
| 97 | // This room is closed, so every column of the picture is wall: the runs |
| 98 | // start at the left edge, meet end to end and reach the right one. That is |
| 99 | // the same picture the old one-fill-a-column loop drew. |
| 100 | expect(walls[0]!.x).toBe(0); |
| 101 | let reach = 0; |
| 102 | for (const rect of walls) { |
| 103 | expect(rect.x).toBeLessThanOrEqual(reach); |
| 104 | reach = Math.max(reach, rect.x + rect.w); |
| 105 | } |
| 106 | expect(reach).toBeGreaterThanOrEqual(view.width); |
| 107 | // And it costs a fraction of the fills: one a column would be 900. |
| 108 | expect(walls.length).toBeLessThan(view.width / 2); |
| 109 | }); |
| 110 | |
| 111 | test("a body in the open is drawn; the same body behind the pillar is not", () => { |
| 112 | const open = fakeContext(); |
| 113 | drawScene(open as never, makeScene(), world, eye, snapshot([body()]), view); |
| 114 | // The head is the only arc in this scene, so it stands for the body. |
| 115 | expect(open.ops.some((o) => o.op === "arc")).toBe(true); |
| 116 | |
| 117 | const hidden = fakeContext(); |
| 118 | drawScene(hidden as never, makeScene(), world, eye, snapshot([body({ x: 700 })]), view); |
| 119 | expect(hidden.ops.some((o) => o.op === "arc")).toBe(false); |
| 120 | }); |
| 121 | |
| 122 | test("a team mate gets a name tag and an enemy never does", () => { |
| 123 | const mate = fakeContext(); |
| 124 | drawScene(mate as never, makeScene(), world, eye, snapshot([body({ mate: true })]), view); |
| 125 | expect(mate.ops.some((o) => o.op === "fillText" && o.args[0] === "Sable")).toBe(true); |
| 126 | |
| 127 | const enemy = fakeContext(); |
| 128 | drawScene(enemy as never, makeScene(), world, eye, snapshot([body()]), view); |
| 129 | expect(enemy.ops.some((o) => o.op === "fillText")).toBe(false); |
| 130 | }); |
| 131 | |
| 132 | test("the dead lie on the floor and wear no markers at all", () => { |
| 133 | const ctx = fakeContext(); |
| 134 | drawScene(ctx as never, makeScene(), world, eye, snapshot([body({ mate: true, alive: false, hp: 0 })]), view); |
| 135 | // Flat on the ground: an ellipse, not an upright body with a head. |
| 136 | expect(ctx.ops.some((o) => o.op === "ellipse")).toBe(true); |
| 137 | // No name tag, and no chevron — the chevron is the only closed path here. |
| 138 | expect(ctx.ops.some((o) => o.op === "fillText")).toBe(false); |
| 139 | expect(ctx.ops.some((o) => o.op === "closePath")).toBe(false); |
| 140 | }); |
| 141 | |
| 142 | test("an enemy wears a chevron and a team mate does not", () => { |
| 143 | const enemy = fakeContext(); |
| 144 | drawScene(enemy as never, makeScene(), world, eye, snapshot([body()]), view); |
| 145 | expect(enemy.ops.some((o) => o.op === "closePath")).toBe(true); |
| 146 | |
| 147 | const mate = fakeContext(); |
| 148 | drawScene(mate as never, makeScene(), world, eye, snapshot([body({ mate: true })]), view); |
| 149 | expect(mate.ops.some((o) => o.op === "closePath")).toBe(false); |
| 150 | expect(mate.ops.some((o) => o.op === "fillText")).toBe(true); |
| 151 | }); |
| 152 | |
| 153 | test("a body has a hard dark edge, whichever side it is on", () => { |
| 154 | const ctx = fakeContext(); |
| 155 | drawScene(ctx as never, makeScene(), world, eye, snapshot([body()]), view); |
| 156 | expect(ctx.ops.some((o) => o.op === "strokeRect")).toBe(true); |
| 157 | }); |
| 158 | |
| 159 | // ------------------------------------------- coming into sight, and leaving |
| 160 | |
| 161 | test("a body arriving is drawn faded and small, and solid once it is in", () => { |
| 162 | const edgeOf = (ctx: ReturnType<typeof fakeContext>) => ctx.ops.find((o) => o.op === "strokeRect")!; |
| 163 | |
| 164 | const arriving = fakeContext(); |
| 165 | drawScene(arriving as never, makeScene(), world, eye, snapshot([body()]), { ...view, shown: () => 0.4 }); |
| 166 | const solid = fakeContext(); |
| 167 | drawScene(solid as never, makeScene(), world, eye, snapshot([body()]), { ...view, shown: () => 1 }); |
| 168 | |
| 169 | expect(edgeOf(arriving).alpha).toBeCloseTo(0.4, 5); |
| 170 | expect(edgeOf(solid).alpha).toBe(1); |
| 171 | // And smaller: it grows from its feet as it comes in. |
| 172 | expect(Number(edgeOf(arriving).args[2])).toBeLessThan(Number(edgeOf(solid).args[2])); |
| 173 | expect(Number(edgeOf(arriving).args[2])).toBeGreaterThan(Number(edgeOf(solid).args[2]) * 0.7); |
| 174 | }); |
| 175 | |
| 176 | test("a body the presence store has only just seen is the one being faded", () => { |
| 177 | const seen = makePresence(); |
| 178 | const them = body(); |
| 179 | sighted(seen, [them], 100); |
| 180 | const draw = (at: number) => { |
| 181 | const ctx = fakeContext(); |
| 182 | drawScene(ctx as never, makeScene(), world, eye, snapshot([them]), { |
| 183 | ...view, |
| 184 | shown: (p: { id: string }) => shown(seen, p.id, at), |
| 185 | }); |
| 186 | return ctx.ops.find((o) => o.op === "strokeRect")!; |
| 187 | }; |
| 188 | expect(draw(100.03).alpha).toBeGreaterThan(0); |
| 189 | expect(draw(100.03).alpha).toBeLessThan(1); |
| 190 | expect(draw(100.5).alpha).toBe(1); |
| 191 | }); |
| 192 | |
| 193 | test("a last-seen mark is an outline, dimming, and never a body", () => { |
| 194 | const marks = [{ id: "them", x: 280, y: 200, angle: 0, mate: false, fade: 0.6 }]; |
| 195 | const ctx = fakeContext(); |
| 196 | drawScene(ctx as never, makeScene(), world, eye, snapshot([]), { ...view, marks }); |
| 197 | const edge = ctx.ops.find((o) => o.op === "strokeRect"); |
| 198 | expect(edge).toBeDefined(); |
| 199 | expect(edge!.alpha).toBeCloseTo(0.6, 5); |
| 200 | // No head, no chevron, no name: a memory of a sighting, not a sighting. |
| 201 | expect(ctx.ops.some((o) => o.op === "arc")).toBe(false); |
| 202 | expect(ctx.ops.some((o) => o.op === "closePath")).toBe(false); |
| 203 | expect(ctx.ops.some((o) => o.op === "fillText")).toBe(false); |
| 204 | expect(GHOST).toBeGreaterThan(0); |
| 205 | }); |
| 206 | |
| 207 | test("a body that steps out of sight leaves the outline the store hands over", () => { |
| 208 | const seen = makePresence(); |
| 209 | const them = body(); |
| 210 | sighted(seen, [them], 100); // in sight, in the open |
| 211 | sighted(seen, [], 100.1); // and gone behind the corner |
| 212 | const ctx = fakeContext(); |
| 213 | drawScene(ctx as never, makeScene(), world, eye, snapshot([]), { |
| 214 | ...view, |
| 215 | marks: ghosts(seen, 100.1), |
| 216 | }); |
| 217 | // Nobody is in the snapshot at all, and there is still something there. |
| 218 | const edge = ctx.ops.find((o) => o.op === "strokeRect"); |
| 219 | expect(edge).toBeDefined(); |
| 220 | expect(edge!.alpha).toBeCloseTo(1, 2); |
| 221 | |
| 222 | // A second later the store hands over nothing, and nothing is drawn. |
| 223 | const after = fakeContext(); |
| 224 | drawScene(after as never, makeScene(), world, eye, snapshot([]), { |
| 225 | ...view, |
| 226 | marks: ghosts(seen, 100.1 + GHOST), |
| 227 | }); |
| 228 | expect(after.ops.some((o) => o.op === "strokeRect")).toBe(false); |
| 229 | }); |
| 230 | |
| 231 | test("a mark where the wall now stands between you and it is not drawn", () => { |
| 232 | const marks = [{ id: "them", x: 700, y: 200, angle: 0, mate: false, fade: 1 }]; |
| 233 | const ctx = fakeContext(); |
| 234 | drawScene(ctx as never, makeScene(), world, eye, snapshot([]), { ...view, marks }); |
| 235 | expect(ctx.ops.some((o) => o.op === "strokeRect")).toBe(false); |
| 236 | }); |
| 237 | |
| 238 | // ------------------------------------------------------- colour, by side |
| 239 | |
| 240 | /** The fill of each upright body, left to right: the rect its edge is drawn round. */ |
| 241 | function skins(ctx: ReturnType<typeof fakeContext>): string[] { |
| 242 | const out: { x: number; fill: string }[] = []; |
| 243 | ctx.ops.forEach((o, i) => { |
| 244 | if (o.op !== "strokeRect") return; |
| 245 | const body = ctx.ops[i - 1]!; |
| 246 | if (body.op === "fillRect") out.push({ x: Number(body.args[0]), fill: body.fill }); |
| 247 | }); |
| 248 | return out.sort((a, b) => a.x - b.x).map((b) => b.fill); |
| 249 | } |
| 250 | |
| 251 | test("without `side`, a mate and an enemy wear exactly today's colours", () => { |
| 252 | const ctx = fakeContext(); |
| 253 | drawScene(ctx as never, makeScene(), world, eye, snapshot([body({ id: "m", mate: true, y: 150 }), body({ id: "e", y: 250 })]), view); |
| 254 | // Looking east, the smaller y is on the left. |
| 255 | expect(skins(ctx)).toEqual(["#3f9fd6", "#d8483f"]); |
| 256 | // And a last-seen enemy is outlined in today's colour too. |
| 257 | const marked = fakeContext(); |
| 258 | drawScene(marked as never, makeScene(), world, eye, snapshot([]), { |
| 259 | ...view, |
| 260 | marks: [{ id: "e", x: 280, y: 200, angle: 0, mate: false, fade: 1 }], |
| 261 | }); |
| 262 | expect(marked.ops.find((o) => o.op === "strokeRect")!.stroke).toBe("rgba(235,150,120,0.9)"); |
| 263 | }); |
| 264 | |
| 265 | test("three enemy sides draw in three colours, and each keeps its colour next frame", () => { |
| 266 | const three = [ |
| 267 | body({ id: "a", side: "scene-north", y: 130 }), |
| 268 | body({ id: "b", side: "scene-east", y: 200 }), |
| 269 | body({ id: "c", side: "scene-west", y: 270 }), |
| 270 | ]; |
| 271 | const first = fakeContext(); |
| 272 | drawScene(first as never, makeScene(), world, eye, snapshot(three), view); |
| 273 | const colours = skins(first); |
| 274 | expect(colours).toHaveLength(3); |
| 275 | expect(new Set(colours).size).toBe(3); |
| 276 | // None of them is the mate's blue. |
| 277 | expect(colours).not.toContain("#3f9fd6"); |
| 278 | |
| 279 | // Next frame the server lists them the other way round: same colours. |
| 280 | const second = fakeContext(); |
| 281 | drawScene(second as never, makeScene(), world, eye, snapshot([...three].reverse()), view); |
| 282 | expect(skins(second)).toEqual(colours); |
| 283 | |
| 284 | // And a side alone on screen still wears its own, not the first slot's. |
| 285 | const alone = fakeContext(); |
| 286 | drawScene(alone as never, makeScene(), world, eye, snapshot([three[2]]), view); |
| 287 | expect(skins(alone)).toEqual([colours[2]!]); |
| 288 | }); |
| 289 | |
| 290 | test("a mate with a side is still the mate's colour, and the dead are still grey", () => { |
| 291 | const ctx = fakeContext(); |
| 292 | drawScene(ctx as never, makeScene(), world, eye, snapshot([body({ mate: true, side: "scene-mine" })]), view); |
| 293 | expect(skins(ctx)).toEqual(["#3f9fd6"]); |
| 294 | |
| 295 | const dead = fakeContext(); |
| 296 | drawScene(dead as never, makeScene(), world, eye, snapshot([body({ side: "scene-dead", alive: false, hp: 0 })]), view); |
| 297 | expect(dead.ops.find((o) => o.op === "fill")!.fill).toBe("#4a4e57"); |
| 298 | }); |