Skip to content

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

Debug runtimes take a host clock (bevy-ts HostClock)

SandroMaglione79495b607fc9
diff --git a/src/dev/devtools.ts b/src/dev/devtools.ts
index 6b24acc..60461d2 100644
--- a/src/dev/devtools.ts
+++ b/src/dev/devtools.ts
@@ -5,6 +5,7 @@
* `vite.config.ts`) when a step fails, when an invariant breaks, or when the
* monitor's "Save replay" button is pressed. See PLAYTEST.md.
*/
+import type { HostClock } from "@typeonce/bevy-ts"
import { Invariant, Monitor, Replay } from "@typeonce/bevy-ts-devtools"
import * as ReplayViteSink from "@typeonce/bevy-ts-devtools/ReplayViteSink"
@@ -37,7 +38,14 @@ export interface DevtoolsHost {
/** Where the monitor's panel goes. */
readonly mount: HTMLElement
readonly fetch: ReplayViteSink.Options["fetch"]
- readonly now: () => number
+ /**
+ * A wall clock for the recorder: a replay's `savedAt` names its file in
+ * `.replays/` (`new Date(savedAt)`), so it must be real time, not the
+ * page-relative `performance.now` the runtime's debug timings read.
+ */
+ readonly wallClock: HostClock.HostClock
+ /** The page the monitor listens to. */
+ readonly host: Window
/** Breaks `FewerThanTenBuildings` on purpose, to try a replay out. */
readonly demo: boolean
}
@@ -49,9 +57,9 @@ export const attachDevtools = (host: DevtoolsHost) => {
keep: 60,
stepSeconds: STEP_SECONDS,
sink: ReplayViteSink.make({ fetch: host.fetch, onError: (error) => console.warn("The replay could not reach the dev server", error) }),
- now: host.now,
+ hostClock: host.wallClock,
saveOn: { failure: true, invariants: host.demo ? [CoinsAreWhole, FewerThanTenBuildings] : [CoinsAreWhole] }
})
- const monitor = Monitor.attach(host.runtime, { schedules: { simulate, present }, mount: host.mount, replay: recorder })
+ const monitor = Monitor.attach(host.runtime, { schedules: { simulate, present }, mount: host.mount, host: host.host, replay: recorder })
return { recorder, monitor }
}
diff --git a/src/main.ts b/src/main.ts
index a6ada61..075085e 100644
--- a/src/main.ts
+++ b/src/main.ts
@@ -4,9 +4,9 @@ import { resolveRules } from "./rules/config.ts"
* bevy-ts ECS with a 3D view. The simulation runs on a fixed step;
* the views follow once a frame.
*/
-import { Result } from "@typeonce/bevy-ts"
+import { HostClock, Result } from "@typeonce/bevy-ts"
import { Effect } from "effect"
-import { FixedLoop } from "@typeonce/bevy-ts-browser"
+import { BrowserHostClock, FixedLoop } from "@typeonce/bevy-ts-browser"
import type { BuildingView, FieldView, HouseView, RockView, RuinView, TreeView, WalkerView } from "./render3d/views.ts"
import { lands, sizes, STEP_SECONDS } from "./content.ts"
@@ -59,6 +59,8 @@ const start = async () => {
stage.resetPresentation()
resetPage()
}
+ // The page's clock: the debug handle times systems with it.
+ const hostClock = BrowserHostClock.make(window)
const made = Game.Runtime.make({
services: Game.Runtime.services(
Game.Runtime.service(Ui, ui),
@@ -76,7 +78,7 @@ const start = async () => {
),
resources: blankResources(),
// Dev builds carry the debug handle, for the monitor and the replay recorder.
- debug: import.meta.env.DEV
+ debug: import.meta.env.DEV ? { hostClock } : undefined
})
if (!made.ok) {
mount.textContent = `The town could not start: ${JSON.stringify(made.error)}`
@@ -94,7 +96,7 @@ const start = async () => {
if (runtime.debug !== undefined) {
// Loaded in dev only: the production bundle never includes devtools.
const { attachDevtools } = await import("./dev/devtools.ts")
- attachDevtools({ runtime: { ...runtime, debug: runtime.debug }, mount: document.body, fetch: (url, init) => fetch(url, init), now: () => Date.now(), demo: new URLSearchParams(location.search).has("replay-demo") })
+ attachDevtools({ runtime: { ...runtime, debug: runtime.debug }, mount: document.body, fetch: (url, init) => fetch(url, init), wallClock: HostClock.standard, host: window, demo: new URLSearchParams(location.search).has("replay-demo") })
}
}
diff --git a/src/sim/headless.ts b/src/sim/headless.ts
index fe1aabc..719cfe9 100644
--- a/src/sim/headless.ts
+++ b/src/sim/headless.ts
@@ -4,6 +4,7 @@
* this, pushing the same edits the page does and reading the world through
* the same snapshots it saves.
*/
+import { HostClock } from "@typeonce/bevy-ts"
import type * as Replay from "@typeonce/bevy-ts-devtools/Replay"
import { STEP_SECONDS, type CharterValue } from "../content.ts"
@@ -44,13 +45,15 @@ const Toasts = Game.Inspector("Town/Toasts", { events: { toast: Game.System.read
* `pnpm replay`): the recorded edits instead of a page's.
*/
export const replayTarget = (playback: Replay.Playback) => {
+ // A test clock: every timing reads 0, so a replay's text output is the same on every run.
+ const hostClock = HostClock.test()
const made = Game.Runtime.make({
services: Game.Runtime.services(Game.Runtime.service(Edits, playback.source(Edits)), Game.Runtime.service(Rules, resolveRules())),
resources: blankResources(),
- debug: true
+ debug: { hostClock }
})
if (!made.ok) throw new Error(`The town could not start: ${JSON.stringify(made.error)}`)
- return { runtime: made.value, schedules: { simulate } }
+ return { runtime: made.value, schedules: { simulate }, hostClock }
}
/**
@@ -64,7 +67,7 @@ export const lintTarget = async () => {
const made = Game.Runtime.make({
services: Game.Runtime.services(Game.Runtime.service(Edits, editsOf(blankUi(lintCharter, () => {}))), Game.Runtime.service(Rules, resolveRules())),
resources: blankResources(),
- debug: true
+ debug: { hostClock: HostClock.test() }
})
if (!made.ok) throw new Error(`The town could not start: ${JSON.stringify(made.error)}`)
return { runtime: made.value, schedules: { simulate }, describe: { present } }