Skip to content

A town simulation in pale isometric line art, implemented with Ritmo ECS and PixiJs 3D

The founding screen: the land drawn large, the seed tucked away, and Continue for a town saved here

SandroMaglioneb0e0002cd120
diff --git a/src/factory.css b/src/factory.css
index 40050cc..ccc4c49 100644
--- a/src/factory.css
+++ b/src/factory.css
@@ -286,12 +286,17 @@ body { font-family: ui-monospace, "SF Mono", "JetBrains Mono", Menlo, monospace;
.dialog .frame:hover { background: #e6e9e2; }
.dialog .frame.primary { background: #3d4943; color: #f7f8f4; }
.dialog .actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 22px; }
-.dialog .charter { display: grid; grid-template-columns: 1fr 200px; gap: 22px; }
+.dialog .charter { display: grid; grid-template-columns: 1fr 300px; gap: 24px; }
.dialog .choices { display: flex; gap: 6px; flex-wrap: wrap; }
.dialog .choice { border-color: #c9cec4; color: #6c766f; }
.dialog .choice.on { border-color: #3d4943; color: #3d4943; font-weight: 700; background: #e6e9e2; }
.dialog .preview { display: flex; flex-direction: column; gap: 8px; }
-.dialog .preview canvas { width: 192px; height: 192px; background: #e2e9e6; border: 1px solid #c9cec4; image-rendering: pixelated; }
+/* The land is the picture the charter is chosen by: as large as the sheet allows, with its reroll on it. */
+.dialog .preview .map { position: relative; }
+.dialog .preview canvas { display: block; width: 100%; height: auto; aspect-ratio: 1; box-sizing: border-box; background: #e2e9e6; border: 1px solid #c9cec4; border-radius: 4px; image-rendering: pixelated; }
+.dialog .preview .reroll { position: absolute; right: 8px; bottom: 8px; background: rgba(247, 248, 244, 0.92); font-size: 12px; }
+.dialog .preview .reroll:hover { background: #f7f8f4; }
+.dialog .actions .saved { flex-basis: 100%; margin: 0 0 2px; font-size: 12px; color: #6c766f; }
.dialog .preview .sub { font-size: 12px; }
.dialog .facts { display: grid; grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); gap: 10px; margin: 14px 0; }
.dialog .fact { display: flex; flex-direction: column; border-left: 2px solid #d3d7ce; padding-left: 8px; }
diff --git a/src/host.ts b/src/host.ts
index a9d493d..98e508f 100644
--- a/src/host.ts
+++ b/src/host.ts
@@ -120,8 +120,15 @@ export interface Persistence {
readonly shareFile: (camera: CameraView) => string
}
-/** `shared`: the page opened on a shared town's link, so it loads that rather than asking for a charter. */
-export const createHost = async (mount: HTMLElement, persistence: () => Persistence, options: { readonly shared?: boolean } = {}) => {
+/**
+ * `shared`: the page opened on a shared town's link, so it loads that rather than asking for a charter.
+ * `saved`: the newest town saved in this browser, which the first charter offers to carry on with.
+ */
+export const createHost = async (
+ mount: HTMLElement,
+ persistence: () => Persistence,
+ options: { readonly shared?: boolean; readonly saved?: { readonly title: string; readonly savedAt: number } | null } = {}
+) => {
const app = new Application()
// Pixi remains an offscreen thumbnail renderer for the unchanged menus.
await app.init({ width: 64, height: 64, antialias: true, resolution: 2, autoStart: false, backgroundAlpha: 0 })
@@ -1298,6 +1305,7 @@ export const createHost = async (mount: HTMLElement, persistence: () => Persiste
}
// ── Founding a town ──
+ const savedTown = options.saved ?? null
const choice: { -readonly [K in keyof CharterValue]: CharterValue[K] } = defaultCharter((Date.now() & 0x7fffffff) >>> 0)
/** The land a charter would found, drawn small: a dot a cell. */
const drawLand = (canvas: HTMLCanvasElement) => {
@@ -1331,7 +1339,7 @@ export const createHost = async (mount: HTMLElement, persistence: () => Persiste
const openCharter = (cancellable: boolean) => {
ui.founding = true
dialog.replaceChildren()
- const card = el("div", "sheet")
+ const card = el("div", "sheet wide")
card.append(
el("h2", "", "found a town"),
el(
@@ -1343,9 +1351,11 @@ export const createHost = async (mount: HTMLElement, persistence: () => Persiste
const body = el("div", "charter")
const options = el("div", "")
const preview = el("div", "preview")
+ const mapBox = el("div", "map")
const map = document.createElement("canvas")
+ mapBox.append(map)
const facts = el("p", "sub")
- preview.append(map, facts)
+ preview.append(mapBox, facts)
body.append(options, preview)
const group = <T extends string | number>(
title: string,
@@ -1400,19 +1410,17 @@ export const createHost = async (mount: HTMLElement, persistence: () => Persiste
(v) => (choice.land = v)
)
]
- const seedLine = el("div", "row")
- const seedText = el("span", "")
- const reroll = el("button", "frame", "other land")
+ // The land is drawn from a seed: a fresh one on a click; the number itself only on hover, for whoever wants it.
+ const reroll = el("button", "frame reroll", "↻ other land")
reroll.onclick = () => {
choice.seed = (Math.random() * 0x7fffffff) >>> 0
refresh()
}
- seedLine.append(seedText, reroll)
- preview.append(seedLine)
+ mapBox.append(reroll)
const refresh = () => {
for (const r of refreshers) r()
const land = drawLand(map)
- seedText.textContent = `seed ${choice.seed}`
+ reroll.title = `Draw other land (this is seed ${choice.seed})`
facts.textContent = `${land.cells} cells of land · ${land.trees.length} trees · ${land.rocks.filter((r) => !r.ore).length} stone · ${land.rocks.filter((r) => r.ore).length} ore · ${lands[choice.land].coins} coins to start`
}
refresh()
@@ -1430,6 +1438,26 @@ export const createHost = async (mount: HTMLElement, persistence: () => Persiste
setSpeed(1)
}
actions.append(found)
+ // On the way in, a town saved in this browser: carry on with it instead.
+ if (!cancellable && savedTown) {
+ const saved = savedTown
+ const resume = el("button", "frame", "Continue")
+ resume.title = `Carry on with the town saved in this browser: ${saved.title}`
+ resume.onclick = () => {
+ const said = persistence().load() ?? "Loaded"
+ ui.toast(said)
+ if (!said.startsWith("Loaded")) return
+ history.length = 0
+ ui.selected = null
+ ui.menu = null
+ closeCharter()
+ choose("select")
+ setSpeed(1)
+ }
+ const note = el("p", "saved", `your town: ${saved.title}${saved.savedAt > 0 ? ` · saved ${ago(saved.savedAt)}` : ""}`)
+ actions.prepend(note)
+ actions.append(resume)
+ }
if (cancellable) {
const cancel = el("button", "frame", "Cancel")
cancel.onclick = () => closeCharter()
@@ -1944,3 +1972,14 @@ const flowHtml = (s: Stats): string => {
}).join("")
return `<div class="flow-grid">${head}${body}</div>`
}
+
+/** How long ago a save was made, roughly: "just now", "12 min ago", "3 h ago", "2 days ago". */
+const ago = (at: number): string => {
+ const minutes = Math.floor((Date.now() - at) / 60000)
+ if (minutes < 1) return "just now"
+ if (minutes < 60) return `${minutes} min ago`
+ const hours = Math.floor(minutes / 60)
+ if (hours < 24) return `${hours} h ago`
+ const days = Math.floor(hours / 24)
+ return days === 1 ? "yesterday" : `${days} days ago`
+}
diff --git a/src/main.ts b/src/main.ts
index 366aae9..ea01bf4 100644
--- a/src/main.ts
+++ b/src/main.ts
@@ -12,7 +12,7 @@ import type { BuildingView, FieldView, HouseView, RockView, RuinView, TreeView,
import { lands, sizes, STEP_SECONDS } from "./content.ts"
import { createHost, type Persistence } from "./host.ts"
import { present } from "./present.ts"
-import { browserStorage, describe, fileOf, loadLatest, loadText, save, Storage, World } from "./save.ts"
+import { browserStorage, describe, fileOf, latest, loadLatest, loadText, save, Storage, World } from "./save.ts"
import type { CameraView } from "./render3d/scene.ts"
import { download, sharedId } from "./share/client.ts"
import { BuildingNodes, Clock, FieldNodes, Game, Rules, HouseNodes, RockNodes, RuinNodes, StageService, TreeNodes, Ui, WalkerNodes } from "./schema.ts"
@@ -35,7 +35,10 @@ const start = async () => {
// A shared town's link (`/t/<id>`): fetch it while the page starts, and load it once the town is up.
const shared = sharedId(location.pathname)
let arriving: string | null = null
- const { stage, ui, syncPage, resetPage, visit, visitFailed, visiting } = await createHost(mount, () => persistence, { shared: shared !== null })
+ // A town saved in this browser before: the founding screen offers to carry on with it.
+ const last = Effect.runSync(Effect.result(latest.pipe(Effect.provideService(Storage, browserStorage(localStorage)))))
+ const saved = last._tag === "Success" && last.success !== null ? { title: last.success.title, savedAt: last.success.savedAt } : null
+ const { stage, ui, syncPage, resetPage, visit, visitFailed, visiting } = await createHost(mount, () => persistence, { shared: shared !== null, saved })
if (shared)
download(shared).then(
(text) => (arriving = text),
diff --git a/src/phone.css b/src/phone.css
index 3c225b8..4e28656 100644
--- a/src/phone.css
+++ b/src/phone.css
@@ -152,7 +152,6 @@
.dialog .sub { font-size: 12px; }
.dialog section { margin-top: 12px; }
.dialog .choices .frame { flex: 1; justify-content: center; padding: 8px 6px; }
- .dialog .preview canvas { width: 132px; height: 132px; }
.dialog .chronicle { max-height: none; }
}
@@ -167,7 +166,7 @@
.panel-open .toolbar { left: calc(8px + env(safe-area-inset-left)); transform: none; max-width: calc(54vw - 16px); }
.panel-open .hud .ticker.peek { display: none; }
/* The picker: the options beside the map. */
- .dialog .charter { grid-template-columns: 1fr 150px; gap: 16px; }
+ .dialog .charter { grid-template-columns: 1fr min(42%, 300px); gap: 16px; }
}
/* ── Portrait phone: sheets from the bottom, so the town stays in view above them ── */
@@ -186,10 +185,10 @@
.panel::before { content: ""; position: absolute; top: 8px; left: 50%; width: 40px; height: 4px; margin-left: -20px; border-radius: 2px; background: #c9cec4; }
.panel.open { transform: none; }
.panel-open .hud .stock, .panel-open .hud .ticker, .panel-open .views, .panel-open .legend { display: none; }
+ /* The land first, the full width of the sheet; the choices under it. */
.dialog .charter { grid-template-columns: 1fr; gap: 0; }
- .dialog .preview { flex-direction: row; flex-wrap: wrap; align-items: flex-start; gap: 10px; margin-top: 12px; }
- .dialog .preview .sub { flex: 1; min-width: 0; margin: 0; }
- .dialog .preview .row { flex-basis: 100%; }
+ .dialog .preview { order: -1; gap: 6px; margin-top: 4px; }
+ .dialog .preview .sub { margin: 0; }
}
/* A very narrow phone: the tool bar's buttons a little narrower so it still fits. */
diff --git a/src/save.ts b/src/save.ts
index f4a9036..cdeafcb 100644
--- a/src/save.ts
+++ b/src/save.ts
@@ -188,12 +188,15 @@ export const load = (slot: Slot) =>
return file
})
+/** The slot saved last that can still be read (the mayor's own save, or the autosave), without loading it; `null` when there is none. */
+export const latest = Effect.gen(function* () {
+ const files = yield* Effect.forEach(slots, (slot) => read(slot).pipe(Effect.option))
+ return files.flatMap((f) => (f._tag === "Some" ? [f.value] : [])).sort((a, b) => b.savedAt - a.savedAt)[0] ?? null
+})
+
/** Loads whichever slot was saved last: the mayor's own save, or the autosave. */
export const loadLatest = Effect.gen(function* () {
- const files = yield* Effect.forEach(slots, (slot) => read(slot).pipe(Effect.option))
- const newest = files
- .flatMap((f) => (f._tag === "Some" ? [f.value] : []))
- .sort((a, b) => b.savedAt - a.savedAt)[0]
+ const newest = yield* latest
return yield* load(newest?.slot ?? "manual")
})