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(gameplay): the three facts a swing is made of

esengine1de3c88c11dc
diff --git a/docs/astro/src/content/docs/gameplay/physics-3d.mdx b/docs/astro/src/content/docs/gameplay/physics-3d.mdx
index 157405c58..7f7c6a727 100644
--- a/docs/astro/src/content/docs/gameplay/physics-3d.mdx
+++ b/docs/astro/src/content/docs/gameplay/physics-3d.mdx
@@ -192,6 +192,82 @@ still walks the character across the floor. Pitch never reaches the character.
when something solid stands between. It moves nothing but itself.
</Aside>
+## Melee combat
+
+Three facts, and the whole of a combat system is keeping them apart:
+
+```text
+an animation event ≠ a hit ≠ damage
+```
+
+The clip says only that the swing reached its effective moment. What was in that
+space is the physics world's answer. What that does to anyone is gameplay's.
+Collapse any two and you get a combat system that cannot be re-aimed, re-timed
+or re-balanced without touching the other two.
+
+`MeleeAttack` sits beside an `Animator` whose attack clip declares three beats
+on a `customEvent` track — `attack-start`, `hit`, `attack-end`:
+
+```typescript
+player.add(MeleeAttack, { radius: 70, damage: 25, anchor: swordSocket });
+dummy.add(Health, { current: 100, max: 100 });
+```
+
+| Field | What it does |
+|---|---|
+| `radius` | Radius of the sphere a `hit` beat tests, world units. |
+| `damage` | What a landed blow takes off. |
+| `layers` | Physics layers the swing can reach; `0` is every layer. |
+| `anchor` | The entity whose WORLD placement the query is centred on. |
+| `attackId` | Engine-written: the live swing, `0` when none is running. |
+| `hitCount` | Engine-written: how many targets it has landed on. |
+
+<Aside type="caution">
+ `anchor` should be a socket parented to an animated joint, not the character.
+ Centre the query on the character and the reach is an offset from its feet:
+ the skeleton is drawn but never consulted, and a swing that misses on screen
+ still lands. The anchor's world placement is composed before the query, so
+ what it asks about is what the animation did.
+</Aside>
+
+A swing is an INSTANCE with its own id and its own set of who it has already
+reached, so one swing lands on one target once and the next one may land again.
+A hit window is normally several `hit` beats, and leaning on "the event only
+fires once" instead breaks the first time a clip declares two — silently, as
+double damage, which reads as a balance problem rather than a bug.
+
+Nothing in the swing writes health. A landed blow is sent on the `Damage` bus and
+one system answers it, so `Health` has a single author and every rule that will
+ever sit between a hit and a health bar has one seam to sit in:
+
+```typescript
+import { defineSystem, EventReader, GetWorld, Res, Damage, Children, ParticleEmitter, Particle } from 'esengine';
+
+const hitSparks = defineSystem(
+ [EventReader(Damage), GetWorld(), Res(Particle)],
+ (blows, world, particles) => {
+ for (const blow of blows) {
+ if (!world.has(blow.target, Children)) continue;
+ for (const child of world.get(blow.target, Children).entities) {
+ if (world.has(child, ParticleEmitter)) particles.play(child);
+ }
+ }
+ },
+ { name: 'HitSparkSystem' },
+);
+```
+
+A blow carries `target`, `source`, `amount` and the point it landed at — that
+point cannot be recovered later, because by the time a spark is drawn the swing
+has moved on. The bus is double buffered, so a blow sent this frame lands on the
+next.
+
+<Aside type="tip">
+ The physics query answers with colliders and nothing else. Self, already-hit
+ and not-damageable are gameplay's rules and live with the swing — the floor is
+ a collider too, and it is `Health` that says an entity can be hurt at all.
+</Aside>
+
## Joints
A door, a rope bridge, a lift, a ragdoll: none of them are one body and a shape.
diff --git a/docs/astro/src/content/docs/zh-cn/gameplay/physics-3d.mdx b/docs/astro/src/content/docs/zh-cn/gameplay/physics-3d.mdx
index 2deef4006..bed8f5db7 100644
--- a/docs/astro/src/content/docs/zh-cn/gameplay/physics-3d.mdx
+++ b/docs/astro/src/content/docs/zh-cn/gameplay/physics-3d.mdx
@@ -172,6 +172,74 @@ rig.add(ThirdPersonCamera, { target: player, distance: 420 });
相机会在支点与眼睛之间扫掠物理世界,有实体挡住时把自己拉近。它只移动自己。
</Aside>
+## 近战
+
+三件事,把它们分开就是战斗系统的全部:
+
+```text
+动画事件 ≠ 命中 ≠ 伤害
+```
+
+剪辑只说明这一刀到了它的有效时刻。那片空间里有谁,是物理世界的答案。这对谁造成什么,
+是玩法的事。任何两件合并,就得到一个不改另外两件就无法重新瞄准、重新配时、重新平衡的
+战斗系统。
+
+`MeleeAttack` 挂在一个 `Animator` 旁边,它的攻击剪辑用 `customEvent` 轨道声明三个节拍
+——`attack-start`、`hit`、`attack-end`:
+
+```typescript
+player.add(MeleeAttack, { radius: 70, damage: 25, anchor: swordSocket });
+dummy.add(Health, { current: 100, max: 100 });
+```
+
+| 字段 | 作用 |
+|---|---|
+| `radius` | `hit` 节拍测试的球半径,世界单位。 |
+| `damage` | 一次落地的攻击扣掉多少。 |
+| `layers` | 这一刀能够到的物理层;`0` 表示所有层。 |
+| `anchor` | 查询以哪个实体的**世界**位置为中心。 |
+| `attackId` | 引擎写入:正在进行的那一刀,没有时为 `0`。 |
+| `hitCount` | 引擎写入:这一刀已经打中了几个目标。 |
+
+<Aside type="caution">
+ `anchor` 应该是挂在动画骨骼下的插槽,而不是角色本身。把查询放在角色身上,攻击范围
+ 就变成了从脚底算起的一个固定偏移:骨架被画出来了,却从来没有被问过,屏幕上明明挥空
+ 的一刀照样命中。查询前会先合成 anchor 的世界位置,所以它问的就是动画真正做到的地方。
+</Aside>
+
+一刀是一个带自己 id、自己「已经打到过谁」集合的**实例**,所以一刀对一个目标最多命中
+一次,而下一刀可以再次命中。命中窗口通常就是好几个 `hit` 节拍,靠「事件只触发一次」来
+保证单次命中,在剪辑第一次声明两个节拍时就会破——而且是静默地破,表现为双倍伤害,
+看起来像数值没配好而不是 bug。
+
+这一刀不写任何血量。落地的攻击发到 `Damage` 总线上,由一个系统来回应,所以 `Health`
+只有一个作者,将来任何要坐在「命中」与「血条」之间的规则都有唯一一个位置可坐:
+
+```typescript
+import { defineSystem, EventReader, GetWorld, Res, Damage, Children, ParticleEmitter, Particle } from 'esengine';
+
+const hitSparks = defineSystem(
+ [EventReader(Damage), GetWorld(), Res(Particle)],
+ (blows, world, particles) => {
+ for (const blow of blows) {
+ if (!world.has(blow.target, Children)) continue;
+ for (const child of world.get(blow.target, Children).entities) {
+ if (world.has(child, ParticleEmitter)) particles.play(child);
+ }
+ }
+ },
+ { name: 'HitSparkSystem' },
+);
+```
+
+一次攻击携带 `target`、`source`、`amount` 和它落在哪个点——这个点事后是找不回来的,
+等火花画出来的时候,那一刀早就挥过去了。总线是双缓冲的,本帧发出的攻击在下一帧落地。
+
+<Aside type="tip">
+ 物理查询只回答碰撞体,别的什么都不答。「不能打自己」「已经打过了」「打不动」这三条是
+ 玩法的规则,跟着这一刀走——地板也是碰撞体,而一个实体能不能被打,是 `Health` 说了算。
+</Aside>
+
## 关节
门、绳桥、升降台、布娃娃:它们都不是「一个刚体加一个形状」,
diff --git a/docs/astro/src/data/apiStability.generated.json b/docs/astro/src/data/apiStability.generated.json
index 09a5ce56b..fc9acef0b 100644
--- a/docs/astro/src/data/apiStability.generated.json
+++ b/docs/astro/src/data/apiStability.generated.json
@@ -3,10 +3,10 @@
"counts": {
"public": 113,
"beta": 91,
- "experimental": 1745,
+ "experimental": 1760,
"deprecated": 3
},
- "total": 1949,
+ "total": 1964,
"subsystems": [
{
"id": "ecs",
diff --git a/docs/astro/src/data/componentDocs.ts b/docs/astro/src/data/componentDocs.ts
index 2e02d9cd4..8ce3789df 100644
--- a/docs/astro/src/data/componentDocs.ts
+++ b/docs/astro/src/data/componentDocs.ts
@@ -117,6 +117,8 @@ export const COMPONENT_DOCS: Record<string, ComponentDoc> = {
CharacterController3D: { category: 'physics', summary: 'A kinematic 3D mover: swept against the world, climbs steps, stays glued to slopes, and reports the ground it is on.', doc: 'gameplay/physics-3d', anchor: 'characters', anchorZh: '角色' },
ThirdPersonController: { category: 'physics', summary: 'Turns input into a movement request for a CharacterController3D, and reports what the character actually did to its Animator.', doc: 'gameplay/physics-3d', anchor: 'third-person', anchorZh: '第三人称' },
ThirdPersonCamera: { category: 'physics', summary: 'An eye that orbits a target under pointer control and pulls in when something solid stands between.', doc: 'gameplay/physics-3d', anchor: 'third-person', anchorZh: '第三人称' },
+ MeleeAttack: { category: 'physics', summary: 'A swing whose timing is the animation’s and whose reach is the physics world’s: a `hit` beat asks what is around an animated anchor, and gameplay decides who may be hurt.', doc: 'gameplay/physics-3d', anchor: 'melee-combat', anchorZh: '近战' },
+ Health: { category: 'physics', summary: 'What something has left, and what makes it damageable at all — written only by the system that answers the Damage bus.', doc: 'gameplay/physics-3d', anchor: 'melee-combat', anchorZh: '近战' },
PointJoint3D: { category: 'physics', summary: 'Holds two 3D bodies at a shared point, free to turn on all three axes — a chain link, a ragdoll limb.', doc: 'gameplay/physics-3d', anchor: 'joints', anchorZh: '关节' },
HingeJoint3D: { category: 'physics', summary: 'One axis of rotation between two 3D bodies — a door or a driven wheel, with optional limits and a motor.', doc: 'gameplay/physics-3d', anchor: 'joints', anchorZh: '关节' },
SliderJoint3D: { category: 'physics', summary: 'One axis of travel between two 3D bodies — a lift or a piston, with optional limits and a motor.', doc: 'gameplay/physics-3d', anchor: 'joints', anchorZh: '关节' },
diff --git a/docs/astro/src/data/components.generated.json b/docs/astro/src/data/components.generated.json
index d6683b3da..9cd0c2425 100644
--- a/docs/astro/src/data/components.generated.json
+++ b/docs/astro/src/data/components.generated.json
@@ -1580,6 +1580,28 @@
"damping"
]
},
+ {
+ "name": "Health",
+ "source": "ts",
+ "fields": [
+ {
+ "key": "current",
+ "type": "number",
+ "default": "100",
+ "range": "≥ 0"
+ },
+ {
+ "key": "max",
+ "type": "number",
+ "default": "100",
+ "range": "≥ 0"
+ }
+ ],
+ "animatable": [
+ "current",
+ "max"
+ ]
+ },
{
"name": "HingeJoint2D",
"source": "ts",
@@ -1887,6 +1909,64 @@
}
]
},
+ {
+ "name": "MeleeAttack",
+ "source": "ts",
+ "fields": [
+ {
+ "key": "radius",
+ "type": "number",
+ "default": "60",
+ "range": "≥ 0"
+ },
+ {
+ "key": "damage",
+ "type": "number",
+ "default": "25",
+ "range": "≥ 0"
+ },
+ {
+ "key": "layers",
+ "type": "number",
+ "default": "0",
+ "range": "0–15, step 1",
+ "advanced": true
+ },
+ {
+ "key": "anchor",
+ "type": "Entity",
+ "default": "none",
+ "doc": "The animated socket the swing reaches from."
+ },
+ {
+ "key": "enabled",
+ "type": "boolean",
+ "default": "true"
+ },
+ {
+ "key": "attackId",
+ "type": "number",
+ "default": "0",
+ "readonly": true,
+ "advanced": true
+ },
+ {
+ "key": "hitCount",
+ "type": "number",
+ "default": "0",
+ "readonly": true,
+ "advanced": true
+ }
+ ],
+ "animatable": [
+ "radius",
+ "damage",
+ "layers",
+ "anchor",
+ "attackId",
+ "hitCount"
+ ]
+ },
{
"name": "MeshCollider3D",
"source": "cpp",