Skip to content

异步(分步)A*寻路算法,适用于游戏开发(cocos 或 使用ts的游戏)

Notifications You must be signed in to change notification settings

cat9/async-astar

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

简介

async-astar 异步(分步)A*寻路算法,基于 javascript-astar iam-binaryheap-typescript ,适用于cocos 或 使用ts的游戏

用法

复制src里面的四个文件进去自己的工程里面使用即可

对角线不可通行(四个方向可通行)

let graph = new Graph([
    [1,1,1,1],
    [0,1,1,0],
    [0,0,1,1]
]);
//closest:如果目标点不能是不可通行,是否自动选择目标最近可通行的点
//maxIterationTimesInStep:单次step里面最多可迭代次数
const astar = new AsyncAStar(graph,function (steps, result){
    //寻路结果回调
    console.log("steps:"+steps);
    console.log(result);
},{closest:true,maxIterationTimesInStep:100});

let start = graph.grid[0][0];
let end = graph.grid[1][2];
astar.search(start, end);


//call on every frame update
//游戏每帧回调函数里面调用这个
astar.step();

对角线可通行(八个方向可通行)

//开启对角线(八个方向)可通行 diagonal: true
let graphDiagonal = new Graph([
    [1,1,1,1],
    [0,1,1,0],
    [0,0,1,1]
], { diagonal: true });
const astar = new AsyncAStar(graph,function (steps, result){
    //寻路结果回调
    console.log("steps:"+steps);
    console.log(result);
},{closest:true,maxIterationTimesInStep:100});

let start = graphDiagonal.grid[0][0];
let end = graphDiagonal.grid[1][2];
astar.search(start, end);


//call on every frame update
//游戏每帧回调函数里面调用这个
astar.step();

包含权重的地图

//包含权重的地图
// Weight can easily be added by increasing the values within the graph, and where 0 is infinite (a wall)
let graphWithWeight = new Graph([
    [1,1,2,30],
    [0,4,1.3,0],
    [0,0,5,1]
]);
const astar = new AsyncAStar(graph,function (steps, result){
    //寻路结果回调
    console.log("steps:"+steps);
    console.log(result);
},{closest:true,maxIterationTimesInStep:100});

let startWithWeight = graphWithWeight.grid[0][0];
let endWithWeight = graphWithWeight.grid[1][2];
astar.search(startWithWeight, endWithWeight);


//call on every frame update
//游戏每帧回调函数里面调用这个
astar.step();

测试

//安装依赖
npm install
//运行测试
npm run dev

About

异步(分步)A*寻路算法,适用于游戏开发(cocos 或 使用ts的游戏)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published