@xtia/grid 0.0.10 → 0.0.11

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 CHANGED
@@ -91,7 +91,7 @@ const mask = (x: number, y: number) => {
91
91
  const dist = Math.hypot(x - centre.x, y - centre.y);
92
92
  return dist > 12 && dist < 18; // mountain ring
93
93
  };
94
- world.fill("mountain", mask);
94
+ world.writeMask(mask).fill("mountain");
95
95
 
96
96
  // get a view of just the interesting area
97
97
  const lakeRegion = world.region(25, 25, 30, 30);
@@ -203,9 +203,8 @@ const visibility = start.createVisibilityMap(
203
203
  cell => cell.value === "grass" // anything except grass blocks vision
204
204
  );
205
205
 
206
- // any 2d source, including visibility maps, can be used as a mask for paste/fill operations
207
- // paste(source: Source2D<T>, mask?: Source2D<boolean>)
208
- screenGrid.paste(spriteMap, 0, 0, visibility);
206
+ // writeMask() creates a live view that only allows writes at positions allowed by the mask Source2D/function
207
+ screenGrid.writeMask(visibility).paste(spriteMap, 0, 0);
209
208
  ```
210
209
 
211
210
  ### Reusing path data
@@ -234,8 +233,17 @@ We can perform batched updates, suppressing the 'change' event until a process c
234
233
  Pipe2D makes it easy to save and restore grid data:
235
234
 
236
235
  ```ts
237
- const saved = world.pipe.toFlatArrayXY(); // ["mountain", "forest", "grass", ...]
236
+ const snapshot = world.pipe.stash();
238
237
 
239
- const restoredPipe = Pipe2D.fromFlatArrayXY(saved);
240
- const restored = Grid.from(restoredPipe);
241
- ```
238
+ const restored = Grid.from(snapshot);
239
+ // or paste it to an existing grid
240
+ world.paste(snapshot);
241
+ ```
242
+
243
+ For persistent storage or transmission, Pipe2D can export as array:
244
+
245
+ ```ts
246
+ const saved = snapshot.toFlatArrayXY(); // ["mountain", "forest", "grass", ...]
247
+
248
+ const restoredSnapshot = Pipe2D.fromFlatArrayXY(saved);
249
+ ```
package/lib/grid.d.ts CHANGED
@@ -64,8 +64,9 @@ export declare class Grid<T> {
64
64
  get(x: number, y: number): T;
65
65
  set(x: number, y: number, value: T): void;
66
66
  trySet(x: number, y: number, value: T): boolean;
67
- fill(value: T, mask?: Source2D<boolean> | GetXYFunc<boolean>): void;
68
- paste(source: Source2D<T>, x?: number, y?: number, mask?: Source2D<boolean> | GetXYFunc<boolean>): void;
67
+ fill(value: T): void;
68
+ paste(source: Source2D<T>, x?: number, y?: number): void;
69
+ writeMask(mask: Source2D<boolean> | GetXYFunc<boolean>): Grid<T>;
69
70
  [Symbol.iterator](): IterableIterator<{
70
71
  x: number;
71
72
  y: number;
package/lib/grid.js CHANGED
@@ -245,41 +245,36 @@ export class Grid {
245
245
  this.set(x, y, value);
246
246
  return true;
247
247
  }
248
- fill(value, mask) {
249
- const maskSource = typeof mask == "function" ? {
250
- width: this.width,
251
- height: this.height,
252
- get: mask
253
- } : mask;
248
+ fill(value) {
254
249
  this.batchUpdate(() => {
255
250
  for (let y = 0; y < this.height; y++) {
256
251
  for (let x = 0; x < this.width; x++) {
257
- if (maskSource && !maskSource.get(x, y))
258
- continue;
259
252
  this.trySet(x, y, value);
260
253
  }
261
254
  }
262
255
  });
263
256
  }
264
- paste(source, x = 0, y = 0, mask) {
265
- const maskSource = typeof mask == "function" ? {
266
- width: this.width,
267
- height: this.height,
268
- get: mask
269
- } : mask;
257
+ paste(source, x = 0, y = 0) {
270
258
  const cachedSource = new GridBase(source);
271
259
  this.batchUpdate(() => {
272
260
  for (let oy = 0; oy < source.height; oy++) {
273
261
  for (let ox = 0; ox < source.width; ox++) {
274
- const tx = ox + x;
275
- const ty = oy + y;
276
- if (maskSource && !maskSource.get(tx, ty))
277
- continue;
278
- this.trySet(tx, ty, cachedSource.get(ox, oy));
262
+ this.trySet(ox + x, oy + y, cachedSource.get(ox, oy));
279
263
  }
280
264
  }
281
265
  });
282
266
  }
267
+ writeMask(mask) {
268
+ const getMask = typeof mask == "function"
269
+ ? mask
270
+ : (x, y) => mask.get(x, y);
271
+ return new Grid(this.width, this.height, this.parentGet, (x, y, v) => {
272
+ const writable = getMask(x, y);
273
+ if (writable) {
274
+ this.set(x, y, v);
275
+ }
276
+ }, cb => this.batch(cb));
277
+ }
283
278
  *[Symbol.iterator]() {
284
279
  for (let y = 0; y < this.height; y++) {
285
280
  for (let x = 0; x < this.width; x++) {
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.0.10",
2
+ "version": "0.0.11",
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": {