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.

docs: source entity from a Query consistently in guide examples

yhh3187239cd473
diff --git a/docs/astro/src/content/docs/guides/animation.mdx b/docs/astro/src/content/docs/guides/animation.mdx
index 50e6234f9..f1981bd48 100644
--- a/docs/astro/src/content/docs/guides/animation.mdx
+++ b/docs/astro/src/content/docs/guides/animation.mdx
@@ -28,13 +28,15 @@ The `Tween` resource interpolates one property from a start value to an end valu
over a duration:
```typescript
-import { defineSystem, Res, Tween, TweenTarget, EasingType, LoopMode } from 'esengine';
+import { defineSystem, Query, Res, Transform, Tween, TweenTarget, EasingType, LoopMode } from 'esengine';
-const animate = defineSystem([Res(Tween)], (tween) => {
- tween.to(entity, TweenTarget.PositionX, 0, 200, 1.0, {
- easing: EasingType.EaseInOutQuad,
- loop: LoopMode.PingPong,
- });
+const animate = defineSystem([Query(Transform), Res(Tween)], (q, tween) => {
+ for (const [entity] of q) {
+ tween.to(entity, TweenTarget.PositionX, 0, 200, 1.0, {
+ easing: EasingType.EaseInOutQuad,
+ loop: LoopMode.PingPong,
+ });
+ }
});
```
@@ -49,12 +51,14 @@ states, transitions, and 1D blend trees driven by parameters. At runtime you set
those parameters through `Res(AnimatorController)`:
```typescript
-import { defineSystem, Res, AnimatorController } from 'esengine';
+import { defineSystem, Query, Res, SpriteAnimator, AnimatorController } from 'esengine';
-const drive = defineSystem([Res(AnimatorController)], (animator) => {
- animator.setFloat(entity, 'speed', 4.2);
- animator.setBool(entity, 'grounded', true);
- animator.setTrigger(entity, 'jump');
+const drive = defineSystem([Query(SpriteAnimator), Res(AnimatorController)], (q, animator) => {
+ for (const [entity] of q) {
+ animator.setFloat(entity, 'speed', 4.2);
+ animator.setBool(entity, 'grounded', true);
+ animator.setTrigger(entity, 'jump');
+ }
});
```
diff --git a/docs/astro/src/content/docs/guides/particle.mdx b/docs/astro/src/content/docs/guides/particle.mdx
index edc70a9b3..fb4c518cb 100644
--- a/docs/astro/src/content/docs/guides/particle.mdx
+++ b/docs/astro/src/content/docs/guides/particle.mdx
@@ -30,12 +30,14 @@ Read the `Particle` resource to start, stop, or clear an emitter, and to read
its live particle count:
```typescript
-import { defineSystem, Res, Particle } from 'esengine';
-
-const control = defineSystem([Res(Particle)], (particle) => {
- particle.play(entity); // start emitting
- particle.stop(entity); // stop emitting; existing particles finish out
- particle.reset(entity); // clear all particles immediately
- const alive = particle.getAliveCount(entity);
+import { defineSystem, Query, Res, ParticleEmitter, Particle } from 'esengine';
+
+const control = defineSystem([Query(ParticleEmitter), Res(Particle)], (q, particle) => {
+ for (const [entity] of q) {
+ particle.play(entity); // start emitting
+ particle.stop(entity); // stop emitting; existing particles finish out
+ particle.reset(entity); // clear all particles immediately
+ const alive = particle.getAliveCount(entity);
+ }
});
```
diff --git a/docs/astro/src/content/docs/guides/physics.mdx b/docs/astro/src/content/docs/guides/physics.mdx
index 6a2224366..5fc22e4b6 100644
--- a/docs/astro/src/content/docs/guides/physics.mdx
+++ b/docs/astro/src/content/docs/guides/physics.mdx
@@ -76,13 +76,15 @@ const LAYER_GROUND = 0x0008;
Read the `PhysicsAPI` resource in a system to push bodies around:
```typescript
-import { defineSystem, Res } from 'esengine';
+import { defineSystem, Query, Res, RigidBody } from 'esengine';
import { PhysicsAPI } from 'esengine/physics';
-const push = defineSystem([Res(PhysicsAPI)], (physics) => {
- physics.applyImpulse(entity, { x: 0, y: 5 }); // jump
- physics.setLinearVelocity(entity, { x: 3, y: 0 });
- physics.applyTorque(entity, 2);
+const push = defineSystem([Query(RigidBody), Res(PhysicsAPI)], (q, physics) => {
+ for (const [entity] of q) { // narrow to your player component in practice
+ physics.applyImpulse(entity, { x: 0, y: 5 }); // jump
+ physics.setLinearVelocity(entity, { x: 3, y: 0 });
+ physics.applyTorque(entity, 2);
+ }
});
```
diff --git a/docs/astro/src/content/docs/guides/postprocess.mdx b/docs/astro/src/content/docs/guides/postprocess.mdx
index ae94e2e48..1cb8292bb 100644
--- a/docs/astro/src/content/docs/guides/postprocess.mdx
+++ b/docs/astro/src/content/docs/guides/postprocess.mdx
@@ -39,11 +39,13 @@ For a custom pipeline, read the `PostProcess` resource — create a stack and bi
it to a camera:
```typescript
-import { defineSystem, Res, PostProcess } from 'esengine';
+import { defineSystem, Query, Res, Camera, PostProcess } from 'esengine';
-const setup = defineSystem([Res(PostProcess)], (pp) => {
- const stack = pp.createStack();
- pp.bind(cameraEntity, stack); // pp.unbind(cameraEntity) to remove
+const setup = defineSystem([Query(Camera), Res(PostProcess)], (q, pp) => {
+ for (const [camera] of q) {
+ const stack = pp.createStack();
+ pp.bind(camera, stack); // pp.unbind(camera) to remove
+ }
});
```
diff --git a/docs/astro/src/content/docs/zh-cn/guides/animation.mdx b/docs/astro/src/content/docs/zh-cn/guides/animation.mdx
index af8f57e1d..2a05d480c 100644
--- a/docs/astro/src/content/docs/zh-cn/guides/animation.mdx
+++ b/docs/astro/src/content/docs/zh-cn/guides/animation.mdx
@@ -27,13 +27,15 @@ const setClip = defineSystem([Query(Mut(SpriteAnimator))], (q) => {
`Tween` 资源在一段时长内把某个属性从起始值插值到结束值:
```typescript
-import { defineSystem, Res, Tween, TweenTarget, EasingType, LoopMode } from 'esengine';
+import { defineSystem, Query, Res, Transform, Tween, TweenTarget, EasingType, LoopMode } from 'esengine';
-const animate = defineSystem([Res(Tween)], (tween) => {
- tween.to(entity, TweenTarget.PositionX, 0, 200, 1.0, {
- easing: EasingType.EaseInOutQuad,
- loop: LoopMode.PingPong,
- });
+const animate = defineSystem([Query(Transform), Res(Tween)], (q, tween) => {
+ for (const [entity] of q) {
+ tween.to(entity, TweenTarget.PositionX, 0, 200, 1.0, {
+ easing: EasingType.EaseInOutQuad,
+ loop: LoopMode.PingPong,
+ });
+ }
});
```
@@ -47,12 +49,14 @@ const animate = defineSystem([Res(Tween)], (tween) => {
树(blend tree)。运行时你通过 `Res(AnimatorController)` 设置这些参数:
```typescript
-import { defineSystem, Res, AnimatorController } from 'esengine';
+import { defineSystem, Query, Res, SpriteAnimator, AnimatorController } from 'esengine';
-const drive = defineSystem([Res(AnimatorController)], (animator) => {
- animator.setFloat(entity, 'speed', 4.2);
- animator.setBool(entity, 'grounded', true);
- animator.setTrigger(entity, 'jump');
+const drive = defineSystem([Query(SpriteAnimator), Res(AnimatorController)], (q, animator) => {
+ for (const [entity] of q) {
+ animator.setFloat(entity, 'speed', 4.2);
+ animator.setBool(entity, 'grounded', true);
+ animator.setTrigger(entity, 'jump');
+ }
});
```
diff --git a/docs/astro/src/content/docs/zh-cn/guides/particle.mdx b/docs/astro/src/content/docs/zh-cn/guides/particle.mdx
index 79cea93b6..e1cef52e2 100644
--- a/docs/astro/src/content/docs/zh-cn/guides/particle.mdx
+++ b/docs/astro/src/content/docs/zh-cn/guides/particle.mdx
@@ -27,12 +27,14 @@ Estella 在 CPU 上模拟粒子,并用 GPU 实例化渲染。给实体加一个
读取 `Particle` 资源来启动、停止或清空一个发射器,并读取它的存活粒子数:
```typescript
-import { defineSystem, Res, Particle } from 'esengine';
-
-const control = defineSystem([Res(Particle)], (particle) => {
- particle.play(entity); // 开始发射
- particle.stop(entity); // 停止发射;已有粒子自然消亡
- particle.reset(entity); // 立即清空所有粒子
- const alive = particle.getAliveCount(entity);
+import { defineSystem, Query, Res, ParticleEmitter, Particle } from 'esengine';
+
+const control = defineSystem([Query(ParticleEmitter), Res(Particle)], (q, particle) => {
+ for (const [entity] of q) {
+ particle.play(entity); // 开始发射
+ particle.stop(entity); // 停止发射;已有粒子自然消亡
+ particle.reset(entity); // 立即清空所有粒子
+ const alive = particle.getAliveCount(entity);
+ }
});
```
diff --git a/docs/astro/src/content/docs/zh-cn/guides/physics.mdx b/docs/astro/src/content/docs/zh-cn/guides/physics.mdx
index 86826adf9..67d40e748 100644
--- a/docs/astro/src/content/docs/zh-cn/guides/physics.mdx
+++ b/docs/astro/src/content/docs/zh-cn/guides/physics.mdx
@@ -71,13 +71,15 @@ const LAYER_GROUND = 0x0008;
在系统里读取 `PhysicsAPI` 资源来推动刚体:
```typescript
-import { defineSystem, Res } from 'esengine';
+import { defineSystem, Query, Res, RigidBody } from 'esengine';
import { PhysicsAPI } from 'esengine/physics';
-const push = defineSystem([Res(PhysicsAPI)], (physics) => {
- physics.applyImpulse(entity, { x: 0, y: 5 }); // 跳跃
- physics.setLinearVelocity(entity, { x: 3, y: 0 });
- physics.applyTorque(entity, 2);
+const push = defineSystem([Query(RigidBody), Res(PhysicsAPI)], (q, physics) => {
+ for (const [entity] of q) { // 实际中可缩小到你的玩家组件
+ physics.applyImpulse(entity, { x: 0, y: 5 }); // 跳跃
+ physics.setLinearVelocity(entity, { x: 3, y: 0 });
+ physics.applyTorque(entity, 2);
+ }
});
```
diff --git a/docs/astro/src/content/docs/zh-cn/guides/postprocess.mdx b/docs/astro/src/content/docs/zh-cn/guides/postprocess.mdx
index c0c4141d6..174cb60a8 100644
--- a/docs/astro/src/content/docs/zh-cn/guides/postprocess.mdx
+++ b/docs/astro/src/content/docs/zh-cn/guides/postprocess.mdx
@@ -35,11 +35,13 @@ Estella 通过 ping-pong 帧缓冲运行全屏后处理。加一个 `PostProcess
要自定义管线,读取 `PostProcess` 资源——创建一个 stack 并绑定到相机:
```typescript
-import { defineSystem, Res, PostProcess } from 'esengine';
+import { defineSystem, Query, Res, Camera, PostProcess } from 'esengine';
-const setup = defineSystem([Res(PostProcess)], (pp) => {
- const stack = pp.createStack();
- pp.bind(cameraEntity, stack); // pp.unbind(cameraEntity) 可移除
+const setup = defineSystem([Query(Camera), Res(PostProcess)], (q, pp) => {
+ for (const [camera] of q) {
+ const stack = pp.createStack();
+ pp.bind(camera, stack); // pp.unbind(camera) 可移除
+ }
});
```