js版A星算法
2016-04-18 09:46
435 查看
var A_star_algorithm = cc.Class.extend({
ctor:function()
{
this._direction = [{x:-1,y:0},{x:0,y:1},{x:1,y:0},{x:0,y:-1}];
this._baseId =0;
},
SetPoints:function(posList,startPos,endPos)
{
if (posList)
{
this._posList = posList;
}
this._startPos = startPos;
this._endPos = endPos;
if ((!endPos) ||(!startPos))
{
return
}
this._Init();
},
StartSearch:function()
{
var startTime = Date.now()
var endTime = startTime;
HHLog("startFindTime::"+startTime+"\n");
if(!this._GetStartPosItem() || !this._GetEndPosItem())return;
while (this._GetOpenList().length>0)
{
this._SortListByF(this._GetOpenList())
var curItem = this._GetOpenList()[0];
this._RemoveItemFromList(curItem,this._GetOpenList());
this._InsertItemToList(curItem,this._GetCloseList());
if (!this._CheckIfItemEqual(curItem,this._GetEndPosItem()))
{
this._StartFind(curItem);
}
else
{
var paths = [];
var item = curItem;
var parent = item.GetParent();
while(parent)
{
paths.unshift(item);
item = item.GetParent()
parent = item.GetParent();
}
if (this._CheckIfItemEqual(item,this._GetStartPosItem()))
{
paths.unshift(item);
this._pathList = paths;
HHLog("a_star_algorithm_find---success\n");
}
else
{
HHLog("a_star_algorithm_find---not find a path to destPos");
}
break;
}
}
if (this.GetPath().length==0)
{
HHLog("a_star_algorithm_find---not find a path to destPos");
}
endTime = Date.now();
HHLog("startFindTime::"+endTime+"\n");
HHLog("timeCost::::::::"+(endTime-startTime)+"\n");
},
GetPath:function()
{
var pathList =[];
for (var i=0;i
{
var item = this._pathList[i];
pathList.push(item.GetPos());
}
return pathList;
},
GetPosList:function()
{
return this._posList;
},
GetPosItemList:function()
{
return this._posItemList;
},
GetPosItemByPos:function(pos)
{
var item = null;
var posItemList = this.GetPosItemList();
for (var i =0;i
{
var temp = posItemList[i].GetPos();
if (pos.x == temp.x && pos.y == temp.y)
{
item = posItemList[i];
break;
}
}
return item;
},
_Init:function()
{
this._openList = [];
this._closeList = [];
this._startPosItem = null;
this._endPosItem = null;
this._pathList = [];
this._posItemList = [];
this._baseId =0;
this._curPosItem = null;
this._CreatePosItemList();
this._InsertItemToList(this._GetStartPosItem(),this._GetOpenList());
},
_StartFind:function(curPosItem)
{
this._SetCurPosItem(curPosItem);
var curPosItem = this._GetCurPosItem();
var endPosItem = this._GetEndPosItem();
var curPos = curPosItem.GetPos();
var endPos = endPosItem.GetPos();
var direction = this._direction;
for (var i =0;i
{
var nextPos = cc.pAdd(curPos,direction[i]);
var item = this.GetPosItemByPos(nextPos);
var isInOpenList = this._CheckIfItemInList(item,this._GetOpenList());
var isInCloseList = this._CheckIfItemInList(item,this._GetCloseList());
if (item)
{
if(!isInOpenList && !isInCloseList)
{
var h = this.GetHValue(nextPos,endPos);
var g = curPosItem.GetG()+1;
item.SetG(g);
item.SetH(h);
item.SetParent(curPosItem);
this._InsertItemToList(item,this._GetOpenList());
}
}
}
},
_GetStartPosItem:function()
{
return this._startPosItem;
},
_GetEndPosItem:function()
{
return this._endPosItem;
},
_SortListByF:function(list)
{
if(list.length<=1)return;
list.sort(function(a,b)
{
return a.GetF()- b.GetF();
})
},
_GetCloseList:function()
{
return this._closeList;
},
_GetOpenList:function()
{
return this._openList;
},
_GetEndPosItem:function()
{
return this._endPosItem;
},
_GetCurPosItem:function()
{
return this._curPosItem;
},
_SetCurPosItem:function(posItem)
{
this._curPosItem = posItem;
},
_InsertItemToList:function(item,list)
{
if(item)
{
list.push(item);
}
},
_RemoveItemFromList:function(item,list)
{
var index = null;
for (var i in list)
{
var temp = list[i];
if (temp.GetId() == item.GetId())
{
list.splice(i,1);
}
}
},
_CheckIfItemEqual:function(cmp_a,cmp_b)
{
var isEqual = false;
if (cmp_a && cmp_b)
{
isEqual = (cmp_a.GetId() == cmp_b.GetId())
}
return isEqual;
},
_GetId:function()
{
return this._baseId++;
},
_CheckIfItemInList:function(item,checkList)
{
var isIn = false;
if (!item) return isIn;
for (var i =0;i
{
var temp = checkList[i];
if (item.GetId() == temp.GetId())
{
isIn = true;
break;
}
}
return isIn;
},
//曼哈顿长度
GetHValue:function(startPos=cc.p(0,0),endPos=cc.p(0,0))
{
var temp = cc.pSub(startPos,endPos);
var h = Math.abs(temp.x)+Math.abs(temp.y);
return h;
},
_CreatePosItem:function(pos,g,h,parent)
{
return PosItem.create(pos,g,h,parent);
},
_CreatePosItemList:function()
{
var posList = this.GetPosList();
for (var i =0;i
{
var pos = posList[i];
var posItem = this._CreatePosItem(pos,0,0,null);
posItem.SetId(this._GetId());
this._posItemList.push(posItem);
if (!this._startPosItem && (pos.x == this._startPos.x && pos.y == this._startPos.y))
{
this._startPosItem = posItem;
}
if (!this._endPosItem && (pos.x == this._endPos.x && pos.y == this._endPos.y))
{
this._endPosItem = posItem;
}
}
},
})
var PosItem = cc.Class.extend({
ctor:function(pos,g=0,h=0,parent=null)
{
this._pos = pos;
this._g = g;
this._h = h;
this._parent =parent;
this._id = 0;
},
SetId:function(id)
{
this._id = id;
},
GetId:function()
{
return this._id;
},
SetParent:function(parent)
{
if (parent)
{
this._parent = parent
}
},
GetPos:function()
{
return this._pos;
},
GetParent:function()
{
return this._parent;
},
SetG:function(g)
{
this._g = g;
},
SetH:function(h)
{
this._h = h;
},
GetG:function()
{
return this._g;
},
GetH:function()
{
return this._h;
},
GetF:function()
{
return this.GetG()+this.GetH();
},
})
PosItem.create = function(pos,g,h,parent)
{
return new this(pos,g,h,parent);
}
相关文章推荐
- Hibernate3.3.2+Spring2.5.5+Struts2.1.6+Extjs3.0.0 Annotations注解框架整合及其代码分享
- JS正则表达式将url转成json格式
- javascript:设计模式之单例模式
- json序列化
- js统计当前输入字数在指定的输入区域显示
- ExtJS的12种布局风格
- js 计算时间差
- arcgis for javascript ArcGISDynamicMapServiceLayer 过滤图层点
- 重识Javascript系列---BOM
- js将字符串转成日期格式,返回年月日
- ExtJS 3.4 类继承机制
- ArcGIS Javascript学习笔记(一)arcgis API 的本地部署
- JS基于MSClass和setInterval实现ajax定时采集信息并滚动显示的方法
- 模板插件aTpl.js新增功能
- js的左移右移计算
- javascript闭包和闭包的几种写法及用途
- JavaScript系列:再巩固-原型链
- javascript 规范
- 总结ASP.NET C#中经常用到的13个JS脚本代码
- JS基于MSClass和setInterval实现ajax定时采集信息并滚动显示的方法