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.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
feat(game): a pack, and the inventory that was supposed to hurt
esenginef8f1e020fef1
diff --git a/examples/celestial-heights/assets/i18n/en.eslocale b/examples/celestial-heights/assets/i18n/en.eslocaleindex 364460362..24261239f 100644--- a/examples/celestial-heights/assets/i18n/en.eslocale+++ b/examples/celestial-heights/assets/i18n/en.eslocale@@ -5,6 +5,10 @@"area.main": "Cherry Blossom Landing","area.moonlit-gallery": "Moonlit Gallery","hud.vitality": "Vitality",- "menu.paused": "Paused"+ "menu.paused": "Paused",+ "menu.pack": "Pack",+ "item.petal": "Petal",+ "item.shard": "Shard",+ "item.ember": "Ember"}}diff --git a/examples/celestial-heights/assets/i18n/zh-CN.eslocale b/examples/celestial-heights/assets/i18n/zh-CN.eslocaleindex 0069ddbb0..243d43539 100644--- a/examples/celestial-heights/assets/i18n/zh-CN.eslocale+++ b/examples/celestial-heights/assets/i18n/zh-CN.eslocale@@ -5,6 +5,10 @@"area.main": "樱林渡口","area.moonlit-gallery": "孤月回廊","hud.vitality": "生命",- "menu.paused": "已暂停"+ "menu.paused": "已暂停",+ "menu.pack": "行囊",+ "item.petal": "花瓣",+ "item.shard": "晶屑",+ "item.ember": "余烬"}}diff --git a/examples/celestial-heights/assets/scenes/main.esscene b/examples/celestial-heights/assets/scenes/main.essceneindex 10423e11c..ebceb19bb 100644--- a/examples/celestial-heights/assets/scenes/main.esscene+++ b/examples/celestial-heights/assets/scenes/main.esscene@@ -126,6 +126,174 @@}]},+ {+ "id": 80,+ "name": "Pickup_petal_1",+ "parent": null,+ "children": [],+ "folder": "Actors",+ "visible": true,+ "components": [+ {+ "type": "Transform",+ "data": {+ "position": {+ "x": -736,+ "y": -32,+ "z": 0+ }+ }+ },+ {+ "type": "Sprite",+ "data": {+ "size": {+ "x": 44,+ "y": 44+ },+ "color": {+ "r": 0.937,+ "g": 0.588,+ "b": 0.749,+ "a": 1+ },+ "layer": 2+ }+ },+ {+ "type": "Item",+ "data": {+ "kind": "petal"+ }+ }+ ]+ },+ {+ "id": 81,+ "name": "Pickup_shard_2",+ "parent": null,+ "children": [],+ "folder": "Actors",+ "visible": true,+ "components": [+ {+ "type": "Transform",+ "data": {+ "position": {+ "x": -288,+ "y": -32,+ "z": 0+ }+ }+ },+ {+ "type": "Sprite",+ "data": {+ "size": {+ "x": 44,+ "y": 44+ },+ "color": {+ "r": 0.545,+ "g": 0.902,+ "b": 0.918,+ "a": 1+ },+ "layer": 2+ }+ },+ {+ "type": "Item",+ "data": {+ "kind": "shard"+ }+ }+ ]+ },+ {+ "id": 82,+ "name": "Pickup_ember_3",+ "parent": null,+ "children": [],+ "folder": "Actors",+ "visible": true,+ "components": [+ {+ "type": "Transform",+ "data": {+ "position": {+ "x": 160,+ "y": -32,+ "z": 0+ }+ }+ },+ {+ "type": "Sprite",+ "data": {+ "size": {+ "x": 44,+ "y": 44+ },+ "color": {+ "r": 0.976,+ "g": 0.706,+ "b": 0.376,+ "a": 1+ },+ "layer": 2+ }+ },+ {+ "type": "Item",+ "data": {+ "kind": "ember"+ }+ }+ ]+ },+ {+ "id": 83,+ "name": "Pickup_petal_4",+ "parent": null,+ "children": [],+ "folder": "Actors",+ "visible": true,+ "components": [+ {+ "type": "Transform",+ "data": {+ "position": {+ "x": 544,+ "y": -32,+ "z": 0+ }+ }+ },+ {+ "type": "Sprite",+ "data": {+ "size": {+ "x": 44,+ "y": 44+ },+ "color": {+ "r": 0.937,+ "g": 0.588,+ "b": 0.749,+ "a": 1+ },+ "layer": 2+ }+ },+ {+ "type": "Item",+ "data": {+ "kind": "petal"+ }+ }+ ]+ },{"id": 20,"name": "Lyra_Player",@@ -799,7 +967,8 @@"parent": null,"children": [51,- 60+ 60,+ 70],"visible": true,"components": [@@ -1079,6 +1248,145 @@}]},+ {+ "id": 70,+ "name": "PackPanel",+ "parent": 50,+ "children": [+ 71,+ 72+ ],+ "visible": true,+ "components": [+ {+ "type": "Transform",+ "data": {}+ },+ {+ "type": "UINode",+ "data": {+ "position": 1,+ "display": 1,+ "insetLeft": {+ "value": 560,+ "unit": 0+ },+ "insetTop": {+ "value": 300,+ "unit": 0+ },+ "width": {+ "value": 800,+ "unit": 0+ },+ "height": {+ "value": 360,+ "unit": 0+ }+ }+ },+ {+ "type": "FlexContainer",+ "data": {+ "direction": 1,+ "justifyContent": 0,+ "alignItems": 1,+ "gap": {+ "x": 0,+ "y": 16+ },+ "padding": {+ "left": 24,+ "top": 20,+ "right": 24,+ "bottom": 24+ }+ }+ },+ {+ "type": "UIVisual",+ "data": {+ "visualType": 1,+ "color": {+ "r": 0.055,+ "g": 0.047,+ "b": 0.098,+ "a": 0.92+ },+ "enabled": true+ }+ }+ ]+ },+ {+ "id": 71,+ "name": "PackTitle",+ "parent": 70,+ "children": [],+ "visible": true,+ "components": [+ {+ "type": "Transform",+ "data": {}+ },+ {+ "type": "UINode",+ "data": {+ "height": {+ "value": 38,+ "unit": 0+ },+ "width": {+ "value": 100,+ "unit": 1+ }+ }+ },+ {+ "type": "Text",+ "data": {+ "content": "",+ "i18nKey": "menu.pack",+ "fontSize": 32,+ "bold": true,+ "color": {+ "r": 0.925,+ "g": 0.906,+ "b": 0.98,+ "a": 1+ },+ "align": 1,+ "verticalAlign": 1+ }+ }+ ]+ },+ {+ "id": 72,+ "name": "PackGrid",+ "parent": 70,+ "children": [],+ "visible": true,+ "components": [+ {+ "type": "Transform",+ "data": {}+ },+ {+ "type": "UINode",+ "data": {+ "width": {+ "value": 408,+ "unit": 0+ },+ "height": {+ "value": 200,+ "unit": 0+ }+ }+ }+ ]+ },{"id": 61,"name": "PauseLabel",diff --git a/examples/celestial-heights/assets/scenes/moonlit-gallery.esscene b/examples/celestial-heights/assets/scenes/moonlit-gallery.essceneindex 161d0baa6..4a837dc55 100644--- a/examples/celestial-heights/assets/scenes/moonlit-gallery.esscene+++ b/examples/celestial-heights/assets/scenes/moonlit-gallery.esscene@@ -126,6 +126,90 @@}]},+ {+ "id": 80,+ "name": "Pickup_shard_5",+ "parent": null,+ "children": [],+ "folder": "Actors",+ "visible": true,+ "components": [+ {+ "type": "Transform",+ "data": {+ "position": {+ "x": -416,+ "y": -32,+ "z": 0+ }+ }+ },+ {+ "type": "Sprite",+ "data": {+ "size": {+ "x": 44,+ "y": 44+ },+ "color": {+ "r": 0.545,+ "g": 0.902,+ "b": 0.918,+ "a": 1+ },+ "layer": 2+ }+ },+ {+ "type": "Item",+ "data": {+ "kind": "shard"+ }+ }+ ]+ },+ {+ "id": 81,+ "name": "Pickup_ember_6",+ "parent": null,+ "children": [],+ "folder": "Actors",+ "visible": true,+ "components": [+ {+ "type": "Transform",+ "data": {+ "position": {+ "x": 288,+ "y": -32,+ "z": 0+ }+ }+ },+ {+ "type": "Sprite",+ "data": {+ "size": {+ "x": 44,+ "y": 44+ },+ "color": {+ "r": 0.976,+ "g": 0.706,+ "b": 0.376,+ "a": 1+ },+ "layer": 2+ }+ },+ {+ "type": "Item",+ "data": {+ "kind": "ember"+ }+ }+ ]+ },{"id": 20,"name": "Lyra_Player",@@ -611,7 +695,8 @@"parent": null,"children": [51,- 60+ 60,+ 70],"visible": true,"components": [@@ -891,6 +976,145 @@}]},+ {+ "id": 70,+ "name": "PackPanel",+ "parent": 50,+ "children": [+ 71,+ 72+ ],+ "visible": true,+ "components": [+ {+ "type": "Transform",+ "data": {}+ },+ {+ "type": "UINode",+ "data": {+ "position": 1,+ "display": 1,+ "insetLeft": {+ "value": 560,+ "unit": 0+ },+ "insetTop": {+ "value": 300,+ "unit": 0+ },+ "width": {+ "value": 800,+ "unit": 0+ },+ "height": {+ "value": 360,+ "unit": 0+ }+ }+ },+ {+ "type": "FlexContainer",+ "data": {+ "direction": 1,+ "justifyContent": 0,+ "alignItems": 1,+ "gap": {+ "x": 0,+ "y": 16+ },+ "padding": {+ "left": 24,+ "top": 20,+ "right": 24,+ "bottom": 24+ }+ }+ },+ {+ "type": "UIVisual",+ "data": {+ "visualType": 1,+ "color": {+ "r": 0.055,+ "g": 0.047,+ "b": 0.098,+ "a": 0.92+ },+ "enabled": true+ }+ }+ ]+ },+ {+ "id": 71,+ "name": "PackTitle",+ "parent": 70,+ "children": [],+ "visible": true,+ "components": [+ {+ "type": "Transform",+ "data": {}+ },+ {+ "type": "UINode",+ "data": {+ "height": {+ "value": 38,+ "unit": 0+ },+ "width": {+ "value": 100,+ "unit": 1+ }+ }+ },+ {+ "type": "Text",+ "data": {+ "content": "",+ "i18nKey": "menu.pack",+ "fontSize": 32,+ "bold": true,+ "color": {+ "r": 0.925,+ "g": 0.906,+ "b": 0.98,+ "a": 1+ },+ "align": 1,+ "verticalAlign": 1+ }+ }+ ]+ },+ {+ "id": 72,+ "name": "PackGrid",+ "parent": 70,+ "children": [],+ "visible": true,+ "components": [+ {+ "type": "Transform",+ "data": {}+ },+ {+ "type": "UINode",+ "data": {+ "width": {+ "value": 408,+ "unit": 0+ },+ "height": {+ "value": 200,+ "unit": 0+ }+ }+ }+ ]+ },{"id": 61,"name": "PauseLabel",diff --git a/examples/celestial-heights/src/components.ts b/examples/celestial-heights/src/components.tsindex c6e397c4b..0d24b3eed 100644--- a/examples/celestial-heights/src/components.ts+++ b/examples/celestial-heights/src/components.ts@@ -27,6 +27,11 @@ export const VitalityMeter = defineTag('VitalityMeter');/** The panel shown while the game is paused. */export const PauseOverlay = defineTag('PauseOverlay');+/** A pickup lying in the world. `kind` keys into ITEM_COLOR and the locale table. */+export const Item = defineComponent('Item', {+ kind: '',+});+/*** Where the actor is facing, as a unit vector. Melee arcs and animation both* read it, so it is written once per actor rather than derived twice fromdiff --git a/examples/celestial-heights/src/config.ts b/examples/celestial-heights/src/config.tsindex 889653dc4..2efc8d390 100644--- a/examples/celestial-heights/src/config.ts+++ b/examples/celestial-heights/src/config.ts@@ -30,3 +30,16 @@ export const DEPTH_FORESHORTEN = 0.6;* noise, so a facing cone rejects or accepts it at random.*/export const POINT_BLANK = 40;++/** What a pickup is, and how its tile reads in the pack. */+export const ITEM_KINDS = ['petal', 'shard', 'ember'] as const;+export type ItemKind = typeof ITEM_KINDS[number];++export const ITEM_COLOR: Record<string, { r: number; g: number; b: number; a: number }> = {+ petal: { r: 0.937, g: 0.588, b: 0.749, a: 1 },+ shard: { r: 0.545, g: 0.902, b: 0.918, a: 1 },+ ember: { r: 0.976, g: 0.706, b: 0.376, a: 1 },+};++/** How close Lyra has to be to pick something up, in world units. */+export const PICKUP_RADIUS = 70;diff --git a/examples/celestial-heights/src/main.ts b/examples/celestial-heights/src/main.tsindex 9c73bfae0..932809e9e 100644--- a/examples/celestial-heights/src/main.ts+++ b/examples/celestial-heights/src/main.ts@@ -15,6 +15,9 @@ import { cycleLanguageSystem } from './systems/settings';import {saveRunSystem, loadRunSystem, applyRestoreSystem, rememberRosterSystem,} from './systems/save';+import {+ pickupSystem, inventoryInputSystem, inventoryBuildSystem, inventorySyncSystem,+} from './systems/inventory';import { perceiverFacingSystem } from './ai/wisp';// Perception, nav following, the behaviour-tree tick and scene switching are@@ -27,11 +30,12 @@ addSystemToSchedule(Schedule.PreUpdate, navFromTerrainSystem);addSystemToSchedule(Schedule.PreUpdate, cameraBindSystem);addSystemToSchedule(Schedule.PreUpdate, rememberRosterSystem);addSystemToSchedule(Schedule.PreUpdate, applyRestoreSystem);+addSystemToSchedule(Schedule.PreUpdate, inventoryBuildSystem);// Everything the pause freezes says so once, as a set with one run condition,// rather than the same paused check written into eight systems.addSystemSetToSchedule(Schedule.Update, defineSystemSet('gameplay', {- systems: [playerMoveSystem, playerAttackSystem, perceiverFacingSystem, gateSystem],+ systems: [playerMoveSystem, playerAttackSystem, perceiverFacingSystem, gateSystem, pickupSystem],runIf: () => !session.paused,}));@@ -49,7 +53,9 @@ addSystemToSchedule(Schedule.Update, pauseTimeSystem);addSystemToSchedule(Schedule.Update, cycleLanguageSystem);addSystemToSchedule(Schedule.Update, saveRunSystem);addSystemToSchedule(Schedule.Update, loadRunSystem);+addSystemToSchedule(Schedule.Update, inventoryInputSystem);addSystemToSchedule(Schedule.PostUpdate, pauseOverlaySystem);addSystemToSchedule(Schedule.PostUpdate, healthBarSystem);addSystemToSchedule(Schedule.PostUpdate, vitalityMeterSystem);addSystemToSchedule(Schedule.PostUpdate, hitFlashSystem);+addSystemToSchedule(Schedule.PostUpdate, inventorySyncSystem);diff --git a/examples/celestial-heights/src/save.ts b/examples/celestial-heights/src/save.tsindex 1f406b56f..6938357d0 100644--- a/examples/celestial-heights/src/save.ts+++ b/examples/celestial-heights/src/save.ts@@ -14,6 +14,8 @@ export interface RunState {/** Names of enemies already killed in that area. */slain: string[];locale: string;+ /** Item kind → count. */+ pack: Record<string, number>;}export const SLOT = 'run';diff --git a/examples/celestial-heights/src/state.ts b/examples/celestial-heights/src/state.tsindex 4f8d58db2..e1c620d57 100644--- a/examples/celestial-heights/src/state.ts+++ b/examples/celestial-heights/src/state.ts@@ -12,4 +12,7 @@ export const session = {/** Area name → the enemies it was authored with. */enemiesByArea: {} as Record<string, string[]>,savedAt: 0,+ /** Item kind → how many Lyra is carrying. */+ inventory: {} as Record<string, number>,+ inventoryOpen: false,};diff --git a/examples/celestial-heights/src/systems/inventory.ts b/examples/celestial-heights/src/systems/inventory.tsnew file mode 100644index 000000000..8e1f87a5d--- /dev/null+++ b/examples/celestial-heights/src/systems/inventory.ts@@ -0,0 +1,135 @@+import {+ defineSystem, Query, Mut, Res, Commands, GetWorld,+ Input, Transform, UINode, UIDisplay, Text, Localization,+ ArrayDataSource, createListView, spawnUIEntity, px, uiPlugin,+ UIPositionType, TextAlign, TextVerticalAlign,+} from 'esengine';+import type { Entity, World, TextData, UIVisualData } from 'esengine';+import { UIVisual } from 'esengine';+import { Item, Player } from '../components';+import { ITEM_COLOR, PICKUP_RADIUS } from '../config';+import { session } from '../state';++interface Slot {+ kind: string;+ count: number;+}++const TILE = 96;+const COLUMNS = 4;++/** Walking over a pickup takes it. */+export const pickupSystem = defineSystem(+ [Query(Transform, Item), Query(Transform, Player), Commands()],+ (items, players, commands) => {+ for (const [, playerTransform] of players) {+ for (const [entity, transform, item] of items) {+ const dx = transform.position.x - playerTransform.position.x;+ const dy = transform.position.y - playerTransform.position.y;+ if (Math.hypot(dx, dy) > PICKUP_RADIUS) continue;+ session.inventory[item.kind] = (session.inventory[item.kind] ?? 0) + 1;+ commands.despawn(entity);+ }+ return;+ }+ },+ { name: 'PickupSystem' },+);++export const inventoryInputSystem = defineSystem(+ [Res(Input)],+ (input) => {+ if (input.isKeyPressed('Tab')) session.inventoryOpen = !session.inventoryOpen;+ },+ { name: 'InventoryInputSystem' },+);++// The pack's grid is built into an authored anchor, and an area switch replaces+// that anchor along with the rest of its scene — so the guard is whether the+// anchor this was built into is still alive, not whether it was ever built.+let built: { anchor: Entity; data: ArrayDataSource<Slot>; shown: Slot[] } | null = null;+const tileLabel = new Map<Entity, Entity>();++function slots(): Slot[] {+ return Object.entries(session.inventory)+ .filter(([, count]) => count > 0)+ .map(([kind, count]) => ({ kind, count }));+}++export const inventoryBuildSystem = defineSystem(+ [GetWorld(), Res(Localization)],+ (world: World, i18n) => {+ if (built && world.valid(built.anchor)) return;+ const anchor = world.findEntityByName('PackGrid');+ if (anchor === null) return;++ const data = new ArrayDataSource<Slot>(slots());+ createListView<Slot>({+ world,+ host: uiPlugin,+ parent: anchor,+ viewportSize: { x: COLUMNS * TILE + (COLUMNS - 1) * 8, y: 2 * TILE + 8 },+ data,+ layout: { columns: COLUMNS, itemSize: { x: TILE, y: TILE }, spacing: { x: 8, y: 8 } },+ item: {+ create: (w, parent) => {+ const tile = spawnUIEntity({+ world: w, parent, visual: { color: { r: 1, g: 1, b: 1, a: 1 } },+ });+ const label = spawnUIEntity({+ world: w, parent: tile,+ node: {+ position: UIPositionType.Absolute,+ insetLeft: px(0), insetRight: px(0), insetBottom: px(6), height: px(28),+ },+ text: {+ content: '', fontSize: 24, bold: true,+ color: { r: 0.08, g: 0.07, b: 0.13, a: 1 },+ align: TextAlign.Center, verticalAlign: TextVerticalAlign.Middle,+ },+ });+ tileLabel.set(tile, label);+ return tile;+ },+ bind: (entity, slot) => {+ const visual = world.get(entity, UIVisual) as UIVisualData;+ visual.color = ITEM_COLOR[slot.kind] ?? { r: 0.6, g: 0.6, b: 0.6, a: 1 };+ world.insert(entity, UIVisual, visual);+ const label = tileLabel.get(entity);+ if (label === undefined) return;+ const text = world.get(label, Text) as TextData;+ text.content = `${i18n.t(`item.${slot.kind}`)} ${slot.count}`;+ world.insert(label, Text, text);+ },+ },+ });+ built = { anchor, data, shown: slots() };+ },+ { name: 'InventoryBuildSystem' },+);++/** Pushes what Lyra carries into the grid, and shows or hides the pack. */+export const inventorySyncSystem = defineSystem(+ [Query(Mut(UINode)), GetWorld()],+ (nodes, world) => {+ if (!built) return;+ const next = slots();+ const shown = built.shown;+ const changed = next.length !== shown.length+ || next.some((s, i) => s.kind !== shown[i].kind || s.count !== shown[i].count);+ if (changed) {+ built.data.setItems(next);+ built.shown = next;+ }++ const panel = world.findEntityByName('PackPanel');+ if (panel === null) return;+ const display = session.inventoryOpen ? UIDisplay.Flex : UIDisplay.None;+ for (const [entity, node] of nodes) {+ if (entity !== panel) continue;+ if (node.display !== display) node.display = display;+ return;+ }+ },+ { name: 'InventorySyncSystem' },+);diff --git a/examples/celestial-heights/src/systems/save.ts b/examples/celestial-heights/src/systems/save.tsindex 5a0d7facf..295fcb9ac 100644--- a/examples/celestial-heights/src/systems/save.ts+++ b/examples/celestial-heights/src/systems/save.ts@@ -24,6 +24,7 @@ export const saveRunSystem = defineSystem(health: health.current,slain: (session.enemiesByArea[area] ?? []).filter((n) => !alive.has(n)),locale: i18n.locale,+ pack: { ...session.inventory },});session.savedAt = Date.now();return;@@ -39,6 +40,7 @@ export const loadRunSystem = defineSystem(const run = saves.load<RunState>(SLOT);if (!run) return;i18n.setLocale(run.locale);+ session.inventory = { ...(run.pack ?? {}) };session.restore = run;session.paused = false;if (scenes.getActive() !== run.area) void transitionTo(scenes, run.area, { type: 'fade', duration: 0.25 });diff --git a/tools/goldenProjects.mjs b/tools/goldenProjects.mjsindex f1a14b999..6470461c1 100644--- a/tools/goldenProjects.mjs+++ b/tools/goldenProjects.mjs@@ -80,7 +80,6 @@ export const KNOWN_GAPS = {settings: 'no project persists graphics/audio/language/rebinds and reads them back — Celestial Heights P3',controller: 'input-actions binds a gamepad, but no golden run drives one — Celestial Heights P5',achievements: 'the achievements service has no project that unlocks one in play — Celestial Heights P4',- 'ui-inventory': 'ui-list virtualizes rows; no project builds an inventory a player operates — Celestial Heights P3',// Measured: hello-world exports a 2.82MB single file, video-puzzle 3.22MB — so// the floor is the runtime, not the game, and no project reaches the 2MB cap.'startup-size': 'the playable runtime floor (~2.8MB) exceeds the 2MB default profile cap; see REARCH_EXPORT',@@ -174,7 +173,7 @@ export const GOLDEN = [certifies: ['tilemap', 'tile-collision', 'navigation', 'behavior-tree','scene-transition', 'y-sort', 'localization', 'ui-layout', 'text',- 'persistence',+ 'persistence', 'ui-inventory',],targets: ['web', 'desktop'],// Nightly, not pr: a project earns the release gate by having run, and this