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: correct the tilemap guide against real SDK API

yhh0859c6086378
diff --git a/docs/astro/src/content/docs/guides/tilemap.mdx b/docs/astro/src/content/docs/guides/tilemap.mdx
index 16ef23766..cb35259d2 100644
--- a/docs/astro/src/content/docs/guides/tilemap.mdx
+++ b/docs/astro/src/content/docs/guides/tilemap.mdx
@@ -1,6 +1,6 @@
---
title: Tilemaps
-description: Load Tiled maps, or build and edit tile layers through the API.
+description: Load Tiled maps, or build tile layers with the TilemapAPI.
---
import { Aside } from '@astrojs/starlight/components';
@@ -11,50 +11,56 @@ for building and editing layers yourself.
## High-level: load a Tiled map
-Give an entity a `Tilemap` component pointing at a Tiled JSON (`.tmj`) file. The
-plugin loads it, spawns a `TilemapLayer` entity per layer, and (in play mode)
-applies tile collision:
+The `Tilemap` component points an entity at a Tiled JSON (`.tmj`) file via its
+one field, `source`. You usually add it in the editor and pick the file; the
+runtime then loads the map and spawns a `TilemapLayer` per layer. To do it
+procedurally, spawn from a system that takes `Commands()`:
```typescript
-import { Tilemap, Transform } from 'esengine';
+import { defineSystem, Commands, Tilemap } from 'esengine';
-world.spawn({
- [Tilemap]: { source: 'assets/maps/level.tmj' },
- [Transform]: { position: { x: 0, y: 0, z: 0 } },
+const loadLevel = defineSystem([Commands()], (cmds) => {
+ cmds.spawn().insert(Tilemap, { source: 'assets/maps/level.tmj' });
});
```
## Low-level: build layers with `TilemapAPI`
-Each layer is a `TilemapLayer` component (cell size, tileset texture, columns,
-render layer, tint, opacity, parallax). Edit tiles through `TilemapAPI`:
+`TilemapAPI` is a singleton you import from `esengine` and call directly with a
+layer entity. Spawn an entity, initialise a layer on it, then write tiles:
```typescript
-import { defineSystem, Res, TilemapAPI } from 'esengine';
+import { defineSystem, Commands, Transform, TilemapAPI } from 'esengine';
-defineSystem([Res(TilemapAPI)], (tilemap) => {
- tilemap.initLayer(entity, 32, 32, 16, 16); // 32x32 tiles, 16px cells
+const buildLayer = defineSystem([Commands()], (cmds) => {
+ const layer = cmds
+ .spawn()
+ .insert(Transform, { position: { x: 0, y: 0, z: 0 } })
+ .id(); // the layer entity
- const tiles = new Uint16Array(32 * 32); // tile ids (0 = empty)
+ TilemapAPI.initLayer(layer, 32, 32, 16, 16); // 32x32 tiles, 16px cells
+
+ const tiles = new Uint16Array(32 * 32); // tile ids (0 = empty)
tiles[0] = 1;
- tilemap.setTiles(entity, tiles);
+ TilemapAPI.setTiles(layer, tiles);
- tilemap.setTile(entity, 5, 3, 7); // single tile
- tilemap.flipTile(entity, 5, 3, true, false, false); // flip horizontally
+ TilemapAPI.setTile(layer, 5, 3, 7); // one tile
+ TilemapAPI.flipTile(layer, 5, 3, true, false, false); // flip horizontally
- tilemap.setTileAnimation(entity, 10, [ // animate tile id 10
+ TilemapAPI.setTileAnimation(layer, 10, [ // animate tile id 10
{ tileId: 10, duration: 100 },
{ tileId: 11, duration: 100 },
]);
});
```
-Other methods: `getTile`, `fillRect`, `rotateTile`, `initInfiniteLayer` (chunked
-infinite maps), `setTilesets`, `tileToWorld` / `worldToTile`.
+Other `TilemapAPI` methods: `getTile`, `fillRect`, `rotateTile`,
+`initInfiniteLayer` (chunked infinite maps), `setTilesets`, `setTint`,
+`setVisible`, and `tileToWorld` / `worldToTile`.
<Aside type="note">
Tile **collision is play-mode only** — static colliders are spawned from
collidable tiles (marked in the `.estileset` tileset) when the scene runs; they
are never in the edit world or serialized. Tile animations advance
- automatically once set; you only call `setTileAnimation` once.
+ automatically once set — you call `setTileAnimation` once.
</Aside>
diff --git a/docs/astro/src/content/docs/zh-cn/guides/tilemap.mdx b/docs/astro/src/content/docs/zh-cn/guides/tilemap.mdx
index c79bcdd6f..beb0f29e8 100644
--- a/docs/astro/src/content/docs/zh-cn/guides/tilemap.mdx
+++ b/docs/astro/src/content/docs/zh-cn/guides/tilemap.mdx
@@ -1,57 +1,63 @@
---
title: 瓦片地图
-description: 加载 Tiled 地图,或通过 API 构建和编辑瓦片图层。
+description: 加载 Tiled 地图,或用 TilemapAPI 构建瓦片图层。
---
import { Aside } from '@astrojs/starlight/components';
-Estella 在 C++/WebAssembly 内核中渲染瓦片图层。有两种用法:一个从文件加载整张地图的
-高层组件,以及一套自己构建、编辑图层的低层 API。
+Estella 在 C++/WebAssembly 内核中渲染瓦片图层。有两种用法:一个从文件加载整张地图的
+高层组件,以及一套自己构建、编辑图层的低层 API。
-## 高层:加载 Tiled 地图
+## 高层:加载 Tiled 地图
-给实体一个 `Tilemap` 组件,指向 Tiled JSON(`.tmj`)文件。插件会加载它,为每个图层
-生成一个 `TilemapLayer` 实体,并在运行模式下应用瓦片碰撞:
+`Tilemap` 组件通过它唯一的字段 `source`,把实体指向一个 Tiled JSON(`.tmj`)文件。
+通常你在编辑器里添加该组件并选择文件;运行时随后加载地图,并为每个图层生成一个
+`TilemapLayer`。要以代码方式做,在带 `Commands()` 参数的系统里生成:
```typescript
-import { Tilemap, Transform } from 'esengine';
+import { defineSystem, Commands, Tilemap } from 'esengine';
-world.spawn({
- [Tilemap]: { source: 'assets/maps/level.tmj' },
- [Transform]: { position: { x: 0, y: 0, z: 0 } },
+const loadLevel = defineSystem([Commands()], (cmds) => {
+ cmds.spawn().insert(Tilemap, { source: 'assets/maps/level.tmj' });
});
```
-## 低层:用 `TilemapAPI` 构建图层
+## 低层:用 `TilemapAPI` 构建图层
-每个图层是一个 `TilemapLayer` 组件(格子尺寸、图集贴图、列数、渲染层、着色、不透明度、
-视差)。通过 `TilemapAPI` 编辑瓦片:
+`TilemapAPI` 是一个从 `esengine` 导入的单例,直接传入图层实体来调用。先生成一个实体,
+在它上面初始化图层,再写入瓦片:
```typescript
-import { defineSystem, Res, TilemapAPI } from 'esengine';
+import { defineSystem, Commands, Transform, TilemapAPI } from 'esengine';
-defineSystem([Res(TilemapAPI)], (tilemap) => {
- tilemap.initLayer(entity, 32, 32, 16, 16); // 32x32 格,16px 格子
+const buildLayer = defineSystem([Commands()], (cmds) => {
+ const layer = cmds
+ .spawn()
+ .insert(Transform, { position: { x: 0, y: 0, z: 0 } })
+ .id(); // 图层实体
- const tiles = new Uint16Array(32 * 32); // 瓦片 id(0 = 空)
+ TilemapAPI.initLayer(layer, 32, 32, 16, 16); // 32x32 格,16px 格子
+
+ const tiles = new Uint16Array(32 * 32); // 瓦片 id(0 = 空)
tiles[0] = 1;
- tilemap.setTiles(entity, tiles);
+ TilemapAPI.setTiles(layer, tiles);
- tilemap.setTile(entity, 5, 3, 7); // 单个瓦片
- tilemap.flipTile(entity, 5, 3, true, false, false); // 水平翻转
+ TilemapAPI.setTile(layer, 5, 3, 7); // 单个瓦片
+ TilemapAPI.flipTile(layer, 5, 3, true, false, false); // 水平翻转
- tilemap.setTileAnimation(entity, 10, [ // 让瓦片 id 10 动起来
+ TilemapAPI.setTileAnimation(layer, 10, [ // 让瓦片 id 10 动起来
{ tileId: 10, duration: 100 },
{ tileId: 11, duration: 100 },
]);
});
```
-其他方法:`getTile`、`fillRect`、`rotateTile`、`initInfiniteLayer`(分块的无限地图)、
-`setTilesets`、`tileToWorld` / `worldToTile`。
+`TilemapAPI` 的其他方法:`getTile`、`fillRect`、`rotateTile`、`initInfiniteLayer`
+(分块的无限地图)、`setTilesets`、`setTint`、`setVisible`,以及 `tileToWorld` /
+`worldToTile`。
<Aside type="note">
- 瓦片**碰撞仅在运行模式**存在——静态碰撞体在场景运行时由可碰撞瓦片(在 `.estileset`
- 图集里标记)生成;它们不存在于编辑世界中,也不会被序列化。瓦片动画一旦设置便自动
- 推进;`setTileAnimation` 只需调用一次。
+ 瓦片**碰撞仅在运行模式**存在——静态碰撞体在场景运行时由可碰撞瓦片(在 `.estileset`
+ 图集里标记)生成;它们不存在于编辑世界中,也不会被序列化。瓦片动画一旦设置便自动
+ 推进——`setTileAnimation` 只需调用一次。
</Aside>