@xtia/grid 0.0.5 → 0.0.6
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/README.md +30 -2
- package/lib/grid.d.ts +1 -0
- package/lib/grid.js +3 -0
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -22,7 +22,7 @@ npm i @xtia/grid
|
|
|
22
22
|
```
|
|
23
23
|
|
|
24
24
|
```ts
|
|
25
|
-
import { Grid } from "@xtia/grid"; // ~
|
|
25
|
+
import { Grid } from "@xtia/grid"; // ~4.8kb gzipped
|
|
26
26
|
|
|
27
27
|
// initialise a 30x20 Grid<number> of 0's
|
|
28
28
|
const numGrid = Grid.solid(30, 20, 0);
|
|
@@ -46,6 +46,34 @@ const source = imagePipe
|
|
|
46
46
|
const grid = Grid.from(source);
|
|
47
47
|
```
|
|
48
48
|
|
|
49
|
+
### Wrapping existing structures
|
|
50
|
+
|
|
51
|
+
Use Grid's interface over any read/write 2D structure:
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
const gameMap = [
|
|
55
|
+
[0, 1, -1, 2],
|
|
56
|
+
[1, 0, 1, 0],
|
|
57
|
+
[-1, 1, 0, 2],
|
|
58
|
+
[2, 0, 2, -1]
|
|
59
|
+
];
|
|
60
|
+
|
|
61
|
+
// create a *live view* Grid over gameMap
|
|
62
|
+
const grid = Grid.wrap(
|
|
63
|
+
gameMap[0].length, // width
|
|
64
|
+
gameMap.length, // height
|
|
65
|
+
(x, y) => gameMap[y][x], // get
|
|
66
|
+
(x, y, value) => gameMap[y][x] = value // set
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
// reading the grid = reading the source
|
|
70
|
+
gameMap[0][0] = 50;
|
|
71
|
+
console.log(grid.get(0, 0)); // 50
|
|
72
|
+
// writing to the grid = writing to the source
|
|
73
|
+
grid.set(3, 3, 100);
|
|
74
|
+
console.log(gameMap[3][3]); // 100
|
|
75
|
+
```
|
|
76
|
+
|
|
49
77
|
## Regions
|
|
50
78
|
|
|
51
79
|
Use `grid.region(x, y, w, h)` to define a subgrid. The subgrid is **zero-copy view** into the parent; changes to the subgrid affect the parent and vice-versa.
|
|
@@ -176,7 +204,7 @@ const pathToCentre = pathMap.get(50, 50);
|
|
|
176
204
|
const pathToCorner = pathMap.get(99, 99);
|
|
177
205
|
```
|
|
178
206
|
|
|
179
|
-
Unlike visibility maps, which are lazily evaluated according to the supplied `
|
|
207
|
+
Unlike visibility maps, which are lazily evaluated according to the supplied `isClear` function *when the map is queried* (but can be easily cached with `visMap.withCache()`), path maps use a relatively expensive traversal map that's created *when the map is created*. This distinction is hinted through the `create*` vs `get*` naming.
|
|
180
208
|
|
|
181
209
|
## The storage layer
|
|
182
210
|
|
package/lib/grid.d.ts
CHANGED
|
@@ -77,6 +77,7 @@ export declare class Grid<T> {
|
|
|
77
77
|
static init<T>(width: number, height: number, initCell: (x: number, y: number) => T): GridBase<T>;
|
|
78
78
|
static from<T>(source: Source2D<T>): GridBase<T>;
|
|
79
79
|
static solid<T>(width: number, height: number, fillValue: T): GridBase<T>;
|
|
80
|
+
static wrap<T>(width: number, height: number, get: (x: number, y: number) => T, set: (x: number, y: number, value: T) => void, batch?: (callback: () => void) => void): Grid<T>;
|
|
80
81
|
}
|
|
81
82
|
export declare class GridBase<T> extends Grid<T> {
|
|
82
83
|
private data;
|
package/lib/grid.js
CHANGED
|
@@ -314,6 +314,9 @@ export class Grid {
|
|
|
314
314
|
static solid(width, height, fillValue) {
|
|
315
315
|
return new GridBase(Pipe2D.solid(fillValue, width, height));
|
|
316
316
|
}
|
|
317
|
+
static wrap(width, height, get, set, batch = cb => cb()) {
|
|
318
|
+
return new Grid(width, height, get, set, batch);
|
|
319
|
+
}
|
|
317
320
|
}
|
|
318
321
|
export class GridBase extends Grid {
|
|
319
322
|
triggerEvent(name, data) {
|
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "0.0.
|
|
2
|
+
"version": "0.0.6",
|
|
3
3
|
"description": "2D store and utilities, with zero-copy regions and transform layers, pathfinding, Pipe2D interop",
|
|
4
4
|
"name": "@xtia/grid",
|
|
5
5
|
"exports": {
|
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
},
|
|
10
10
|
"./lib/*": null
|
|
11
11
|
},
|
|
12
|
+
"keywords": ["grid", "game-dev", "pathfinding", "cellular-automata"],
|
|
12
13
|
"scripts": {
|
|
13
14
|
"build": "npx tsc",
|
|
14
15
|
"test": "npx vitest run"
|