Created
September 25, 2014 01:39
-
-
Save jacobparis/2108ef9264a14009f9c7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package service; | |
import service.Node; | |
class Engine { | |
public var map:Array<Array<Node>>; | |
public var startNode(default, set_startNode):Node; | |
public var endNode(default, set_endNode):Node; | |
public function new(){ | |
this.map = new Array(); | |
var openList = new Array(); | |
var closeList = new Array(); | |
} | |
/** | |
* Generates the map | |
* @param width Width in tiles | |
* @param height Height in tiles | |
* @return | |
*/ | |
public function generateMap(width:Int, height:Int):Array<Array<Node>> { | |
for(xi in 0...width){ | |
map[xi] = new Array(); | |
for(yi in 0...height){ | |
map[xi][yi] = new Node(xi,yi,10, NORMAL_NODE); | |
map[xi][yi].setEngine(this); | |
} | |
} | |
return map; | |
} | |
public function getMap():Array<Array<Node>> { | |
return this.map; | |
} | |
public function set_startNode(node:Node):Node { | |
map[node.x][node.y] = node; | |
node.type = START_NODE; | |
return node; | |
} | |
public function set_endNode(node:Node):Node { | |
map[node.x][node.y] = node; | |
node.type = END_NODE; | |
return node; | |
} | |
public function getPath():Array<Node> { | |
var currentNode:Node = null; | |
var adjacent = null; | |
trace(this.startNode.x); | |
currentNode = this.startNode; | |
while (true) { | |
adjacent = currentNode.getAdjacentNodes(map); | |
adjacent.sort(function(node_a, node_b){ | |
var num = node_a.getF() - node_b.getF(); | |
if(num == 0){ | |
num = node_a.getH()-node_b.getH(); | |
} | |
return num; | |
}); | |
for(node in adjacent){ | |
if(!node.open){ | |
node.open = true; | |
node.parent = currentNode; | |
} | |
} | |
currentNode.close = true; | |
if (currentNode == endNode) { | |
trace("break"); | |
break; | |
} | |
currentNode = adjacent[0]; | |
} | |
var path:Array<Node> = new Array(); | |
var currentNode = this.endNode; | |
while(true){ | |
path.push(currentNode); | |
currentNode = currentNode.parent; | |
if(currentNode == startNode){ | |
break; | |
} | |
} | |
path.reverse(); | |
return path; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment