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.

feat(game): a run you can put down and pick up

esengine3ae5c4d0a3e1
diff --git a/examples/celestial-heights/src/main.ts b/examples/celestial-heights/src/main.ts
index 4547cee51..9c73bfae0 100644
--- a/examples/celestial-heights/src/main.ts
+++ b/examples/celestial-heights/src/main.ts
@@ -12,6 +12,9 @@ import { healthBarSystem, hitFlashSystem, vitalityMeterSystem } from './systems/
import { cameraBindSystem, navFromTerrainSystem, gateSystem } from './systems/world';
import { pauseInputSystem, pauseTimeSystem, pauseOverlaySystem } from './systems/pause';
import { cycleLanguageSystem } from './systems/settings';
+import {
+ saveRunSystem, loadRunSystem, applyRestoreSystem, rememberRosterSystem,
+} from './systems/save';
import { perceiverFacingSystem } from './ai/wisp';
// Perception, nav following, the behaviour-tree tick and scene switching are
@@ -22,6 +25,8 @@ import { perceiverFacingSystem } from './ai/wisp';
// brings a new camera, a new terrain and a new player, and startup is long past.
addSystemToSchedule(Schedule.PreUpdate, navFromTerrainSystem);
addSystemToSchedule(Schedule.PreUpdate, cameraBindSystem);
+addSystemToSchedule(Schedule.PreUpdate, rememberRosterSystem);
+addSystemToSchedule(Schedule.PreUpdate, applyRestoreSystem);
// Everything the pause freezes says so once, as a set with one run condition,
// rather than the same paused check written into eight systems.
@@ -42,6 +47,8 @@ addSystemSetToSchedule(Schedule.PostUpdate, defineSystemSet('combat', {
addSystemToSchedule(Schedule.Update, pauseInputSystem);
addSystemToSchedule(Schedule.Update, pauseTimeSystem);
addSystemToSchedule(Schedule.Update, cycleLanguageSystem);
+addSystemToSchedule(Schedule.Update, saveRunSystem);
+addSystemToSchedule(Schedule.Update, loadRunSystem);
addSystemToSchedule(Schedule.PostUpdate, pauseOverlaySystem);
addSystemToSchedule(Schedule.PostUpdate, healthBarSystem);
addSystemToSchedule(Schedule.PostUpdate, vitalityMeterSystem);
diff --git a/examples/celestial-heights/src/save.ts b/examples/celestial-heights/src/save.ts
new file mode 100644
index 000000000..1f406b56f
--- /dev/null
+++ b/examples/celestial-heights/src/save.ts
@@ -0,0 +1,21 @@
+import { SaveManager } from 'esengine';
+
+/**
+ * What a run is, written down. Deliberately the state a player would notice
+ * losing — where they were, how hurt, what they had already killed — and not a
+ * dump of the world, which would break the moment a scene is re-authored.
+ */
+export interface RunState {
+ /** Packaged scene name, the id `SceneManager.switchTo` takes. */
+ area: string;
+ x: number;
+ y: number;
+ health: number;
+ /** Names of enemies already killed in that area. */
+ slain: string[];
+ locale: string;
+}
+
+export const SLOT = 'run';
+
+export const saves = new SaveManager({ version: 1 });
diff --git a/examples/celestial-heights/src/state.ts b/examples/celestial-heights/src/state.ts
index 2c9a57df7..4f8d58db2 100644
--- a/examples/celestial-heights/src/state.ts
+++ b/examples/celestial-heights/src/state.ts
@@ -1,7 +1,15 @@
+import type { RunState } from './save';
+
/**
- * Session state that is not a component: things a run condition asks about,
- * which by definition cannot take a `World`.
+ * Session state that is not a component: what a run condition asks about (which
+ * by definition cannot take a `World`), and what has to survive a scene swap —
+ * scene-owned entities do not.
*/
export const session = {
paused: false,
+ /** A loaded run waiting for its area to finish arriving. */
+ restore: null as RunState | null,
+ /** Area name → the enemies it was authored with. */
+ enemiesByArea: {} as Record<string, string[]>,
+ savedAt: 0,
};
diff --git a/examples/celestial-heights/src/systems/save.ts b/examples/celestial-heights/src/systems/save.ts
new file mode 100644
index 000000000..5a0d7facf
--- /dev/null
+++ b/examples/celestial-heights/src/systems/save.ts
@@ -0,0 +1,89 @@
+import {
+ defineSystem, Query, Mut, Res, Commands, GetWorld,
+ Input, Transform, Name, SceneManager, Localization, transitionTo,
+} from 'esengine';
+import { Enemy, Health, Player } from '../components';
+import { saves, SLOT, type RunState } from '../save';
+import { session } from '../state';
+
+export const saveRunSystem = defineSystem(
+ [Query(Transform, Health, Player), Query(Name, Enemy), Res(Input), Res(SceneManager), Res(Localization)],
+ (players, living, input, scenes, i18n) => {
+ if (!input.isKeyPressed('F5')) return;
+ const area = scenes.getActive();
+ if (!area) return;
+ // Which enemies the scene HAD, minus the ones still standing: a save
+ // says what is gone, so a scene that gains an enemy later still spawns it.
+ const alive = new Set<string>();
+ for (const [, name] of living) alive.add(name.value);
+ for (const [, transform, health] of players) {
+ saves.save<RunState>(SLOT, {
+ area,
+ x: transform.position.x,
+ y: transform.position.y,
+ health: health.current,
+ slain: (session.enemiesByArea[area] ?? []).filter((n) => !alive.has(n)),
+ locale: i18n.locale,
+ });
+ session.savedAt = Date.now();
+ return;
+ }
+ },
+ { name: 'SaveRunSystem' },
+);
+
+export const loadRunSystem = defineSystem(
+ [Res(Input), Res(SceneManager), Res(Localization)],
+ (input, scenes, i18n) => {
+ if (!input.isKeyPressed('F9')) return;
+ const run = saves.load<RunState>(SLOT);
+ if (!run) return;
+ i18n.setLocale(run.locale);
+ session.restore = run;
+ session.paused = false;
+ if (scenes.getActive() !== run.area) void transitionTo(scenes, run.area, { type: 'fade', duration: 0.25 });
+ },
+ { name: 'LoadRunSystem' },
+);
+
+/**
+ * Applies a pending restore once the right area is live. Loading is two acts —
+ * the scene arrives asynchronously — so the state waits for its world rather
+ * than being written into whichever one happened to be up.
+ */
+export const applyRestoreSystem = defineSystem(
+ [Query(Mut(Transform), Mut(Health), Player), Query(Name, Enemy), Res(SceneManager), Commands(), GetWorld()],
+ (players, living, scenes, commands, world) => {
+ const run = session.restore;
+ if (!run || scenes.getActive() !== run.area || scenes.isTransitioning()) return;
+ let placed = false;
+ for (const [, transform, health] of players) {
+ transform.position.x = run.x;
+ transform.position.y = run.y;
+ health.current = run.health;
+ placed = true;
+ break;
+ }
+ if (!placed) return;
+ const slain = new Set(run.slain);
+ for (const [entity, name] of living) {
+ if (slain.has(name.value)) commands.despawn(entity);
+ }
+ void world;
+ session.restore = null;
+ },
+ { name: 'ApplyRestoreSystem' },
+);
+
+/** Records the enemies an area was authored with, so a save can say what is gone. */
+export const rememberRosterSystem = defineSystem(
+ [Query(Name, Enemy), Res(SceneManager)],
+ (living, scenes) => {
+ const area = scenes.getActive();
+ if (!area || session.enemiesByArea[area]) return;
+ const names: string[] = [];
+ for (const [, name] of living) names.push(name.value);
+ if (names.length > 0) session.enemiesByArea[area] = names;
+ },
+ { name: 'RememberRosterSystem' },
+);
diff --git a/tools/goldenProjects.mjs b/tools/goldenProjects.mjs
index db9aa4491..f1a14b999 100644
--- a/tools/goldenProjects.mjs
+++ b/tools/goldenProjects.mjs
@@ -174,6 +174,7 @@ export const GOLDEN = [
certifies: [
'tilemap', 'tile-collision', 'navigation', 'behavior-tree',
'scene-transition', 'y-sort', 'localization', 'ui-layout', 'text',
+ 'persistence',
],
targets: ['web', 'desktop'],
// Nightly, not pr: a project earns the release gate by having run, and this