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(examples): a 3D AI example, and the last thing sensing could not act on
esengine65b4f8e62548
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 16f7e418d..e60ba305e 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -170,6 +170,18 @@ published separately; it ships inside the editor.than the box around them. Nothing waits for the solver any more: the geometry isin the scene from the first frame.+- **A 3D AI example, and the last thing sensing could not act on.** `Perception`+ has reported where a target is on all three axes since sensing became+ three-dimensional, and `targetZ` / `dirZ` had no reader anywhere: the chase a+ game writes is what reads them, and the one example that wrote one was a flat+ scene. `examples/enemy-ai-3d` is the other one — hunters that see the player+ through a doorway, route round the wall between them, climb a terrace by a link+ the ground does not provide, give way to each other on the way, and turn to look+ at what they saw. Every navigation part in one project, which is also what the+ new editor check `enemy-ai-3d` opens and watches: sabotaged by dropping the third+ axis from the chase, the hunters walk to the spot 420 units short of the player,+ which is exactly what the declared gap said would happen.+- **Two characters no longer walk through each other.** A `CharacterVirtual` isswept against the WORLD, and the world does not contain characters — so nothingwas ever checking one against another, and two of them met by occupying the samediff --git a/desktop b/desktopindex a90cf390c..cc28fd94f 160000--- a/desktop+++ b/desktop@@ -1 +1 @@-Subproject commit a90cf390c9cfaee8776997d3a913f628f68478b4+Subproject commit cc28fd94ff07706b4affbdee2a8fd8bd0a5215f0diff --git a/examples/enemy-ai-3d/README.md b/examples/enemy-ai-3d/README.mdnew file mode 100644index 000000000..b0af05930--- /dev/null+++ b/examples/enemy-ai-3d/README.md@@ -0,0 +1,21 @@+# Enemy AI 3D++Hunters that sense the player in three dimensions and walk a navigation mesh baked+out of the scene's own collision geometry.++- **WASD / arrows** — walk.+- **G** — shut the doorway and open it again.++What the scene authors, and what each part is there to show:++| In the scene | What it demonstrates |+|---|---|+| `NavVolume` | The box the mesh is baked over. Nothing in code bakes anything. |+| `NavLink` | A way onto the terrace that the ground does not provide. |+| `NavObstacle` | A gate across the doorway: shut it and the navigable world is rebuilt while the game runs. |+| `Perceiver` on each hunter | Sight in three dimensions, blocked by the walls between them. |+| `NavAgent` with a `radius` | Hunters that give way to each other instead of arriving as one body. |+| `CharacterController3D` | Agents that are steered rather than moved, so they collide and fall. |++The project ships no art. Both debug overlays are turned on at startup, so what+the solver built and what the bake found are the whole picture.diff --git a/examples/enemy-ai-3d/assets/scenes/main.esscene b/examples/enemy-ai-3d/assets/scenes/main.esscenenew file mode 100644index 000000000..85f9c0b4e--- /dev/null+++ b/examples/enemy-ai-3d/assets/scenes/main.esscene@@ -0,0 +1,454 @@+{+ "version": 1,+ "name": "Main",+ "entities": [+ {+ "id": 0,+ "name": "Camera",+ "parent": null,+ "children": [],+ "visible": true,+ "components": [+ {+ "type": "Transform",+ "data": {+ "position": {+ "x": 0,+ "y": 700,+ "z": 1000+ }+ }+ },+ {+ "type": "Camera",+ "data": {+ "projectionType": 0,+ "fov": 55,+ "nearPlane": 10,+ "farPlane": 6000,+ "isActive": true,+ "priority": 0+ }+ }+ ]+ },+ {+ "id": 1,+ "name": "Ground",+ "parent": null,+ "children": [],+ "visible": true,+ "components": [+ {+ "type": "Transform",+ "data": {+ "position": {+ "x": 0,+ "y": -20,+ "z": 0+ }+ }+ },+ {+ "type": "RigidBody3D",+ "data": {+ "bodyType": 0+ }+ },+ {+ "type": "BoxCollider3D",+ "data": {+ "halfExtents": {+ "x": 900,+ "y": 20,+ "z": 700+ }+ }+ }+ ]+ },+ {+ "id": 2,+ "name": "Wall West",+ "parent": null,+ "children": [],+ "visible": true,+ "components": [+ {+ "type": "Transform",+ "data": {+ "position": {+ "x": -525,+ "y": 100,+ "z": -100+ }+ }+ },+ {+ "type": "RigidBody3D",+ "data": {+ "bodyType": 0+ }+ },+ {+ "type": "BoxCollider3D",+ "data": {+ "halfExtents": {+ "x": 375,+ "y": 100,+ "z": 20+ }+ }+ }+ ]+ },+ {+ "id": 3,+ "name": "Wall East",+ "parent": null,+ "children": [],+ "visible": true,+ "components": [+ {+ "type": "Transform",+ "data": {+ "position": {+ "x": 525,+ "y": 100,+ "z": -100+ }+ }+ },+ {+ "type": "RigidBody3D",+ "data": {+ "bodyType": 0+ }+ },+ {+ "type": "BoxCollider3D",+ "data": {+ "halfExtents": {+ "x": 375,+ "y": 100,+ "z": 20+ }+ }+ }+ ]+ },+ {+ "id": 4,+ "name": "Terrace",+ "parent": null,+ "children": [],+ "visible": true,+ "components": [+ {+ "type": "Transform",+ "data": {+ "position": {+ "x": 660,+ "y": 130,+ "z": -400+ }+ }+ },+ {+ "type": "RigidBody3D",+ "data": {+ "bodyType": 0+ }+ },+ {+ "type": "BoxCollider3D",+ "data": {+ "halfExtents": {+ "x": 320,+ "y": 30,+ "z": 300+ }+ }+ }+ ]+ },+ {+ "id": 5,+ "name": "Terrace Steps",+ "parent": null,+ "children": [],+ "visible": true,+ "components": [+ {+ "type": "Transform",+ "data": {+ "position": {+ "x": 200,+ "y": 80,+ "z": -400+ }+ }+ },+ {+ "type": "NavLink",+ "data": {+ "start": {+ "x": 0,+ "y": -80,+ "z": 0+ },+ "end": {+ "x": 460,+ "y": 80,+ "z": 0+ },+ "bidirectional": true,+ "radius": 90,+ "enabled": true+ }+ }+ ]+ },+ {+ "id": 6,+ "name": "Gate",+ "parent": null,+ "children": [],+ "visible": true,+ "components": [+ {+ "type": "Transform",+ "data": {+ "position": {+ "x": 0,+ "y": 100,+ "z": -100+ }+ }+ },+ {+ "type": "NavObstacle",+ "data": {+ "halfExtents": {+ "x": 150,+ "y": 100,+ "z": 30+ },+ "enabled": false+ }+ }+ ]+ },+ {+ "id": 7,+ "name": "Nav Volume",+ "parent": null,+ "children": [],+ "visible": true,+ "components": [+ {+ "type": "Transform",+ "data": {+ "position": {+ "x": 0,+ "y": 300,+ "z": 0+ }+ }+ },+ {+ "type": "NavVolume",+ "data": {+ "halfExtents": {+ "x": 1000,+ "y": 500,+ "z": 800+ },+ "cellSize": 40,+ "cellHeight": 12,+ "maxSlopeDegrees": 45,+ "agentHeight": 160,+ "agentRadius": 45,+ "stepHeight": 45,+ "layers": 0+ }+ }+ ]+ },+ {+ "id": 8,+ "name": "Player",+ "parent": null,+ "children": [],+ "visible": true,+ "components": [+ {+ "type": "Transform",+ "data": {+ "position": {+ "x": 0,+ "y": 80,+ "z": 420+ }+ }+ },+ {+ "type": "CharacterController3D",+ "data": {+ "radius": 35,+ "halfHeight": 40,+ "maxSlope": 0.87,+ "stepHeight": 45,+ "snapDown": 50,+ "pushForce": 6000+ }+ },+ {+ "type": "PerceptionTarget",+ "data": {}+ }+ ]+ },+ {+ "id": 9,+ "name": "Hunter A",+ "parent": null,+ "children": [],+ "visible": true,+ "components": [+ {+ "type": "Transform",+ "data": {+ "position": {+ "x": -600,+ "y": 80,+ "z": -400+ }+ }+ },+ {+ "type": "CharacterController3D",+ "data": {+ "radius": 35,+ "halfHeight": 40,+ "maxSlope": 0.87,+ "stepHeight": 45,+ "snapDown": 50+ }+ },+ {+ "type": "NavAgent",+ "data": {+ "speed": 260,+ "radius": 45,+ "arriveRadius": 110,+ "repathInterval": 0.4+ }+ },+ {+ "type": "Perceiver",+ "data": {+ "range": 1200,+ "fovDegrees": 360,+ "facingAxis": "-z",+ "losLayers": 0+ }+ }+ ]+ },+ {+ "id": 10,+ "name": "Hunter B",+ "parent": null,+ "children": [],+ "visible": true,+ "components": [+ {+ "type": "Transform",+ "data": {+ "position": {+ "x": -200,+ "y": 80,+ "z": -400+ }+ }+ },+ {+ "type": "CharacterController3D",+ "data": {+ "radius": 35,+ "halfHeight": 40,+ "maxSlope": 0.87,+ "stepHeight": 45,+ "snapDown": 50+ }+ },+ {+ "type": "NavAgent",+ "data": {+ "speed": 260,+ "radius": 45,+ "arriveRadius": 110,+ "repathInterval": 0.4+ }+ },+ {+ "type": "Perceiver",+ "data": {+ "range": 1200,+ "fovDegrees": 360,+ "facingAxis": "-z",+ "losLayers": 0+ }+ }+ ]+ },+ {+ "id": 11,+ "name": "Hunter C",+ "parent": null,+ "children": [],+ "visible": true,+ "components": [+ {+ "type": "Transform",+ "data": {+ "position": {+ "x": 200,+ "y": 80,+ "z": -400+ }+ }+ },+ {+ "type": "CharacterController3D",+ "data": {+ "radius": 35,+ "halfHeight": 40,+ "maxSlope": 0.87,+ "stepHeight": 45,+ "snapDown": 50+ }+ },+ {+ "type": "NavAgent",+ "data": {+ "speed": 260,+ "radius": 45,+ "arriveRadius": 110,+ "repathInterval": 0.4+ }+ },+ {+ "type": "Perceiver",+ "data": {+ "range": 1200,+ "fovDegrees": 360,+ "facingAxis": "-z",+ "losLayers": 0+ }+ }+ ]+ }+ ]+}diff --git a/examples/enemy-ai-3d/assets/scenes/main.esscene.meta b/examples/enemy-ai-3d/assets/scenes/main.esscene.metanew file mode 100644index 000000000..e84d523c4--- /dev/null+++ b/examples/enemy-ai-3d/assets/scenes/main.esscene.meta@@ -0,0 +1,8 @@+{+ "uuid": "edfc5c25-f864-4331-8306-efababa77f07",+ "version": "2.0",+ "type": "scene",+ "importer": {+ "autoMigrate": true+ }+}diff --git a/examples/enemy-ai-3d/project.esproject b/examples/enemy-ai-3d/project.esprojectnew file mode 100644index 000000000..6217b1c16--- /dev/null+++ b/examples/enemy-ai-3d/project.esproject@@ -0,0 +1,13 @@+{+ "formatVersion": "1",+ "name": "Enemy AI 3D",+ "description": "Hunters sense the player through a doorway, route round the wall between them, and climb a terrace by a link the ground does not provide.",+ "tag": "AI",+ "version": "0.1.0",+ "defaultScene": "assets/scenes/main.esscene",+ "designResolution": {+ "width": 960,+ "height": 640+ },+ "spineVersion": "none"+}diff --git a/examples/enemy-ai-3d/src/enemy.ts b/examples/enemy-ai-3d/src/enemy.tsnew file mode 100644index 000000000..75074f62e--- /dev/null+++ b/examples/enemy-ai-3d/src/enemy.ts@@ -0,0 +1,60 @@+import {+ defineSystem, Query, Mut, GetWorld, Transform, NavAgent, Perception,+ setNavDestination, stopNavAgent,+} from 'esengine';++/**+ * Chase what was seen, in three dimensions.+ *+ * `Perception` reports where the target IS, on all three axes — a hunter on the+ * ground and a player on the terrace are not in the same place, and a chase that+ * dropped the third axis would send it to the spot below.+ */+export const chaseSystem = defineSystem(+ [Query(Perception, NavAgent), GetWorld()],+ (hunters, world) => {+ for (const [entity, sight] of hunters) {+ if (sight.visible) {+ setNavDestination(world, entity, {+ x: sight.targetX, y: sight.targetY, z: sight.targetZ,+ });+ } else {+ stopNavAgent(world, entity);+ }+ }+ },+ { name: 'ChaseSystem' },+);++/** How fast a hunter turns to look at what it sees, in turns per second. */+const TURN_RATE = 6;++/**+ * Look at the target while it is in sight. The direction is `Perception`'s, not+ * the route's: a hunter coming round a corner is already facing the player it+ * cannot walk straight at, which is the whole reason the direction is reported.+ */+export const faceTargetSystem = defineSystem(+ [Query(Mut(Transform), Perception)],+ (hunters) => {+ for (const [, transform, sight] of hunters) {+ if (!sight.visible) continue;+ // A model's forward is -Z, so the yaw that points at (dirX, dirZ) is+ // measured from there. Only the yaw: a hunter does not lean.+ const yaw = Math.atan2(sight.dirX, -sight.dirZ);+ const half = yaw / 2;+ const rotation = transform.rotation;+ const wantY = Math.sin(half);+ const wantW = Math.cos(half);+ const t = Math.min(1, TURN_RATE / 60);+ rotation.x = 0;+ rotation.z = 0;+ rotation.y += (wantY - rotation.y) * t;+ rotation.w += (wantW - rotation.w) * t;+ const length = Math.hypot(rotation.y, rotation.w) || 1;+ rotation.y /= length;+ rotation.w /= length;+ }+ },+ { name: 'FaceTargetSystem' },+);diff --git a/examples/enemy-ai-3d/src/main.ts b/examples/enemy-ai-3d/src/main.tsnew file mode 100644index 000000000..7a13ce251--- /dev/null+++ b/examples/enemy-ai-3d/src/main.ts@@ -0,0 +1,13 @@+import { addSystemToSchedule, Schedule } from 'esengine';+import { chaseSystem, faceTargetSystem } from './enemy';+import { cameraFollowSystem, controlSystem, gateSystem, showWorldSystem } from './player';++// Only project systems are registered here. Perception, the navmesh bake and the+// agent follow are the engine's own AI plugins — the scene authors a NavVolume, a+// NavLink and a NavObstacle, and nothing in code has to bake or join anything.+addSystemToSchedule(Schedule.Startup, showWorldSystem);+addSystemToSchedule(Schedule.Update, controlSystem);+addSystemToSchedule(Schedule.Update, gateSystem);+addSystemToSchedule(Schedule.Update, chaseSystem);+addSystemToSchedule(Schedule.Update, faceTargetSystem);+addSystemToSchedule(Schedule.Update, cameraFollowSystem);diff --git a/examples/enemy-ai-3d/src/player.ts b/examples/enemy-ai-3d/src/player.tsnew file mode 100644index 000000000..20cc6e2cf--- /dev/null+++ b/examples/enemy-ai-3d/src/player.ts@@ -0,0 +1,77 @@+import {+ defineSystem, Query, Mut, Res, ResMut, Input, Time, Transform, Camera,+ CharacterController3D, NavAgent, NavDebugDraw, Physics3DDebugDraw, NavObstacle,+} from 'esengine';++const WALK_SPEED = 340;++/**+ * Drive the player from the keyboard. Only the horizontal axes are written — the+ * vertical one is the world's, which makes walking off the terrace a fall. The+ * player is the character with no NavAgent; everything else on two legs here is+ * being driven by one.+ */+export const controlSystem = defineSystem(+ [Query(Mut(CharacterController3D)).without(NavAgent), Res(Input)],+ (players, input) => {+ for (const [, character] of players) {+ let dx = 0;+ let dz = 0;+ if (input.isKeyDown('KeyW') || input.isKeyDown('ArrowUp')) dz -= 1;+ if (input.isKeyDown('KeyS') || input.isKeyDown('ArrowDown')) dz += 1;+ if (input.isKeyDown('KeyA') || input.isKeyDown('ArrowLeft')) dx -= 1;+ if (input.isKeyDown('KeyD') || input.isKeyDown('ArrowRight')) dx += 1;+ const length = Math.hypot(dx, dz);+ character.velocity.x = length > 0 ? (dx / length) * WALK_SPEED : 0;+ character.velocity.z = length > 0 ? (dz / length) * WALK_SPEED : 0;+ character.velocity.y = 0;+ }+ },+ { name: 'ControlSystem' },+);++/** Fraction of the remaining distance the eye closes per second. */+const CATCH_UP = 4;+const EYE = { x: 0, y: 620, z: 900 };++export const cameraFollowSystem = defineSystem(+ [Query(Mut(Transform), Camera), Query(Transform, CharacterController3D).without(NavAgent), Res(Time)],+ (cameras, players, time) => {+ let target: { x: number; y: number; z: number } | null = null;+ for (const [, transform] of players) {+ target = transform.position;+ break;+ }+ if (!target) return;+ const t = Math.min(1, CATCH_UP * time.delta);+ for (const [, transform] of cameras) {+ const p = transform.position;+ p.x += (target.x + EYE.x - p.x) * t;+ p.y += (target.y + EYE.y - p.y) * t;+ p.z += (target.z + EYE.z - p.z) * t;+ }+ },+ { name: 'CameraFollowSystem' },+);++/** The overlays ARE the scenery: this project ships no art, so what the solver+ * built and what the bake found are the whole picture. */+export const showWorldSystem = defineSystem(+ [ResMut(Physics3DDebugDraw), ResMut(NavDebugDraw)],+ (shapes, nav) => {+ shapes.modify((config) => { config.enabled = true; });+ nav.modify((config) => { config.enabled = true; });+ },+ { name: 'ShowWorldSystem' },+);++/** G shuts the doorway and opens it again — the navigable world is rebuilt for it+ * while the game runs, and the hunters go the long way round instead. */+export const gateSystem = defineSystem(+ [Query(Mut(NavObstacle)), Res(Input)],+ (gates, input) => {+ if (!input.isKeyPressed('KeyG')) return;+ for (const [, gate] of gates) gate.enabled = !gate.enabled;+ },+ { name: 'GateSystem' },+);diff --git a/examples/enemy-ai-3d/tsconfig.json b/examples/enemy-ai-3d/tsconfig.jsonnew file mode 100644index 000000000..1f11ff310--- /dev/null+++ b/examples/enemy-ai-3d/tsconfig.json@@ -0,0 +1,20 @@+{+ "compilerOptions": {+ "target": "ES2020",+ "module": "ESNext",+ "moduleResolution": "bundler",+ "strict": true,+ "esModuleInterop": true,+ "skipLibCheck": true,+ "forceConsistentCasingInFileNames": true,+ "declaration": false,+ "outDir": "./dist",+ "rootDir": ".",+ "paths": {+ "esengine": ["./.esengine/sdk/index.d.ts"],+ "esengine/wasm": ["./.esengine/sdk/wasm.d.ts"]+ }+ },+ "include": ["src/**/*"],+ "exclude": ["node_modules"]+}diff --git a/tools/check-component-fields.mjs b/tools/check-component-fields.mjsindex 749de492c..507222413 100644--- a/tools/check-component-fields.mjs+++ b/tools/check-component-fields.mjs@@ -40,10 +40,6 @@ const DECLARED_GAPS = {+ ' own built-in shader and never consult the table','SpineAnimation.material': 'see ParticleEmitter.material','DragonBonesAnimation.material': 'see ParticleEmitter.material',- 'Perception.targetZ': 'sensing is three-dimensional and the navigation under it now'- + ' is too, but the chase itself is a game\'s to write and the one example that'- + ' writes it is a flat scene. Closed by an example that chases through a NavVolume',- 'Perception.dirZ': 'see Perception.targetZ',};const components = JSON.parse(