@xtia/grid 0.0.4 → 0.0.5
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 +31 -9
- package/lib/grid.js +1 -1
- package/package.json +1 -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"; // ~5.3kb gzipped
|
|
26
26
|
|
|
27
27
|
// initialise a 30x20 Grid<number> of 0's
|
|
28
28
|
const numGrid = Grid.solid(30, 20, 0);
|
|
@@ -74,12 +74,12 @@ const lakeGrid = Grid.from(lakeRegion);
|
|
|
74
74
|
|
|
75
75
|
## Transformation Layers
|
|
76
76
|
|
|
77
|
-
`grid.map(read, write)` creates a **view** that reads and writes the parent through the provided transformation functions.
|
|
77
|
+
`grid.map(read, write)` creates a **zero-copy view** that reads and writes the parent through the provided transformation functions.
|
|
78
78
|
|
|
79
79
|
```ts
|
|
80
80
|
const spriteMap = world.map(
|
|
81
81
|
type => type + ".png",
|
|
82
|
-
sprite => sprite.replace(
|
|
82
|
+
sprite => sprite.replace(/\..*/, '')
|
|
83
83
|
);
|
|
84
84
|
|
|
85
85
|
world.set(0, 0, "mountain"); // modify the parent
|
|
@@ -90,6 +90,20 @@ console.log(world.get(1, 0)); // read the parent: "forest"
|
|
|
90
90
|
|
|
91
91
|
For one-way read-mapping, use `grid.pipe` - a [Pipe2D](https://github.com/tiadrop/pipe2d) view into the grid's data.
|
|
92
92
|
|
|
93
|
+
```ts
|
|
94
|
+
// create a one-way, player-centred sprite map
|
|
95
|
+
const viewport = world.pipe
|
|
96
|
+
.oob("mountain")
|
|
97
|
+
.map(t => t + ".png")
|
|
98
|
+
.crop(playerX - 5, playerY - 5, 10, 10);
|
|
99
|
+
|
|
100
|
+
// or get a list of locations of cells with mountains
|
|
101
|
+
const mountainCells = world.cells.toFlatArrayXY()
|
|
102
|
+
.filter(cell => cell.value === "mountain")
|
|
103
|
+
.map(cell => [cell.x, cell.y]);
|
|
104
|
+
|
|
105
|
+
```
|
|
106
|
+
|
|
93
107
|
## Cell interface
|
|
94
108
|
|
|
95
109
|
`grid.cells` provides a Pipe2D, for convenient transformation, of live-view interfaces relating to positions in the grid.
|
|
@@ -98,12 +112,12 @@ For one-way read-mapping, use `grid.pipe` - a [Pipe2D](https://github.com/tiadro
|
|
|
98
112
|
const topLeft = world.cells.get(0, 0);
|
|
99
113
|
console.log(topLeft.value); // "mountain"
|
|
100
114
|
|
|
101
|
-
topLeft.value = "forest; // modifies the underlying data
|
|
115
|
+
topLeft.value = "forest"; // modifies the underlying data
|
|
102
116
|
console.log(spriteMap.get(0, 0)); // now "forest.png";
|
|
103
117
|
|
|
104
118
|
// navigate by offset
|
|
105
119
|
const adjacentCell = topLeft.look(1, 0);
|
|
106
|
-
console.log(adjacentCell
|
|
120
|
+
console.log(adjacentCell?.x, adjacentCell?.y); // 1, 0
|
|
107
121
|
```
|
|
108
122
|
|
|
109
123
|
Cells provide methods for locational utilities such as pathfinding and visibility mapping.
|
|
@@ -149,9 +163,6 @@ const visibility = start.createVisibilityMap(
|
|
|
149
163
|
// any 2d source, including visibility maps, can be used as a mask for paste/fill operations
|
|
150
164
|
// paste(source: Source2D<T>, mask?: Source2D<boolean>)
|
|
151
165
|
screenGrid.paste(spriteMap, visibility);
|
|
152
|
-
|
|
153
|
-
// using pipe2d's convenience
|
|
154
|
-
const mountainCells = world.cells.toFlatArrayXY().filter(c => c.value === "mountain");
|
|
155
166
|
```
|
|
156
167
|
|
|
157
168
|
### Reusing path data
|
|
@@ -173,4 +184,15 @@ Unlike visibility maps, which are lazily evaluated according to the supplied `is
|
|
|
173
184
|
|
|
174
185
|
Although the Grid factory methods (`Grid.solid()`, `Grid.from()`, `Grid.init()`) return a `GridBase<T>`, the mental model is that we're simply working with `Grid`s, therefore those factory methods live on `Grid`. The only API distinction is that `GridBase` provides a 'change' event, via `grid.on("change", handler)`.
|
|
175
186
|
|
|
176
|
-
You can perform batched updates, holding off the 'change' event until the batch process concludes, with `grid.batchUpdate(callback)`.
|
|
187
|
+
You can perform batched updates, holding off the 'change' event until the batch process concludes, with `grid.batchUpdate(callback)`.
|
|
188
|
+
|
|
189
|
+
## Save and load
|
|
190
|
+
|
|
191
|
+
Pipe2D makes it easy to save and restore grid data:
|
|
192
|
+
|
|
193
|
+
```ts
|
|
194
|
+
const saved = world.pipe.toFlatArrayXY(); // ["mountain", "forest", "grass", ...]
|
|
195
|
+
|
|
196
|
+
const restoredPipe = Pipe2D.fromFlatArrayXY(saved);
|
|
197
|
+
const restored = Grid.from(restoredPipe);
|
|
198
|
+
```
|
package/lib/grid.js
CHANGED
|
@@ -132,7 +132,7 @@ export class Cell {
|
|
|
132
132
|
return path;
|
|
133
133
|
});
|
|
134
134
|
}
|
|
135
|
-
return this.gridView.cells.map(
|
|
135
|
+
return this.gridView.cells.map(costs, () => Infinity)
|
|
136
136
|
.strict();
|
|
137
137
|
}
|
|
138
138
|
*getLineTo(target) {
|
package/package.json
CHANGED