@moku-labs/game 0.15.50 → 0.15.51

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.
@@ -5,78 +5,6 @@ import { pathToFileURL } from "node:url";
5
5
  import { createHash } from "node:crypto";
6
6
  import { mkdir, open, readFile, readdir, rm, rmdir, stat, writeFile } from "node:fs/promises";
7
7
  import { MaxRectsBin, MaxRectsPacker } from "maxrects-packer";
8
- /** Bytes per pixel of the decoded images: RGBA, 8 bits each. */
9
- const CHANNELS = 4;
10
- /**
11
- * Copies the rows of one decoded texture into the pixels of a page. A copy, not a blend: the
12
- * page is transparent everywhere else, so the frame holds exactly the source's pixels.
13
- *
14
- * @param page - The RGBA pixels of the page.
15
- * @param pageWidth - Width of the page.
16
- * @param part - The texture and its place.
17
- */
18
- function blit(page, pageWidth, part) {
19
- const { pixels, x, y } = part;
20
- const rowBytes = pixels.width * CHANNELS;
21
- for (let row = 0; row < pixels.height; row += 1) {
22
- const from = row * rowBytes;
23
- page.set(pixels.data.subarray(from, from + rowBytes), ((y + row) * pageWidth + x) * CHANNELS);
24
- }
25
- }
26
- /**
27
- * Builds the encoder over the `sharp` factory.
28
- *
29
- * @param sharp - The default export of the `sharp` module.
30
- * @returns The encoder.
31
- */
32
- function encoderOf(sharp) {
33
- const webpOptions = {
34
- quality: 80,
35
- alphaQuality: 80
36
- };
37
- return {
38
- version: sharp.versions.sharp ?? "unknown",
39
- decode: async (bytes) => {
40
- const { data, info } = await sharp(bytes).toColourspace("srgb").ensureAlpha().raw().toBuffer({ resolveWithObject: true });
41
- return {
42
- width: info.width,
43
- height: info.height,
44
- data: new Uint8Array(data)
45
- };
46
- },
47
- page: async (width, height, parts) => {
48
- const pixels = new Uint8Array(width * height * CHANNELS);
49
- for (const part of parts) blit(pixels, width, part);
50
- const encoded = await sharp(pixels, { raw: {
51
- width,
52
- height,
53
- channels: CHANNELS
54
- } }).webp(webpOptions).toBuffer();
55
- return new Uint8Array(encoded);
56
- },
57
- webp: async (bytes) => new Uint8Array(await sharp(bytes).webp(webpOptions).toBuffer())
58
- };
59
- }
60
- /**
61
- * Loads `sharp` and builds the encoder. `sharp` is an optional peer dependency of the engine,
62
- * so a game installs it for its pack command only.
63
- *
64
- * @returns The encoder.
65
- * @throws {Error} When `sharp` is not installed, with the command that installs it.
66
- * @example
67
- * ```ts
68
- * const encoder = await loadEncoder();
69
- * encoder.version; // the installed sharp version, such as "0.34.5"
70
- * ```
71
- */
72
- async function loadEncoder() {
73
- try {
74
- return encoderOf((await import("sharp")).default);
75
- } catch (error) {
76
- throw new Error("[game] assets: \"--pack\" needs sharp.\n Run \"bun add -d sharp\".", { cause: error });
77
- }
78
- }
79
- //#endregion
80
8
  //#region src/plugins/assets/scan/pack/layout.ts
81
9
  /**
82
10
  * @file assets packer, build time — where each texture of an atlas group lies on its pages. Pure
@@ -176,6 +104,111 @@ function layoutGroup(bundle, group, members) {
176
104
  problems
177
105
  };
178
106
  }
107
+ /** Bytes per pixel of the decoded images: RGBA, 8 bits each. */
108
+ const CHANNELS = 4;
109
+ /**
110
+ * Copies the rows of one decoded texture into the pixels of a page, then its edge pixels `EXTRUDE` out into the
111
+ * padding around it (F105): a frame drawn at a fractional place samples its own edge there, not the page's clear
112
+ * padding. A copy, not a blend; a pixel outside the page is skipped.
113
+ *
114
+ * @param page - The RGBA pixels of the page.
115
+ * @param size - Width and height of the page.
116
+ * @param size.width - Width of the page.
117
+ * @param size.height - Height of the page.
118
+ * @param part - The texture and its place.
119
+ */
120
+ function blit(page, size, part) {
121
+ const { pixels, x, y } = part;
122
+ const rowBytes = pixels.width * CHANNELS;
123
+ for (let row = 0; row < pixels.height; row += 1) {
124
+ const from = row * rowBytes;
125
+ page.set(pixels.data.subarray(from, from + rowBytes), ((y + row) * size.width + x) * CHANNELS);
126
+ }
127
+ if (pixels.width === 0 || pixels.height === 0) return;
128
+ for (let row = y - 1; row < y + pixels.height + 1; row += 1) {
129
+ if (row < 0 || row >= size.height) continue;
130
+ const inner = row >= y && row < y + pixels.height;
131
+ for (let column = x - 1; column < x + pixels.width + 1; column += 1) {
132
+ if (column < 0 || column >= size.width) continue;
133
+ if (inner && column >= x && column < x + pixels.width) continue;
134
+ const sourceX = Math.min(Math.max(column - x, 0), pixels.width - 1);
135
+ const from = (Math.min(Math.max(row - y, 0), pixels.height - 1) * pixels.width + sourceX) * CHANNELS;
136
+ page.set(pixels.data.subarray(from, from + CHANNELS), (row * size.width + column) * CHANNELS);
137
+ }
138
+ }
139
+ }
140
+ /**
141
+ * Composes a page: a transparent page with every texture copied onto its place and its edges extruded. Pure.
142
+ *
143
+ * @param width - Width of the page.
144
+ * @param height - Height of the page.
145
+ * @param parts - The textures and their places.
146
+ * @returns The RGBA pixels of the page.
147
+ * @example
148
+ * ```ts
149
+ * // One opaque 1×1 pixel at (1, 1) of a 3×3 page: its colour fills the whole page.
150
+ * composePage(3, 3, [{ pixels: { width: 1, height: 1, data: Uint8Array.of(255, 0, 0, 255) }, x: 1, y: 1 }])[3]; // 255
151
+ * ```
152
+ */
153
+ function composePage(width, height, parts) {
154
+ const pixels = new Uint8Array(width * height * CHANNELS);
155
+ for (const part of parts) blit(pixels, {
156
+ width,
157
+ height
158
+ }, part);
159
+ return pixels;
160
+ }
161
+ /**
162
+ * Builds the encoder over the `sharp` factory.
163
+ *
164
+ * @param sharp - The default export of the `sharp` module.
165
+ * @returns The encoder.
166
+ */
167
+ function encoderOf(sharp) {
168
+ const webpOptions = {
169
+ quality: 80,
170
+ alphaQuality: 80
171
+ };
172
+ return {
173
+ version: sharp.versions.sharp ?? "unknown",
174
+ decode: async (bytes) => {
175
+ const { data, info } = await sharp(bytes).toColourspace("srgb").ensureAlpha().raw().toBuffer({ resolveWithObject: true });
176
+ return {
177
+ width: info.width,
178
+ height: info.height,
179
+ data: new Uint8Array(data)
180
+ };
181
+ },
182
+ page: async (width, height, parts) => {
183
+ const encoded = await sharp(composePage(width, height, parts), { raw: {
184
+ width,
185
+ height,
186
+ channels: CHANNELS
187
+ } }).webp(webpOptions).toBuffer();
188
+ return new Uint8Array(encoded);
189
+ },
190
+ webp: async (bytes) => new Uint8Array(await sharp(bytes).webp(webpOptions).toBuffer())
191
+ };
192
+ }
193
+ /**
194
+ * Loads `sharp` and builds the encoder. `sharp` is an optional peer dependency of the engine,
195
+ * so a game installs it for its pack command only.
196
+ *
197
+ * @returns The encoder.
198
+ * @throws {Error} When `sharp` is not installed, with the command that installs it.
199
+ * @example
200
+ * ```ts
201
+ * const encoder = await loadEncoder();
202
+ * encoder.version; // the installed sharp version, such as "0.34.5"
203
+ * ```
204
+ */
205
+ async function loadEncoder() {
206
+ try {
207
+ return encoderOf((await import("sharp")).default);
208
+ } catch (error) {
209
+ throw new Error("[game] assets: \"--pack\" needs sharp.\n Run \"bun add -d sharp\".", { cause: error });
210
+ }
211
+ }
179
212
  //#endregion
180
213
  //#region src/plugins/assets/scan/pack/cache.ts
181
214
  /**
@@ -242,6 +275,7 @@ function cacheKey(input) {
242
275
  page: PAGE_SIZE,
243
276
  border: 2,
244
277
  padding: 2,
278
+ extrude: 1,
245
279
  quality: 80,
246
280
  alphaQuality: 80,
247
281
  members
@@ -1698,8 +1732,8 @@ function countLoose(bundles) {
1698
1732
  }
1699
1733
  /**
1700
1734
  * Packs the art of a game for production: per bundle, the textures of the `fx` group and of the
1701
- * `main` group go onto WebP atlas pages (2048 px at most, 2 px padding and border, never trimmed
1702
- * or rotated), a texture with a side above 512 px and a group of one stay loose files, fonts and
1735
+ * `main` group go onto WebP atlas pages (2048 px at most, 2 px padding and border with each frame's
1736
+ * edge extruded 1 px into it, never trimmed or rotated), a texture with a side above 512 px and a group of one stay loose files, fonts and
1703
1737
  * sounds are copied, every file name carries the hash of its bytes, and the packed manifest
1704
1738
  * (version 2) names it all. Nothing is written while a problem is open; afterwards the pack folder
1705
1739
  * holds exactly what the manifest references, plus `manifest.json`. Two runs on the same tree and
package/dist/assets.d.mts CHANGED
@@ -358,8 +358,8 @@ type PackResult = {
358
358
  };
359
359
  /**
360
360
  * Packs the art of a game for production: per bundle, the textures of the `fx` group and of the
361
- * `main` group go onto WebP atlas pages (2048 px at most, 2 px padding and border, never trimmed
362
- * or rotated), a texture with a side above 512 px and a group of one stay loose files, fonts and
361
+ * `main` group go onto WebP atlas pages (2048 px at most, 2 px padding and border with each frame's
362
+ * edge extruded 1 px into it, never trimmed or rotated), a texture with a side above 512 px and a group of one stay loose files, fonts and
363
363
  * sounds are copied, every file name carries the hash of its bytes, and the packed manifest
364
364
  * (version 2) names it all. Nothing is written while a problem is open; afterwards the pack folder
365
365
  * holds exactly what the manifest references, plus `manifest.json`. Two runs on the same tree and
package/dist/assets.mjs CHANGED
@@ -1,2 +1,2 @@
1
- import { a as runCli, c as emitKeys, i as compileStrings, l as emitManifest, n as importStrings, o as scanAssets, r as checkStrings, s as packAssets, t as exportStrings } from "./assets-DIYDAar4.mjs";
1
+ import { a as runCli, c as emitKeys, i as compileStrings, l as emitManifest, n as importStrings, o as scanAssets, r as checkStrings, s as packAssets, t as exportStrings } from "./assets-BRTUagOC.mjs";
2
2
  export { checkStrings, compileStrings, emitKeys, emitManifest, exportStrings, importStrings, packAssets, runCli, scanAssets };
package/dist/cli.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  import { t as resolveConfig } from "./config-B87aFTW-.mjs";
2
2
  import { t as findFeatureOwners } from "./features-DowEilZm.mjs";
3
3
  import { r as watchTree, t as createBatcher, x as isSkippedFolder$1 } from "./watch-BSgptowj.mjs";
4
- import { a as runCli$1, i as compileStrings, n as importStrings, t as exportStrings } from "./assets-DIYDAar4.mjs";
4
+ import { a as runCli$1, i as compileStrings, n as importStrings, t as exportStrings } from "./assets-BRTUagOC.mjs";
5
5
  import { constants, copyFileSync, existsSync, mkdirSync, readFileSync, readdirSync, realpathSync, rmSync, statSync, watch, writeFileSync } from "node:fs";
6
6
  import path from "node:path";
7
7
  import { createBrandConsole } from "@moku-labs/common/cli";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@moku-labs/game",
3
- "version": "0.15.50",
3
+ "version": "0.15.51",
4
4
  "description": "2D puzzle game engine on TypeScript and PixiJS, built on @moku-labs/core.",
5
5
  "type": "module",
6
6
  "sideEffects": [