Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package {
- import PathNode;
- import flash.utils.Dictionary;
- public class PathSearcher {
- private var lvlData:Array;
- private var beginPts:Array;
- private var endPts:Array;
- private var openList:Dictionary = new Dictionary;
- private var closedList:Dictionary = new Dictionary;
- private var resultsArr:Array = new Array;
- public function PathSearcher(levelData:Array, beginPoints:Array, endPoints:Array) {
- // Allow the whole class to access it
- lvlData = levelData;
- beginPts = beginPoints;
- endPts = endPoints;
- var myNode:PathNode = new PathNode(beginPoints,myNode);
- openList[beginPoints[0] + " " + beginPoints[1]] = myNode;
- myNode.distS = 0;
- FindPath(myNode);
- }
- public function FindPath(node:PathNode):void {
- //Is this the end node?
- if (node.xpos == endPts[0] && node.ypos == endPts[1]) {
- retracePath();
- return;
- }
- //Adds current node to closed list and removes it from open list.
- delete openList[node.xpos + " " + node.ypos];
- closedList[node.xpos + " " + node.ypos] = node;
- //Finds neighbouring nodes
- for (var i = -1; i < 2; i++) {
- for (var j = -1; j < 2; j++) {
- if (i == 0 && j == 0) {
- //Prevent it from calculating for the current node
- } else {
- if (node.xpos + i >= 0 && node.ypos + j >= 0 && node.xpos + i < lvlData.length && node.ypos + j < lvlData[0].length) { //is it within boundaries?
- if (openList[(node.xpos + i) + " " + (node.ypos + j)] == undefined) { //not on open list
- if (closedList[(node.xpos + i) + " " + (node.ypos + j)] == undefined) { //not on closed list either
- if (lvlData[node.xpos + i][node.ypos + j] == 0) { //makes sure that it is walkable terrain
- //Adds it to openlist, also creates new pathnode.
- openList[(node.xpos + i) + " " + (node.ypos + j)] = new PathNode([(node.xpos + i),(node.ypos + j)], node);
- var theNode:PathNode = openList[(node.xpos + i) + " " + (node.ypos + j)];
- //Calculates distance to end node
- theNode.distE = (Math.abs(theNode.xpos - endPts[0]) + Math.abs(theNode.ypos - endPts[1])) * 10;
- //Calculates distance to start node by adding 14 or 10 to the parent's distS score
- var cost = isDiag([theNode.parentNode.xpos,theNode.parentNode.ypos],[theNode.xpos,theNode.ypos]);
- theNode.distS = theNode.parentNode.distS + cost;
- }
- }
- } else { //what to do if it is already on the open list
- //this checks to see if it would be better to go to theNode through the currentNode instead of theNode's current parent.
- var theNode:PathNode = openList[(node.xpos + i) + " " + (node.ypos + j)];
- var cost = isDiag([node.xpos,node.ypos],[theNode.xpos,theNode.ypos]);
- if (node.distS + cost < theNode.distS) {
- //more efficient to make theNode's parents the current node.
- theNode.parentNode = node;
- }
- }
- }
- }
- }
- }
- var lowestTScore:int = 10000;
- var pointerTo:PathNode;
- for each (var Cur:PathNode in openList) {
- //loops through each node in openlist and finds the one with lowest total score.
- if (Cur.distS + Cur.distE < lowestTScore) {
- lowestTScore = Cur.distS + Cur.distE;
- pointerTo = Cur;
- }
- }
- //is openlist empty?
- if (lowestTScore == 10000) {
- return;
- } else {
- //do this again.
- FindPath(pointerTo);
- }
- }
- private function isDiag(par:Array, cur:Array):int {
- //returns 14 or 10
- var returnV:uint = 0;
- if (par[0] != cur[0]) {
- returnV++;
- }
- if (par[1] != cur[1]) {
- returnV++;
- }
- if (returnV == 2) {
- return 14;
- } else {
- return 10;
- }
- }
- private function retracePath():void{
- //retraces path from end to start.
- var endNode:PathNode = openList[endPts[0] + " " + endPts[1]];
- while(endNode.xpos != beginPts[0] && endNode.ypos != beginPts[1]){
- resultsArr.push([endNode.ypos, endNode.xpos, endNode.distS]); // 'ypos' first because I seem to have mixed up x and y so for the results I flip them.
- endNode = endNode.parentNode;
- }
- trace(resultsArr);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement