@xtia/grid 0.1.1 → 0.2.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/README.md +5 -5
- package/lib/grid.d.ts +1 -1
- package/lib/grid.js +5 -6
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -43,7 +43,7 @@ const source = imagePipe
|
|
|
43
43
|
.crop(10, 10, 64, 64)
|
|
44
44
|
.scale(.5)
|
|
45
45
|
.rotateLeft();
|
|
46
|
-
const grid = Grid.
|
|
46
|
+
const grid = Grid.init(source);
|
|
47
47
|
```
|
|
48
48
|
|
|
49
49
|
### Wrapping existing structures
|
|
@@ -97,7 +97,7 @@ world.writeMask(mask).fill("mountain");
|
|
|
97
97
|
const lakeRegion = world.region(25, 25, 30, 30);
|
|
98
98
|
|
|
99
99
|
// do we want a distinct, self-contained copy of the region?
|
|
100
|
-
const lakeGrid = Grid.
|
|
100
|
+
const lakeGrid = Grid.init(lakeRegion);
|
|
101
101
|
```
|
|
102
102
|
|
|
103
103
|
## Transformation Layers
|
|
@@ -225,9 +225,9 @@ Unlike visibility maps, which are lazily evaluated according to the supplied `is
|
|
|
225
225
|
|
|
226
226
|
## The storage layer
|
|
227
227
|
|
|
228
|
-
`Grid` itself is an interface
|
|
228
|
+
`Grid` itself is an interface that sits on top of a synchronous 2D read/write mechanism. `GridBase` is a subclass of `Grid` with built-in storage.
|
|
229
229
|
|
|
230
|
-
Although the
|
|
230
|
+
Although the factory methods `Grid.solid<T>()` and `Grid.init<T>()`) belong to `Grid`, they return a `GridBase<T>`. `Grid.wrap<T>()` is an exception, returning `Grid<T>`, as it uses a storage layer provided by the user. The only API distinction is that `GridBase` provides a 'change' event, via `grid.on("change", handler)`.
|
|
231
231
|
|
|
232
232
|
We can perform batched updates, suppressing the 'change' event until a process concludes, with `grid.batchUpdate(callback)`.
|
|
233
233
|
|
|
@@ -238,7 +238,7 @@ Pipe2D makes it easy to save and restore grid data:
|
|
|
238
238
|
```ts
|
|
239
239
|
const snapshot = world.pipe.stash();
|
|
240
240
|
|
|
241
|
-
const restored = Grid.
|
|
241
|
+
const restored = Grid.init(snapshot);
|
|
242
242
|
// or paste it to an existing grid
|
|
243
243
|
world.paste(snapshot);
|
|
244
244
|
```
|
package/lib/grid.d.ts
CHANGED
|
@@ -73,7 +73,7 @@ export declare class Grid<T> {
|
|
|
73
73
|
region(x: number, y: number, width: number, height: number): Grid<T>;
|
|
74
74
|
scroll(xDelta: number, yDelta: number, fill: T): this;
|
|
75
75
|
static init<T>(width: number, height: number, initCell: (x: number, y: number) => T): GridBase<T>;
|
|
76
|
-
static
|
|
76
|
+
static init<T>(source: Source2D<T>): GridBase<T>;
|
|
77
77
|
static solid<T>(width: number, height: number, fillValue: T): GridBase<T>;
|
|
78
78
|
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>;
|
|
79
79
|
}
|
package/lib/grid.js
CHANGED
|
@@ -342,16 +342,15 @@ export class Grid {
|
|
|
342
342
|
}
|
|
343
343
|
return this;
|
|
344
344
|
}
|
|
345
|
-
static init(
|
|
345
|
+
static init(widthOrSource, height, initCell) {
|
|
346
|
+
if (typeof widthOrSource == "object")
|
|
347
|
+
return new GridBase(widthOrSource);
|
|
346
348
|
return new GridBase({
|
|
347
|
-
width,
|
|
348
|
-
height,
|
|
349
|
+
width: widthOrSource,
|
|
350
|
+
height: height,
|
|
349
351
|
get: initCell
|
|
350
352
|
});
|
|
351
353
|
}
|
|
352
|
-
static from(source) {
|
|
353
|
-
return new GridBase(source);
|
|
354
|
-
}
|
|
355
354
|
static solid(width, height, fillValue) {
|
|
356
355
|
return new GridBase(Pipe2D.solid(fillValue, width, height));
|
|
357
356
|
}
|
package/package.json
CHANGED