@flare-engine/cli 0.2.1 → 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/LICENSE +201 -21
- package/NOTICE +2 -0
- package/README.md +5 -4
- package/dist/bin.js +76 -326
- package/dist/bin.js.map +1 -1
- package/dist/bin.mjs +74 -72
- package/dist/bin.mjs.map +1 -1
- package/dist/index.d.mts +86 -80
- package/dist/index.d.mts.map +1 -0
- package/dist/index.d.ts +86 -80
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -272
- package/dist/index.mjs +2 -9
- package/dist/tiled-to-flare-CEdmaHp_.js +273 -0
- package/dist/tiled-to-flare-CEdmaHp_.js.map +1 -0
- package/dist/tiled-to-flare-Cc-ofNer.mjs +239 -0
- package/dist/tiled-to-flare-Cc-ofNer.mjs.map +1 -0
- package/package.json +12 -7
- package/dist/chunk-SUMZ7TVL.mjs +0 -235
- package/dist/chunk-SUMZ7TVL.mjs.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/index.mjs.map +0 -1
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
import { mkdir, readFile, readdir, writeFile } from "node:fs/promises";
|
|
2
|
+
import { basename, dirname, extname, isAbsolute, join, resolve } from "node:path";
|
|
3
|
+
import { MaxRectsPacker } from "maxrects-packer";
|
|
4
|
+
import sharp from "sharp";
|
|
5
|
+
//#region src/pack-atlas.ts
|
|
6
|
+
function absPath$1(p) {
|
|
7
|
+
return isAbsolute(p) ? p : resolve(process.cwd(), p);
|
|
8
|
+
}
|
|
9
|
+
async function listPngs(dir) {
|
|
10
|
+
const entries = await readdir(dir, { withFileTypes: true });
|
|
11
|
+
const result = [];
|
|
12
|
+
for (const e of entries) {
|
|
13
|
+
if (!e.isFile()) continue;
|
|
14
|
+
if (extname(e.name).toLowerCase() === ".png") result.push(join(dir, e.name));
|
|
15
|
+
}
|
|
16
|
+
result.sort();
|
|
17
|
+
return result;
|
|
18
|
+
}
|
|
19
|
+
async function readSprite(path) {
|
|
20
|
+
const meta = await sharp(path).metadata();
|
|
21
|
+
if (!meta.width || !meta.height) throw new Error(`[pack-atlas] unable to read dimensions for ${path}`);
|
|
22
|
+
return {
|
|
23
|
+
name: basename(path, extname(path)),
|
|
24
|
+
path,
|
|
25
|
+
width: meta.width,
|
|
26
|
+
height: meta.height
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Pack a directory of PNG sprites into a single TexturePacker-compatible
|
|
31
|
+
* atlas (JSON + image) consumable by `@flare-engine/assets`.
|
|
32
|
+
*/
|
|
33
|
+
async function packAtlas(opts) {
|
|
34
|
+
const inputDir = absPath$1(opts.input);
|
|
35
|
+
const outDir = absPath$1(opts.out ?? opts.input);
|
|
36
|
+
const name = opts.name ?? "atlas";
|
|
37
|
+
const padding = opts.padding ?? 2;
|
|
38
|
+
const maxSize = opts.maxSize ?? 2048;
|
|
39
|
+
const format = opts.format ?? "png";
|
|
40
|
+
await mkdir(outDir, { recursive: true });
|
|
41
|
+
const files = await listPngs(inputDir);
|
|
42
|
+
if (files.length === 0) throw new Error(`[pack-atlas] no .png files found in ${inputDir}`);
|
|
43
|
+
const sprites = [];
|
|
44
|
+
for (const f of files) sprites.push(await readSprite(f));
|
|
45
|
+
const packer = new MaxRectsPacker(maxSize, maxSize, padding, {
|
|
46
|
+
smart: true,
|
|
47
|
+
pot: false,
|
|
48
|
+
square: false,
|
|
49
|
+
allowRotation: false,
|
|
50
|
+
tag: false,
|
|
51
|
+
border: 0
|
|
52
|
+
});
|
|
53
|
+
for (const s of sprites) packer.add(s.width, s.height, s);
|
|
54
|
+
if (packer.bins.length !== 1) throw new Error(`[pack-atlas] sprites do not fit in a single ${maxSize}x${maxSize} atlas (got ${packer.bins.length} bins)`);
|
|
55
|
+
const bin = packer.bins[0];
|
|
56
|
+
if (!bin) throw new Error("[pack-atlas] packing produced no bin");
|
|
57
|
+
const atlasWidth = bin.width;
|
|
58
|
+
const atlasHeight = bin.height;
|
|
59
|
+
const frames = [];
|
|
60
|
+
const composites = [];
|
|
61
|
+
for (const rect of bin.rects) {
|
|
62
|
+
const sprite = rect.data;
|
|
63
|
+
const buf = await readFile(sprite.path);
|
|
64
|
+
composites.push({
|
|
65
|
+
input: buf,
|
|
66
|
+
left: rect.x,
|
|
67
|
+
top: rect.y
|
|
68
|
+
});
|
|
69
|
+
frames.push({
|
|
70
|
+
name: sprite.name,
|
|
71
|
+
x: rect.x,
|
|
72
|
+
y: rect.y,
|
|
73
|
+
w: sprite.width,
|
|
74
|
+
h: sprite.height
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
frames.sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0);
|
|
78
|
+
const imageName = `${name}.${format}`;
|
|
79
|
+
const jsonName = `${name}.json`;
|
|
80
|
+
const imagePath = join(outDir, imageName);
|
|
81
|
+
const jsonPath = join(outDir, jsonName);
|
|
82
|
+
let pipeline = sharp({ create: {
|
|
83
|
+
width: atlasWidth,
|
|
84
|
+
height: atlasHeight,
|
|
85
|
+
channels: 4,
|
|
86
|
+
background: {
|
|
87
|
+
r: 0,
|
|
88
|
+
g: 0,
|
|
89
|
+
b: 0,
|
|
90
|
+
alpha: 0
|
|
91
|
+
}
|
|
92
|
+
} }).composite(composites);
|
|
93
|
+
pipeline = format === "webp" ? pipeline.webp() : pipeline.png();
|
|
94
|
+
await pipeline.toFile(imagePath);
|
|
95
|
+
const jsonFrames = {};
|
|
96
|
+
for (const f of frames) jsonFrames[f.name] = { frame: {
|
|
97
|
+
x: f.x,
|
|
98
|
+
y: f.y,
|
|
99
|
+
w: f.w,
|
|
100
|
+
h: f.h
|
|
101
|
+
} };
|
|
102
|
+
await writeFile(jsonPath, `${JSON.stringify({
|
|
103
|
+
frames: jsonFrames,
|
|
104
|
+
meta: {
|
|
105
|
+
app: "@flare-engine/cli",
|
|
106
|
+
version: "1.0",
|
|
107
|
+
image: imageName,
|
|
108
|
+
format: "RGBA8888",
|
|
109
|
+
size: {
|
|
110
|
+
w: atlasWidth,
|
|
111
|
+
h: atlasHeight
|
|
112
|
+
},
|
|
113
|
+
scale: "1"
|
|
114
|
+
}
|
|
115
|
+
}, null, 2)}\n`, "utf8");
|
|
116
|
+
return {
|
|
117
|
+
jsonPath,
|
|
118
|
+
imagePath,
|
|
119
|
+
frames,
|
|
120
|
+
width: atlasWidth,
|
|
121
|
+
height: atlasHeight
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
//#endregion
|
|
125
|
+
//#region src/tiled-to-flare.ts
|
|
126
|
+
function absPath(p, base) {
|
|
127
|
+
return isAbsolute(p) ? p : resolve(base, p);
|
|
128
|
+
}
|
|
129
|
+
function propsToRecord(props) {
|
|
130
|
+
const out = {};
|
|
131
|
+
if (!props) return out;
|
|
132
|
+
for (const p of props) out[p.name] = p.value;
|
|
133
|
+
return out;
|
|
134
|
+
}
|
|
135
|
+
async function resolveTileset(ref, mapDir, embed) {
|
|
136
|
+
let data = ref;
|
|
137
|
+
if (ref.source && embed) {
|
|
138
|
+
const raw = await readFile(absPath(ref.source, mapDir), "utf8");
|
|
139
|
+
data = {
|
|
140
|
+
...JSON.parse(raw),
|
|
141
|
+
firstgid: ref.firstgid
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
return {
|
|
145
|
+
name: data.name ?? "tileset",
|
|
146
|
+
firstgid: data.firstgid,
|
|
147
|
+
tilewidth: data.tilewidth ?? 0,
|
|
148
|
+
tileheight: data.tileheight ?? 0,
|
|
149
|
+
columns: data.columns ?? 0,
|
|
150
|
+
tilecount: data.tilecount ?? 0,
|
|
151
|
+
image: data.image ?? "",
|
|
152
|
+
imagewidth: data.imagewidth ?? 0,
|
|
153
|
+
imageheight: data.imageheight ?? 0,
|
|
154
|
+
...ref.source && !embed ? { source: ref.source } : {}
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
function flattenLayers(layers) {
|
|
158
|
+
const out = [];
|
|
159
|
+
const walk = (ls) => {
|
|
160
|
+
for (const l of ls) if (l.type === "group") {
|
|
161
|
+
const inner = l.layers;
|
|
162
|
+
if (inner) walk(inner);
|
|
163
|
+
} else out.push(l);
|
|
164
|
+
};
|
|
165
|
+
walk(layers);
|
|
166
|
+
return out;
|
|
167
|
+
}
|
|
168
|
+
function convertLayer(layer) {
|
|
169
|
+
const visible = layer.visible !== false;
|
|
170
|
+
const opacity = typeof layer.opacity === "number" ? layer.opacity : 1;
|
|
171
|
+
if (layer.type === "tilelayer") return {
|
|
172
|
+
kind: "tile",
|
|
173
|
+
name: layer.name,
|
|
174
|
+
width: layer.width ?? 0,
|
|
175
|
+
height: layer.height ?? 0,
|
|
176
|
+
data: layer.data ?? [],
|
|
177
|
+
visible,
|
|
178
|
+
opacity
|
|
179
|
+
};
|
|
180
|
+
if (layer.type === "objectgroup") {
|
|
181
|
+
const objects = (layer.objects ?? []).map((o) => ({
|
|
182
|
+
id: o.id,
|
|
183
|
+
name: o.name ?? "",
|
|
184
|
+
type: o.type ?? o.class ?? "",
|
|
185
|
+
x: o.x,
|
|
186
|
+
y: o.y,
|
|
187
|
+
width: o.width,
|
|
188
|
+
height: o.height,
|
|
189
|
+
properties: propsToRecord(o.properties)
|
|
190
|
+
}));
|
|
191
|
+
return {
|
|
192
|
+
kind: "object",
|
|
193
|
+
name: layer.name,
|
|
194
|
+
objects,
|
|
195
|
+
visible,
|
|
196
|
+
opacity
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
return null;
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Convert a Tiled `.tmj` JSON map into Flare's normalized runtime format.
|
|
203
|
+
* Flattens group layers, resolves external tilesets (when `embedTilesets`),
|
|
204
|
+
* and drops image/editor-only layers.
|
|
205
|
+
*/
|
|
206
|
+
async function convertTiled(opts) {
|
|
207
|
+
const inputPath = absPath(opts.input, process.cwd());
|
|
208
|
+
const outPath = opts.out ? absPath(opts.out, process.cwd()) : join(dirname(inputPath), `${basename(inputPath).replace(/\.(tmj|json)$/i, "")}.flare.json`);
|
|
209
|
+
const embed = opts.embedTilesets !== false;
|
|
210
|
+
const raw = await readFile(inputPath, "utf8");
|
|
211
|
+
const parsed = JSON.parse(raw);
|
|
212
|
+
const mapDir = dirname(inputPath);
|
|
213
|
+
const tilesets = [];
|
|
214
|
+
for (const ref of parsed.tilesets ?? []) tilesets.push(await resolveTileset(ref, mapDir, embed));
|
|
215
|
+
const layers = [];
|
|
216
|
+
for (const l of flattenLayers(parsed.layers ?? [])) {
|
|
217
|
+
const converted = convertLayer(l);
|
|
218
|
+
if (converted) layers.push(converted);
|
|
219
|
+
}
|
|
220
|
+
const map = {
|
|
221
|
+
width: parsed.width,
|
|
222
|
+
height: parsed.height,
|
|
223
|
+
tilewidth: parsed.tilewidth,
|
|
224
|
+
tileheight: parsed.tileheight,
|
|
225
|
+
tilesets,
|
|
226
|
+
layers
|
|
227
|
+
};
|
|
228
|
+
await writeFile(outPath, `${JSON.stringify(map, null, 2)}\n`, "utf8");
|
|
229
|
+
return {
|
|
230
|
+
outPath,
|
|
231
|
+
layers,
|
|
232
|
+
tilesets,
|
|
233
|
+
map
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
//#endregion
|
|
237
|
+
export { packAtlas as n, convertTiled as t };
|
|
238
|
+
|
|
239
|
+
//# sourceMappingURL=tiled-to-flare-Cc-ofNer.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tiled-to-flare-Cc-ofNer.mjs","names":["absPath"],"sources":["../src/pack-atlas.ts","../src/tiled-to-flare.ts"],"sourcesContent":["import { mkdir, readdir, readFile, writeFile } from 'node:fs/promises';\nimport { basename, extname, isAbsolute, join, resolve } from 'node:path';\nimport { MaxRectsPacker } from 'maxrects-packer';\nimport sharp from 'sharp';\nimport type { AtlasFrame, AtlasPackerOptions, AtlasResult } from './types.js';\n\ninterface SpriteInput {\n name: string;\n path: string;\n width: number;\n height: number;\n}\n\nfunction absPath(p: string): string {\n return isAbsolute(p) ? p : resolve(process.cwd(), p);\n}\n\nasync function listPngs(dir: string): Promise<string[]> {\n const entries = await readdir(dir, { withFileTypes: true });\n const result: string[] = [];\n for (const e of entries) {\n if (!e.isFile()) continue;\n if (extname(e.name).toLowerCase() === '.png') result.push(join(dir, e.name));\n }\n result.sort();\n return result;\n}\n\nasync function readSprite(path: string): Promise<SpriteInput> {\n const meta = await sharp(path).metadata();\n if (!meta.width || !meta.height) {\n throw new Error(`[pack-atlas] unable to read dimensions for ${path}`);\n }\n return {\n name: basename(path, extname(path)),\n path,\n width: meta.width,\n height: meta.height,\n };\n}\n\n/**\n * Pack a directory of PNG sprites into a single TexturePacker-compatible\n * atlas (JSON + image) consumable by `@flare-engine/assets`.\n */\nexport async function packAtlas(opts: AtlasPackerOptions): Promise<AtlasResult> {\n const inputDir = absPath(opts.input);\n const outDir = absPath(opts.out ?? opts.input);\n const name = opts.name ?? 'atlas';\n const padding = opts.padding ?? 2;\n const maxSize = opts.maxSize ?? 2048;\n const format = opts.format ?? 'png';\n\n await mkdir(outDir, { recursive: true });\n\n const files = await listPngs(inputDir);\n if (files.length === 0) {\n throw new Error(`[pack-atlas] no .png files found in ${inputDir}`);\n }\n\n const sprites: SpriteInput[] = [];\n for (const f of files) sprites.push(await readSprite(f));\n\n const packer = new MaxRectsPacker(maxSize, maxSize, padding, {\n smart: true,\n pot: false,\n square: false,\n allowRotation: false,\n tag: false,\n border: 0,\n });\n for (const s of sprites) packer.add(s.width, s.height, s);\n\n if (packer.bins.length !== 1) {\n throw new Error(\n `[pack-atlas] sprites do not fit in a single ${maxSize}x${maxSize} atlas (got ${packer.bins.length} bins)`,\n );\n }\n const bin = packer.bins[0];\n if (!bin) throw new Error('[pack-atlas] packing produced no bin');\n\n const atlasWidth = bin.width;\n const atlasHeight = bin.height;\n\n const frames: AtlasFrame[] = [];\n const composites: { input: Buffer; left: number; top: number }[] = [];\n\n for (const rect of bin.rects) {\n const sprite = rect.data as SpriteInput;\n const buf = await readFile(sprite.path);\n composites.push({ input: buf, left: rect.x, top: rect.y });\n frames.push({\n name: sprite.name,\n x: rect.x,\n y: rect.y,\n w: sprite.width,\n h: sprite.height,\n });\n }\n frames.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));\n\n const imageName = `${name}.${format}`;\n const jsonName = `${name}.json`;\n const imagePath = join(outDir, imageName);\n const jsonPath = join(outDir, jsonName);\n\n let pipeline = sharp({\n create: {\n width: atlasWidth,\n height: atlasHeight,\n channels: 4,\n background: { r: 0, g: 0, b: 0, alpha: 0 },\n },\n }).composite(composites);\n pipeline = format === 'webp' ? pipeline.webp() : pipeline.png();\n await pipeline.toFile(imagePath);\n\n // TexturePacker-compatible JSON (hash format).\n const jsonFrames: Record<string, { frame: { x: number; y: number; w: number; h: number } }> = {};\n for (const f of frames) {\n jsonFrames[f.name] = { frame: { x: f.x, y: f.y, w: f.w, h: f.h } };\n }\n const jsonDoc = {\n frames: jsonFrames,\n meta: {\n app: '@flare-engine/cli',\n version: '1.0',\n image: imageName,\n format: 'RGBA8888',\n size: { w: atlasWidth, h: atlasHeight },\n scale: '1',\n },\n };\n await writeFile(jsonPath, `${JSON.stringify(jsonDoc, null, 2)}\\n`, 'utf8');\n\n return {\n jsonPath,\n imagePath,\n frames,\n width: atlasWidth,\n height: atlasHeight,\n };\n}\n","import { readFile, writeFile } from 'node:fs/promises';\nimport { basename, dirname, isAbsolute, join, resolve } from 'node:path';\nimport type {\n FlareLayer,\n FlareMap,\n FlareObject,\n FlareObjectLayer,\n FlareTileLayer,\n FlareTileset,\n TiledConvertOptions,\n TiledResult,\n} from './types.js';\n\ninterface TiledProperty {\n name: string;\n type?: string;\n value: string | number | boolean;\n}\n\ninterface TiledObject {\n id: number;\n name?: string;\n type?: string;\n class?: string;\n x: number;\n y: number;\n width: number;\n height: number;\n visible?: boolean;\n properties?: TiledProperty[];\n}\n\ninterface TiledLayer {\n type: 'tilelayer' | 'objectgroup' | 'imagelayer' | 'group';\n name: string;\n visible?: boolean;\n opacity?: number;\n width?: number;\n height?: number;\n data?: number[];\n objects?: TiledObject[];\n}\n\ninterface TiledTilesetRef {\n firstgid: number;\n source?: string;\n name?: string;\n tilewidth?: number;\n tileheight?: number;\n columns?: number;\n tilecount?: number;\n image?: string;\n imagewidth?: number;\n imageheight?: number;\n}\n\ninterface TiledMap {\n width: number;\n height: number;\n tilewidth: number;\n tileheight: number;\n layers: TiledLayer[];\n tilesets: TiledTilesetRef[];\n}\n\nfunction absPath(p: string, base: string): string {\n return isAbsolute(p) ? p : resolve(base, p);\n}\n\nfunction propsToRecord(\n props: TiledProperty[] | undefined,\n): Record<string, string | number | boolean> {\n const out: Record<string, string | number | boolean> = {};\n if (!props) return out;\n for (const p of props) out[p.name] = p.value;\n return out;\n}\n\nasync function resolveTileset(\n ref: TiledTilesetRef,\n mapDir: string,\n embed: boolean,\n): Promise<FlareTileset> {\n let data: TiledTilesetRef = ref;\n if (ref.source && embed) {\n const srcPath = absPath(ref.source, mapDir);\n const raw = await readFile(srcPath, 'utf8');\n const external = JSON.parse(raw) as TiledTilesetRef;\n data = { ...external, firstgid: ref.firstgid };\n }\n return {\n name: data.name ?? 'tileset',\n firstgid: data.firstgid,\n tilewidth: data.tilewidth ?? 0,\n tileheight: data.tileheight ?? 0,\n columns: data.columns ?? 0,\n tilecount: data.tilecount ?? 0,\n image: data.image ?? '',\n imagewidth: data.imagewidth ?? 0,\n imageheight: data.imageheight ?? 0,\n ...(ref.source && !embed ? { source: ref.source } : {}),\n };\n}\n\nfunction flattenLayers(layers: TiledLayer[]): TiledLayer[] {\n const out: TiledLayer[] = [];\n const walk = (ls: TiledLayer[]): void => {\n for (const l of ls) {\n if (l.type === 'group') {\n const inner = (l as unknown as { layers?: TiledLayer[] }).layers;\n if (inner) walk(inner);\n } else {\n out.push(l);\n }\n }\n };\n walk(layers);\n return out;\n}\n\nfunction convertLayer(layer: TiledLayer): FlareLayer | null {\n const visible = layer.visible !== false;\n const opacity = typeof layer.opacity === 'number' ? layer.opacity : 1;\n if (layer.type === 'tilelayer') {\n const tile: FlareTileLayer = {\n kind: 'tile',\n name: layer.name,\n width: layer.width ?? 0,\n height: layer.height ?? 0,\n data: layer.data ?? [],\n visible,\n opacity,\n };\n return tile;\n }\n if (layer.type === 'objectgroup') {\n const objects: FlareObject[] = (layer.objects ?? []).map((o) => ({\n id: o.id,\n name: o.name ?? '',\n type: o.type ?? o.class ?? '',\n x: o.x,\n y: o.y,\n width: o.width,\n height: o.height,\n properties: propsToRecord(o.properties),\n }));\n const obj: FlareObjectLayer = {\n kind: 'object',\n name: layer.name,\n objects,\n visible,\n opacity,\n };\n return obj;\n }\n return null;\n}\n\n/**\n * Convert a Tiled `.tmj` JSON map into Flare's normalized runtime format.\n * Flattens group layers, resolves external tilesets (when `embedTilesets`),\n * and drops image/editor-only layers.\n */\nexport async function convertTiled(opts: TiledConvertOptions): Promise<TiledResult> {\n const inputPath = absPath(opts.input, process.cwd());\n const outPath = opts.out\n ? absPath(opts.out, process.cwd())\n : join(dirname(inputPath), `${basename(inputPath).replace(/\\.(tmj|json)$/i, '')}.flare.json`);\n const embed = opts.embedTilesets !== false;\n\n const raw = await readFile(inputPath, 'utf8');\n const parsed = JSON.parse(raw) as TiledMap;\n const mapDir = dirname(inputPath);\n\n const tilesets: FlareTileset[] = [];\n for (const ref of parsed.tilesets ?? []) {\n tilesets.push(await resolveTileset(ref, mapDir, embed));\n }\n\n const layers: FlareLayer[] = [];\n for (const l of flattenLayers(parsed.layers ?? [])) {\n const converted = convertLayer(l);\n if (converted) layers.push(converted);\n }\n\n const map: FlareMap = {\n width: parsed.width,\n height: parsed.height,\n tilewidth: parsed.tilewidth,\n tileheight: parsed.tileheight,\n tilesets,\n layers,\n };\n\n await writeFile(outPath, `${JSON.stringify(map, null, 2)}\\n`, 'utf8');\n return { outPath, layers, tilesets, map };\n}\n"],"mappings":";;;;;AAaA,SAASA,UAAQ,GAAmB;CAClC,OAAO,WAAW,CAAC,IAAI,IAAI,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7B,MAAM,SAAS,QAAQ,SAAS;CAEhC,MAAM,WAA2B,CAAC;CAClC,KAAK,MAAM,OAAO,OAAO,YAAY,CAAC,GACpC,SAAS,KAAK,MAAM,eAAe,KAAK,QAAQ,KAAK,CAAC;CAGxD,MAAM,SAAuB,CAAC;CAC9B,KAAK,MAAM,KAAK,cAAc,OAAO,UAAU,CAAC,CAAC,GAAG;EAClD,MAAM,YAAY,aAAa,CAAC;EAChC,IAAI,WAAW,OAAO,KAAK,SAAS;CACtC;CAEA,MAAM,MAAgB;EACpB,OAAO,OAAO;EACd,QAAQ,OAAO;EACf,WAAW,OAAO;EAClB,YAAY,OAAO;EACnB;EACA;CACF;CAEA,MAAM,UAAU,SAAS,GAAG,KAAK,UAAU,KAAK,MAAM,CAAC,EAAE,KAAK,MAAM;CACpE,OAAO;EAAE;EAAS;EAAQ;EAAU;CAAI;AAC1C"}
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@flare-engine/cli",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Flare engine CLI — pack-atlas, tiled-to-flare, new app scaffolder",
|
|
5
5
|
"keywords": ["flare-engine", "cli", "atlas", "tiled", "sprite-sheet", "scaffold"],
|
|
6
|
-
"license": "
|
|
6
|
+
"license": "Apache-2.0",
|
|
7
7
|
"author": "Grzegorz Otto",
|
|
8
8
|
"homepage": "https://github.com/grzott/flare-engine#readme",
|
|
9
9
|
"bugs": {
|
|
@@ -19,17 +19,22 @@
|
|
|
19
19
|
"types": "dist/index.d.ts",
|
|
20
20
|
"exports": {
|
|
21
21
|
".": {
|
|
22
|
-
"
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
"import": {
|
|
23
|
+
"types": "./dist/index.d.mts",
|
|
24
|
+
"default": "./dist/index.mjs"
|
|
25
|
+
},
|
|
26
|
+
"require": {
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"default": "./dist/index.js"
|
|
29
|
+
}
|
|
25
30
|
}
|
|
26
31
|
},
|
|
27
32
|
"bin": {
|
|
28
33
|
"flare": "dist/bin.js"
|
|
29
34
|
},
|
|
30
|
-
"files": ["dist", "README.md", "LICENSE"],
|
|
35
|
+
"files": ["dist", "README.md", "LICENSE", "NOTICE"],
|
|
31
36
|
"scripts": {
|
|
32
|
-
"build": "
|
|
37
|
+
"build": "tsdown",
|
|
33
38
|
"test": "bun test",
|
|
34
39
|
"lint": "biome check src/",
|
|
35
40
|
"typecheck": "tsc --noEmit"
|
package/dist/chunk-SUMZ7TVL.mjs
DELETED
|
@@ -1,235 +0,0 @@
|
|
|
1
|
-
// src/pack-atlas.ts
|
|
2
|
-
import { mkdir, readFile, readdir, writeFile } from "fs/promises";
|
|
3
|
-
import { basename, extname, isAbsolute, join, resolve } from "path";
|
|
4
|
-
import { MaxRectsPacker } from "maxrects-packer";
|
|
5
|
-
import sharp from "sharp";
|
|
6
|
-
function absPath(p) {
|
|
7
|
-
return isAbsolute(p) ? p : resolve(process.cwd(), p);
|
|
8
|
-
}
|
|
9
|
-
async function listPngs(dir) {
|
|
10
|
-
const entries = await readdir(dir, { withFileTypes: true });
|
|
11
|
-
const result = [];
|
|
12
|
-
for (const e of entries) {
|
|
13
|
-
if (!e.isFile()) continue;
|
|
14
|
-
if (extname(e.name).toLowerCase() === ".png") result.push(join(dir, e.name));
|
|
15
|
-
}
|
|
16
|
-
result.sort();
|
|
17
|
-
return result;
|
|
18
|
-
}
|
|
19
|
-
async function readSprite(path) {
|
|
20
|
-
const meta = await sharp(path).metadata();
|
|
21
|
-
if (!meta.width || !meta.height) {
|
|
22
|
-
throw new Error(`[pack-atlas] unable to read dimensions for ${path}`);
|
|
23
|
-
}
|
|
24
|
-
return {
|
|
25
|
-
name: basename(path, extname(path)),
|
|
26
|
-
path,
|
|
27
|
-
width: meta.width,
|
|
28
|
-
height: meta.height
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
async function packAtlas(opts) {
|
|
32
|
-
const inputDir = absPath(opts.input);
|
|
33
|
-
const outDir = absPath(opts.out ?? opts.input);
|
|
34
|
-
const name = opts.name ?? "atlas";
|
|
35
|
-
const padding = opts.padding ?? 2;
|
|
36
|
-
const maxSize = opts.maxSize ?? 2048;
|
|
37
|
-
const format = opts.format ?? "png";
|
|
38
|
-
await mkdir(outDir, { recursive: true });
|
|
39
|
-
const files = await listPngs(inputDir);
|
|
40
|
-
if (files.length === 0) {
|
|
41
|
-
throw new Error(`[pack-atlas] no .png files found in ${inputDir}`);
|
|
42
|
-
}
|
|
43
|
-
const sprites = [];
|
|
44
|
-
for (const f of files) sprites.push(await readSprite(f));
|
|
45
|
-
const packer = new MaxRectsPacker(maxSize, maxSize, padding, {
|
|
46
|
-
smart: true,
|
|
47
|
-
pot: false,
|
|
48
|
-
square: false,
|
|
49
|
-
allowRotation: false,
|
|
50
|
-
tag: false,
|
|
51
|
-
border: 0
|
|
52
|
-
});
|
|
53
|
-
for (const s of sprites) packer.add(s.width, s.height, s);
|
|
54
|
-
if (packer.bins.length !== 1) {
|
|
55
|
-
throw new Error(
|
|
56
|
-
`[pack-atlas] sprites do not fit in a single ${maxSize}x${maxSize} atlas (got ${packer.bins.length} bins)`
|
|
57
|
-
);
|
|
58
|
-
}
|
|
59
|
-
const bin = packer.bins[0];
|
|
60
|
-
if (!bin) throw new Error("[pack-atlas] packing produced no bin");
|
|
61
|
-
const atlasWidth = bin.width;
|
|
62
|
-
const atlasHeight = bin.height;
|
|
63
|
-
const frames = [];
|
|
64
|
-
const composites = [];
|
|
65
|
-
for (const rect of bin.rects) {
|
|
66
|
-
const sprite = rect.data;
|
|
67
|
-
const buf = await readFile(sprite.path);
|
|
68
|
-
composites.push({ input: buf, left: rect.x, top: rect.y });
|
|
69
|
-
frames.push({
|
|
70
|
-
name: sprite.name,
|
|
71
|
-
x: rect.x,
|
|
72
|
-
y: rect.y,
|
|
73
|
-
w: sprite.width,
|
|
74
|
-
h: sprite.height
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
|
-
frames.sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0);
|
|
78
|
-
const imageName = `${name}.${format}`;
|
|
79
|
-
const jsonName = `${name}.json`;
|
|
80
|
-
const imagePath = join(outDir, imageName);
|
|
81
|
-
const jsonPath = join(outDir, jsonName);
|
|
82
|
-
let pipeline = sharp({
|
|
83
|
-
create: {
|
|
84
|
-
width: atlasWidth,
|
|
85
|
-
height: atlasHeight,
|
|
86
|
-
channels: 4,
|
|
87
|
-
background: { r: 0, g: 0, b: 0, alpha: 0 }
|
|
88
|
-
}
|
|
89
|
-
}).composite(composites);
|
|
90
|
-
pipeline = format === "webp" ? pipeline.webp() : pipeline.png();
|
|
91
|
-
await pipeline.toFile(imagePath);
|
|
92
|
-
const jsonFrames = {};
|
|
93
|
-
for (const f of frames) {
|
|
94
|
-
jsonFrames[f.name] = { frame: { x: f.x, y: f.y, w: f.w, h: f.h } };
|
|
95
|
-
}
|
|
96
|
-
const jsonDoc = {
|
|
97
|
-
frames: jsonFrames,
|
|
98
|
-
meta: {
|
|
99
|
-
app: "@flare-engine/cli",
|
|
100
|
-
version: "1.0",
|
|
101
|
-
image: imageName,
|
|
102
|
-
format: "RGBA8888",
|
|
103
|
-
size: { w: atlasWidth, h: atlasHeight },
|
|
104
|
-
scale: "1"
|
|
105
|
-
}
|
|
106
|
-
};
|
|
107
|
-
await writeFile(jsonPath, `${JSON.stringify(jsonDoc, null, 2)}
|
|
108
|
-
`, "utf8");
|
|
109
|
-
return {
|
|
110
|
-
jsonPath,
|
|
111
|
-
imagePath,
|
|
112
|
-
frames,
|
|
113
|
-
width: atlasWidth,
|
|
114
|
-
height: atlasHeight
|
|
115
|
-
};
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
// src/tiled-to-flare.ts
|
|
119
|
-
import { readFile as readFile2, writeFile as writeFile2 } from "fs/promises";
|
|
120
|
-
import { dirname, isAbsolute as isAbsolute2, join as join2, resolve as resolve2 } from "path";
|
|
121
|
-
function absPath2(p, base) {
|
|
122
|
-
return isAbsolute2(p) ? p : resolve2(base, p);
|
|
123
|
-
}
|
|
124
|
-
function propsToRecord(props) {
|
|
125
|
-
const out = {};
|
|
126
|
-
if (!props) return out;
|
|
127
|
-
for (const p of props) out[p.name] = p.value;
|
|
128
|
-
return out;
|
|
129
|
-
}
|
|
130
|
-
async function resolveTileset(ref, mapDir, embed) {
|
|
131
|
-
let data = ref;
|
|
132
|
-
if (ref.source && embed) {
|
|
133
|
-
const srcPath = absPath2(ref.source, mapDir);
|
|
134
|
-
const raw = await readFile2(srcPath, "utf8");
|
|
135
|
-
const external = JSON.parse(raw);
|
|
136
|
-
data = { ...external, firstgid: ref.firstgid };
|
|
137
|
-
}
|
|
138
|
-
return {
|
|
139
|
-
name: data.name ?? "tileset",
|
|
140
|
-
firstgid: data.firstgid,
|
|
141
|
-
tilewidth: data.tilewidth ?? 0,
|
|
142
|
-
tileheight: data.tileheight ?? 0,
|
|
143
|
-
columns: data.columns ?? 0,
|
|
144
|
-
tilecount: data.tilecount ?? 0,
|
|
145
|
-
image: data.image ?? "",
|
|
146
|
-
imagewidth: data.imagewidth ?? 0,
|
|
147
|
-
imageheight: data.imageheight ?? 0
|
|
148
|
-
};
|
|
149
|
-
}
|
|
150
|
-
function flattenLayers(layers) {
|
|
151
|
-
const out = [];
|
|
152
|
-
const walk = (ls) => {
|
|
153
|
-
for (const l of ls) {
|
|
154
|
-
if (l.type === "group") {
|
|
155
|
-
const inner = l.layers;
|
|
156
|
-
if (inner) walk(inner);
|
|
157
|
-
} else {
|
|
158
|
-
out.push(l);
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
};
|
|
162
|
-
walk(layers);
|
|
163
|
-
return out;
|
|
164
|
-
}
|
|
165
|
-
function convertLayer(layer) {
|
|
166
|
-
const visible = layer.visible !== false;
|
|
167
|
-
const opacity = typeof layer.opacity === "number" ? layer.opacity : 1;
|
|
168
|
-
if (layer.type === "tilelayer") {
|
|
169
|
-
const tile = {
|
|
170
|
-
kind: "tile",
|
|
171
|
-
name: layer.name,
|
|
172
|
-
width: layer.width ?? 0,
|
|
173
|
-
height: layer.height ?? 0,
|
|
174
|
-
data: layer.data ?? [],
|
|
175
|
-
visible,
|
|
176
|
-
opacity
|
|
177
|
-
};
|
|
178
|
-
return tile;
|
|
179
|
-
}
|
|
180
|
-
if (layer.type === "objectgroup") {
|
|
181
|
-
const objects = (layer.objects ?? []).map((o) => ({
|
|
182
|
-
id: o.id,
|
|
183
|
-
name: o.name ?? "",
|
|
184
|
-
type: o.type ?? o.class ?? "",
|
|
185
|
-
x: o.x,
|
|
186
|
-
y: o.y,
|
|
187
|
-
width: o.width,
|
|
188
|
-
height: o.height,
|
|
189
|
-
properties: propsToRecord(o.properties)
|
|
190
|
-
}));
|
|
191
|
-
const obj = {
|
|
192
|
-
kind: "object",
|
|
193
|
-
name: layer.name,
|
|
194
|
-
objects,
|
|
195
|
-
visible,
|
|
196
|
-
opacity
|
|
197
|
-
};
|
|
198
|
-
return obj;
|
|
199
|
-
}
|
|
200
|
-
return null;
|
|
201
|
-
}
|
|
202
|
-
async function convertTiled(opts) {
|
|
203
|
-
const inputPath = absPath2(opts.input, process.cwd());
|
|
204
|
-
const outPath = opts.out ? absPath2(opts.out, process.cwd()) : join2(dirname(inputPath), `${inputPath.replace(/\.tmj$|\.json$/i, "")}.flare.json`);
|
|
205
|
-
const embed = opts.embedTilesets !== false;
|
|
206
|
-
const raw = await readFile2(inputPath, "utf8");
|
|
207
|
-
const parsed = JSON.parse(raw);
|
|
208
|
-
const mapDir = dirname(inputPath);
|
|
209
|
-
const tilesets = [];
|
|
210
|
-
for (const ref of parsed.tilesets ?? []) {
|
|
211
|
-
tilesets.push(await resolveTileset(ref, mapDir, embed));
|
|
212
|
-
}
|
|
213
|
-
const layers = [];
|
|
214
|
-
for (const l of flattenLayers(parsed.layers ?? [])) {
|
|
215
|
-
const converted = convertLayer(l);
|
|
216
|
-
if (converted) layers.push(converted);
|
|
217
|
-
}
|
|
218
|
-
const map = {
|
|
219
|
-
width: parsed.width,
|
|
220
|
-
height: parsed.height,
|
|
221
|
-
tilewidth: parsed.tilewidth,
|
|
222
|
-
tileheight: parsed.tileheight,
|
|
223
|
-
tilesets,
|
|
224
|
-
layers
|
|
225
|
-
};
|
|
226
|
-
await writeFile2(outPath, `${JSON.stringify(map, null, 2)}
|
|
227
|
-
`, "utf8");
|
|
228
|
-
return { outPath, layers, tilesets, map };
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
export {
|
|
232
|
-
packAtlas,
|
|
233
|
-
convertTiled
|
|
234
|
-
};
|
|
235
|
-
//# sourceMappingURL=chunk-SUMZ7TVL.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/pack-atlas.ts","../src/tiled-to-flare.ts"],"sourcesContent":["import { mkdir, readFile, readdir, writeFile } from 'node:fs/promises';\nimport { basename, extname, isAbsolute, join, resolve } from 'node:path';\nimport { MaxRectsPacker } from 'maxrects-packer';\nimport sharp from 'sharp';\nimport type { AtlasFrame, AtlasPackerOptions, AtlasResult } from './types.js';\n\ninterface SpriteInput {\n name: string;\n path: string;\n width: number;\n height: number;\n}\n\nfunction absPath(p: string): string {\n return isAbsolute(p) ? p : resolve(process.cwd(), p);\n}\n\nasync function listPngs(dir: string): Promise<string[]> {\n const entries = await readdir(dir, { withFileTypes: true });\n const result: string[] = [];\n for (const e of entries) {\n if (!e.isFile()) continue;\n if (extname(e.name).toLowerCase() === '.png') result.push(join(dir, e.name));\n }\n result.sort();\n return result;\n}\n\nasync function readSprite(path: string): Promise<SpriteInput> {\n const meta = await sharp(path).metadata();\n if (!meta.width || !meta.height) {\n throw new Error(`[pack-atlas] unable to read dimensions for ${path}`);\n }\n return {\n name: basename(path, extname(path)),\n path,\n width: meta.width,\n height: meta.height,\n };\n}\n\n/**\n * Pack a directory of PNG sprites into a single TexturePacker-compatible\n * atlas (JSON + image) consumable by `@flare-engine/assets`.\n */\nexport async function packAtlas(opts: AtlasPackerOptions): Promise<AtlasResult> {\n const inputDir = absPath(opts.input);\n const outDir = absPath(opts.out ?? opts.input);\n const name = opts.name ?? 'atlas';\n const padding = opts.padding ?? 2;\n const maxSize = opts.maxSize ?? 2048;\n const format = opts.format ?? 'png';\n\n await mkdir(outDir, { recursive: true });\n\n const files = await listPngs(inputDir);\n if (files.length === 0) {\n throw new Error(`[pack-atlas] no .png files found in ${inputDir}`);\n }\n\n const sprites: SpriteInput[] = [];\n for (const f of files) sprites.push(await readSprite(f));\n\n const packer = new MaxRectsPacker(maxSize, maxSize, padding, {\n smart: true,\n pot: false,\n square: false,\n allowRotation: false,\n tag: false,\n border: 0,\n });\n for (const s of sprites) packer.add(s.width, s.height, s);\n\n if (packer.bins.length !== 1) {\n throw new Error(\n `[pack-atlas] sprites do not fit in a single ${maxSize}x${maxSize} atlas (got ${packer.bins.length} bins)`,\n );\n }\n const bin = packer.bins[0];\n if (!bin) throw new Error('[pack-atlas] packing produced no bin');\n\n const atlasWidth = bin.width;\n const atlasHeight = bin.height;\n\n const frames: AtlasFrame[] = [];\n const composites: { input: Buffer; left: number; top: number }[] = [];\n\n for (const rect of bin.rects) {\n const sprite = rect.data as SpriteInput;\n const buf = await readFile(sprite.path);\n composites.push({ input: buf, left: rect.x, top: rect.y });\n frames.push({\n name: sprite.name,\n x: rect.x,\n y: rect.y,\n w: sprite.width,\n h: sprite.height,\n });\n }\n frames.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));\n\n const imageName = `${name}.${format}`;\n const jsonName = `${name}.json`;\n const imagePath = join(outDir, imageName);\n const jsonPath = join(outDir, jsonName);\n\n let pipeline = sharp({\n create: {\n width: atlasWidth,\n height: atlasHeight,\n channels: 4,\n background: { r: 0, g: 0, b: 0, alpha: 0 },\n },\n }).composite(composites);\n pipeline = format === 'webp' ? pipeline.webp() : pipeline.png();\n await pipeline.toFile(imagePath);\n\n // TexturePacker-compatible JSON (hash format).\n const jsonFrames: Record<string, { frame: { x: number; y: number; w: number; h: number } }> = {};\n for (const f of frames) {\n jsonFrames[f.name] = { frame: { x: f.x, y: f.y, w: f.w, h: f.h } };\n }\n const jsonDoc = {\n frames: jsonFrames,\n meta: {\n app: '@flare-engine/cli',\n version: '1.0',\n image: imageName,\n format: 'RGBA8888',\n size: { w: atlasWidth, h: atlasHeight },\n scale: '1',\n },\n };\n await writeFile(jsonPath, `${JSON.stringify(jsonDoc, null, 2)}\\n`, 'utf8');\n\n return {\n jsonPath,\n imagePath,\n frames,\n width: atlasWidth,\n height: atlasHeight,\n };\n}\n","import { readFile, writeFile } from 'node:fs/promises';\nimport { dirname, isAbsolute, join, resolve } from 'node:path';\nimport type {\n FlareLayer,\n FlareMap,\n FlareObject,\n FlareObjectLayer,\n FlareTileLayer,\n FlareTileset,\n TiledConvertOptions,\n TiledResult,\n} from './types.js';\n\ninterface TiledProperty {\n name: string;\n type?: string;\n value: string | number | boolean;\n}\n\ninterface TiledObject {\n id: number;\n name?: string;\n type?: string;\n class?: string;\n x: number;\n y: number;\n width: number;\n height: number;\n visible?: boolean;\n properties?: TiledProperty[];\n}\n\ninterface TiledLayer {\n type: 'tilelayer' | 'objectgroup' | 'imagelayer' | 'group';\n name: string;\n visible?: boolean;\n opacity?: number;\n width?: number;\n height?: number;\n data?: number[];\n objects?: TiledObject[];\n}\n\ninterface TiledTilesetRef {\n firstgid: number;\n source?: string;\n name?: string;\n tilewidth?: number;\n tileheight?: number;\n columns?: number;\n tilecount?: number;\n image?: string;\n imagewidth?: number;\n imageheight?: number;\n}\n\ninterface TiledMap {\n width: number;\n height: number;\n tilewidth: number;\n tileheight: number;\n layers: TiledLayer[];\n tilesets: TiledTilesetRef[];\n}\n\nfunction absPath(p: string, base: string): string {\n return isAbsolute(p) ? p : resolve(base, p);\n}\n\nfunction propsToRecord(\n props: TiledProperty[] | undefined,\n): Record<string, string | number | boolean> {\n const out: Record<string, string | number | boolean> = {};\n if (!props) return out;\n for (const p of props) out[p.name] = p.value;\n return out;\n}\n\nasync function resolveTileset(\n ref: TiledTilesetRef,\n mapDir: string,\n embed: boolean,\n): Promise<FlareTileset> {\n let data: TiledTilesetRef = ref;\n if (ref.source && embed) {\n const srcPath = absPath(ref.source, mapDir);\n const raw = await readFile(srcPath, 'utf8');\n const external = JSON.parse(raw) as TiledTilesetRef;\n data = { ...external, firstgid: ref.firstgid };\n }\n return {\n name: data.name ?? 'tileset',\n firstgid: data.firstgid,\n tilewidth: data.tilewidth ?? 0,\n tileheight: data.tileheight ?? 0,\n columns: data.columns ?? 0,\n tilecount: data.tilecount ?? 0,\n image: data.image ?? '',\n imagewidth: data.imagewidth ?? 0,\n imageheight: data.imageheight ?? 0,\n };\n}\n\nfunction flattenLayers(layers: TiledLayer[]): TiledLayer[] {\n const out: TiledLayer[] = [];\n const walk = (ls: TiledLayer[]): void => {\n for (const l of ls) {\n if (l.type === 'group') {\n const inner = (l as unknown as { layers?: TiledLayer[] }).layers;\n if (inner) walk(inner);\n } else {\n out.push(l);\n }\n }\n };\n walk(layers);\n return out;\n}\n\nfunction convertLayer(layer: TiledLayer): FlareLayer | null {\n const visible = layer.visible !== false;\n const opacity = typeof layer.opacity === 'number' ? layer.opacity : 1;\n if (layer.type === 'tilelayer') {\n const tile: FlareTileLayer = {\n kind: 'tile',\n name: layer.name,\n width: layer.width ?? 0,\n height: layer.height ?? 0,\n data: layer.data ?? [],\n visible,\n opacity,\n };\n return tile;\n }\n if (layer.type === 'objectgroup') {\n const objects: FlareObject[] = (layer.objects ?? []).map((o) => ({\n id: o.id,\n name: o.name ?? '',\n type: o.type ?? o.class ?? '',\n x: o.x,\n y: o.y,\n width: o.width,\n height: o.height,\n properties: propsToRecord(o.properties),\n }));\n const obj: FlareObjectLayer = {\n kind: 'object',\n name: layer.name,\n objects,\n visible,\n opacity,\n };\n return obj;\n }\n return null;\n}\n\n/**\n * Convert a Tiled `.tmj` JSON map into Flare's normalized runtime format.\n * Flattens group layers, resolves external tilesets (when `embedTilesets`),\n * and drops image/editor-only layers.\n */\nexport async function convertTiled(opts: TiledConvertOptions): Promise<TiledResult> {\n const inputPath = absPath(opts.input, process.cwd());\n const outPath = opts.out\n ? absPath(opts.out, process.cwd())\n : join(dirname(inputPath), `${inputPath.replace(/\\.tmj$|\\.json$/i, '')}.flare.json`);\n const embed = opts.embedTilesets !== false;\n\n const raw = await readFile(inputPath, 'utf8');\n const parsed = JSON.parse(raw) as TiledMap;\n const mapDir = dirname(inputPath);\n\n const tilesets: FlareTileset[] = [];\n for (const ref of parsed.tilesets ?? []) {\n tilesets.push(await resolveTileset(ref, mapDir, embed));\n }\n\n const layers: FlareLayer[] = [];\n for (const l of flattenLayers(parsed.layers ?? [])) {\n const converted = convertLayer(l);\n if (converted) layers.push(converted);\n }\n\n const map: FlareMap = {\n width: parsed.width,\n height: parsed.height,\n tilewidth: parsed.tilewidth,\n tileheight: parsed.tileheight,\n tilesets,\n layers,\n };\n\n await writeFile(outPath, `${JSON.stringify(map, null, 2)}\\n`, 'utf8');\n return { outPath, layers, tilesets, map };\n}\n"],"mappings":";AAAA,SAAS,OAAO,UAAU,SAAS,iBAAiB;AACpD,SAAS,UAAU,SAAS,YAAY,MAAM,eAAe;AAC7D,SAAS,sBAAsB;AAC/B,OAAO,WAAW;AAUlB,SAAS,QAAQ,GAAmB;AAClC,SAAO,WAAW,CAAC,IAAI,IAAI,QAAQ,QAAQ,IAAI,GAAG,CAAC;AACrD;AAEA,eAAe,SAAS,KAAgC;AACtD,QAAM,UAAU,MAAM,QAAQ,KAAK,EAAE,eAAe,KAAK,CAAC;AAC1D,QAAM,SAAmB,CAAC;AAC1B,aAAW,KAAK,SAAS;AACvB,QAAI,CAAC,EAAE,OAAO,EAAG;AACjB,QAAI,QAAQ,EAAE,IAAI,EAAE,YAAY,MAAM,OAAQ,QAAO,KAAK,KAAK,KAAK,EAAE,IAAI,CAAC;AAAA,EAC7E;AACA,SAAO,KAAK;AACZ,SAAO;AACT;AAE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MAAiD;AAClF,QAAM,YAAYK,SAAQ,KAAK,OAAO,QAAQ,IAAI,CAAC;AACnD,QAAM,UAAU,KAAK,MACjBA,SAAQ,KAAK,KAAK,QAAQ,IAAI,CAAC,IAC/BF,MAAK,QAAQ,SAAS,GAAG,GAAG,UAAU,QAAQ,mBAAmB,EAAE,CAAC,aAAa;AACrF,QAAM,QAAQ,KAAK,kBAAkB;AAErC,QAAM,MAAM,MAAMH,UAAS,WAAW,MAAM;AAC5C,QAAM,SAAS,KAAK,MAAM,GAAG;AAC7B,QAAM,SAAS,QAAQ,SAAS;AAEhC,QAAM,WAA2B,CAAC;AAClC,aAAW,OAAO,OAAO,YAAY,CAAC,GAAG;AACvC,aAAS,KAAK,MAAM,eAAe,KAAK,QAAQ,KAAK,CAAC;AAAA,EACxD;AAEA,QAAM,SAAuB,CAAC;AAC9B,aAAW,KAAK,cAAc,OAAO,UAAU,CAAC,CAAC,GAAG;AAClD,UAAM,YAAY,aAAa,CAAC;AAChC,QAAI,UAAW,QAAO,KAAK,SAAS;AAAA,EACtC;AAEA,QAAM,MAAgB;AAAA,IACpB,OAAO,OAAO;AAAA,IACd,QAAQ,OAAO;AAAA,IACf,WAAW,OAAO;AAAA,IAClB,YAAY,OAAO;AAAA,IACnB;AAAA,IACA;AAAA,EACF;AAEA,QAAMC,WAAU,SAAS,GAAG,KAAK,UAAU,KAAK,MAAM,CAAC,CAAC;AAAA,GAAM,MAAM;AACpE,SAAO,EAAE,SAAS,QAAQ,UAAU,IAAI;AAC1C;","names":["readFile","writeFile","isAbsolute","join","resolve","absPath"]}
|
package/dist/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/pack-atlas.ts","../src/tiled-to-flare.ts"],"sourcesContent":["export type {\n AtlasFrame,\n AtlasPackerOptions,\n AtlasResult,\n FlareLayer,\n FlareMap,\n FlareObject,\n FlareObjectLayer,\n FlareTileLayer,\n FlareTileset,\n TiledConvertOptions,\n TiledResult,\n} from './types.js';\n\nexport { packAtlas } from './pack-atlas.js';\nexport { convertTiled } from './tiled-to-flare.js';\n","import { mkdir, readFile, readdir, writeFile } from 'node:fs/promises';\nimport { basename, extname, isAbsolute, join, resolve } from 'node:path';\nimport { MaxRectsPacker } from 'maxrects-packer';\nimport sharp from 'sharp';\nimport type { AtlasFrame, AtlasPackerOptions, AtlasResult } from './types.js';\n\ninterface SpriteInput {\n name: string;\n path: string;\n width: number;\n height: number;\n}\n\nfunction absPath(p: string): string {\n return isAbsolute(p) ? p : resolve(process.cwd(), p);\n}\n\nasync function listPngs(dir: string): Promise<string[]> {\n const entries = await readdir(dir, { withFileTypes: true });\n const result: string[] = [];\n for (const e of entries) {\n if (!e.isFile()) continue;\n if (extname(e.name).toLowerCase() === '.png') result.push(join(dir, e.name));\n }\n result.sort();\n return result;\n}\n\nasync function readSprite(path: string): Promise<SpriteInput> {\n const meta = await sharp(path).metadata();\n if (!meta.width || !meta.height) {\n throw new Error(`[pack-atlas] unable to read dimensions for ${path}`);\n }\n return {\n name: basename(path, extname(path)),\n path,\n width: meta.width,\n height: meta.height,\n };\n}\n\n/**\n * Pack a directory of PNG sprites into a single TexturePacker-compatible\n * atlas (JSON + image) consumable by `@flare-engine/assets`.\n */\nexport async function packAtlas(opts: AtlasPackerOptions): Promise<AtlasResult> {\n const inputDir = absPath(opts.input);\n const outDir = absPath(opts.out ?? opts.input);\n const name = opts.name ?? 'atlas';\n const padding = opts.padding ?? 2;\n const maxSize = opts.maxSize ?? 2048;\n const format = opts.format ?? 'png';\n\n await mkdir(outDir, { recursive: true });\n\n const files = await listPngs(inputDir);\n if (files.length === 0) {\n throw new Error(`[pack-atlas] no .png files found in ${inputDir}`);\n }\n\n const sprites: SpriteInput[] = [];\n for (const f of files) sprites.push(await readSprite(f));\n\n const packer = new MaxRectsPacker(maxSize, maxSize, padding, {\n smart: true,\n pot: false,\n square: false,\n allowRotation: false,\n tag: false,\n border: 0,\n });\n for (const s of sprites) packer.add(s.width, s.height, s);\n\n if (packer.bins.length !== 1) {\n throw new Error(\n `[pack-atlas] sprites do not fit in a single ${maxSize}x${maxSize} atlas (got ${packer.bins.length} bins)`,\n );\n }\n const bin = packer.bins[0];\n if (!bin) throw new Error('[pack-atlas] packing produced no bin');\n\n const atlasWidth = bin.width;\n const atlasHeight = bin.height;\n\n const frames: AtlasFrame[] = [];\n const composites: { input: Buffer; left: number; top: number }[] = [];\n\n for (const rect of bin.rects) {\n const sprite = rect.data as SpriteInput;\n const buf = await readFile(sprite.path);\n composites.push({ input: buf, left: rect.x, top: rect.y });\n frames.push({\n name: sprite.name,\n x: rect.x,\n y: rect.y,\n w: sprite.width,\n h: sprite.height,\n });\n }\n frames.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));\n\n const imageName = `${name}.${format}`;\n const jsonName = `${name}.json`;\n const imagePath = join(outDir, imageName);\n const jsonPath = join(outDir, jsonName);\n\n let pipeline = sharp({\n create: {\n width: atlasWidth,\n height: atlasHeight,\n channels: 4,\n background: { r: 0, g: 0, b: 0, alpha: 0 },\n },\n }).composite(composites);\n pipeline = format === 'webp' ? pipeline.webp() : pipeline.png();\n await pipeline.toFile(imagePath);\n\n // TexturePacker-compatible JSON (hash format).\n const jsonFrames: Record<string, { frame: { x: number; y: number; w: number; h: number } }> = {};\n for (const f of frames) {\n jsonFrames[f.name] = { frame: { x: f.x, y: f.y, w: f.w, h: f.h } };\n }\n const jsonDoc = {\n frames: jsonFrames,\n meta: {\n app: '@flare-engine/cli',\n version: '1.0',\n image: imageName,\n format: 'RGBA8888',\n size: { w: atlasWidth, h: atlasHeight },\n scale: '1',\n },\n };\n await writeFile(jsonPath, `${JSON.stringify(jsonDoc, null, 2)}\\n`, 'utf8');\n\n return {\n jsonPath,\n imagePath,\n frames,\n width: atlasWidth,\n height: atlasHeight,\n };\n}\n","import { readFile, writeFile } from 'node:fs/promises';\nimport { dirname, isAbsolute, join, resolve } from 'node:path';\nimport type {\n FlareLayer,\n FlareMap,\n FlareObject,\n FlareObjectLayer,\n FlareTileLayer,\n FlareTileset,\n TiledConvertOptions,\n TiledResult,\n} from './types.js';\n\ninterface TiledProperty {\n name: string;\n type?: string;\n value: string | number | boolean;\n}\n\ninterface TiledObject {\n id: number;\n name?: string;\n type?: string;\n class?: string;\n x: number;\n y: number;\n width: number;\n height: number;\n visible?: boolean;\n properties?: TiledProperty[];\n}\n\ninterface TiledLayer {\n type: 'tilelayer' | 'objectgroup' | 'imagelayer' | 'group';\n name: string;\n visible?: boolean;\n opacity?: number;\n width?: number;\n height?: number;\n data?: number[];\n objects?: TiledObject[];\n}\n\ninterface TiledTilesetRef {\n firstgid: number;\n source?: string;\n name?: string;\n tilewidth?: number;\n tileheight?: number;\n columns?: number;\n tilecount?: number;\n image?: string;\n imagewidth?: number;\n imageheight?: number;\n}\n\ninterface TiledMap {\n width: number;\n height: number;\n tilewidth: number;\n tileheight: number;\n layers: TiledLayer[];\n tilesets: TiledTilesetRef[];\n}\n\nfunction absPath(p: string, base: string): string {\n return isAbsolute(p) ? p : resolve(base, p);\n}\n\nfunction propsToRecord(\n props: TiledProperty[] | undefined,\n): Record<string, string | number | boolean> {\n const out: Record<string, string | number | boolean> = {};\n if (!props) return out;\n for (const p of props) out[p.name] = p.value;\n return out;\n}\n\nasync function resolveTileset(\n ref: TiledTilesetRef,\n mapDir: string,\n embed: boolean,\n): Promise<FlareTileset> {\n let data: TiledTilesetRef = ref;\n if (ref.source && embed) {\n const srcPath = absPath(ref.source, mapDir);\n const raw = await readFile(srcPath, 'utf8');\n const external = JSON.parse(raw) as TiledTilesetRef;\n data = { ...external, firstgid: ref.firstgid };\n }\n return {\n name: data.name ?? 'tileset',\n firstgid: data.firstgid,\n tilewidth: data.tilewidth ?? 0,\n tileheight: data.tileheight ?? 0,\n columns: data.columns ?? 0,\n tilecount: data.tilecount ?? 0,\n image: data.image ?? '',\n imagewidth: data.imagewidth ?? 0,\n imageheight: data.imageheight ?? 0,\n };\n}\n\nfunction flattenLayers(layers: TiledLayer[]): TiledLayer[] {\n const out: TiledLayer[] = [];\n const walk = (ls: TiledLayer[]): void => {\n for (const l of ls) {\n if (l.type === 'group') {\n const inner = (l as unknown as { layers?: TiledLayer[] }).layers;\n if (inner) walk(inner);\n } else {\n out.push(l);\n }\n }\n };\n walk(layers);\n return out;\n}\n\nfunction convertLayer(layer: TiledLayer): FlareLayer | null {\n const visible = layer.visible !== false;\n const opacity = typeof layer.opacity === 'number' ? layer.opacity : 1;\n if (layer.type === 'tilelayer') {\n const tile: FlareTileLayer = {\n kind: 'tile',\n name: layer.name,\n width: layer.width ?? 0,\n height: layer.height ?? 0,\n data: layer.data ?? [],\n visible,\n opacity,\n };\n return tile;\n }\n if (layer.type === 'objectgroup') {\n const objects: FlareObject[] = (layer.objects ?? []).map((o) => ({\n id: o.id,\n name: o.name ?? '',\n type: o.type ?? o.class ?? '',\n x: o.x,\n y: o.y,\n width: o.width,\n height: o.height,\n properties: propsToRecord(o.properties),\n }));\n const obj: FlareObjectLayer = {\n kind: 'object',\n name: layer.name,\n objects,\n visible,\n opacity,\n };\n return obj;\n }\n return null;\n}\n\n/**\n * Convert a Tiled `.tmj` JSON map into Flare's normalized runtime format.\n * Flattens group layers, resolves external tilesets (when `embedTilesets`),\n * and drops image/editor-only layers.\n */\nexport async function convertTiled(opts: TiledConvertOptions): Promise<TiledResult> {\n const inputPath = absPath(opts.input, process.cwd());\n const outPath = opts.out\n ? absPath(opts.out, process.cwd())\n : join(dirname(inputPath), `${inputPath.replace(/\\.tmj$|\\.json$/i, '')}.flare.json`);\n const embed = opts.embedTilesets !== false;\n\n const raw = await readFile(inputPath, 'utf8');\n const parsed = JSON.parse(raw) as TiledMap;\n const mapDir = dirname(inputPath);\n\n const tilesets: FlareTileset[] = [];\n for (const ref of parsed.tilesets ?? []) {\n tilesets.push(await resolveTileset(ref, mapDir, embed));\n }\n\n const layers: FlareLayer[] = [];\n for (const l of flattenLayers(parsed.layers ?? [])) {\n const converted = convertLayer(l);\n if (converted) layers.push(converted);\n }\n\n const map: FlareMap = {\n width: parsed.width,\n height: parsed.height,\n tilewidth: parsed.tilewidth,\n tileheight: parsed.tileheight,\n tilesets,\n layers,\n };\n\n await writeFile(outPath, `${JSON.stringify(map, null, 2)}\\n`, 'utf8');\n return { outPath, layers, tilesets, map };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,sBAAoD;AACpD,uBAA6D;AAC7D,6BAA+B;AAC/B,mBAAkB;AAUlB,SAAS,QAAQ,GAAmB;AAClC,aAAO,6BAAW,CAAC,IAAI,QAAI,0BAAQ,QAAQ,IAAI,GAAG,CAAC;AACrD;AAEA,eAAe,SAAS,KAAgC;AACtD,QAAM,UAAU,UAAM,yBAAQ,KAAK,EAAE,eAAe,KAAK,CAAC;AAC1D,QAAM,SAAmB,CAAC;AAC1B,aAAW,KAAK,SAAS;AACvB,QAAI,CAAC,EAAE,OAAO,EAAG;AACjB,YAAI,0BAAQ,EAAE,IAAI,EAAE,YAAY,MAAM,OAAQ,QAAO,SAAK,uBAAK,KAAK,EAAE,IAAI,CAAC;AAAA,EAC7E;AACA,SAAO,KAAK;AACZ,SAAO;AACT;AAEA,eAAe,WAAW,MAAoC;AAC5D,QAAM,OAAO,UAAM,aAAAA,SAAM,IAAI,EAAE,SAAS;AACxC,MAAI,CAAC,KAAK,SAAS,CAAC,KAAK,QAAQ;AAC/B,UAAM,IAAI,MAAM,8CAA8C,IAAI,EAAE;AAAA,EACtE;AACA,SAAO;AAAA,IACL,UAAM,2BAAS,UAAM,0BAAQ,IAAI,CAAC;AAAA,IAClC;AAAA,IACA,OAAO,KAAK;AAAA,IACZ,QAAQ,KAAK;AAAA,EACf;AACF;AAMA,eAAsB,UAAU,MAAgD;AAC9E,QAAM,WAAW,QAAQ,KAAK,KAAK;AACnC,QAAM,SAAS,QAAQ,KAAK,OAAO,KAAK,KAAK;AAC7C,QAAM,OAAO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C;AAAA,EACxD;AAEA,QAAM,SAAuB,CAAC;AAC9B,aAAW,KAAK,cAAc,OAAO,UAAU,CAAC,CAAC,GAAG;AAClD,UAAM,YAAY,aAAa,CAAC;AAChC,QAAI,UAAW,QAAO,KAAK,SAAS;AAAA,EACtC;AAEA,QAAM,MAAgB;AAAA,IACpB,OAAO,OAAO;AAAA,IACd,QAAQ,OAAO;AAAA,IACf,WAAW,OAAO;AAAA,IAClB,YAAY,OAAO;AAAA,IACnB;AAAA,IACA;AAAA,EACF;AAEA,YAAM,4BAAU,SAAS,GAAG,KAAK,UAAU,KAAK,MAAM,CAAC,CAAC;AAAA,GAAM,MAAM;AACpE,SAAO,EAAE,SAAS,QAAQ,UAAU,IAAI;AAC1C;","names":["sharp","import_promises","import_node_path","absPath"]}
|
package/dist/index.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|