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
examples(enemy-ai): editor-authored .esfsm/.esbt assets + dual paradigm
esenginef46520f86056
diff --git a/examples/enemy-ai/README.md b/examples/enemy-ai/README.mdindex 856b0b2fc..ba74ff416 100644--- a/examples/enemy-ai/README.md+++ b/examples/enemy-ai/README.md@@ -1,29 +1,40 @@# Enemy AI-Two enemies **patrol** until they **sense** the player, then **chase** via A\*-navigation — a state machine and the nav agent working together. Move the player-with **WASD** / arrow keys; walk within range and the red enemies path toward you,-break off and idle when you slip away.+Two enemies hunt the player, each driven by a **different, editor-authored AI+asset**, but sharing the same senses and actions:++- **Enemy A** runs a **state machine** (`assets/ai/enemy.esfsm`): `Patrol ⇄ Chase`.+- **Enemy B** runs a **behavior tree** (`assets/ai/enemy.esbt`): a Selector that+ chases when it sees the player, otherwise patrols.++Move the player with **WASD** / arrow keys; walk into an enemy's sight range and+it paths toward you (A\* navigation), then breaks off and idles when you slip away.## What it shows-- **State machine** (`registerFsm`) as pure data: `Patrol ⇄ Chase`, switched by a- `seesPlayer` blackboard guard. Authorable in the editor's State Machine editor- (a `.esfsm` asset) or in code as here.-- **Named actions** (`registerAction`): the `chase` leaf steers the agent's- NavAgent at the player — decision (FSM) and locomotion (nav) stay decoupled.-- **Navigation** (`NavGrid` + `NavAgent` + `setNavDestination`): grid A\* plans the- path; the built-in nav plugin follows it. No per-frame movement code.-- **Perception** feeding the blackboard: a small system writes `seesPlayer` from- enemy↔player distance, which the FSM guard reads.+- **Editor-authored AI assets, not code.** Open `assets/ai/enemy.esfsm` and+ `assets/ai/enemy.esbt` in the editor (double-click them in the Content Browser)+ to see and edit the graphs. The scene's `StateMachineAgent.fsm` /+ `BehaviorTreeAgent.bt` just point at these asset paths; the engine loads them —+ there is no `registerFsm`/`registerBt` in the project code.+- **A state machine and a behavior tree side by side** — the same behavior, two+ paradigms, so you can compare them.+- **One shared registry.** The `seesPlayer` / `lostPlayer` conditions and the+ `chase` / `patrol` actions (in `src/enemy.ts`) serve *both* the FSM and the BT.+- **Perception → decision → navigation.** A `Perceiver` senses the player (a+ `PerceptionTarget`) into a `Perception` component; the FSM/BT read it via+ `ctx.get(Perception)`; the `chase` action calls `setNavDestination`, and the+ built-in nav plugin paths the agent there.## Files-- `src/enemy.ts` — the FSM definition, the `chase` action, the perception system,- and the startup grid.-- `src/player.ts` — the WASD-controlled player.-- `src/main.ts` — registers the systems (the fsm/nav plugins are built in).+- `assets/ai/enemy.esfsm` — the state machine (open it in the editor to edit).+- `assets/ai/enemy.esbt` — the behavior tree (open it in the editor to edit).+- `src/enemy.ts` — the shared conditions/actions + the startup nav grid.+- `src/player.ts` + `src/components.ts` — the WASD player and its `PlayerControl`.+- `src/main.ts` — registers the player + nav-grid systems (perception, the FSM/BT+ ticks and nav follow are all built-in engine plugins).-The `StateMachineAgent` and `NavAgent` components are attached to the enemies in-`assets/scenes/main.esscene`; the FSM they reference (`"enemy"`) is registered in-code. Both are engine built-ins — no plugin wiring needed.+`StateMachineAgent` / `BehaviorTreeAgent` / `NavAgent` / `Perceiver` /+`PerceptionTarget` are engine built-ins, so they aren't declared in+`src/components.ts` — only the project's own `PlayerControl` is.diff --git a/examples/enemy-ai/assets/ai/enemy.esbt b/examples/enemy-ai/assets/ai/enemy.esbtnew file mode 100644index 000000000..53b2f03c2--- /dev/null+++ b/examples/enemy-ai/assets/ai/enemy.esbt@@ -0,0 +1,21 @@+{+ "root": {+ "type": "selector",+ "id": "n0",+ "x": 60,+ "y": 150,+ "children": [+ {+ "type": "sequence",+ "id": "n1",+ "x": 300,+ "y": 70,+ "children": [+ { "type": "condition", "id": "n2", "name": "seesPlayer", "x": 540, "y": 40 },+ { "type": "action", "id": "n3", "name": "chase", "x": 540, "y": 120 }+ ]+ },+ { "type": "action", "id": "n4", "name": "patrol", "x": 300, "y": 240 }+ ]+ }+}diff --git a/examples/enemy-ai/assets/ai/enemy.esbt.meta b/examples/enemy-ai/assets/ai/enemy.esbt.metanew file mode 100644index 000000000..5929bb31e--- /dev/null+++ b/examples/enemy-ai/assets/ai/enemy.esbt.meta@@ -0,0 +1,6 @@+{+ "uuid": "b2f3d4e5-f6c7-48b9-ad12-2233445566bb",+ "version": "1.0",+ "type": "behaviortree",+ "importer": { "autoMigrate": true }+}diff --git a/examples/enemy-ai/assets/ai/enemy.esfsm b/examples/enemy-ai/assets/ai/enemy.esfsmnew file mode 100644index 000000000..4c2950517--- /dev/null+++ b/examples/enemy-ai/assets/ai/enemy.esfsm@@ -0,0 +1,22 @@+{+ "initial": "Patrol",+ "states": [+ {+ "name": "Patrol",+ "x": 80,+ "y": 110,+ "transitions": [+ { "to": "Chase", "condition": "seesPlayer" }+ ]+ },+ {+ "name": "Chase",+ "x": 380,+ "y": 110,+ "onUpdate": "chase",+ "transitions": [+ { "to": "Patrol", "condition": "lostPlayer" }+ ]+ }+ ]+}diff --git a/examples/enemy-ai/assets/ai/enemy.esfsm.meta b/examples/enemy-ai/assets/ai/enemy.esfsm.metanew file mode 100644index 000000000..704b6dc24--- /dev/null+++ b/examples/enemy-ai/assets/ai/enemy.esfsm.meta@@ -0,0 +1,6 @@+{+ "uuid": "a1f2c3d4-e5b6-47a8-9c01-1122334455aa",+ "version": "1.0",+ "type": "statemachine",+ "importer": { "autoMigrate": true }+}diff --git a/examples/enemy-ai/assets/scenes/main.esscene b/examples/enemy-ai/assets/scenes/main.essceneindex d12316915..66d0ea6dc 100644--- a/examples/enemy-ai/assets/scenes/main.esscene+++ b/examples/enemy-ai/assets/scenes/main.esscene@@ -34,7 +34,7 @@"components": [{ "type": "Transform", "data": { "position": { "x": 200, "y": 150, "z": 0 } } },{ "type": "Sprite", "data": { "size": { "x": 36, "y": 36 }, "color": { "r": 0.9, "g": 0.35, "b": 0.3, "a": 1 }, "layer": 2 } },- { "type": "StateMachineAgent", "data": { "fsm": "enemy" } },+ { "type": "StateMachineAgent", "data": { "fsm": "assets/ai/enemy.esfsm" } },{ "type": "NavAgent", "data": { "speed": 120, "repathInterval": 0.35 } },{ "type": "Perceiver", "data": { "range": 260, "fovDegrees": 360 } }],@@ -48,7 +48,7 @@"components": [{ "type": "Transform", "data": { "position": { "x": 260, "y": -160, "z": 0 } } },{ "type": "Sprite", "data": { "size": { "x": 36, "y": 36 }, "color": { "r": 0.95, "g": 0.55, "b": 0.25, "a": 1 }, "layer": 2 } },- { "type": "StateMachineAgent", "data": { "fsm": "enemy" } },+ { "type": "BehaviorTreeAgent", "data": { "bt": "assets/ai/enemy.esbt" } },{ "type": "NavAgent", "data": { "speed": 95, "repathInterval": 0.5 } },{ "type": "Perceiver", "data": { "range": 220, "fovDegrees": 360 } }],diff --git a/examples/enemy-ai/src/enemy.ts b/examples/enemy-ai/src/enemy.tsindex 3796e6111..11c581baa 100644--- a/examples/enemy-ai/src/enemy.ts+++ b/examples/enemy-ai/src/enemy.ts@@ -1,30 +1,22 @@import {defineSystem, Res,- registerFsm, registerAction, registerCondition, setNavDestination, Nav, NavGrid, Perception,+ registerAction, registerCondition, setNavDestination, Nav, NavGrid, Perception,} from 'esengine';-// The enemy brain reads the Perception component that the engine's built-in-// perception system writes (sight range + field-of-view over PerceptionTargets),-// so sensing and decision-making stay decoupled — no per-project sensing code.+// Leaf actions/conditions shared by BOTH brains — the state machine+// (assets/ai/enemy.esfsm) and the behavior tree (assets/ai/enemy.esbt) resolve+// these names from the same registry. The graphs are authored in the editor;+// the scene's StateMachineAgent.fsm / BehaviorTreeAgent.bt point at the asset+// paths, and the engine loads them (no registerFsm/registerBt in code).registerCondition('seesPlayer', ctx => ctx.has(Perception) && ctx.get(Perception).visible);registerCondition('lostPlayer', ctx => !ctx.has(Perception) || !ctx.get(Perception).visible);-// Patrol until the player is seen, chase while seen, break off when lost.-registerFsm('enemy', {- initial: 'Patrol',- states: [- { name: 'Patrol', transitions: [{ to: 'Chase', condition: 'seesPlayer' }] },- { name: 'Chase', onUpdate: 'chase', transitions: [{ to: 'Patrol', condition: 'lostPlayer' }] },- ],-});--// Chase leaf: steer this agent's NavAgent at the last-seen player position. The-// nav layer plans the A* path and moves the body — the action only sets intent.registerAction('chase', ctx => {if (!ctx.has(Perception)) return;const per = ctx.get(Perception);if (per.visible) setNavDestination(ctx.world, ctx.entity, { x: per.targetX, y: per.targetY });});+registerAction('patrol', () => { /* hold position until the player is seen */ });/** Install an open navigation grid over the arena on startup. */export const setupNavGridSystem = defineSystem(