Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow an array to be used for aStar functions #19

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
22 changes: 17 additions & 5 deletions a-star.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace scene {
//% onTilesOf.decompileIndirectFixedInstances=true
//% help=github:arcade-tilemap-a-star/docs/a-star
//% group="Path Following" weight=10
export function aStar(start: tiles.Location, end: tiles.Location, onTilesOf: Image = null) {
export function aStar(start: tiles.Location, end: tiles.Location, onTilesOf: Image | Image[] = null) {
const tm = game.currentScene().tileMap;
if (!tm || !start || !end)
return undefined;
Expand All @@ -52,7 +52,7 @@ namespace scene {
l => l.col == end1.col && l.row == end1.row);
}

export function aStarToAnyOfType(start: tiles.Location, tile: Image, onTilesOf: Image) {
export function aStarToAnyOfType(start: tiles.Location, tile: Image, onTilesOf: Image | Image[]) {
const tm = game.currentScene().tileMap;
if (!tm || !start)
return undefined;
Expand All @@ -70,7 +70,7 @@ namespace scene {
});
}

export function generalAStar(tm: tiles.TileMap, start: SimpleLocation, onTilesOf: Image,
export function generalAStar(tm: tiles.TileMap, start: SimpleLocation, onTilesOf: Image | Image[],
heuristic: (tile: SimpleLocation) => number,
isEnd: (tile: SimpleLocation) => boolean): tiles.Location[] {

Expand Down Expand Up @@ -228,10 +228,22 @@ namespace scene {
(DIAGONAL_COST - NEIGHBOR_COST)
}

function isWalkable(loc: SimpleLocation, onTilesOf: Image, tm: tiles.TileMap): boolean {
function isWalkable(loc: SimpleLocation, onTilesOf: Image | Image[], tm: tiles.TileMap): boolean {
if (tm.isObstacle(loc.col, loc.row)) return false;
if (!onTilesOf) return true;
const img = tm.getTileImage(tm.getTileIndex(loc.col, loc.row))
return img.equals(onTilesOf);
if (isImageLike(onTilesOf)) return img.equals(onTilesOf)
if (isArrayLike<Image>(onTilesOf)) return onTilesOf.some((item: Image) => img.equals(item));
return true
}

function isArrayLike<T>(val: T | T[]): val is Array<T> {
return val && typeof(val) === "object" &&
Object.keys(val).some(k => k == "length");
}
function isImageLike(val: any): val is Image {
return val && typeof (val) === "object" &&
Object.keys(val).some(k => k == "width") &&
Object.keys(val).some(k => k == "height");
}
}
2 changes: 1 addition & 1 deletion pxt.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "arcade-tilemap-a-star",
"version": "0.4.0",
"version": "0.4.1",
"description": "",
"dependencies": {
"device": "*"
Expand Down