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
docs: rewrite the Animation guide to the complete-reference template
yhh621ed0f710ee
diff --git a/docs/astro/src/content/docs/guides/animation.mdx b/docs/astro/src/content/docs/guides/animation.mdxindex 597aaa028..57db53e44 100644--- a/docs/astro/src/content/docs/guides/animation.mdx+++ b/docs/astro/src/content/docs/guides/animation.mdx@@ -1,19 +1,23 @@---title: Animation-description: Sprite sheets, tweens (with sequencing), and animator state machines.+description: The complete guide to Estella's three animation systems — sprite-sheet playback, tweens with sequencing, and parameter-driven animator state machines.---import { Aside } from '@astrojs/starlight/components';-Estella has three animation systems, each for a different job: **sprite-animation** for frame-by-frame art, **tweens** for interpolating properties, and-the **animator state machine** for character animation driven by parameters.+Estella has three animation systems, each for a different job:++- **Sprite animation** — frame-by-frame sprite-sheet playback (`SpriteAnimator`).+- **Tweens** — interpolate any property over time with easing (`Tween`).+- **Animator state machine** — character animation driven by parameters (`Animator`).++They compose: a state machine can drive sprite clips, and tweens can juice a UI+element while a sprite loops.## Sprite animation-The `SpriteAnimator` component plays sprite-sheet clips. Its fields are `clip`-(the current clip name), `speed`, `playing`, `loop`, plus the read-only-`currentFrame` and `frameTimer`. Switch clips by writing the component:+The `SpriteAnimator` component plays named sprite-sheet clips (authored as+`anim-clip` assets). Switch clips by writing the component through a `Mut` query:```typescriptimport { defineSystem, Query, Mut, SpriteAnimator } from 'esengine';@@ -27,27 +31,44 @@ const setClip = defineSystem([Query(Mut(SpriteAnimator))], (q) => {});```-For finer control, the `SpriteAnimation` resource can jump to a specific frame or-a named label, and register clips at runtime:+| Property | Type | Default | Description |+|---|---|---|---|+| `clip` | asset | `''` | Current clip (an `anim-clip` asset name). |+| `speed` | number | `1` | Playback speed multiplier. |+| `playing` | boolean | `true` | Whether playback is advancing. |+| `loop` | boolean | `true` | Loop the clip vs. hold the last frame. |+| `enabled` | boolean | `true` | Disable to freeze the animator. |+| `currentFrame` | number | `0` | Read-only: the frame index being shown. |+| `frameTimer` | number | `0` | Read-only: time accumulated on the current frame. |++For finer control, the `SpriteAnimation` resource jumps to a specific frame or a+named label and registers clips at runtime:```typescriptimport { defineSystem, Query, Mut, Res, SpriteAnimator, SpriteAnimation } from 'esengine';const jump = defineSystem([Query(Mut(SpriteAnimator)), Res(SpriteAnimation)], (q, sprites) => {for (const [entity, animator] of q) {- sprites.gotoFrame(animator, 0, true); // frame index, and play- sprites.gotoLabel(animator, 'attack', true);+ sprites.gotoFrame(animator, 0, true); // frame index, and play+ sprites.gotoLabel(animator, 'attack', true); // named label, and play}});```+| Method | Description |+|---|---|+| `gotoFrame(animator, index, andPlay?)` | Jump to a frame index (optionally start playing). |+| `gotoLabel(animator, label, andPlay?)` | Jump to a named label in the clip. |+| `registerClip(clip)` | Register a `SpriteAnimClip` at runtime. |+| `getClip(name)` | Look up a registered clip. |+Clips can carry **frame events** (`SpriteAnimEvent`) that fire when playbackreaches a frame — use them for footstep sounds or hit frames.## TweensThe `Tween` resource interpolates a property from one value to another over a-duration:+duration. `to(entity, target, from, to, duration, options?)` returns a handle:```typescriptimport { defineSystem, Res, Tween, TweenTarget, EasingType, LoopMode } from 'esengine';@@ -62,57 +83,110 @@ const animate = defineSystem([Res(Tween)], (tween) => {});```-**Targets** (`TweenTarget`): `PositionX` / `PositionY` / `PositionZ`, `ScaleX` /-`ScaleY`, `RotationZ`, `ColorR` / `ColorG` / `ColorB` / `ColorA`, `SizeX` /-`SizeY`, and `CameraOrthoSize`. `EasingType` has the usual curves, and a handle-can use a custom cubic-bezier via `.bezier(p1x, p1y, p2x, p2y)`.+### Targets++`TweenTarget` selects the property to drive:++| Group | Values |+|---|---|+| Position | `PositionX`, `PositionY`, `PositionZ` |+| Scale | `ScaleX`, `ScaleY` |+| Rotation | `RotationZ` |+| Color | `ColorR`, `ColorG`, `ColorB`, `ColorA` |+| Size | `SizeX`, `SizeY` |+| Camera | `CameraOrthoSize` |++### Options++| Option | Type | Default | Description |+|---|---|---|---|+| `easing` | `EasingType` | `Linear` | Easing curve (see below). |+| `delay` | number | `0` | Seconds to wait before starting. |+| `loop` | `LoopMode` | `None` | `None` / `Restart` / `PingPong`. |+| `loopCount` | number | `0` | Number of loops; `-1` = forever. |-### Sequencing and control+`EasingType` covers `Linear`, the quad/cubic/back/elastic families+(`EaseIn*` / `EaseOut*` / `EaseInOut*`), `EaseOutBounce`, `Step`, and `CubicBezier`+— use a custom bezier with the handle's `.bezier(p1x, p1y, p2x, p2y)`.-Compose tweens, and cancel or pause them:+### Sequencing & control++The returned `TweenHandle` chains and controls playback:```typescript-// Chain: run one after another.-tween.to(e, TweenTarget.ScaleX, 1, 1.4, 0.15).then(- tween.to(e, TweenTarget.ScaleX, 1.4, 1, 0.15)-);+// Chain: run one after another (a squash-and-stretch pop).+tween.to(e, TweenTarget.ScaleX, 1, 1.4, 0.15)+ .then(tween.to(e, TweenTarget.ScaleX, 1.4, 1, 0.15))+ .bezier(0.2, 0, 0, 1);tween.parallel([...]); // run several at once (a group)-tween.sequence([...]); // run in order (factories)+tween.sequence([...]); // run tween factories in ordertween.delay(0.5); // a timed gaptween.cancel(handle); // stop onetween.cancelAll(entity); // stop everything on an entity```-`tween.value(...)` tweens a plain number (not a component target) — for driving-custom logic through a callback.+| Method | Description |+|---|---|+| `to(entity, target, from, to, duration, opts?)` | Tween a component property; returns a `TweenHandle`. |+| `value(from, to, duration, cb, opts?)` | Tween a plain number, delivered to `cb` each frame. |+| `parallel(tweens)` | Run several tweens together as a group. |+| `sequence(factories)` | Run tween factories one after another. |+| `delay(seconds)` | An empty timed gap (for use in sequences). |+| `cancel(handle)` / `cancelAll(entity)` | Stop one tween, or all on an entity. |++`TweenHandle` has `.then(next)`, `.bezier(...)`, `.pause()`, `.resume()`, and+`.cancel()`.## Animator state machine-For character animation, an animator controller (authored in the editor) holds-states, transitions, and 1D blend trees driven by **parameters**. At runtime you-set the parameters through `Res(AnimatorController)`; the controller resolves the-current state, clip, and blend:+For character animation, an **animator controller** holds states, transitions, and+1D blend trees driven by named **parameters**. Author it in the editor (or+register one in code), attach an `Animator` component naming the controller, then+set parameters each frame through `Res(AnimatorController)` — the controller+resolves the current state, clip, and blend:```typescript-import { defineSystem, Query, Res, SpriteAnimator, AnimatorController } from 'esengine';+import { defineSystem, Query, Res, Animator, AnimatorController } from 'esengine';-const drive = defineSystem([Query(SpriteAnimator), Res(AnimatorController)], (q, animator) => {+const drive = defineSystem([Query(Animator), Res(AnimatorController)], (q, anim) => {for (const [entity] of q) {- animator.setFloat(entity, 'speed', 4.2); // drives a 1D blend / transition- animator.setBool(entity, 'grounded', true);- animator.setTrigger(entity, 'jump'); // one-shot; resetTrigger to clear- const speed = animator.getFloat(entity, 'speed');+ anim.setFloat(entity, 'speed', 4.2); // drives a 1D blend / transition+ anim.setBool(entity, 'grounded', true);+ anim.setTrigger(entity, 'jump'); // one-shot; resetTrigger to clear+ const speed = anim.getFloat(entity, 'speed');}});```-The controller can drive both sprite and Spine animations, so one state machine-handles a whole character.+| `Animator` field | Type | Default | Description |+|---|---|---|---|+| `controller` | string | `''` | Registered controller name. |+| `currentState` | string | `''` | Active state (seeded from the controller's initial state). |+| `enabled` | boolean | `true` | Disable to freeze the state machine. |++| `AnimatorController` method | Description |+|---|---|+| `setFloat(entity, name, value)` | Set a float parameter (blends / transition conditions). |+| `setBool(entity, name, value)` | Set a bool parameter. |+| `setTrigger(entity, name)` | Fire a one-shot trigger. |+| `resetTrigger(entity, name)` | Clear a trigger before it's consumed. |+| `getFloat(entity, name)` / `getBool(entity, name)` | Read a parameter back. |++The controller can drive both sprite **and** [Spine](/docs/guides/spine/)+animations, so one state machine handles a whole character.++## Choosing the right tool<Aside type="tip">Reach for **tweens** for one-off UI/juice (a button pop, a fade), **spriteanimation** for looping art, and the **state machine** when transitions betweenmany clips depend on gameplay parameters.</Aside>++## See also++- [Spine Animation](/docs/guides/spine/) — skeletal animation, also driveable by the state machine.+- [Timeline](/docs/guides/timeline/) — keyframed cutscenes and sequenced events.+- [Camera](/docs/guides/camera/) — tween `CameraOrthoSize` for smooth zooms.diff --git a/docs/astro/src/content/docs/zh-cn/guides/animation.mdx b/docs/astro/src/content/docs/zh-cn/guides/animation.mdxindex 30028f23a..1c69974d2 100644--- a/docs/astro/src/content/docs/zh-cn/guides/animation.mdx+++ b/docs/astro/src/content/docs/zh-cn/guides/animation.mdx@@ -1,17 +1,22 @@---title: 动画-description: 精灵表、补间(含序列)与动画状态机。+description: Estella 三套动画系统的完整指南——精灵表播放、带序列的补间(tween),以及参数驱动的动画状态机。---import { Aside } from '@astrojs/starlight/components';-Estella 有三套动画系统,各司其职:**精灵动画**做逐帧美术,**补间**做属性插值,-**动画状态机**做由参数驱动的角色动画。+Estella 有三套动画系统,各司其职:++- **精灵动画** —— 逐帧的精灵表播放(`SpriteAnimator`)。+- **补间(Tween)** —— 用缓动随时间插值任意属性(`Tween`)。+- **动画状态机** —— 参数驱动的角色动画(`Animator`)。++它们可以组合:状态机可以驱动精灵片段,补间可以在精灵循环的同时给 UI 元素加"手感"。## 精灵动画-`SpriteAnimator` 组件播放精灵表片段。它的字段有 `clip`(当前片段名)、`speed`、-`playing`、`loop`,以及只读的 `currentFrame` 和 `frameTimer`。写这个组件即可切换片段:+`SpriteAnimator` 组件播放命名的精灵表片段(作为 `anim-clip` 资源编辑)。通过 `Mut` 查询+写组件来切换片段:```typescriptimport { defineSystem, Query, Mut, SpriteAnimator } from 'esengine';@@ -25,24 +30,42 @@ const setClip = defineSystem([Query(Mut(SpriteAnimator))], (q) => {});```-想更精细,`SpriteAnimation` 资源可以跳到指定帧或命名 label,并在运行时注册片段:+| 属性 | 类型 | 默认 | 说明 |+|---|---|---|---|+| `clip` | asset | `''` | 当前片段(一个 `anim-clip` 资源名)。 |+| `speed` | number | `1` | 播放速度倍率。 |+| `playing` | boolean | `true` | 播放是否在推进。 |+| `loop` | boolean | `true` | 循环片段 vs 停在最后一帧。 |+| `enabled` | boolean | `true` | 关掉即冻结动画器。 |+| `currentFrame` | number | `0` | 只读:正在显示的帧索引。 |+| `frameTimer` | number | `0` | 只读:当前帧已累积的时间。 |++要更精细地控制,`SpriteAnimation` 资源可跳到指定帧或命名标签,并在运行时注册片段:```typescriptimport { defineSystem, Query, Mut, Res, SpriteAnimator, SpriteAnimation } from 'esengine';const jump = defineSystem([Query(Mut(SpriteAnimator)), Res(SpriteAnimation)], (q, sprites) => {for (const [entity, animator] of q) {- sprites.gotoFrame(animator, 0, true); // 帧索引,并播放- sprites.gotoLabel(animator, 'attack', true);+ sprites.gotoFrame(animator, 0, true); // frame index, and play+ sprites.gotoLabel(animator, 'attack', true); // named label, and play}});```+| 方法 | 说明 |+|---|---|+| `gotoFrame(animator, index, andPlay?)` | 跳到某帧索引(可选是否开始播放)。 |+| `gotoLabel(animator, label, andPlay?)` | 跳到片段里的命名标签。 |+| `registerClip(clip)` | 在运行时注册一个 `SpriteAnimClip`。 |+| `getClip(name)` | 查找一个已注册的片段。 |+片段可以携带**帧事件**(`SpriteAnimEvent`),播放到某帧时触发——用于脚步声或命中帧。## 补间(Tween)-`Tween` 资源在一段时长内把某个属性从起始值插值到结束值:+`Tween` 资源在一段时长内把属性从一个值插值到另一个值。`to(entity, target, from, to,+duration, options?)` 返回一个句柄:```typescriptimport { defineSystem, Res, Tween, TweenTarget, EasingType, LoopMode } from 'esengine';@@ -52,57 +75,111 @@ const animate = defineSystem([Res(Tween)], (tween) => {easing: EasingType.EaseInOutQuad,delay: 0.2,loop: LoopMode.PingPong,- loopCount: 3, // -1 = 永远+ loopCount: 3, // -1 = forever});});```-**目标**(`TweenTarget`):`PositionX` / `PositionY` / `PositionZ`、`ScaleX` /-`ScaleY`、`RotationZ`、`ColorR` / `ColorG` / `ColorB` / `ColorA`、`SizeX` /-`SizeY`,以及 `CameraOrthoSize`。`EasingType` 提供常见曲线,句柄还可以用-`.bezier(p1x, p1y, p2x, p2y)` 自定义三次贝塞尔。+### 目标++`TweenTarget` 选择要驱动的属性:++| 分组 | 取值 |+|---|---|+| 位置 | `PositionX`、`PositionY`、`PositionZ` |+| 缩放 | `ScaleX`、`ScaleY` |+| 旋转 | `RotationZ` |+| 颜色 | `ColorR`、`ColorG`、`ColorB`、`ColorA` |+| 尺寸 | `SizeX`、`SizeY` |+| 相机 | `CameraOrthoSize` |++### 选项++| 选项 | 类型 | 默认 | 说明 |+|---|---|---|---|+| `easing` | `EasingType` | `Linear` | 缓动曲线(见下)。 |+| `delay` | number | `0` | 开始前等待的秒数。 |+| `loop` | `LoopMode` | `None` | `None` / `Restart` / `PingPong`。 |+| `loopCount` | number | `0` | 循环次数;`-1` = 永远。 |++`EasingType` 覆盖 `Linear`、quad/cubic/back/elastic 系列(`EaseIn*` / `EaseOut*` /+`EaseInOut*`)、`EaseOutBounce`、`Step` 和 `CubicBezier`——自定义贝塞尔用句柄的+`.bezier(p1x, p1y, p2x, p2y)`。### 序列与控制-组合补间,并取消或暂停:+返回的 `TweenHandle` 可链式串接并控制播放:```typescript-// 链式:一个接一个。-tween.to(e, TweenTarget.ScaleX, 1, 1.4, 0.15).then(- tween.to(e, TweenTarget.ScaleX, 1.4, 1, 0.15)-);+// Chain: run one after another (a squash-and-stretch pop).+tween.to(e, TweenTarget.ScaleX, 1, 1.4, 0.15)+ .then(tween.to(e, TweenTarget.ScaleX, 1.4, 1, 0.15))+ .bezier(0.2, 0, 0, 1);-tween.parallel([...]); // 同时运行多个(一个组)-tween.sequence([...]); // 按顺序运行(工厂函数)-tween.delay(0.5); // 一段定时间隔+tween.parallel([...]); // run several at once (a group)+tween.sequence([...]); // run tween factories in order+tween.delay(0.5); // a timed gap-tween.cancel(handle); // 停止一个-tween.cancelAll(entity); // 停止某实体上的全部+tween.cancel(handle); // stop one+tween.cancelAll(entity); // stop everything on an entity```-`tween.value(...)` 补间一个普通数字(而非组件目标)——用于通过回调驱动自定义逻辑。+| 方法 | 说明 |+|---|---|+| `to(entity, target, from, to, duration, opts?)` | 补间一个组件属性;返回 `TweenHandle`。 |+| `value(from, to, duration, cb, opts?)` | 补间一个普通数值,每帧交给 `cb`。 |+| `parallel(tweens)` | 把多个补间作为一组同时运行。 |+| `sequence(factories)` | 依次运行补间工厂。 |+| `delay(seconds)` | 一段空的计时间隔(用于序列)。 |+| `cancel(handle)` / `cancelAll(entity)` | 停止一个补间,或某实体上的全部。 |++`TweenHandle` 有 `.then(next)`、`.bezier(...)`、`.pause()`、`.resume()` 和 `.cancel()`。## 动画状态机-对于角色动画,动画控制器(在编辑器里编排)持有状态、过渡和由**参数**驱动的 1D 混合树。-运行时你通过 `Res(AnimatorController)` 设置参数;控制器解析出当前状态、片段与混合:+角色动画用一个**动画控制器**:它持有状态、转移和由命名**参数**驱动的 1D 混合树。在编辑器+里编辑它(或用代码注册),给实体加一个指明控制器名的 `Animator` 组件,然后每帧通过+`Res(AnimatorController)` 设置参数——控制器会解析出当前状态、片段与混合:```typescript-import { defineSystem, Query, Res, SpriteAnimator, AnimatorController } from 'esengine';+import { defineSystem, Query, Res, Animator, AnimatorController } from 'esengine';-const drive = defineSystem([Query(SpriteAnimator), Res(AnimatorController)], (q, animator) => {+const drive = defineSystem([Query(Animator), Res(AnimatorController)], (q, anim) => {for (const [entity] of q) {- animator.setFloat(entity, 'speed', 4.2); // 驱动 1D 混合 / 过渡- animator.setBool(entity, 'grounded', true);- animator.setTrigger(entity, 'jump'); // 一次性;用 resetTrigger 清除- const speed = animator.getFloat(entity, 'speed');+ anim.setFloat(entity, 'speed', 4.2); // drives a 1D blend / transition+ anim.setBool(entity, 'grounded', true);+ anim.setTrigger(entity, 'jump'); // one-shot; resetTrigger to clear+ const speed = anim.getFloat(entity, 'speed');}});```-控制器可以同时驱动精灵动画和 Spine 动画,因此一个状态机就能管一整个角色。+| `Animator` 字段 | 类型 | 默认 | 说明 |+|---|---|---|---|+| `controller` | string | `''` | 已注册的控制器名。 |+| `currentState` | string | `''` | 活动状态(从控制器的初始状态种入)。 |+| `enabled` | boolean | `true` | 关掉即冻结状态机。 |++| `AnimatorController` 方法 | 说明 |+|---|---|+| `setFloat(entity, name, value)` | 设置 float 参数(混合 / 转移条件)。 |+| `setBool(entity, name, value)` | 设置 bool 参数。 |+| `setTrigger(entity, name)` | 触发一次性 trigger。 |+| `resetTrigger(entity, name)` | 在被消费前清除一个 trigger。 |+| `getFloat(entity, name)` / `getBool(entity, name)` | 读回一个参数。 |++控制器可以同时驱动精灵**和** [Spine](/docs/zh-cn/guides/spine/) 动画,所以一套状态机就能+管一整个角色。++## 如何选择<Aside type="tip">- 一次性的 UI/手感(按钮弹跳、淡入淡出)用**补间**;循环的美术用**精灵动画**;当多个- 片段之间的过渡取决于游戏参数时,用**状态机**。+ 一次性的 UI/手感(按钮弹跳、淡入淡出)用**补间**,循环美术用**精灵动画**,当多个片段之间的+ 转移依赖玩法参数时用**状态机**。</Aside>++## 参见++- [Spine 动画](/docs/zh-cn/guides/spine/) —— 骨骼动画,也能被状态机驱动。+- [时间轴](/docs/zh-cn/guides/timeline/) —— 关键帧过场与序列化事件。+- [相机](/docs/zh-cn/guides/camera/) —— 补间 `CameraOrthoSize` 做平滑缩放。