@flare-engine/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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Grzegorz Otto
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,27 @@
1
+ # @flare-engine/tilemap
2
+
3
+ Tiled `.tmj` and LDtk parsing + culled rendering + collider extraction. `parseTiled` / `parseLDtk` return a normalized `TilemapData`; `TilemapRenderer` owns its own `SpriteBatch` and only queues tiles inside the camera bounds; `extractColliders` produces collision shapes for `@flare-engine/physics`.
4
+
5
+ ## Install
6
+
7
+ ```sh
8
+ bun add @flare-engine/tilemap
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```ts
14
+ import { parseTiled, TilemapRenderer, extractColliders } from '@flare-engine/tilemap';
15
+
16
+ const data = parseTiled(json);
17
+ const renderer = new TilemapRenderer(data, atlas);
18
+ renderer.draw(camera, (batch) => batch.flush(skia.drawAtlas, gameRenderer));
19
+
20
+ const colliders = extractColliders(data);
21
+ ```
22
+
23
+ See [API spec](../../docs/design/api-specs.md#flare-enginetilemap--8kb-gzip) and [skill reference](../../.github/skills/flare-tilemap/SKILL.md).
24
+
25
+ ## License
26
+
27
+ MIT
@@ -0,0 +1,182 @@
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
+
6
+ /** A single tile in a tilemap grid. */
7
+ 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>;
14
+ }
15
+ /** A layer of tiles in a grid. */
16
+ 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>;
35
+ }
36
+ /** An object in an object layer. */
37
+ 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
+ }
55
+ /** A layer of objects. */
56
+ 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>;
68
+ }
69
+ /** Union of layer types. */
70
+ type MapLayer = TileLayer | ObjectLayer;
71
+ /** Information about a tileset used in the map. */
72
+ 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>;
95
+ }
96
+ /** Complete parsed tilemap data — format-independent. */
97
+ 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>;
112
+ }
113
+ /** Create an empty TilemapData. */
114
+ declare function createTilemapData(width: number, height: number, tileWidth: number, tileHeight: number): TilemapData;
115
+ /** Get a tile GID at a grid position from a tile layer. Returns 0 if out of bounds. */
116
+ declare function getTileAt(layer: TileLayer, col: number, row: number): number;
117
+ /** Get the tileset that owns a given GID. */
118
+ declare function getTilesetForGid(tilesets: TilesetRef[], gid: number): TilesetRef | undefined;
119
+ /** Compute the source rect in a tileset image for a given GID. */
120
+ declare function getTileSourceRect(tileset: TilesetRef, gid: number): Rect;
121
+
122
+ /**
123
+ * Parse a Tiled .tmj (JSON) map into TilemapData.
124
+ * Supports orthogonal maps with tile layers and object layers.
125
+ */
126
+ declare function parseTiled(json: unknown): TilemapData;
127
+
128
+ /**
129
+ * Parse an LDtk JSON project into an array of TilemapData (one per level).
130
+ * Returns all levels with their tile and entity layers.
131
+ */
132
+ declare function parseLDtk(json: unknown): TilemapData[];
133
+
134
+ /**
135
+ * TilemapRenderer — draws a tilemap with viewport culling.
136
+ * Only tiles visible to the camera are submitted to the SpriteBatch.
137
+ */
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
+ * Draw visible tile layers using the camera for culling.
152
+ * Call `flush` on a callback to issue the actual draw calls.
153
+ */
154
+ draw(camera: Camera2D, flush: (batch: SpriteBatch) => void): void;
155
+ private _drawTileLayer;
156
+ }
157
+
158
+ /** A collision body extracted from a solid tile. */
159
+ interface TileCollider {
160
+ /** World position of the tile center. */
161
+ x: number;
162
+ y: number;
163
+ /** AABB collision shape. */
164
+ shape: AABBShape;
165
+ /** Grid column. */
166
+ col: number;
167
+ /** Grid row. */
168
+ row: number;
169
+ /** The tile GID. */
170
+ gid: number;
171
+ }
172
+ /**
173
+ * Extract collision shapes from solid tiles in a tilemap.
174
+ * A tile is considered solid if its tileset marks the local tile ID as solid.
175
+ *
176
+ * @param map The parsed tilemap data.
177
+ * @param layerName Optional: only extract from this layer. If omitted, extracts from all tile layers.
178
+ * @returns Array of TileCollider bodies.
179
+ */
180
+ declare function extractColliders(map: TilemapData, layerName?: string): TileCollider[];
181
+
182
+ 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 };
@@ -0,0 +1,182 @@
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
+
6
+ /** A single tile in a tilemap grid. */
7
+ 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>;
14
+ }
15
+ /** A layer of tiles in a grid. */
16
+ 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>;
35
+ }
36
+ /** An object in an object layer. */
37
+ 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
+ }
55
+ /** A layer of objects. */
56
+ 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>;
68
+ }
69
+ /** Union of layer types. */
70
+ type MapLayer = TileLayer | ObjectLayer;
71
+ /** Information about a tileset used in the map. */
72
+ 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>;
95
+ }
96
+ /** Complete parsed tilemap data — format-independent. */
97
+ 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>;
112
+ }
113
+ /** Create an empty TilemapData. */
114
+ declare function createTilemapData(width: number, height: number, tileWidth: number, tileHeight: number): TilemapData;
115
+ /** Get a tile GID at a grid position from a tile layer. Returns 0 if out of bounds. */
116
+ declare function getTileAt(layer: TileLayer, col: number, row: number): number;
117
+ /** Get the tileset that owns a given GID. */
118
+ declare function getTilesetForGid(tilesets: TilesetRef[], gid: number): TilesetRef | undefined;
119
+ /** Compute the source rect in a tileset image for a given GID. */
120
+ declare function getTileSourceRect(tileset: TilesetRef, gid: number): Rect;
121
+
122
+ /**
123
+ * Parse a Tiled .tmj (JSON) map into TilemapData.
124
+ * Supports orthogonal maps with tile layers and object layers.
125
+ */
126
+ declare function parseTiled(json: unknown): TilemapData;
127
+
128
+ /**
129
+ * Parse an LDtk JSON project into an array of TilemapData (one per level).
130
+ * Returns all levels with their tile and entity layers.
131
+ */
132
+ declare function parseLDtk(json: unknown): TilemapData[];
133
+
134
+ /**
135
+ * TilemapRenderer — draws a tilemap with viewport culling.
136
+ * Only tiles visible to the camera are submitted to the SpriteBatch.
137
+ */
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
+ * Draw visible tile layers using the camera for culling.
152
+ * Call `flush` on a callback to issue the actual draw calls.
153
+ */
154
+ draw(camera: Camera2D, flush: (batch: SpriteBatch) => void): void;
155
+ private _drawTileLayer;
156
+ }
157
+
158
+ /** A collision body extracted from a solid tile. */
159
+ interface TileCollider {
160
+ /** World position of the tile center. */
161
+ x: number;
162
+ y: number;
163
+ /** AABB collision shape. */
164
+ shape: AABBShape;
165
+ /** Grid column. */
166
+ col: number;
167
+ /** Grid row. */
168
+ row: number;
169
+ /** The tile GID. */
170
+ gid: number;
171
+ }
172
+ /**
173
+ * Extract collision shapes from solid tiles in a tilemap.
174
+ * A tile is considered solid if its tileset marks the local tile ID as solid.
175
+ *
176
+ * @param map The parsed tilemap data.
177
+ * @param layerName Optional: only extract from this layer. If omitted, extracts from all tile layers.
178
+ * @returns Array of TileCollider bodies.
179
+ */
180
+ declare function extractColliders(map: TilemapData, layerName?: string): TileCollider[];
181
+
182
+ 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 };