zx-kit 0.22.0 → 0.24.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.
@@ -0,0 +1,27 @@
1
+ import type { TileMap } from './tilemap.js';
2
+ /**
3
+ * Returns the map's full size in pixels — handy for `worldW`/`worldH` when
4
+ * constructing a {@link "camera" | Camera}.
5
+ *
6
+ * @example
7
+ * const { width, height } = tileMapWorldSize(map)
8
+ */
9
+ export declare function tileMapWorldSize(map: TileMap): {
10
+ width: number;
11
+ height: number;
12
+ };
13
+ /**
14
+ * Renders `map` with the viewport's top-left at world pixel `(camX, camY)`.
15
+ * The camera position is rounded to whole pixels for crisp output, then the
16
+ * visible tile range (plus one overscan row/column) is drawn at
17
+ * `tileX * CELL - camX`, giving smooth scrolling at any pixel offset.
18
+ *
19
+ * @param viewW - Visible width in pixels (default `256`). Must be positive.
20
+ * @param viewH - Visible height in pixels (default `192`). Must be positive.
21
+ *
22
+ * @example
23
+ * drawTileMapAt(ctx, map, cam.x, cam.y) // standard 256×192
24
+ * drawTileMapAt(ctx, map, cam.x, cam.y, 256, 176) // shorter play area
25
+ */
26
+ export declare function drawTileMapAt(ctx: CanvasRenderingContext2D, map: TileMap, camX: number, camY: number, viewW?: number, viewH?: number): void;
27
+ //# sourceMappingURL=tilescroll.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tilescroll.d.ts","sourceRoot":"","sources":["../src/tilescroll.ts"],"names":[],"mappings":"AAyBA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AAE3C;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,OAAO,GAAG;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAEhF;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,aAAa,CAC3B,GAAG,EAAE,wBAAwB,EAC7B,GAAG,EAAE,OAAO,EACZ,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,EACZ,KAAK,SAAM,EACX,KAAK,SAAM,GACV,IAAI,CAuBN"}
@@ -0,0 +1,74 @@
1
+ /**
2
+ * @module tilescroll
3
+ *
4
+ * **Pixel-smooth tile-map scrolling.** The built-in {@link "tilemap" |
5
+ * TileMap.render} takes a viewport in *whole tiles*, so a camera can only move
6
+ * in 8-pixel steps — fine for grid games, visibly steppy for a platformer where
7
+ * the player moves sub-pixel-smooth while jumping.
8
+ *
9
+ * {@link drawTileMapAt} renders the map at an arbitrary pixel camera position by
10
+ * drawing one extra row/column and offsetting every tile by `camX % CELL` /
11
+ * `camY % CELL`. Off-screen and empty cells are skipped; the leading/trailing
12
+ * partial tiles are drawn and naturally clipped by the canvas bounds.
13
+ *
14
+ * @example
15
+ * ```ts
16
+ * const cam = createCamera({ viewW: 256, viewH: 192, ...tileMapWorldSize(map) })
17
+ * // game loop:
18
+ * setCameraTarget(cam, player.x, player.y)
19
+ * tickCamera(cam, dt)
20
+ * drawTileMapAt(ctx, map, cam.x, cam.y) // smooth background
21
+ * // …draw entities at (e.x - cam.x, e.y - cam.y)…
22
+ * ```
23
+ */
24
+ import { CELL } from './palette.js';
25
+ import { drawSprite } from './renderer.js';
26
+ /**
27
+ * Returns the map's full size in pixels — handy for `worldW`/`worldH` when
28
+ * constructing a {@link "camera" | Camera}.
29
+ *
30
+ * @example
31
+ * const { width, height } = tileMapWorldSize(map)
32
+ */
33
+ export function tileMapWorldSize(map) {
34
+ return { width: map.cols * CELL, height: map.rows * CELL };
35
+ }
36
+ /**
37
+ * Renders `map` with the viewport's top-left at world pixel `(camX, camY)`.
38
+ * The camera position is rounded to whole pixels for crisp output, then the
39
+ * visible tile range (plus one overscan row/column) is drawn at
40
+ * `tileX * CELL - camX`, giving smooth scrolling at any pixel offset.
41
+ *
42
+ * @param viewW - Visible width in pixels (default `256`). Must be positive.
43
+ * @param viewH - Visible height in pixels (default `192`). Must be positive.
44
+ *
45
+ * @example
46
+ * drawTileMapAt(ctx, map, cam.x, cam.y) // standard 256×192
47
+ * drawTileMapAt(ctx, map, cam.x, cam.y, 256, 176) // shorter play area
48
+ */
49
+ export function drawTileMapAt(ctx, map, camX, camY, viewW = 256, viewH = 192) {
50
+ if (viewW <= 0 || viewH <= 0) {
51
+ throw new Error(`drawTileMapAt: viewW and viewH must be positive, got ${viewW}×${viewH}`);
52
+ }
53
+ const ox = Math.round(camX);
54
+ const oy = Math.round(camY);
55
+ const startCol = Math.floor(ox / CELL);
56
+ const startRow = Math.floor(oy / CELL);
57
+ const cols = Math.ceil(viewW / CELL) + 1;
58
+ const rows = Math.ceil(viewH / CELL) + 1;
59
+ for (let ry = 0; ry < rows; ry++) {
60
+ const ty = startRow + ry;
61
+ if (ty < 0 || ty >= map.rows)
62
+ continue;
63
+ for (let rx = 0; rx < cols; rx++) {
64
+ const tx = startCol + rx;
65
+ if (tx < 0 || tx >= map.cols)
66
+ continue;
67
+ const tile = map.getTile(tx, ty);
68
+ if (tile === null)
69
+ continue;
70
+ drawSprite(ctx, tile.sprite, tx * CELL - ox, ty * CELL - oy, tile.ink, tile.paper);
71
+ }
72
+ }
73
+ }
74
+ //# sourceMappingURL=tilescroll.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tilescroll.js","sourceRoot":"","sources":["../src/tilescroll.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,OAAO,EAAE,IAAI,EAAE,MAAM,cAAc,CAAA;AACnC,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAA;AAG1C;;;;;;GAMG;AACH,MAAM,UAAU,gBAAgB,CAAC,GAAY;IAC3C,OAAO,EAAE,KAAK,EAAE,GAAG,CAAC,IAAI,GAAG,IAAI,EAAE,MAAM,EAAE,GAAG,CAAC,IAAI,GAAG,IAAI,EAAE,CAAA;AAC5D,CAAC;AAED;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,aAAa,CAC3B,GAA6B,EAC7B,GAA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