A town simulation in pale isometric line art, implemented with Ritmo ECS and PixiJs 3D
Every picture in the panels and menus is the map's own model
SandroMaglionebf173315115b
diff --git a/src/host.ts b/src/host.tsindex 9a49407..bfd3d33 100644--- a/src/host.ts+++ b/src/host.ts@@ -5,7 +5,6 @@*/import { Application, Graphics } from "pixi.js"-import { drawParkPiece } from "./art.ts"import {categoryColor,categoryOrder,@@ -61,10 +60,7 @@ import { handheld, offerLink, offerPicture, upload } from "./share/client.ts"import { PICTURE_HEIGHT, PICTURE_QUALITY, PICTURE_WIDTH } from "./share/format.ts"import { MapInput } from "./render3d/input.ts"import { portrait } from "./render3d/portrait.ts"-import { serviceBuilding } from "./asset-lab/service-upgrades.ts"-import { buildingDimensions, makeBuilding } from "./asset-lab/buildings.ts"-import { dressedBuilding, isDressed, type DressedKind } from "./asset-lab/upgrade-dress.ts"-import { house } from "./asset-lab/homes.ts"+import { pictureAsset } from "./render3d/models.ts"const el = <K extends keyof HTMLElementTagNameMap>(tag: K, className: string, text = "") => {const node = document.createElement(tag)@@ -749,16 +745,8 @@ export const createHost = async (const id = `${String(what)}${upgraded ? `/upgraded${lvl}` : ""}`let url = pictures.get(id)if (!url) {- if (typeof what === "string" && what.startsWith("park:")) {- const g = new Graphics()- drawParkPiece(g, what.slice(5) as GroundId | PropId)- url = app.renderer.extract.base64({ target: g, resolution: 2 })- void url.finally(() => g.destroy())- } else if (what === 1 || what === 2 || what === 3) url = modelPicture(() => house(what, undefined, undefined, { doors: { right: true } }))- else if (upgraded && (what === "market" || what === "chapel")) url = modelPicture(() => serviceBuilding(what, 2, { doors: both }))- else if (upgraded && isDressed(what as KindId))- url = modelPicture(() => dressedBuilding(what as DressedKind, lvl, makeBuilding(what as KindId, { doors: both, motion: false }), buildingDimensions(what as KindId).height))- else url = modelPicture(() => makeBuilding(what as KindId, { doors: both, motion: false }))+ // The model the map draws, as it stands: one source for the map and every picture of it.+ url = modelPicture(() => pictureAsset(what, lvl))pictures.set(id, url)}void url.then((src) => (img.src = src))diff --git a/src/render3d/models.ts b/src/render3d/models.tsindex cf9138e..8e53fd4 100644--- a/src/render3d/models.ts+++ b/src/render3d/models.ts@@ -3,12 +3,12 @@ import { makeBuilding, buildingDimensions, type BuildingOptions } from "../assetimport { house } from "../asset-lab/homes.ts"import { Drawing, palette, square, type InkAsset, type Vertex, type Side } from "../asset-lab/primitives.ts"import { hash } from "../asset-lab/land.ts"-import { kinds, home, type KindId, type Tier } from "../content.ts"+import { kinds, home, type GroundId, type KindId, type PropId, type Tier } from "../content.ts"import { GRID, U, type Point } from "../iso.ts"import { key, cellOf, DIRS } from "../grid.ts"import type { Preview } from "../ui.ts"import { withAppearance, type Appearance } from "../asset-lab/appearance.ts"-import { setts } from "../asset-lab/town-props.ts"+import { parkAsset, setts } from "../asset-lab/town-props.ts"import { isWorkshop, workshopConstruction } from "../asset-lab/workshops.ts"import { isTradeYard, tradeConstruction } from "../asset-lab/trades.ts"import { serviceBuilding, upgradeHeights, type ServiceKind } from "../asset-lab/service-upgrades.ts"@@ -387,3 +387,22 @@ export function overlayAsset(cells: ReadonlyArray<OverlayCell>) {}return d.asset}++/** What a panel or menu pictures: a kind at a level of upgrades, a home's tier, or a piece of park. */+export type Pictured = KindId | Tier | `park:${GroundId | PropId}`+/** The ground a furnishing is pictured on: a lamp along a walk, a fountain or a statue on a paved square. */+const furnishingGround: Readonly<Record<PropId, GroundId>> = { lamp: "path", fountain: "square", statue: "square" }+/**+ * The model a picture shows: the very one the map draws, built finished, through the same functions as the+ * views (`buildingAsset`, `homeAsset`, `parkAsset`), so a picture can never fall behind its model.+ */+export function pictureAsset(what: Pictured, level = 0): InkAsset {+ if (what === 1 || what === 2 || what === 3) return homeAsset(what, 0, 1, null, { ...noDoors, right: true })+ if (typeof what === "string" && what.startsWith("park:")) {+ const piece = what.slice(5) as GroundId | PropId+ const ground = piece in furnishingGround ? furnishingGround[piece as PropId] : (piece as GroundId)+ const prop = piece in furnishingGround ? (piece as PropId) : "none"+ return parkAsset(new Map([[key(0, 0), { x: 0, y: 0, ground, prop }]]))+ }+ return buildingAsset(what as KindId, 1, null, { ...noDoors, right: true, left: true }, 0, {}, level)+}diff --git a/test/pictures.test.ts b/test/pictures.test.tsnew file mode 100644index 0000000..daccf0c--- /dev/null+++ b/test/pictures.test.ts@@ -0,0 +1,41 @@+/**+ * Pictures in the panels and menus are the map's own models (see+ * `pictureAsset`): every kind at every level of upgrades, every home, every+ * piece of park is pictured through the function the map draws it with, so+ * no picture is left behind when a model changes.+ */+import assert from "node:assert/strict"+import { readFileSync } from "node:fs"+import { test } from "node:test"++import { groundIds, kindIds, kinds, propIds } from "../src/content.ts"+import { buildingAsset, homeAsset, noDoors, pictureAsset } from "../src/render3d/models.ts"++test("every building's picture, at every level of upgrades, is the model the map draws", () => {+ for (const id of kindIds) {+ const top = kinds[id].upgrade?.next ? 2 : kinds[id].upgrade ? 1 : 0+ for (let level = 0; level <= top; level++) {+ const pictured = pictureAsset(id, level)+ assert.ok(pictured.faces.length > 0, `${id} ${level} has a picture`)+ assert.deepEqual(pictured, buildingAsset(id, 1, null, { ...noDoors, right: true, left: true }, 0, {}, level), `${id} at level ${level}`)+ }+ }+})++test("every home's picture is the model the map draws", () => {+ for (const tier of [1, 2, 3] as const) assert.deepEqual(pictureAsset(tier), homeAsset(tier, 0, 1, null, { ...noDoors, right: true }))+})++test("every piece of park is pictured as the map draws it, a furnishing standing on its ground", () => {+ for (const ground of groundIds) assert.ok(pictureAsset(`park:${ground}`).faces.length > 0, ground)+ for (const prop of propIds) {+ const bare = pictureAsset(`park:${prop === "lamp" ? "path" : "square"}`)+ assert.ok(pictureAsset(`park:${prop}`).faces.length > bare.faces.length, `${prop} stands on its ground`)+ }+})++test("the page pictures nothing any other way", () => {+ const host = readFileSync(new URL("../src/host.ts", import.meta.url), "utf8")+ assert.match(host, /pictureAsset\(/)+ for (const other of ["drawParkPiece", "makeBuilding", "serviceBuilding", "dressedBuilding"]) assert.equal(host.includes(other), false, `host.ts pictures with ${other}`)+})