@xtia/grid 0.0.12 → 0.0.13
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 +6 -1
- package/lib/grid.js +2 -2
- package/lib/utils.d.ts +2 -3
- package/lib/utils.js +3 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -179,6 +179,9 @@ const numOfAdjacentMines = clickedCell.getNeighbours(true)
|
|
|
179
179
|
```
|
|
180
180
|
|
|
181
181
|
### Path finding
|
|
182
|
+
|
|
183
|
+
`findPath()` computes an optimal path from a cell to another location in the grid, accounting for cell/traversal costs according to a user-provided predicate or `Map<T, number>`.
|
|
184
|
+
|
|
182
185
|
```ts
|
|
183
186
|
const costs = {
|
|
184
187
|
grass: 1,
|
|
@@ -189,6 +192,7 @@ const costs = {
|
|
|
189
192
|
|
|
190
193
|
const start = world.cells.get(5, 5);
|
|
191
194
|
const destination = world.cells.get(90, 90);
|
|
195
|
+
|
|
192
196
|
const path = start.findPath(
|
|
193
197
|
destination, // or simply [90, 90]
|
|
194
198
|
(cell) => costs[cell.value]
|
|
@@ -197,13 +201,14 @@ const path = start.findPath(
|
|
|
197
201
|
|
|
198
202
|
### Visibility mapping
|
|
199
203
|
|
|
204
|
+
`createVisibilityMap()` returns a `Pipe2D<boolean>` that maps which cells are 'visible' from the starting point, according to a user-provided predicate to determine which cells block vision.
|
|
205
|
+
|
|
200
206
|
```ts
|
|
201
207
|
// createVisibilityMap(isClear);
|
|
202
208
|
const visibility = start.createVisibilityMap(
|
|
203
209
|
cell => cell.value === "grass" // anything except grass blocks vision
|
|
204
210
|
);
|
|
205
211
|
|
|
206
|
-
// writeMask() creates a live view that only allows writes at positions allowed by the mask Source2D/function
|
|
207
212
|
screenGrid.writeMask(visibility).paste(spriteMap, 0, 0);
|
|
208
213
|
```
|
|
209
214
|
|
package/lib/grid.js
CHANGED
|
@@ -72,7 +72,7 @@ export class Cell {
|
|
|
72
72
|
? (cell) => { var _a; return (_a = getCost.get(cell.value)) !== null && _a !== void 0 ? _a : Infinity; }
|
|
73
73
|
: getCost;
|
|
74
74
|
costs.set(this, 0);
|
|
75
|
-
const queue = new OrderedQueue(
|
|
75
|
+
const queue = new OrderedQueue([this, 0]);
|
|
76
76
|
const stopAtCell = options.stopAt && this.getSiblingCell(options.stopAt);
|
|
77
77
|
while (queue.length > 0) {
|
|
78
78
|
const current = queue.take();
|
|
@@ -115,7 +115,7 @@ export class Cell {
|
|
|
115
115
|
if (newCost < oldCost && newCost <= maxCost) {
|
|
116
116
|
costs.set(n.cell, newCost);
|
|
117
117
|
pathInfo === null || pathInfo === void 0 ? void 0 : pathInfo.set(n.cell, current);
|
|
118
|
-
queue.add(n.cell);
|
|
118
|
+
queue.add(n.cell, newCost);
|
|
119
119
|
}
|
|
120
120
|
});
|
|
121
121
|
}
|
package/lib/utils.d.ts
CHANGED
|
@@ -5,10 +5,9 @@ export type Source2D<T> = {
|
|
|
5
5
|
get: GetXYFunc<T>;
|
|
6
6
|
};
|
|
7
7
|
export declare class OrderedQueue<T> {
|
|
8
|
-
private getCost;
|
|
9
8
|
private queue;
|
|
10
|
-
constructor(
|
|
11
|
-
add(value: T): void;
|
|
9
|
+
constructor(...items: [T, number][]);
|
|
10
|
+
add(value: T, priority: number): void;
|
|
12
11
|
get length(): number;
|
|
13
12
|
take(): T;
|
|
14
13
|
}
|
package/lib/utils.js
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
export class OrderedQueue {
|
|
2
|
-
constructor(
|
|
3
|
-
this.getCost = getCost;
|
|
2
|
+
constructor(...items) {
|
|
4
3
|
this.queue = [];
|
|
5
|
-
|
|
4
|
+
items.forEach(v => this.add(...v));
|
|
6
5
|
}
|
|
7
|
-
add(value) {
|
|
8
|
-
const priority = this.getCost(value);
|
|
6
|
+
add(value, priority) {
|
|
9
7
|
const item = { value, priority };
|
|
10
8
|
let low = 0, high = this.queue.length;
|
|
11
9
|
while (low < high) {
|
package/package.json
CHANGED