Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

test(gameplay): the level's run is judged on what the level says

esengine54d17c117247
diff --git a/examples/third-person-3d/playthrough.json b/examples/third-person-3d/playthrough.json
new file mode 100644
index 000000000..e32c6790a
--- /dev/null
+++ b/examples/third-person-3d/playthrough.json
@@ -0,0 +1,10 @@
+{
+ "name": "third-person — the facts a run states about itself",
+ "legs": [
+ { "area": "main", "until": { "phase": "playing", "health": 100 }, "timeout": 300 },
+ { "area": "main", "keys": ["KeyD"], "until": { "dead": true }, "timeout": 900 },
+ { "area": "main", "tap": "KeyE", "until": { "dead": false, "phase": "playing" }, "timeout": 600 },
+ { "area": "main", "tap": "Escape", "until": { "paused": true }, "timeout": 600 },
+ { "area": "main", "tap": "Escape", "until": { "paused": false }, "timeout": 600 }
+ ]
+}
diff --git a/examples/third-person-3d/src/main.ts b/examples/third-person-3d/src/main.ts
index 145ac5b56..36f90fc63 100644
--- a/examples/third-person-3d/src/main.ts
+++ b/examples/third-person-3d/src/main.ts
@@ -52,7 +52,7 @@ import './components';
import { runInputSystem, sprintSystem, progressSystem } from './systems/run';
import { coreBobSystem, hazardSystem, interactSystem } from './systems/world';
import { lifecycleSystem } from './systems/lifecycle';
-import { hudSystem, openingSystem } from './systems/hud';
+import { hudSystem, openingSystem, factsSystem } from './systems/hud';
// Order is the frame's argument: input becomes edges, the world reacts to them,
// the lifecycle decides what state that leaves the run in, and only then is the
@@ -66,3 +66,4 @@ addSystemToSchedule(Schedule.Update, interactSystem);
addSystemToSchedule(Schedule.Update, lifecycleSystem);
addSystemToSchedule(Schedule.Update, hudSystem);
addSystemToSchedule(Schedule.Update, openingSystem);
+addSystemToSchedule(Schedule.Update, factsSystem);
diff --git a/examples/third-person-3d/src/systems/hud.ts b/examples/third-person-3d/src/systems/hud.ts
index b109c68a9..78221e352 100644
--- a/examples/third-person-3d/src/systems/hud.ts
+++ b/examples/third-person-3d/src/systems/hud.ts
@@ -1,6 +1,6 @@
import {
- defineSystem, Res, Query, Mut,
- Text, UIVisual, Health,
+ defineSystem, Res, ResMut, Query, Mut,
+ Text, UIVisual, Health, Playthrough, type PlaythroughData,
} from 'esengine';
import { Runner, HealthMeter, ObjectiveText, PromptText, OverlayText } from '../components';
import { Run, MAX_HEALTH, CORES_NEEDED, type RunData } from '../resources';
@@ -60,3 +60,30 @@ export const openingSystem = defineSystem(
},
{ name: 'OpeningSystem' },
);
+
+/**
+ * The same run, stated for automation rather than for the player. A HUD is read
+ * by eye and proves nothing to a gate; these are the facts a playthrough is
+ * judged on, published by the game that owns them.
+ */
+export const factsSystem = defineSystem(
+ [Res(Run), Query(Health, Runner), ResMut(Playthrough)],
+ (run: RunData, runners, factsMut) => {
+ let health = 0;
+ for (const [, hp] of runners) health = hp.current;
+ const out = factsMut.get() as PlaythroughData;
+ out.facts = {
+ phase: run.phase,
+ health,
+ dead: run.phase === 'dead',
+ paused: run.phase === 'paused',
+ victory: run.phase === 'won',
+ cores: run.cores,
+ coresNeeded: CORES_NEEDED,
+ gateOpen: run.cores >= CORES_NEEDED,
+ checkpointZ: run.respawn.z,
+ elapsed: Math.round(run.elapsed),
+ };
+ },
+ { name: 'PlaythroughFactsSystem' },
+);