@flare-engine/tilemap 0.2.0 → 0.3.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/dist/index.d.ts CHANGED
@@ -1,180 +1,198 @@
1
- import { Rect } from '@flare-engine/math';
2
- import { SkImageLike } from '@flare-engine/assets';
3
- import { SpriteBatch, Camera2D } from '@flare-engine/render';
4
- import { AABBShape } from '@flare-engine/physics';
5
-
1
+ import { Rect } from "@flare-engine/math";
2
+ import { AABBShape } from "@flare-engine/physics";
3
+ import { SkImageLike } from "@flare-engine/assets";
4
+ import { Camera2D, SpriteBatch } from "@flare-engine/render";
5
+ //#region src/tilemap-data.d.ts
6
+ /** Low 29 bits of a Tiled GID — Tiled stores flip state in the high 3 bits. */
7
+ declare const GID_FLIP_MASK = 536870911;
8
+ /** Flip state encoded in the high bits of a Tiled GID. */
9
+ interface TileFlipFlags {
10
+ horizontal: boolean;
11
+ vertical: boolean;
12
+ diagonal: boolean;
13
+ }
14
+ /** Strip the Tiled flip bits from a raw GID, returning the real global tile id. */
15
+ declare function stripGidFlags(gid: number): number;
16
+ /**
17
+ * Decode the Tiled flip bits encoded in the high bits of a raw GID.
18
+ * Note: `gid & 0x80000000` yields a negative Int32 when the bit is set;
19
+ * compare with `!== 0`, not `> 0`.
20
+ */
21
+ declare function decodeGidFlags(gid: number): TileFlipFlags;
6
22
  /** A single tile in a tilemap grid. */
7
23
  interface TileData {
8
- /** Global tile ID (0 = empty). */
9
- gid: number;
10
- /** Whether this tile is flagged as solid (for collision). */
11
- solid: boolean;
12
- /** Custom properties on the tile. */
13
- properties: Record<string, unknown>;
24
+ /** Global tile ID (0 = empty). */
25
+ gid: number;
26
+ /** Whether this tile is flagged as solid (for collision). */
27
+ solid: boolean;
28
+ /** Custom properties on the tile. */
29
+ properties: Record<string, unknown>;
14
30
  }
15
31
  /** A layer of tiles in a grid. */
16
32
  interface TileLayer {
17
- readonly type: 'tile';
18
- /** Layer name. */
19
- name: string;
20
- /** Grid width in tiles. */
21
- width: number;
22
- /** Grid height in tiles. */
23
- height: number;
24
- /** Flat array of tile GIDs (row-major, length = width * height). */
25
- tiles: number[];
26
- /** Visibility flag. */
27
- visible: boolean;
28
- /** Opacity (0–1). */
29
- opacity: number;
30
- /** Layer offset in pixels. */
31
- offsetX: number;
32
- offsetY: number;
33
- /** Custom properties. */
34
- properties: Record<string, unknown>;
33
+ readonly type: 'tile';
34
+ /** Layer name. */
35
+ name: string;
36
+ /** Grid width in tiles. */
37
+ width: number;
38
+ /** Grid height in tiles. */
39
+ height: number;
40
+ /** Flat array of tile GIDs (row-major, length = width * height). */
41
+ tiles: number[];
42
+ /** Visibility flag. */
43
+ visible: boolean;
44
+ /** Opacity (0–1). */
45
+ opacity: number;
46
+ /** Layer offset in pixels. */
47
+ offsetX: number;
48
+ offsetY: number;
49
+ /** Custom properties. */
50
+ properties: Record<string, unknown>;
35
51
  }
36
52
  /** An object in an object layer. */
37
53
  interface MapObject {
38
- /** Object ID. */
39
- id: number;
40
- /** Object name. */
41
- name: string;
42
- /** Object type/class. */
43
- type: string;
44
- /** Position in pixels. */
45
- x: number;
46
- y: number;
47
- /** Size in pixels. */
48
- width: number;
49
- height: number;
50
- /** Rotation in degrees. */
51
- rotation: number;
52
- /** Custom properties. */
53
- properties: Record<string, unknown>;
54
+ /** Object ID. */
55
+ id: number;
56
+ /** Object name. */
57
+ name: string;
58
+ /** Object type/class. */
59
+ type: string;
60
+ /** Position in pixels. */
61
+ x: number;
62
+ y: number;
63
+ /** Size in pixels. */
64
+ width: number;
65
+ height: number;
66
+ /** Rotation in degrees. */
67
+ rotation: number;
68
+ /** Custom properties. */
69
+ properties: Record<string, unknown>;
54
70
  }
55
71
  /** A layer of objects. */
56
72
  interface ObjectLayer {
57
- readonly type: 'object';
58
- /** Layer name. */
59
- name: string;
60
- /** Objects in the layer. */
61
- objects: MapObject[];
62
- /** Visibility flag. */
63
- visible: boolean;
64
- /** Opacity (0–1). */
65
- opacity: number;
66
- /** Custom properties. */
67
- properties: Record<string, unknown>;
73
+ readonly type: 'object';
74
+ /** Layer name. */
75
+ name: string;
76
+ /** Objects in the layer. */
77
+ objects: MapObject[];
78
+ /** Visibility flag. */
79
+ visible: boolean;
80
+ /** Opacity (0–1). */
81
+ opacity: number;
82
+ /** Custom properties. */
83
+ properties: Record<string, unknown>;
68
84
  }
69
85
  /** Union of layer types. */
70
86
  type MapLayer = TileLayer | ObjectLayer;
71
87
  /** Information about a tileset used in the map. */
72
88
  interface TilesetRef {
73
- /** First GID in this tileset. */
74
- firstGid: number;
75
- /** Tileset name. */
76
- name: string;
77
- /** Tile width in pixels. */
78
- tileWidth: number;
79
- /** Tile height in pixels. */
80
- tileHeight: number;
81
- /** Number of columns in the tileset image. */
82
- columns: number;
83
- /** Total tile count. */
84
- tileCount: number;
85
- /** Image source path (may need resolving). */
86
- image: string;
87
- /** Image width in pixels. */
88
- imageWidth: number;
89
- /** Image height in pixels. */
90
- imageHeight: number;
91
- /** Per-tile properties (indexed by local tile ID). */
92
- tileProperties: Map<number, Record<string, unknown>>;
93
- /** Tile IDs marked as solid/collision. */
94
- solidTiles: Set<number>;
89
+ /** First GID in this tileset. */
90
+ firstGid: number;
91
+ /** Tileset name. */
92
+ name: string;
93
+ /** Tile width in pixels. */
94
+ tileWidth: number;
95
+ /** Tile height in pixels. */
96
+ tileHeight: number;
97
+ /** Number of columns in the tileset image. */
98
+ columns: number;
99
+ /** Total tile count. */
100
+ tileCount: number;
101
+ /** Image source path (may need resolving). */
102
+ image: string;
103
+ /** Image width in pixels. */
104
+ imageWidth: number;
105
+ /** Image height in pixels. */
106
+ imageHeight: number;
107
+ /** Per-tile properties (indexed by local tile ID). */
108
+ tileProperties: Map<number, Record<string, unknown>>;
109
+ /** Tile IDs marked as solid/collision. */
110
+ solidTiles: Set<number>;
95
111
  }
96
112
  /** Complete parsed tilemap data — format-independent. */
97
113
  interface TilemapData {
98
- /** Map width in tiles. */
99
- width: number;
100
- /** Map height in tiles. */
101
- height: number;
102
- /** Tile width in pixels. */
103
- tileWidth: number;
104
- /** Tile height in pixels. */
105
- tileHeight: number;
106
- /** All layers in order. */
107
- layers: MapLayer[];
108
- /** Tilesets referenced by this map. */
109
- tilesets: TilesetRef[];
110
- /** Custom map properties. */
111
- properties: Record<string, unknown>;
114
+ /** Map width in tiles. */
115
+ width: number;
116
+ /** Map height in tiles. */
117
+ height: number;
118
+ /** Tile width in pixels. */
119
+ tileWidth: number;
120
+ /** Tile height in pixels. */
121
+ tileHeight: number;
122
+ /** All layers in order. */
123
+ layers: MapLayer[];
124
+ /** Tilesets referenced by this map. */
125
+ tilesets: TilesetRef[];
126
+ /** Custom map properties. */
127
+ properties: Record<string, unknown>;
112
128
  }
113
129
  /** Create an empty TilemapData. */
114
130
  declare function createTilemapData(width: number, height: number, tileWidth: number, tileHeight: number): TilemapData;
115
131
  /** Get a tile GID at a grid position from a tile layer. Returns 0 if out of bounds. */
116
132
  declare function getTileAt(layer: TileLayer, col: number, row: number): number;
117
- /** Get the tileset that owns a given GID. */
133
+ /** Get the tileset that owns a given GID. Handles Tiled flip bits in the high bits. */
118
134
  declare function getTilesetForGid(tilesets: TilesetRef[], gid: number): TilesetRef | undefined;
119
- /** Compute the source rect in a tileset image for a given GID. */
135
+ /** Compute the source rect in a tileset image for a given GID. Strips Tiled flip bits. */
120
136
  declare function getTileSourceRect(tileset: TilesetRef, gid: number): Rect;
121
-
137
+ //#endregion
138
+ //#region src/ldtk-parser.d.ts
139
+ interface ParseLDtkOptions {
140
+ /** Predicate deciding whether an IntGrid value marks a solid cell. Default: value > 0. */
141
+ intGridSolid?: (value: number) => boolean;
142
+ }
143
+ interface LDtkEntity {
144
+ /** LDtk entity identifier (its type, e.g. "Player") — the key you switch on to decide what to spawn. */
145
+ id: string;
146
+ /** Entity position in pixels (LDtk px, pivot-adjusted per the parser convention). */
147
+ x: number;
148
+ y: number;
149
+ /** Entity box size in pixels. */
150
+ width: number;
151
+ height: number;
152
+ /** Custom field instances, keyed by field identifier. */
153
+ fields: Record<string, unknown>;
154
+ }
122
155
  /**
123
- * Parse a Tiled .tmj (JSON) map into TilemapData.
124
- * Supports orthogonal maps with tile layers and object layers.
156
+ * Extract every LDtk entity from a parsed map as a flat, spawn-ready list —
157
+ * the LDtk-entity → ECS spawn convention. Walks all object layers; tile layers are skipped.
125
158
  */
126
- declare function parseTiled(json: unknown): TilemapData;
127
-
159
+ declare function parseLDtkEntities(map: TilemapData): LDtkEntity[];
128
160
  /**
129
161
  * Parse an LDtk JSON project into an array of TilemapData (one per level).
130
162
  * Returns all levels with their tile and entity layers.
163
+ * Throws TilemapParseError for unsupported formats (externalLevels, invalid input).
131
164
  */
132
- declare function parseLDtk(json: unknown): TilemapData[];
133
-
165
+ declare function parseLDtk(json: unknown, options?: ParseLDtkOptions): TilemapData[];
166
+ //#endregion
167
+ //#region src/parse-error.d.ts
168
+ /** Thrown by parseTiled/parseLDtk when a map uses an unsupported format or feature. */
169
+ declare class TilemapParseError extends Error {
170
+ constructor(message: string);
171
+ }
172
+ //#endregion
173
+ //#region src/tiled-parser.d.ts
134
174
  /**
135
- * TilemapRenderer — draws a tilemap with viewport culling.
136
- * Only tiles visible to the camera are submitted to the SpriteBatch.
175
+ * Parse a Tiled .tmj (JSON) map into TilemapData.
176
+ * Supports orthogonal maps with CSV tile layers and object layers.
177
+ * Throws TilemapParseError for unsupported formats (infinite, non-orthogonal,
178
+ * external tilesets, base64/compressed/chunked layer data).
137
179
  */
138
- declare class TilemapRenderer {
139
- /** The tilemap data to render. */
140
- readonly map: TilemapData;
141
- /** SpriteBatch used for batched tile rendering. */
142
- readonly batch: SpriteBatch;
143
- /** Map from tileset name → SkImage. Set via setTilesetImage(). */
144
- private _images;
145
- /** Reusable bounds rect — avoids per-frame allocation from camera.getBounds(). */
146
- private _boundsBuf;
147
- constructor(map: TilemapData);
148
- /** Associate a loaded image with a tileset by name. */
149
- setTilesetImage(tilesetName: string, image: SkImageLike): void;
150
- /**
151
- * Queue visible tile layers into a caller-owned `batch` using the camera for
152
- * culling. Does NOT flush — the caller owns the single flush so tiles z-order
153
- * under sprites. Zero-alloc: reuses the internal bounds buffer.
154
- */
155
- drawInto(camera: Camera2D, batch: SpriteBatch): void;
156
- /**
157
- * Draw visible tile layers using the camera for culling.
158
- * Call `flush` on a callback to issue the actual draw calls.
159
- * Delegates to `drawInto` so culling and queuing logic live in one place.
160
- */
161
- draw(camera: Camera2D, flush: (batch: SpriteBatch) => void): void;
162
- private _drawTileLayer;
163
- }
164
-
180
+ declare function parseTiled(json: unknown): TilemapData;
181
+ //#endregion
182
+ //#region src/tilemap-collider.d.ts
165
183
  /** A collision body extracted from a solid tile. */
166
184
  interface TileCollider {
167
- /** World position of the tile center. */
168
- x: number;
169
- y: number;
170
- /** AABB collision shape. */
171
- shape: AABBShape;
172
- /** Grid column. */
173
- col: number;
174
- /** Grid row. */
175
- row: number;
176
- /** The tile GID. */
177
- gid: number;
185
+ /** World position of the tile center. */
186
+ x: number;
187
+ y: number;
188
+ /** AABB collision shape. */
189
+ shape: AABBShape;
190
+ /** Grid column. */
191
+ col: number;
192
+ /** Grid row. */
193
+ row: number;
194
+ /** The tile GID (flip bits stripped — AABB is symmetric, downstream lookups want the real id). */
195
+ gid: number;
178
196
  }
179
197
  /**
180
198
  * Extract collision shapes from solid tiles in a tilemap.
@@ -185,5 +203,38 @@ interface TileCollider {
185
203
  * @returns Array of TileCollider bodies.
186
204
  */
187
205
  declare function extractColliders(map: TilemapData, layerName?: string): TileCollider[];
188
-
189
- export { type MapLayer, type MapObject, type ObjectLayer, type TileCollider, type TileData, type TileLayer, type TilemapData, TilemapRenderer, type TilesetRef, createTilemapData, extractColliders, getTileAt, getTileSourceRect, getTilesetForGid, parseLDtk, parseTiled };
206
+ //#endregion
207
+ //#region src/tilemap-renderer.d.ts
208
+ /**
209
+ * TilemapRenderer — draws a tilemap with viewport culling.
210
+ * Only tiles visible to the camera are submitted to the SpriteBatch.
211
+ */
212
+ declare class TilemapRenderer {
213
+ /** The tilemap data to render. */
214
+ readonly map: TilemapData;
215
+ /** SpriteBatch used for batched tile rendering. */
216
+ readonly batch: SpriteBatch;
217
+ /** Map from tileset name → SkImage. Set via setTilesetImage(). */
218
+ private _images;
219
+ /** Reusable bounds rect — avoids per-frame allocation from camera.getBounds(). */
220
+ private _boundsBuf;
221
+ constructor(map: TilemapData);
222
+ /** Associate a loaded image with a tileset by name. */
223
+ setTilesetImage(tilesetName: string, image: SkImageLike): void;
224
+ /**
225
+ * Queue visible tile layers into a caller-owned `batch` using the camera for
226
+ * culling. Does NOT flush — the caller owns the single flush so tiles z-order
227
+ * under sprites. Zero-alloc: reuses the internal bounds buffer.
228
+ */
229
+ drawInto(camera: Camera2D, batch: SpriteBatch): void;
230
+ /**
231
+ * Draw visible tile layers using the camera for culling.
232
+ * Call `flush` on a callback to issue the actual draw calls.
233
+ * Delegates to `drawInto` so culling and queuing logic live in one place.
234
+ */
235
+ draw(camera: Camera2D, flush: (batch: SpriteBatch) => void): void;
236
+ private _drawTileLayer;
237
+ }
238
+ //#endregion
239
+ export { GID_FLIP_MASK, type LDtkEntity, type MapLayer, type MapObject, type ObjectLayer, type ParseLDtkOptions, type TileCollider, type TileData, type TileFlipFlags, type TileLayer, type TilemapData, TilemapParseError, TilemapRenderer, type TilesetRef, createTilemapData, decodeGidFlags, extractColliders, getTileAt, getTileSourceRect, getTilesetForGid, parseLDtk, parseLDtkEntities, parseTiled, stripGidFlags };
240
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../src/tilemap-data.ts","../src/ldtk-parser.ts","../src/parse-error.ts","../src/tiled-parser.ts","../src/tilemap-collider.ts","../src/tilemap-renderer.ts"],"mappings":";;;;;;cAGa;;UAOI;EACf;EACA;EACA;;;iBAIc,cAAc;;;;;;iBASd,eAAe,cAAc;;UAS5B;;EAEf;;EAEA;;EAEA,YAAY;;;UAIG;WACN;;EAET;;EAEA;;EAEA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