Working on pathfinding
This commit is contained in:
@@ -2,6 +2,9 @@ Programming:
|
|||||||
|
|
||||||
Andrew Kesterson (@akesterson)
|
Andrew Kesterson (@akesterson)
|
||||||
Phaser JS framework written by @photonstorm
|
Phaser JS framework written by @photonstorm
|
||||||
|
Phaser Pathfinding plugin by casarock (https://github.com/appsbu-de/phaser_plugin_pathfinding)
|
||||||
|
EasyStarJS by prettymuchbryce (https://github.com/prettymuchbryce/easystarjs)
|
||||||
|
|
||||||
|
|
||||||
Narrative/Dialog Writing:
|
Narrative/Dialog Writing:
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,8 @@
|
|||||||
<title>Moonlight Skulk (Working Title)</title>
|
<title>Moonlight Skulk (Working Title)</title>
|
||||||
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
|
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
|
||||||
<script type="text/javascript" src="js/phaser.js"></script>
|
<script type="text/javascript" src="js/phaser.js"></script>
|
||||||
<script type="text/javascript" src="http://polyk.ivank.net/polyk.js"></script>
|
<script type="text/javascript" src="js/PathFinderPlugin.js"></script>
|
||||||
|
<script type="text/javascript" src="js/easystar-0.1.5.js"></script>
|
||||||
<style type="text/css">
|
<style type="text/css">
|
||||||
body {
|
body {
|
||||||
margin: 0;
|
margin: 0;
|
||||||
|
|||||||
110
moonlight/js/PathFinderPlugin.js
Normal file
110
moonlight/js/PathFinderPlugin.js
Normal file
@@ -0,0 +1,110 @@
|
|||||||
|
/**
|
||||||
|
* Constructor.
|
||||||
|
*
|
||||||
|
* @param parent
|
||||||
|
* @constructor
|
||||||
|
*/
|
||||||
|
Phaser.Plugin.PathFinderPlugin = function (parent) {
|
||||||
|
|
||||||
|
if (typeof EasyStar !== 'object') {
|
||||||
|
throw new Error("Easystar is not defined!");
|
||||||
|
}
|
||||||
|
|
||||||
|
this.parent = parent;
|
||||||
|
this._easyStar = new EasyStar.js();
|
||||||
|
this._grid = null;
|
||||||
|
this._callback = null;
|
||||||
|
this._prepared = false;
|
||||||
|
this._walkables = [0];
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
Phaser.Plugin.PathFinderPlugin.prototype = Object.create(Phaser.Plugin.prototype);
|
||||||
|
Phaser.Plugin.PathFinderPlugin.prototype.constructor = Phaser.Plugin.PathFinderPlugin;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set Grid for Pathfinding.
|
||||||
|
*
|
||||||
|
* @param grid Mapdata as a two dimensional array.
|
||||||
|
* @param walkables Tiles which are walkable. Every other tile is marked as blocked.
|
||||||
|
* @param iterationsPerCount
|
||||||
|
*/
|
||||||
|
Phaser.Plugin.PathFinderPlugin.prototype.setGrid = function (grid, walkables, iterationsPerCount) {
|
||||||
|
iterationsPerCount = iterationsPerCount || null;
|
||||||
|
|
||||||
|
this._grid = [];
|
||||||
|
for (var i = 0; i < grid.length; i++)
|
||||||
|
{
|
||||||
|
this._grid[i] = []
|
||||||
|
for (var j = 0; j < grid[i].length; j++)
|
||||||
|
{
|
||||||
|
if (grid[i][j])
|
||||||
|
this._grid[i][j] = grid[i][j].index
|
||||||
|
else
|
||||||
|
this._grid[i][j] = 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this._walkables = walkables;
|
||||||
|
|
||||||
|
this._easyStar.setGrid(this._grid);
|
||||||
|
this._easyStar.setAcceptableTiles(this._walkables);
|
||||||
|
|
||||||
|
// initiate all walkable tiles with cost 1 so they will be walkable even if they are not on the grid map, jet.
|
||||||
|
for (var i = 0; i < walkables.length; i++)
|
||||||
|
{
|
||||||
|
this.setTileCost(walkables[i], 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (iterationsPerCount !== null) {
|
||||||
|
this._easyStar.setIterationsPerCalculation(iterationsPerCount);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the tile cost for a particular tile type.
|
||||||
|
*
|
||||||
|
* @param {Number} The tile type to set the cost for.
|
||||||
|
* @param {Number} The multiplicative cost associated with the given tile.
|
||||||
|
*/
|
||||||
|
Phaser.Plugin.PathFinderPlugin.prototype.setTileCost = function (tileType, cost) {
|
||||||
|
this._easyStar.setTileCost(tileType, cost);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set callback function (Uh, really?)
|
||||||
|
* @param callback
|
||||||
|
*/
|
||||||
|
Phaser.Plugin.PathFinderPlugin.prototype.setCallbackFunction = function (callback) {
|
||||||
|
this._callback = callback;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prepare pathcalculation for easystar.
|
||||||
|
*
|
||||||
|
* @param from array 0: x-coords, 1: y-coords ([x,y])
|
||||||
|
* @param to array 0: x-coords, 1: y-coords ([x,y])
|
||||||
|
*/
|
||||||
|
Phaser.Plugin.PathFinderPlugin.prototype.preparePathCalculation = function (from, to) {
|
||||||
|
if (this._callback === null || typeof this._callback !== "function") {
|
||||||
|
throw new Error("No Callback set!");
|
||||||
|
}
|
||||||
|
|
||||||
|
var startX = from[0],
|
||||||
|
startY = from[1],
|
||||||
|
destinationX = to[0],
|
||||||
|
destinationY = to[1];
|
||||||
|
|
||||||
|
this._easyStar.findPath(startX, startY, destinationX, destinationY, this._callback);
|
||||||
|
this._prepared = true;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Start path calculation.
|
||||||
|
*/
|
||||||
|
Phaser.Plugin.PathFinderPlugin.prototype.calculatePath = function () {
|
||||||
|
if (this._prepared === null) {
|
||||||
|
throw new Error("no Calculation prepared!");
|
||||||
|
}
|
||||||
|
|
||||||
|
this._easyStar.calculate();
|
||||||
|
};
|
||||||
555
moonlight/js/easystar-0.1.8.js
Normal file
555
moonlight/js/easystar-0.1.8.js
Normal file
@@ -0,0 +1,555 @@
|
|||||||
|
//For require.js
|
||||||
|
if (typeof define === "function" && define.amd) {
|
||||||
|
define("easystar", [], function() {
|
||||||
|
return EasyStar;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
//For browserify and node.js
|
||||||
|
if (typeof module !== 'undefined' && module.exports) {
|
||||||
|
module.exports = EasyStar;
|
||||||
|
}
|
||||||
|
//NameSpace
|
||||||
|
var EasyStar = EasyStar || {};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A simple Node that represents a single tile on the grid.
|
||||||
|
* @param {Object} parent The parent node.
|
||||||
|
* @param {Number} x The x position on the grid.
|
||||||
|
* @param {Number} y The y position on the grid.
|
||||||
|
* @param {Number} costSoFar How far this node is in moves*cost from the start.
|
||||||
|
* @param {Number} simpleDistanceToTarget Manhatten distance to the end point.
|
||||||
|
**/
|
||||||
|
EasyStar.Node = function(parent, x, y, costSoFar, simpleDistanceToTarget) {
|
||||||
|
this.parent = parent;
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
|
this.costSoFar = costSoFar;
|
||||||
|
this.simpleDistanceToTarget = simpleDistanceToTarget;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return {Number} Best guess distance of a cost using this node.
|
||||||
|
**/
|
||||||
|
this.bestGuessDistance = function() {
|
||||||
|
return this.costSoFar + this.simpleDistanceToTarget;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
//Constants
|
||||||
|
EasyStar.Node.OPEN_LIST = 0;
|
||||||
|
EasyStar.Node.CLOSED_LIST = 1;
|
||||||
|
/**
|
||||||
|
* This is an improved Priority Queue data type implementation that can be used to sort any object type.
|
||||||
|
* It uses a technique called a binary heap.
|
||||||
|
*
|
||||||
|
* For more on binary heaps see: http://en.wikipedia.org/wiki/Binary_heap
|
||||||
|
*
|
||||||
|
* @param {String} criteria The criteria by which to sort the objects.
|
||||||
|
* This should be a property of the objects you're sorting.
|
||||||
|
*
|
||||||
|
* @param {Number} heapType either PriorityQueue.MAX_HEAP or PriorityQueue.MIN_HEAP.
|
||||||
|
**/
|
||||||
|
EasyStar.PriorityQueue = function(criteria,heapType) {
|
||||||
|
this.length = 0; //The current length of heap.
|
||||||
|
var queue = [];
|
||||||
|
var isMax = false;
|
||||||
|
|
||||||
|
//Constructor
|
||||||
|
if (heapType==EasyStar.PriorityQueue.MAX_HEAP) {
|
||||||
|
isMax = true;
|
||||||
|
} else if (heapType==EasyStar.PriorityQueue.MIN_HEAP) {
|
||||||
|
isMax = false;
|
||||||
|
} else {
|
||||||
|
throw heapType + " not supported.";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Inserts the value into the heap and sorts it.
|
||||||
|
*
|
||||||
|
* @param value The object to insert into the heap.
|
||||||
|
**/
|
||||||
|
this.insert = function(value) {
|
||||||
|
if (!value.hasOwnProperty(criteria)) {
|
||||||
|
throw "Cannot insert " + value + " because it does not have a property by the name of " + criteria + ".";
|
||||||
|
}
|
||||||
|
queue.push(value);
|
||||||
|
this.length++;
|
||||||
|
bubbleUp(this.length-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Peeks at the highest priority element.
|
||||||
|
*
|
||||||
|
* @return the highest priority element
|
||||||
|
**/
|
||||||
|
this.getHighestPriorityElement = function() {
|
||||||
|
return queue[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes and returns the highest priority element from the queue.
|
||||||
|
*
|
||||||
|
* @return the highest priority element
|
||||||
|
**/
|
||||||
|
this.shiftHighestPriorityElement = function() {
|
||||||
|
if (this.length === 0) {
|
||||||
|
throw ("There are no more elements in your priority queue.");
|
||||||
|
} else if (this.length === 1) {
|
||||||
|
var onlyValue = queue[0];
|
||||||
|
queue = [];
|
||||||
|
this.length = 0;
|
||||||
|
return onlyValue;
|
||||||
|
}
|
||||||
|
var oldRoot = queue[0];
|
||||||
|
var newRoot = queue.pop();
|
||||||
|
this.length--;
|
||||||
|
queue[0] = newRoot;
|
||||||
|
swapUntilQueueIsCorrect(0);
|
||||||
|
return oldRoot;
|
||||||
|
}
|
||||||
|
|
||||||
|
var bubbleUp = function(index) {
|
||||||
|
if (index===0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var parent = getParentOf(index);
|
||||||
|
if (evaluate(index,parent)) {
|
||||||
|
swap(index,parent);
|
||||||
|
bubbleUp(parent);
|
||||||
|
} else {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var swapUntilQueueIsCorrect = function(value) {
|
||||||
|
var left = getLeftOf(value);
|
||||||
|
var right = getRightOf(value);
|
||||||
|
if (evaluate(left,value)) {
|
||||||
|
swap(value,left);
|
||||||
|
swapUntilQueueIsCorrect(left);
|
||||||
|
} else if (evaluate(right,value)) {
|
||||||
|
swap(value,right);
|
||||||
|
swapUntilQueueIsCorrect(right);
|
||||||
|
} else if (value==0) {
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
swapUntilQueueIsCorrect(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var swap = function(self,target) {
|
||||||
|
var placeHolder = queue[self];
|
||||||
|
queue[self] = queue[target];
|
||||||
|
queue[target] = placeHolder;
|
||||||
|
}
|
||||||
|
|
||||||
|
var evaluate = function(self,target) {
|
||||||
|
if (queue[target]===undefined||queue[self]===undefined) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
var selfValue;
|
||||||
|
var targetValue;
|
||||||
|
|
||||||
|
//Check if the criteria should be the result of a function call.
|
||||||
|
if (typeof queue[self][criteria] === 'function') {
|
||||||
|
selfValue = queue[self][criteria]();
|
||||||
|
targetValue = queue[target][criteria]();
|
||||||
|
} else {
|
||||||
|
selfValue = queue[self][criteria];
|
||||||
|
targetValue = queue[target][criteria];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isMax) {
|
||||||
|
if (selfValue > targetValue) {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (selfValue < targetValue) {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var getParentOf = function(index) {
|
||||||
|
return Math.floor(index/2)-1;
|
||||||
|
}
|
||||||
|
|
||||||
|
var getLeftOf = function(index) {
|
||||||
|
return index*2 + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
var getRightOf = function(index) {
|
||||||
|
return index*2 + 2;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
//Constants
|
||||||
|
EasyStar.PriorityQueue.MAX_HEAP = 0;
|
||||||
|
EasyStar.PriorityQueue.MIN_HEAP = 1;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a single instance of EasyStar.
|
||||||
|
* A path that is in the queue to eventually be found.
|
||||||
|
*/
|
||||||
|
EasyStar.instance = function() {
|
||||||
|
this.isDoneCalculating = true;
|
||||||
|
this.pointsToAvoid = {};
|
||||||
|
this.startX;
|
||||||
|
this.callback;
|
||||||
|
this.startY;
|
||||||
|
this.endX;
|
||||||
|
this.endY;
|
||||||
|
this.nodeHash = {};
|
||||||
|
this.openList;
|
||||||
|
};
|
||||||
|
/**
|
||||||
|
* EasyStar.js
|
||||||
|
* github.com/prettymuchbryce/EasyStarJS
|
||||||
|
* Licensed under the MIT license.
|
||||||
|
*
|
||||||
|
* Implementation By Bryce Neal (@prettymuchbryce)
|
||||||
|
**/
|
||||||
|
EasyStar.js = function() {
|
||||||
|
var STRAIGHT_COST = 10;
|
||||||
|
var DIAGONAL_COST = 14;
|
||||||
|
var pointsToAvoid = {};
|
||||||
|
var collisionGrid;
|
||||||
|
var costMap = {};
|
||||||
|
var iterationsSoFar;
|
||||||
|
var instances = [];
|
||||||
|
var iterationsPerCalculation = Number.MAX_VALUE;
|
||||||
|
var acceptableTiles;
|
||||||
|
var diagonalsEnabled = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the collision grid that EasyStar uses.
|
||||||
|
*
|
||||||
|
* @param {Array|Number} tiles An array of numbers that represent
|
||||||
|
* which tiles in your grid should be considered
|
||||||
|
* acceptable, or "walkable".
|
||||||
|
**/
|
||||||
|
this.setAcceptableTiles = function(tiles) {
|
||||||
|
if (tiles instanceof Array) {
|
||||||
|
//Array
|
||||||
|
acceptableTiles = tiles;
|
||||||
|
} else if (!isNaN(parseFloat(tiles)) && isFinite(tiles)) {
|
||||||
|
//Number
|
||||||
|
acceptableTiles = [tiles];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enable diagonal pathfinding.
|
||||||
|
*/
|
||||||
|
this.enableDiagonals = function() {
|
||||||
|
diagonalsEnabled = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Disable diagonal pathfinding.
|
||||||
|
*/
|
||||||
|
this.disableDiagonals = function() {
|
||||||
|
diagonalsEnabled = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the collision grid that EasyStar uses.
|
||||||
|
*
|
||||||
|
* @param {Array} grid The collision grid that this EasyStar instance will read from.
|
||||||
|
* This should be a 2D Array of Numbers.
|
||||||
|
**/
|
||||||
|
this.setGrid = function(grid) {
|
||||||
|
collisionGrid = grid;
|
||||||
|
|
||||||
|
//Setup cost map
|
||||||
|
for (var y = 0; y < collisionGrid.length; y++) {
|
||||||
|
for (var x = 0; x < collisionGrid[0].length; x++) {
|
||||||
|
if (!costMap[collisionGrid[y][x]]) {
|
||||||
|
costMap[collisionGrid[y][x]] = 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the tile cost for a particular tile type.
|
||||||
|
*
|
||||||
|
* @param {Number} The tile type to set the cost for.
|
||||||
|
* @param {Number} The multiplicative cost associated with the given tile.
|
||||||
|
**/
|
||||||
|
this.setTileCost = function(tileType, cost) {
|
||||||
|
costMap[tileType] = cost;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the number of search iterations per calculation.
|
||||||
|
* A lower number provides a slower result, but more practical if you
|
||||||
|
* have a large tile-map and don't want to block your thread while
|
||||||
|
* finding a path.
|
||||||
|
*
|
||||||
|
* @param {Number} iterations The number of searches to prefrom per calculate() call.
|
||||||
|
**/
|
||||||
|
this.setIterationsPerCalculation = function(iterations) {
|
||||||
|
iterationsPerCalculation = iterations;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Avoid a particular point on the grid,
|
||||||
|
* regardless of whether or not it is an acceptable tile.
|
||||||
|
*
|
||||||
|
* @param {Number} x The x value of the point to avoid.
|
||||||
|
* @param {Number} y The y value of the point to avoid.
|
||||||
|
**/
|
||||||
|
this.avoidAdditionalPoint = function(x, y) {
|
||||||
|
pointsToAvoid[x + "_" + y] = 1;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stop avoiding a particular point on the grid.
|
||||||
|
*
|
||||||
|
* @param {Number} x The x value of the point to stop avoiding.
|
||||||
|
* @param {Number} y The y value of the point to stop avoiding.
|
||||||
|
**/
|
||||||
|
this.stopAvoidingAdditionalPoint = function(x, y) {
|
||||||
|
delete pointsToAvoid[x + "_" + y];
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stop avoiding all additional points on the grid.
|
||||||
|
**/
|
||||||
|
this.stopAvoidingAllAdditionalPoints = function() {
|
||||||
|
pointsToAvoid = {};
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find a path.
|
||||||
|
*
|
||||||
|
* @param {Number} startX The X position of the starting point.
|
||||||
|
* @param {Number} startY The Y position of the starting point.
|
||||||
|
* @param {Number} endX The X position of the ending point.
|
||||||
|
* @param {Number} endY The Y position of the ending point.
|
||||||
|
* @param {Function} callback A function that is called when your path
|
||||||
|
* is found, or no path is found.
|
||||||
|
*
|
||||||
|
**/
|
||||||
|
this.findPath = function(startX, startY ,endX, endY, callback) {
|
||||||
|
//No acceptable tiles were set
|
||||||
|
if (acceptableTiles === undefined) {
|
||||||
|
throw "You can't set a path without first calling setAcceptableTiles() on EasyStar.";
|
||||||
|
}
|
||||||
|
//No grid was set
|
||||||
|
if (collisionGrid === undefined) {
|
||||||
|
throw "You can't set a path without first calling setGrid() on EasyStar.";
|
||||||
|
}
|
||||||
|
|
||||||
|
//Start or endpoint outside of scope.
|
||||||
|
if (startX < 0 || startY < 0 || endX < 0 || endX < 0 ||
|
||||||
|
startX > collisionGrid[0].length-1 || startY > collisionGrid.length-1 ||
|
||||||
|
endX > collisionGrid[0].length-1 || endY > collisionGrid.length-1) {
|
||||||
|
throw "Your start or end point is outside the scope of your grid.";
|
||||||
|
}
|
||||||
|
|
||||||
|
//Start and end are the same tile.
|
||||||
|
if (startX===endX && startY===endY) {
|
||||||
|
callback([]);
|
||||||
|
}
|
||||||
|
|
||||||
|
//End point is not an acceptable tile.
|
||||||
|
var endTile = collisionGrid[endY][endX];
|
||||||
|
var isAcceptable = false;
|
||||||
|
for (var i = 0; i < acceptableTiles.length; i++) {
|
||||||
|
if (endTile === acceptableTiles[i]) {
|
||||||
|
isAcceptable = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isAcceptable === false) {
|
||||||
|
callback(null);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Create the instance
|
||||||
|
var instance = new EasyStar.instance();
|
||||||
|
instance.openList = new EasyStar.PriorityQueue("bestGuessDistance",EasyStar.PriorityQueue.MIN_HEAP);
|
||||||
|
instance.isDoneCalculating = false;
|
||||||
|
instance.nodeHash = {};
|
||||||
|
instance.startX = startX;
|
||||||
|
instance.startY = startY;
|
||||||
|
instance.endX = endX;
|
||||||
|
instance.endY = endY;
|
||||||
|
instance.callback = callback;
|
||||||
|
|
||||||
|
instance.openList.insert(coordinateToNode(instance, instance.startX,
|
||||||
|
instance.startY, null, STRAIGHT_COST));
|
||||||
|
|
||||||
|
instances.push(instance);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method steps through the A* Algorithm in an attempt to
|
||||||
|
* find your path(s). It will search 4 tiles for every calculation.
|
||||||
|
* You can change the number of calculations done in a call by using
|
||||||
|
* easystar.setIteratonsPerCalculation().
|
||||||
|
**/
|
||||||
|
this.calculate = function() {
|
||||||
|
if (instances.length === 0 || collisionGrid === undefined || acceptableTiles === undefined) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (iterationsSoFar = 0; iterationsSoFar < iterationsPerCalculation; iterationsSoFar++) {
|
||||||
|
if (instances.length === 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Couldn't find a path.
|
||||||
|
if (instances[0].openList.length===0) {
|
||||||
|
instances[0].callback(null);
|
||||||
|
instances.shift();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var searchNode = instances[0].openList.shiftHighestPriorityElement();
|
||||||
|
searchNode.list = EasyStar.Node.CLOSED_LIST;
|
||||||
|
|
||||||
|
if (searchNode.y > 0) {
|
||||||
|
checkAdjacentNode(instances[0], searchNode, 0, -1, STRAIGHT_COST *
|
||||||
|
costMap[collisionGrid[searchNode.y-1][searchNode.x]]);
|
||||||
|
if (instances[0].isDoneCalculating===true) {
|
||||||
|
instances.shift();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (searchNode.x < collisionGrid[0].length-1) {
|
||||||
|
checkAdjacentNode(instances[0], searchNode, 1, 0, STRAIGHT_COST *
|
||||||
|
costMap[collisionGrid[searchNode.y][searchNode.x+1]]);
|
||||||
|
if (instances[0].isDoneCalculating===true) {
|
||||||
|
instances.shift();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (searchNode.y < collisionGrid.length-1) {
|
||||||
|
checkAdjacentNode(instances[0], searchNode, 0, 1, STRAIGHT_COST *
|
||||||
|
costMap[collisionGrid[searchNode.y+1][searchNode.x]]);
|
||||||
|
if (instances[0].isDoneCalculating===true) {
|
||||||
|
instances.shift();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (searchNode.x > 0) {
|
||||||
|
checkAdjacentNode(instances[0], searchNode, -1, 0, STRAIGHT_COST *
|
||||||
|
costMap[collisionGrid[searchNode.y][searchNode.x-1]]);
|
||||||
|
if (instances[0].isDoneCalculating===true) {
|
||||||
|
instances.shift();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (diagonalsEnabled) {
|
||||||
|
if (searchNode.x > 0 && searchNode.y > 0) {
|
||||||
|
checkAdjacentNode(instances[0], searchNode, -1, -1, DIAGONAL_COST *
|
||||||
|
costMap[collisionGrid[searchNode.y-1][searchNode.x-1]]);
|
||||||
|
if (instances[0].isDoneCalculating===true) {
|
||||||
|
instances.shift();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (searchNode.x < collisionGrid[0].length-1 && searchNode.y < collisionGrid.length-1) {
|
||||||
|
checkAdjacentNode(instances[0], searchNode, 1, 1, DIAGONAL_COST *
|
||||||
|
costMap[collisionGrid[searchNode.y+1][searchNode.x+1]]);
|
||||||
|
if (instances[0].isDoneCalculating===true) {
|
||||||
|
instances.shift();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (searchNode.x < collisionGrid[0].length-1 && searchNode.y > 0) {
|
||||||
|
checkAdjacentNode(instances[0], searchNode, 1, -1, DIAGONAL_COST *
|
||||||
|
costMap[collisionGrid[searchNode.y-1][searchNode.x+1]]);
|
||||||
|
if (instances[0].isDoneCalculating===true) {
|
||||||
|
instances.shift();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (searchNode.x > 0 && searchNode.y < collisionGrid.length-1) {
|
||||||
|
checkAdjacentNode(instances[0], searchNode, -1, 1, DIAGONAL_COST *
|
||||||
|
costMap[collisionGrid[searchNode.y+1][searchNode.x-1]]);
|
||||||
|
if (instances[0].isDoneCalculating===true) {
|
||||||
|
instances.shift();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
//Private methods follow
|
||||||
|
|
||||||
|
var checkAdjacentNode = function(instance, searchNode, x, y, cost) {
|
||||||
|
var adjacentCoordinateX = searchNode.x+x;
|
||||||
|
var adjacentCoordinateY = searchNode.y+y;
|
||||||
|
|
||||||
|
if (instance.endX === adjacentCoordinateX && instance.endY === adjacentCoordinateY) {
|
||||||
|
instance.isDoneCalculating = true;
|
||||||
|
var path = [];
|
||||||
|
var pathLen = 0;
|
||||||
|
path[pathLen] = {x: adjacentCoordinateX, y: adjacentCoordinateY};
|
||||||
|
pathLen++;
|
||||||
|
path[pathLen] = {x: searchNode.x, y:searchNode.y};
|
||||||
|
pathLen++;
|
||||||
|
var parent = searchNode.parent;
|
||||||
|
while (parent!=null) {
|
||||||
|
path[pathLen] = {x: parent.x, y:parent.y};
|
||||||
|
pathLen++;
|
||||||
|
parent = parent.parent;
|
||||||
|
}
|
||||||
|
path.reverse();
|
||||||
|
instance.callback(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pointsToAvoid[adjacentCoordinateX + "_" + adjacentCoordinateY] === undefined) {
|
||||||
|
for (var i = 0; i < acceptableTiles.length; i++) {
|
||||||
|
if (collisionGrid[adjacentCoordinateY][adjacentCoordinateX] === acceptableTiles[i]) {
|
||||||
|
|
||||||
|
var node = coordinateToNode(instance, adjacentCoordinateX,
|
||||||
|
adjacentCoordinateY, searchNode, cost);
|
||||||
|
|
||||||
|
if (node.list === undefined) {
|
||||||
|
node.list = EasyStar.Node.OPEN_LIST;
|
||||||
|
instance.openList.insert(node);
|
||||||
|
} else if (node.list === EasyStar.Node.OPEN_LIST) {
|
||||||
|
if (searchNode.costSoFar + cost < node.costSoFar) {
|
||||||
|
node.costSoFar = searchNode.costSoFar + cost;
|
||||||
|
node.parent = searchNode;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
//Helpers
|
||||||
|
|
||||||
|
var coordinateToNode = function(instance, x, y, parent, cost) {
|
||||||
|
if (instance.nodeHash[x + "_" + y]!==undefined) {
|
||||||
|
return instance.nodeHash[x + "_" + y];
|
||||||
|
}
|
||||||
|
var simpleDistanceToTarget = getDistance(x, y, instance.endX, instance.endY);
|
||||||
|
if (parent!==null) {
|
||||||
|
var costSoFar = parent.costSoFar + cost;
|
||||||
|
} else {
|
||||||
|
costSoFar = simpleDistanceToTarget;
|
||||||
|
}
|
||||||
|
var node = new EasyStar.Node(parent,x,y,costSoFar,simpleDistanceToTarget);
|
||||||
|
instance.nodeHash[x + "_" + y] = node;
|
||||||
|
return node;
|
||||||
|
};
|
||||||
|
|
||||||
|
var getDistance = function(x1,y1,x2,y2) {
|
||||||
|
return Math.sqrt(Math.abs(x2-x1)*Math.abs(x2-x1) + Math.abs(y2-y1)*Math.abs(y2-y1)) * STRAIGHT_COST;
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -32,6 +32,8 @@ SPRITE_TOWNSFOLK_FEMALE4 = 8;
|
|||||||
SPRITE_TOWNSFOLK_GUARD1 = 9;
|
SPRITE_TOWNSFOLK_GUARD1 = 9;
|
||||||
SPRITE_TOWNSFOLK_GUARD2 = 10;
|
SPRITE_TOWNSFOLK_GUARD2 = 10;
|
||||||
|
|
||||||
|
var pathfinder = null;
|
||||||
|
|
||||||
var game = new Phaser.Game(640, 480, Phaser.AUTO, '');
|
var game = new Phaser.Game(640, 480, Phaser.AUTO, '');
|
||||||
|
|
||||||
// Create torch objects
|
// Create torch objects
|
||||||
@@ -853,7 +855,26 @@ var AISprite = function(game, x, y, key, frame) {
|
|||||||
|
|
||||||
this.action_chaseplayer = function()
|
this.action_chaseplayer = function()
|
||||||
{
|
{
|
||||||
console.log("I AM CHASING THE PLAYER");
|
var newpath = []
|
||||||
|
var movingstate = STATE_NONE;
|
||||||
|
pathfinder.setCallbackFunction(function(path) {
|
||||||
|
newpath = (path || []);
|
||||||
|
});
|
||||||
|
this.path = newpath;
|
||||||
|
pathfinder.preparePathCalculation([0,0], [player.x/32, player.y/32]);
|
||||||
|
pathfinder.calculatePath();
|
||||||
|
if ( this.path[0].x < this.x ) {
|
||||||
|
movingstate = STATE_FACE_LEFT | STATE_MOVING | STATE_RUNNING;
|
||||||
|
} else if ( this.path[0].x > this.x ) {
|
||||||
|
movingstate = STATE_FACE_RIGHT | STATE_MOVING | STATE_RUNNING;
|
||||||
|
} else if ( this.path[0].y < this.y ) {
|
||||||
|
movingstate = STATE_FACE_UP | STATE_MOVING | STATE_RUNNING;
|
||||||
|
} else if ( this.path[0].y > this.y ) {
|
||||||
|
movingstate = STATE_FACE_DOWN | STATE_MOVING | STATE_RUNNING;
|
||||||
|
} else {
|
||||||
|
movingstate = (this.state & STATES_FACE);
|
||||||
|
}
|
||||||
|
setMovingState(this, movingstate);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.action_reportplayer = function()
|
this.action_reportplayer = function()
|
||||||
@@ -874,9 +895,6 @@ var AISprite = function(game, x, y, key, frame) {
|
|||||||
}
|
}
|
||||||
if ( game.rnd.integerInRange(0, 100) < 95 )
|
if ( game.rnd.integerInRange(0, 100) < 95 )
|
||||||
return;
|
return;
|
||||||
if ( game.rnd.integerInRange(0, 100) > 90 ) {
|
|
||||||
newstate = STATE_RUNNING;
|
|
||||||
}
|
|
||||||
switch ( game.rnd.integerInRange(0, 4) ) {
|
switch ( game.rnd.integerInRange(0, 4) ) {
|
||||||
case 0: {
|
case 0: {
|
||||||
newstate = newstate | (STATE_FACE_RIGHT | STATE_MOVING);
|
newstate = newstate | (STATE_FACE_RIGHT | STATE_MOVING);
|
||||||
@@ -1022,6 +1040,7 @@ var GameState = function(game) {
|
|||||||
|
|
||||||
GameState.prototype.create = function()
|
GameState.prototype.create = function()
|
||||||
{
|
{
|
||||||
|
this.pathfinding_grid = []
|
||||||
this.map = this.add.tilemap('map');
|
this.map = this.add.tilemap('map');
|
||||||
for (var k in moonlightSettings['map']['tilesets']) {
|
for (var k in moonlightSettings['map']['tilesets']) {
|
||||||
var ts = moonlightSettings['map']['tilesets'][k];
|
var ts = moonlightSettings['map']['tilesets'][k];
|
||||||
@@ -1033,7 +1052,7 @@ GameState.prototype.create = function()
|
|||||||
for (var ln in moonlightSettings['map']['layers']) {
|
for (var ln in moonlightSettings['map']['layers']) {
|
||||||
lp = moonlightSettings['map']['layers'][ln];
|
lp = moonlightSettings['map']['layers'][ln];
|
||||||
if ( lp['type'] == "tiles" ) {
|
if ( lp['type'] == "tiles" ) {
|
||||||
layer = this.map.createLayer(ln);
|
layer = this.map.createLayer(ln);
|
||||||
this.map.setCollisionBetween(
|
this.map.setCollisionBetween(
|
||||||
lp['collisionBetween'][0],
|
lp['collisionBetween'][0],
|
||||||
lp['collisionBetween'][1],
|
lp['collisionBetween'][1],
|
||||||
@@ -1052,11 +1071,26 @@ GameState.prototype.create = function()
|
|||||||
};
|
};
|
||||||
if ( lp['collides'] == true ) {
|
if ( lp['collides'] == true ) {
|
||||||
this.map_collision_layers.push(layer);
|
this.map_collision_layers.push(layer);
|
||||||
|
for (var i = 0; i < layer.data.length; i++)
|
||||||
|
{
|
||||||
|
if ( this.pathfinding_grid.length <= i )
|
||||||
|
this.pathfinding_grid[i] = [];
|
||||||
|
for (var j = 0; j < layer.data[i].length; j++)
|
||||||
|
{
|
||||||
|
if (layer.data[i][j])
|
||||||
|
this.pathfinding_grid[i][j] = layer.data[i][j].index;
|
||||||
|
else
|
||||||
|
this.pathfinding_grid[i][j] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
layer.resizeWorld();
|
layer.resizeWorld();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pathfinder = game.plugins.add(Phaser.Plugin.PathFinderPlugin);
|
||||||
|
pathfinder.setGrid(map.layers[0].data, walkables);
|
||||||
|
|
||||||
this.physics.arcade.enable(player);
|
this.physics.arcade.enable(player);
|
||||||
player.body.center = new Phaser.Point(player.body.width / 2, player.body.height + player.body.halfHeight);
|
player.body.center = new Phaser.Point(player.body.width / 2, player.body.height + player.body.halfHeight);
|
||||||
player.body.collideWorldBounds = true;
|
player.body.collideWorldBounds = true;
|
||||||
|
|||||||
Reference in New Issue
Block a user