Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var aStarData:AStarData = aStarQueue.shift();
- var startPos:Position = aStarData.tank.pos;
- var endPos:Position = aStarData.endPos;
- //var startTime:int = new Date().getTime();
- var startNode:AStarNode = nodeMatrix[startPos.x][startPos.y];
- startNode.value = 0;
- startNode.isOpen = true;
- startNode.prev = null;
- nodeMatrix[endPos.x][endPos.y].isEndNode = true;
- // A* algorithm
- var openList:Vector.<AStarNode> = new Vector.<AStarNode>();
- openList[0] = startNode;
- while (openList.length != 0) {
- var currNode:AStarNode = openList.shift();
- if (currNode.isEndNode) break;
- for each (var neighborNode:AStarNode in currNode.neighbors) {
- if (neighborNode.isClosed) continue;
- var tentativeValue:int = currNode.value + currNode.distToNeighbor(neighborNode);
- if (neighborNode.isOpen && tentativeValue >= neighborNode.value) continue;
- neighborNode.prev = currNode;
- neighborNode.value = tentativeValue;
- neighborNode.f = tentativeValue + h(neighborNode, endPos);
- if (neighborNode.isOpen) {
- var index:int = openList.indexOf(neighborNode)
- openList.splice(index--, 1);
- while (index >= 0 && openList[index].f > neighborNode.f) index--;
- openList.splice(index + 1, 0, neighborNode);
- }
- else if (openList.length == 0) {
- openList[0] = neighborNode;
- neighborNode.isOpen = true;
- }
- else {
- var index:int = openList.length - 1;
- while (index >= 0 && openList[index].f > neighborNode.f) index--;
- openList.splice(index + 1, 0, neighborNode);
- neighborNode.isOpen = true;
- }
- }
- currNode.isOpen = false;
- currNode.isClosed = true;
- }
- nodeMatrix[endPos.x][endPos.y].isEndNode = false;
- if (nodeMatrix[endPos.x][endPos.y].prev == null) {
- reset();
- dispatchEvent(new AStarEvent(AStarEvent.FINISHED, aStarData.tank, null));
- return;
- }
Add Comment
Please, Sign In to add comment