@yagejs/tilemap 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +43 -0
- package/dist/index.cjs +618 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +333 -0
- package/dist/index.d.ts +333 -0
- package/dist/index.js +588 -0
- package/dist/index.js.map +1 -0
- package/package.json +59 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
import { Plugin, EngineContext, SystemScheduler, Component, System, Phase, AssetHandle } from '@yagejs/core';
|
|
2
|
+
import { Container, ExtensionType, LoaderParser } from 'pixi.js';
|
|
3
|
+
import { ColliderConfig } from '@yagejs/physics';
|
|
4
|
+
import { CompositeTilemap } from '@pixi/tilemap';
|
|
5
|
+
|
|
6
|
+
/** Plugin that adds Tiled map loading and rendering to YAGE. */
|
|
7
|
+
declare class TilemapPlugin implements Plugin {
|
|
8
|
+
readonly name = "tilemap";
|
|
9
|
+
readonly version = "2.0.0";
|
|
10
|
+
readonly dependencies: readonly ["renderer"];
|
|
11
|
+
install(context: EngineContext): void;
|
|
12
|
+
registerSystems(scheduler: SystemScheduler): void;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
interface TiledMapData {
|
|
16
|
+
width: number;
|
|
17
|
+
height: number;
|
|
18
|
+
tilewidth: number;
|
|
19
|
+
tileheight: number;
|
|
20
|
+
layers: (TileLayer | ObjectGroup)[];
|
|
21
|
+
tilesets: TilesetRef[];
|
|
22
|
+
orientation?: string;
|
|
23
|
+
renderorder?: string;
|
|
24
|
+
infinite?: boolean;
|
|
25
|
+
nextlayerid?: number;
|
|
26
|
+
nextobjectid?: number;
|
|
27
|
+
tiledversion?: string;
|
|
28
|
+
type?: string;
|
|
29
|
+
version?: string | number;
|
|
30
|
+
}
|
|
31
|
+
interface TileLayer {
|
|
32
|
+
type: "tilelayer";
|
|
33
|
+
data: number[];
|
|
34
|
+
width: number;
|
|
35
|
+
height: number;
|
|
36
|
+
id: number;
|
|
37
|
+
name: string;
|
|
38
|
+
opacity: number;
|
|
39
|
+
visible: boolean;
|
|
40
|
+
x: number;
|
|
41
|
+
y: number;
|
|
42
|
+
}
|
|
43
|
+
interface ObjectGroup {
|
|
44
|
+
type: "objectgroup";
|
|
45
|
+
id: number;
|
|
46
|
+
name: string;
|
|
47
|
+
objects: TileObject[];
|
|
48
|
+
opacity: number;
|
|
49
|
+
visible: boolean;
|
|
50
|
+
x: number;
|
|
51
|
+
y: number;
|
|
52
|
+
draworder?: string;
|
|
53
|
+
}
|
|
54
|
+
interface RectangleObject {
|
|
55
|
+
id: number;
|
|
56
|
+
name: string;
|
|
57
|
+
class?: string;
|
|
58
|
+
type?: string;
|
|
59
|
+
x: number;
|
|
60
|
+
y: number;
|
|
61
|
+
width: number;
|
|
62
|
+
height: number;
|
|
63
|
+
rotation: number;
|
|
64
|
+
visible: boolean;
|
|
65
|
+
point?: undefined | false;
|
|
66
|
+
polygon?: undefined;
|
|
67
|
+
properties?: TileObjectProperty[];
|
|
68
|
+
}
|
|
69
|
+
interface PolygonObject {
|
|
70
|
+
id: number;
|
|
71
|
+
name: string;
|
|
72
|
+
class?: string;
|
|
73
|
+
type?: string;
|
|
74
|
+
x: number;
|
|
75
|
+
y: number;
|
|
76
|
+
width: number;
|
|
77
|
+
height: number;
|
|
78
|
+
rotation: number;
|
|
79
|
+
visible: boolean;
|
|
80
|
+
point?: undefined | false;
|
|
81
|
+
polygon: {
|
|
82
|
+
x: number;
|
|
83
|
+
y: number;
|
|
84
|
+
}[];
|
|
85
|
+
properties?: TileObjectProperty[];
|
|
86
|
+
}
|
|
87
|
+
interface PointObject {
|
|
88
|
+
id: number;
|
|
89
|
+
name: string;
|
|
90
|
+
class?: string;
|
|
91
|
+
type?: string;
|
|
92
|
+
x: number;
|
|
93
|
+
y: number;
|
|
94
|
+
width: number;
|
|
95
|
+
height: number;
|
|
96
|
+
rotation: number;
|
|
97
|
+
visible: boolean;
|
|
98
|
+
point: true;
|
|
99
|
+
polygon?: undefined;
|
|
100
|
+
properties?: TileObjectProperty[];
|
|
101
|
+
}
|
|
102
|
+
type TileObject = RectangleObject | PointObject | PolygonObject;
|
|
103
|
+
interface TileObjectProperty {
|
|
104
|
+
name: string;
|
|
105
|
+
type: string;
|
|
106
|
+
value: unknown;
|
|
107
|
+
}
|
|
108
|
+
interface TilesetRef {
|
|
109
|
+
firstgid: number;
|
|
110
|
+
source?: string;
|
|
111
|
+
/** Resolved tileset data — populated by the loader. */
|
|
112
|
+
data?: TilesetData;
|
|
113
|
+
}
|
|
114
|
+
interface TilesetData {
|
|
115
|
+
name: string;
|
|
116
|
+
tilewidth: number;
|
|
117
|
+
tileheight: number;
|
|
118
|
+
tilecount: number;
|
|
119
|
+
columns: number;
|
|
120
|
+
margin?: number;
|
|
121
|
+
spacing?: number;
|
|
122
|
+
/** Single-image tileset: path to the spritesheet image. */
|
|
123
|
+
image?: string;
|
|
124
|
+
imagewidth?: number;
|
|
125
|
+
imageheight?: number;
|
|
126
|
+
/** Collection-of-images tileset: individual tile metadata. */
|
|
127
|
+
tiles?: TileData[];
|
|
128
|
+
tiledversion?: string;
|
|
129
|
+
type?: string;
|
|
130
|
+
version?: string | number;
|
|
131
|
+
}
|
|
132
|
+
interface TileData {
|
|
133
|
+
id: number;
|
|
134
|
+
image?: string;
|
|
135
|
+
imagewidth?: number;
|
|
136
|
+
imageheight?: number;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
interface TilemapData {
|
|
140
|
+
width: number;
|
|
141
|
+
height: number;
|
|
142
|
+
tileWidth: number;
|
|
143
|
+
tileHeight: number;
|
|
144
|
+
tileLayers: TileLayerData[];
|
|
145
|
+
objectLayers: ObjectLayerData[];
|
|
146
|
+
}
|
|
147
|
+
interface TileLayerData {
|
|
148
|
+
name: string;
|
|
149
|
+
data: number[];
|
|
150
|
+
width: number;
|
|
151
|
+
height: number;
|
|
152
|
+
visible: boolean;
|
|
153
|
+
}
|
|
154
|
+
interface ObjectLayerData {
|
|
155
|
+
name: string;
|
|
156
|
+
objects: MapObject[];
|
|
157
|
+
visible: boolean;
|
|
158
|
+
}
|
|
159
|
+
interface MapObject {
|
|
160
|
+
id: number;
|
|
161
|
+
name: string;
|
|
162
|
+
class?: string;
|
|
163
|
+
x: number;
|
|
164
|
+
y: number;
|
|
165
|
+
width: number;
|
|
166
|
+
height: number;
|
|
167
|
+
rotation: number;
|
|
168
|
+
visible: boolean;
|
|
169
|
+
point?: boolean;
|
|
170
|
+
polygon?: {
|
|
171
|
+
x: number;
|
|
172
|
+
y: number;
|
|
173
|
+
}[];
|
|
174
|
+
properties?: MapObjectProperty[];
|
|
175
|
+
}
|
|
176
|
+
interface MapObjectProperty {
|
|
177
|
+
name: string;
|
|
178
|
+
type: string;
|
|
179
|
+
value: unknown;
|
|
180
|
+
}
|
|
181
|
+
/** Interface for anything that has a `properties` array (MapObject, etc.). */
|
|
182
|
+
interface HasProperties {
|
|
183
|
+
properties?: MapObjectProperty[];
|
|
184
|
+
}
|
|
185
|
+
interface RectColliderConfig {
|
|
186
|
+
type: "rect";
|
|
187
|
+
x: number;
|
|
188
|
+
y: number;
|
|
189
|
+
width: number;
|
|
190
|
+
height: number;
|
|
191
|
+
}
|
|
192
|
+
interface PolygonColliderConfig {
|
|
193
|
+
type: "polygon";
|
|
194
|
+
x: number;
|
|
195
|
+
y: number;
|
|
196
|
+
vertices: {
|
|
197
|
+
x: number;
|
|
198
|
+
y: number;
|
|
199
|
+
}[];
|
|
200
|
+
}
|
|
201
|
+
type TilemapColliderConfig = RectColliderConfig | PolygonColliderConfig;
|
|
202
|
+
|
|
203
|
+
/** Options for creating a TilemapComponent. */
|
|
204
|
+
interface TilemapComponentOptions {
|
|
205
|
+
/** Parsed Tiled map data (not serializable). */
|
|
206
|
+
map?: TiledMapData;
|
|
207
|
+
/** Asset path to the Tiled JSON (serializable, resolved via Assets.get). */
|
|
208
|
+
mapKey?: string;
|
|
209
|
+
/** Which tile layers to render. Omit to render all. */
|
|
210
|
+
layers?: string[];
|
|
211
|
+
/** Render layer name. Default: "default". */
|
|
212
|
+
layer?: string;
|
|
213
|
+
}
|
|
214
|
+
/** Serializable snapshot of a TilemapComponent. */
|
|
215
|
+
interface TilemapComponentData {
|
|
216
|
+
mapKey: string;
|
|
217
|
+
layers?: string[];
|
|
218
|
+
layer: string;
|
|
219
|
+
}
|
|
220
|
+
/** Component that renders a Tiled map using @pixi/tilemap. */
|
|
221
|
+
declare class TilemapComponent extends Component {
|
|
222
|
+
readonly container: Container;
|
|
223
|
+
readonly data: TilemapData;
|
|
224
|
+
private readonly _tiledMap;
|
|
225
|
+
private readonly _mapKey;
|
|
226
|
+
private readonly layerNames;
|
|
227
|
+
private readonly renderLayerName;
|
|
228
|
+
constructor(options: TilemapComponentOptions);
|
|
229
|
+
onAdd(): void;
|
|
230
|
+
onDestroy(): void;
|
|
231
|
+
serialize(): TilemapComponentData | null;
|
|
232
|
+
static fromSnapshot(data: TilemapComponentData): TilemapComponent;
|
|
233
|
+
/** Map width in pixels. */
|
|
234
|
+
get widthPx(): number;
|
|
235
|
+
/** Map height in pixels. */
|
|
236
|
+
get heightPx(): number;
|
|
237
|
+
/** Tile width in pixels. */
|
|
238
|
+
get tileWidth(): number;
|
|
239
|
+
/** Tile height in pixels. */
|
|
240
|
+
get tileHeight(): number;
|
|
241
|
+
/**
|
|
242
|
+
* Returns the tile GID at a world position, accounting for entity Transform offset.
|
|
243
|
+
* Returns null if the position is outside the map or the tile is empty.
|
|
244
|
+
*/
|
|
245
|
+
getTileAt(worldX: number, worldY: number, layerName?: string): number | null;
|
|
246
|
+
/** Extract physics-agnostic collision shapes from object layers. */
|
|
247
|
+
getCollisionShapes(objectLayerName?: string): TilemapColliderConfig[];
|
|
248
|
+
/** Extract objects from object layers grouped by class/name. */
|
|
249
|
+
getObjects(objectLayerName?: string): Record<string, MapObject[]>;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/** Syncs Transform to TilemapComponent display containers. */
|
|
253
|
+
declare class TilemapRenderSystem extends System {
|
|
254
|
+
readonly phase = Phase.Render;
|
|
255
|
+
readonly priority = -1;
|
|
256
|
+
private query;
|
|
257
|
+
onRegister(context: EngineContext): void;
|
|
258
|
+
update(): void;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/** Create a typed asset handle for a Tiled map JSON. */
|
|
262
|
+
declare function tiledMap(path: string): AssetHandle<TiledMapData>;
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Extract physics-agnostic collision shapes from object layers.
|
|
266
|
+
*
|
|
267
|
+
* - Rectangle objects -> RectColliderConfig
|
|
268
|
+
* - Polygon objects -> PolygonColliderConfig
|
|
269
|
+
* - Point objects -> skipped (not collision shapes)
|
|
270
|
+
*
|
|
271
|
+
* @param map - Generic TilemapData.
|
|
272
|
+
* @param objectLayerName - Optional: only extract from this layer.
|
|
273
|
+
*/
|
|
274
|
+
declare function extractCollisionShapes(map: TilemapData, objectLayerName?: string): TilemapColliderConfig[];
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Convert tilemap TilemapColliderConfig[] (top-left origin rects/polygons) into
|
|
278
|
+
* physics-package ColliderConfig[] (center-origin shape + offset).
|
|
279
|
+
*/
|
|
280
|
+
declare function toPhysicsColliders(shapes: TilemapColliderConfig[]): ColliderConfig[];
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* Get a single property value by name.
|
|
284
|
+
* Returns `undefined` if the property is not found.
|
|
285
|
+
*/
|
|
286
|
+
declare function getProperty<T = unknown>(obj: HasProperties, name: string): T | undefined;
|
|
287
|
+
/**
|
|
288
|
+
* Get a pseudo-array of property values.
|
|
289
|
+
*
|
|
290
|
+
* Tiled doesn't support array properties natively, so a common convention
|
|
291
|
+
* is to use indexed names like `spawns[0]`, `spawns[1]`, etc.
|
|
292
|
+
* This function collects them into a proper array, preserving index order.
|
|
293
|
+
*/
|
|
294
|
+
declare function getPropertyArray<T = unknown>(obj: HasProperties, name: string): T[];
|
|
295
|
+
/**
|
|
296
|
+
* Resolve a property of `type: "object"` (an ID reference) to the actual MapObject.
|
|
297
|
+
* Returns `undefined` if the property doesn't exist or the referenced object isn't found.
|
|
298
|
+
*/
|
|
299
|
+
declare function resolveObjectRef(obj: HasProperties, propName: string, allObjects: MapObject[]): MapObject | undefined;
|
|
300
|
+
/**
|
|
301
|
+
* Resolve a pseudo-array of object ID references to actual MapObjects.
|
|
302
|
+
* Uses the `name[0]`, `name[1]` convention.
|
|
303
|
+
*/
|
|
304
|
+
declare function resolveObjectRefArray(obj: HasProperties, propName: string, allObjects: MapObject[]): MapObject[];
|
|
305
|
+
|
|
306
|
+
/** PixiJS asset extension bundle for Tiled map JSON files. */
|
|
307
|
+
declare const tiledMapAssetExtension: {
|
|
308
|
+
extension: ExtensionType;
|
|
309
|
+
loader: LoaderParser<TiledMapData, any, Record<string, any>>;
|
|
310
|
+
};
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* Convert Tiled JSON data to the generic TilemapData format.
|
|
314
|
+
*/
|
|
315
|
+
declare function toTilemapData(map: TiledMapData): TilemapData;
|
|
316
|
+
/**
|
|
317
|
+
* Build CompositeTilemap display objects from a parsed Tiled map.
|
|
318
|
+
*
|
|
319
|
+
* @param map - Parsed TiledMapData (with resolved tilesets).
|
|
320
|
+
* @param layerNames - Optional filter: only process these tile layer names.
|
|
321
|
+
* @returns Array of CompositeTilemap, one per tile layer.
|
|
322
|
+
*/
|
|
323
|
+
declare function createTilemapLayers(map: TiledMapData, layerNames?: string[]): CompositeTilemap[];
|
|
324
|
+
/**
|
|
325
|
+
* Extract objects from Tiled object layers, grouped by class/type/name.
|
|
326
|
+
*
|
|
327
|
+
* @param map - Parsed TiledMapData.
|
|
328
|
+
* @param objectLayerName - Optional: only extract from this layer.
|
|
329
|
+
* @returns Record mapping class/type/name to arrays of TileObject.
|
|
330
|
+
*/
|
|
331
|
+
declare function extractObjects(map: TiledMapData, objectLayerName?: string): Record<string, TileObject[]>;
|
|
332
|
+
|
|
333
|
+
export { type HasProperties, type MapObject, type MapObjectProperty, type ObjectGroup, type ObjectLayerData, type PointObject, type PolygonColliderConfig, type PolygonObject, type RectColliderConfig, type RectangleObject, type TileData, type TileLayer, type TileLayerData, type TileObject, type TileObjectProperty, type TiledMapData, type TilemapColliderConfig, TilemapComponent, type TilemapComponentData, type TilemapComponentOptions, type TilemapData, TilemapPlugin, TilemapRenderSystem, type TilesetData, type TilesetRef, createTilemapLayers, extractCollisionShapes, extractObjects, getProperty, getPropertyArray, resolveObjectRef, resolveObjectRefArray, tiledMap, tiledMapAssetExtension, toPhysicsColliders, toTilemapData };
|