mirror of
https://github.com/overte-org/overte.git
synced 2025-04-20 02:03:57 +02:00
Merge pull request #6344 from imgntn/easystar
Adapt EasyStar A* Pathfinding Library and Add Example
This commit is contained in:
commit
c4ad1d978b
3 changed files with 1891 additions and 0 deletions
743
examples/libraries/easyStar.js
Normal file
743
examples/libraries/easyStar.js
Normal file
|
@ -0,0 +1,743 @@
|
|||
// The MIT License (MIT)
|
||||
|
||||
// Copyright (c) 2012-2015 Bryce Neal
|
||||
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
// Adapted for High Fidelity by James B. Pollack on 11/6/2015
|
||||
|
||||
loadEasyStar = function() {
|
||||
var ezStar = eStar();
|
||||
return new ezStar.js()
|
||||
}
|
||||
|
||||
var eStar = function() {
|
||||
|
||||
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 - 1) / 2);
|
||||
}
|
||||
|
||||
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 = 1.0;
|
||||
var DIAGONAL_COST = 1.4;
|
||||
var syncEnabled = false;
|
||||
var pointsToAvoid = {};
|
||||
var collisionGrid;
|
||||
var costMap = {};
|
||||
var pointsToCost = {};
|
||||
var allowCornerCutting = true;
|
||||
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];
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Enables sync mode for this EasyStar instance..
|
||||
* if you're into that sort of thing.
|
||||
**/
|
||||
this.enableSync = function() {
|
||||
syncEnabled = true;
|
||||
};
|
||||
|
||||
/**
|
||||
* Disables sync mode for this EasyStar instance.
|
||||
**/
|
||||
this.disableSync = function() {
|
||||
syncEnabled = false;
|
||||
};
|
||||
|
||||
/**
|
||||
* 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 an additional cost for a particular point.
|
||||
* Overrides the cost from setTileCost.
|
||||
*
|
||||
* @param {Number} x The x value of the point to cost.
|
||||
* @param {Number} y The y value of the point to cost.
|
||||
* @param {Number} The multiplicative cost associated with the given point.
|
||||
**/
|
||||
this.setAdditionalPointCost = function(x, y, cost) {
|
||||
pointsToCost[x + '_' + y] = cost;
|
||||
};
|
||||
|
||||
/**
|
||||
* Remove the additional cost for a particular point.
|
||||
*
|
||||
* @param {Number} x The x value of the point to stop costing.
|
||||
* @param {Number} y The y value of the point to stop costing.
|
||||
**/
|
||||
this.removeAdditionalPointCost = function(x, y) {
|
||||
delete pointsToCost[x + '_' + y];
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove all additional point costs.
|
||||
**/
|
||||
this.removeAllAdditionalPointCosts = function() {
|
||||
pointsToCost = {};
|
||||
}
|
||||
|
||||
/**
|
||||
* 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];
|
||||
};
|
||||
|
||||
/**
|
||||
* Enables corner cutting in diagonal movement.
|
||||
**/
|
||||
this.enableCornerCutting = function() {
|
||||
allowCornerCutting = true;
|
||||
};
|
||||
|
||||
/**
|
||||
* Disables corner cutting in diagonal movement.
|
||||
**/
|
||||
this.disableCornerCutting = function() {
|
||||
allowCornerCutting = false;
|
||||
};
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
// Wraps the callback for sync vs async logic
|
||||
var callbackWrapper = function(result) {
|
||||
if (syncEnabled) {
|
||||
callback(result);
|
||||
} else {
|
||||
Script.setTimeout(function() {
|
||||
callback(result);
|
||||
}, 1);
|
||||
}
|
||||
}
|
||||
|
||||
// No acceptable tiles were set
|
||||
if (acceptableTiles === undefined) {
|
||||
throw new Error("You can't set a path without first calling setAcceptableTiles() on EasyStar.");
|
||||
}
|
||||
// No grid was set
|
||||
if (collisionGrid === undefined) {
|
||||
throw new Error("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 new Error("Your start or end point is outside the scope of your grid.");
|
||||
}
|
||||
|
||||
// Start and end are the same tile.
|
||||
if (startX === endX && startY === endY) {
|
||||
callbackWrapper([]);
|
||||
return;
|
||||
}
|
||||
|
||||
// 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) {
|
||||
callbackWrapper(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 = callbackWrapper;
|
||||
|
||||
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-8 tiles (depending on diagonals) 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;
|
||||
}
|
||||
|
||||
if (syncEnabled) {
|
||||
// If this is a sync instance, we want to make sure that it calculates synchronously.
|
||||
iterationsSoFar = 0;
|
||||
}
|
||||
|
||||
// Couldn't find a path.
|
||||
if (instances[0].openList.length === 0) {
|
||||
var ic = instances[0];
|
||||
ic.callback(null);
|
||||
instances.shift();
|
||||
continue;
|
||||
}
|
||||
|
||||
var searchNode = instances[0].openList.shiftHighestPriorityElement();
|
||||
|
||||
var tilesToSearch = [];
|
||||
searchNode.list = EasyStar.Node.CLOSED_LIST;
|
||||
|
||||
if (searchNode.y > 0) {
|
||||
tilesToSearch.push({
|
||||
instance: instances[0],
|
||||
searchNode: searchNode,
|
||||
x: 0,
|
||||
y: -1,
|
||||
cost: STRAIGHT_COST * getTileCost(searchNode.x, searchNode.y - 1)
|
||||
});
|
||||
}
|
||||
if (searchNode.x < collisionGrid[0].length - 1) {
|
||||
tilesToSearch.push({
|
||||
instance: instances[0],
|
||||
searchNode: searchNode,
|
||||
x: 1,
|
||||
y: 0,
|
||||
cost: STRAIGHT_COST * getTileCost(searchNode.x + 1, searchNode.y)
|
||||
});
|
||||
}
|
||||
if (searchNode.y < collisionGrid.length - 1) {
|
||||
tilesToSearch.push({
|
||||
instance: instances[0],
|
||||
searchNode: searchNode,
|
||||
x: 0,
|
||||
y: 1,
|
||||
cost: STRAIGHT_COST * getTileCost(searchNode.x, searchNode.y + 1)
|
||||
});
|
||||
}
|
||||
if (searchNode.x > 0) {
|
||||
tilesToSearch.push({
|
||||
instance: instances[0],
|
||||
searchNode: searchNode,
|
||||
x: -1,
|
||||
y: 0,
|
||||
cost: STRAIGHT_COST * getTileCost(searchNode.x - 1, searchNode.y)
|
||||
});
|
||||
}
|
||||
if (diagonalsEnabled) {
|
||||
if (searchNode.x > 0 && searchNode.y > 0) {
|
||||
|
||||
if (allowCornerCutting ||
|
||||
(isTileWalkable(collisionGrid, acceptableTiles, searchNode.x, searchNode.y - 1) &&
|
||||
isTileWalkable(collisionGrid, acceptableTiles, searchNode.x - 1, searchNode.y))) {
|
||||
|
||||
tilesToSearch.push({
|
||||
instance: instances[0],
|
||||
searchNode: searchNode,
|
||||
x: -1,
|
||||
y: -1,
|
||||
cost: DIAGONAL_COST * getTileCost(searchNode.x - 1, searchNode.y - 1)
|
||||
});
|
||||
}
|
||||
}
|
||||
if (searchNode.x < collisionGrid[0].length - 1 && searchNode.y < collisionGrid.length - 1) {
|
||||
|
||||
if (allowCornerCutting ||
|
||||
(isTileWalkable(collisionGrid, acceptableTiles, searchNode.x, searchNode.y + 1) &&
|
||||
isTileWalkable(collisionGrid, acceptableTiles, searchNode.x + 1, searchNode.y))) {
|
||||
|
||||
tilesToSearch.push({
|
||||
instance: instances[0],
|
||||
searchNode: searchNode,
|
||||
x: 1,
|
||||
y: 1,
|
||||
cost: DIAGONAL_COST * getTileCost(searchNode.x + 1, searchNode.y + 1)
|
||||
});
|
||||
}
|
||||
}
|
||||
if (searchNode.x < collisionGrid[0].length - 1 && searchNode.y > 0) {
|
||||
|
||||
if (allowCornerCutting ||
|
||||
(isTileWalkable(collisionGrid, acceptableTiles, searchNode.x, searchNode.y - 1) &&
|
||||
isTileWalkable(collisionGrid, acceptableTiles, searchNode.x + 1, searchNode.y))) {
|
||||
|
||||
|
||||
tilesToSearch.push({
|
||||
instance: instances[0],
|
||||
searchNode: searchNode,
|
||||
x: 1,
|
||||
y: -1,
|
||||
cost: DIAGONAL_COST * getTileCost(searchNode.x + 1, searchNode.y - 1)
|
||||
});
|
||||
}
|
||||
}
|
||||
if (searchNode.x > 0 && searchNode.y < collisionGrid.length - 1) {
|
||||
|
||||
if (allowCornerCutting ||
|
||||
(isTileWalkable(collisionGrid, acceptableTiles, searchNode.x, searchNode.y + 1) &&
|
||||
isTileWalkable(collisionGrid, acceptableTiles, searchNode.x - 1, searchNode.y))) {
|
||||
|
||||
|
||||
tilesToSearch.push({
|
||||
instance: instances[0],
|
||||
searchNode: searchNode,
|
||||
x: -1,
|
||||
y: 1,
|
||||
cost: DIAGONAL_COST * getTileCost(searchNode.x - 1, searchNode.y + 1)
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// First sort all of the potential nodes we could search by their cost + heuristic distance.
|
||||
tilesToSearch.sort(function(a, b) {
|
||||
var aCost = a.cost + getDistance(searchNode.x + a.x, searchNode.y + a.y, instances[0].endX, instances[0].endY)
|
||||
var bCost = b.cost + getDistance(searchNode.x + b.x, searchNode.y + b.y, instances[0].endX, instances[0].endY)
|
||||
if (aCost < bCost) {
|
||||
return -1;
|
||||
} else if (aCost === bCost) {
|
||||
return 0;
|
||||
} else {
|
||||
return 1;
|
||||
}
|
||||
});
|
||||
|
||||
var isDoneCalculating = false;
|
||||
|
||||
// Search all of the surrounding nodes
|
||||
for (var i = 0; i < tilesToSearch.length; i++) {
|
||||
checkAdjacentNode(tilesToSearch[i].instance, tilesToSearch[i].searchNode,
|
||||
tilesToSearch[i].x, tilesToSearch[i].y, tilesToSearch[i].cost);
|
||||
if (tilesToSearch[i].instance.isDoneCalculating === true) {
|
||||
isDoneCalculating = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (isDoneCalculating) {
|
||||
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 (pointsToAvoid[adjacentCoordinateX + "_" + adjacentCoordinateY] === undefined) {
|
||||
// Handles the case where we have found the destination
|
||||
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();
|
||||
var ic = instance;
|
||||
var ip = path;
|
||||
ic.callback(ip);
|
||||
return
|
||||
}
|
||||
|
||||
if (isTileWalkable(collisionGrid, acceptableTiles, adjacentCoordinateX, adjacentCoordinateY)) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Helpers
|
||||
var isTileWalkable = function(collisionGrid, acceptableTiles, x, y) {
|
||||
for (var i = 0; i < acceptableTiles.length; i++) {
|
||||
if (collisionGrid[y][x] === acceptableTiles[i]) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
var getTileCost = function(x, y) {
|
||||
return pointsToCost[x + '_' + y] || costMap[collisionGrid[y][x]]
|
||||
};
|
||||
|
||||
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((x2 -= x1) * x2 + (y2 -= y1) * y2);
|
||||
};
|
||||
}
|
||||
return EasyStar
|
||||
}
|
270
examples/libraries/easyStarExample.js
Normal file
270
examples/libraries/easyStarExample.js
Normal file
|
@ -0,0 +1,270 @@
|
|||
//
|
||||
// easyStarExample.js
|
||||
//
|
||||
// Created by James B. Pollack @imgntn on 11/9/2015
|
||||
// Copyright 2015 High Fidelity, Inc.
|
||||
//
|
||||
// This is a script that sets up a grid of obstacles and passable tiles, finds a path, and then moves an entity along the path.
|
||||
//
|
||||
// Distributed under the Apache License, Version 2.0.
|
||||
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
||||
|
||||
// To-Do:
|
||||
// Abstract start position and make tiles, spheres, etc. relative
|
||||
// Handle dynamically changing grids
|
||||
|
||||
Script.include('easyStar.js');
|
||||
var easystar = loadEasyStar();
|
||||
Script.include('tween.js');
|
||||
var TWEEN = loadTween();
|
||||
|
||||
var ANIMATION_DURATION = 350;
|
||||
var grid = [
|
||||
[0, 0, 1, 0, 0, 0, 0, 0, 0],
|
||||
[0, 1, 1, 0, 1, 0, 1, 0, 0],
|
||||
[0, 0, 1, 0, 0, 0, 0, 1, 1],
|
||||
[0, 0, 1, 1, 0, 0, 0, 0, 0],
|
||||
[0, 0, 0, 0, 1, 1, 1, 0, 0],
|
||||
[0, 0, 0, 0, 0, 0, 0, 0, 0]
|
||||
];
|
||||
|
||||
|
||||
easystar.setGrid(grid);
|
||||
|
||||
easystar.setAcceptableTiles([0]);
|
||||
easystar.enableCornerCutting();
|
||||
easystar.findPath(0, 0, 8, 0, function(path) {
|
||||
if (path === null) {
|
||||
print("Path was not found.");
|
||||
Script.update.disconnect(tickEasyStar);
|
||||
} else {
|
||||
print('path' + JSON.stringify(path));
|
||||
|
||||
convertPath(path);
|
||||
Script.update.disconnect(tickEasyStar);
|
||||
}
|
||||
});
|
||||
|
||||
var tickEasyStar = function() {
|
||||
easystar.calculate();
|
||||
}
|
||||
|
||||
Script.update.connect(tickEasyStar);
|
||||
|
||||
//a sphere that will get moved
|
||||
var playerSphere = Entities.addEntity({
|
||||
type: 'Sphere',
|
||||
shape: 'Sphere',
|
||||
color: {
|
||||
red: 255,
|
||||
green: 0,
|
||||
blue: 0
|
||||
},
|
||||
dimensions: {
|
||||
x: 0.5,
|
||||
y: 0.5,
|
||||
z: 0.5
|
||||
},
|
||||
position: {
|
||||
x: 0,
|
||||
y: 0,
|
||||
z: 0
|
||||
},
|
||||
gravity: {
|
||||
x: 0,
|
||||
y: -9.8,
|
||||
z: 0
|
||||
},
|
||||
collisionsWillMove: true,
|
||||
linearDamping: 0.2
|
||||
});
|
||||
|
||||
Script.setInterval(function(){
|
||||
Entities.editEntity(playerSphere,{
|
||||
velocity:{
|
||||
x:0,
|
||||
y:4,
|
||||
z:0
|
||||
}
|
||||
})
|
||||
},1000)
|
||||
|
||||
var sphereProperties;
|
||||
|
||||
//for keeping track of entities
|
||||
var obstacles = [];
|
||||
var passables = [];
|
||||
|
||||
function createPassableAtTilePosition(posX, posY) {
|
||||
var properties = {
|
||||
type: 'Box',
|
||||
shapeType: 'Box',
|
||||
dimensions: {
|
||||
x: 1,
|
||||
y: 1,
|
||||
z: 1
|
||||
},
|
||||
position: {
|
||||
x: posY,
|
||||
y: -1,
|
||||
z: posX
|
||||
},
|
||||
color: {
|
||||
red: 0,
|
||||
green: 0,
|
||||
blue: 255
|
||||
}
|
||||
};
|
||||
var passable = Entities.addEntity(properties);
|
||||
passables.push(passable);
|
||||
}
|
||||
|
||||
|
||||
|
||||
function createObstacleAtTilePosition(posX, posY) {
|
||||
var properties = {
|
||||
type: 'Box',
|
||||
shapeType: 'Box',
|
||||
dimensions: {
|
||||
x: 1,
|
||||
y: 2,
|
||||
z: 1
|
||||
},
|
||||
position: {
|
||||
x: posY,
|
||||
y: 0,
|
||||
z: posX
|
||||
},
|
||||
color: {
|
||||
red: 0,
|
||||
green: 255,
|
||||
blue: 0
|
||||
}
|
||||
};
|
||||
var obstacle = Entities.addEntity(properties);
|
||||
obstacles.push(obstacle);
|
||||
}
|
||||
|
||||
function createObstacles(grid) {
|
||||
grid.forEach(function(row, rowIndex) {
|
||||
row.forEach(function(v, index) {
|
||||
if (v === 1) {
|
||||
createObstacleAtTilePosition(rowIndex, index);
|
||||
}
|
||||
if (v === 0) {
|
||||
createPassableAtTilePosition(rowIndex, index);
|
||||
}
|
||||
})
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
|
||||
createObstacles(grid);
|
||||
|
||||
|
||||
var currentSpherePosition = {
|
||||
x: 0,
|
||||
y: 0,
|
||||
z: 0
|
||||
};
|
||||
|
||||
|
||||
function convertPathPointToCoordinates(x, y) {
|
||||
return {
|
||||
x: y,
|
||||
z: x
|
||||
};
|
||||
}
|
||||
|
||||
var convertedPath = [];
|
||||
|
||||
//convert our path to Vec3s
|
||||
function convertPath(path) {
|
||||
path.forEach(function(point) {
|
||||
var convertedPoint = convertPathPointToCoordinates(point.x, point.y);
|
||||
convertedPath.push(convertedPoint);
|
||||
});
|
||||
createTweenPath(convertedPath);
|
||||
}
|
||||
|
||||
function updatePosition() {
|
||||
sphereProperties = Entities.getEntityProperties(playerSphere, "position");
|
||||
|
||||
Entities.editEntity(playerSphere, {
|
||||
position: {
|
||||
x: currentSpherePosition.z,
|
||||
y: sphereProperties.position.y,
|
||||
z: currentSpherePosition.x
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
var upVelocity = {
|
||||
x: 0,
|
||||
y: 2.5,
|
||||
z: 0
|
||||
}
|
||||
|
||||
var noVelocity = {
|
||||
x: 0,
|
||||
y: -3.5,
|
||||
z: 0
|
||||
}
|
||||
|
||||
function createTweenPath(convertedPath) {
|
||||
var i;
|
||||
var stepTweens = [];
|
||||
var velocityTweens = [];
|
||||
|
||||
//create the tweens
|
||||
for (i = 0; i < convertedPath.length - 1; i++) {
|
||||
var stepTween = new TWEEN.Tween(currentSpherePosition).to(convertedPath[i + 1], ANIMATION_DURATION).onUpdate(updatePosition).onComplete(tweenStep);
|
||||
|
||||
|
||||
stepTweens.push(stepTween);
|
||||
}
|
||||
|
||||
var j;
|
||||
//chain one tween to the next
|
||||
for (j = 0; j < stepTweens.length - 1; j++) {
|
||||
stepTweens[j].chain(stepTweens[j + 1]);
|
||||
}
|
||||
|
||||
//start the tween
|
||||
stepTweens[0].start();
|
||||
|
||||
}
|
||||
|
||||
var velocityShaper = {
|
||||
x: 0,
|
||||
y: 0,
|
||||
z: 0
|
||||
}
|
||||
|
||||
function tweenStep() {
|
||||
// print('step between tweens')
|
||||
}
|
||||
|
||||
function updateTweens() {
|
||||
//hook tween updates into our update loop
|
||||
TWEEN.update();
|
||||
}
|
||||
|
||||
Script.update.connect(updateTweens);
|
||||
|
||||
function cleanup() {
|
||||
while (obstacles.length > 0) {
|
||||
Entities.deleteEntity(obstacles.pop());
|
||||
}
|
||||
while (passables.length > 0) {
|
||||
Entities.deleteEntity(passables.pop());
|
||||
}
|
||||
Entities.deleteEntity(playerSphere);
|
||||
Script.update.disconnect(updateTweens);
|
||||
}
|
||||
|
||||
|
||||
Script.scriptEnding.connect(cleanup);
|
878
examples/libraries/tween.js
Normal file
878
examples/libraries/tween.js
Normal file
|
@ -0,0 +1,878 @@
|
|||
/**
|
||||
* Tween.js - Licensed under the MIT license
|
||||
* https://github.com/tweenjs/tween.js
|
||||
* ----------------------------------------------
|
||||
*
|
||||
* See https://github.com/tweenjs/tween.js/graphs/contributors for the full list of contributors.
|
||||
* Thank you all, you're awesome!
|
||||
*/
|
||||
|
||||
// Include a performance.now polyfill
|
||||
(function () {
|
||||
window= {}
|
||||
if ('performance' in window === false) {
|
||||
window.performance = {};
|
||||
}
|
||||
|
||||
// IE 8
|
||||
Date.now = (Date.now || function () {
|
||||
return new Date().getTime();
|
||||
});
|
||||
|
||||
if ('now' in window.performance === false) {
|
||||
var offset = window.performance.timing && window.performance.timing.navigationStart ? window.performance.timing.navigationStart
|
||||
: Date.now();
|
||||
|
||||
window.performance.now = function () {
|
||||
return Date.now() - offset;
|
||||
};
|
||||
}
|
||||
|
||||
})();
|
||||
|
||||
var TWEEN = TWEEN || (function () {
|
||||
|
||||
var _tweens = [];
|
||||
|
||||
return {
|
||||
|
||||
getAll: function () {
|
||||
|
||||
return _tweens;
|
||||
|
||||
},
|
||||
|
||||
removeAll: function () {
|
||||
|
||||
_tweens = [];
|
||||
|
||||
},
|
||||
|
||||
add: function (tween) {
|
||||
|
||||
_tweens.push(tween);
|
||||
|
||||
},
|
||||
|
||||
remove: function (tween) {
|
||||
|
||||
var i = _tweens.indexOf(tween);
|
||||
|
||||
if (i !== -1) {
|
||||
_tweens.splice(i, 1);
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
update: function (time) {
|
||||
|
||||
if (_tweens.length === 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
var i = 0;
|
||||
|
||||
time = time !== undefined ? time : window.performance.now();
|
||||
|
||||
while (i < _tweens.length) {
|
||||
|
||||
if (_tweens[i].update(time)) {
|
||||
i++;
|
||||
} else {
|
||||
_tweens.splice(i, 1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
})();
|
||||
|
||||
TWEEN.Tween = function (object) {
|
||||
|
||||
var _object = object;
|
||||
var _valuesStart = {};
|
||||
var _valuesEnd = {};
|
||||
var _valuesStartRepeat = {};
|
||||
var _duration = 1000;
|
||||
var _repeat = 0;
|
||||
var _yoyo = false;
|
||||
var _isPlaying = false;
|
||||
var _reversed = false;
|
||||
var _delayTime = 0;
|
||||
var _startTime = null;
|
||||
var _easingFunction = TWEEN.Easing.Linear.None;
|
||||
var _interpolationFunction = TWEEN.Interpolation.Linear;
|
||||
var _chainedTweens = [];
|
||||
var _onStartCallback = null;
|
||||
var _onStartCallbackFired = false;
|
||||
var _onUpdateCallback = null;
|
||||
var _onCompleteCallback = null;
|
||||
var _onStopCallback = null;
|
||||
|
||||
// Set all starting values present on the target object
|
||||
for (var field in object) {
|
||||
_valuesStart[field] = parseFloat(object[field], 10);
|
||||
}
|
||||
|
||||
this.to = function (properties, duration) {
|
||||
|
||||
if (duration !== undefined) {
|
||||
_duration = duration;
|
||||
}
|
||||
|
||||
_valuesEnd = properties;
|
||||
|
||||
return this;
|
||||
|
||||
};
|
||||
|
||||
this.start = function (time) {
|
||||
|
||||
TWEEN.add(this);
|
||||
|
||||
_isPlaying = true;
|
||||
|
||||
_onStartCallbackFired = false;
|
||||
|
||||
_startTime = time !== undefined ? time : window.performance.now();
|
||||
_startTime += _delayTime;
|
||||
|
||||
for (var property in _valuesEnd) {
|
||||
|
||||
// Check if an Array was provided as property value
|
||||
if (_valuesEnd[property] instanceof Array) {
|
||||
|
||||
if (_valuesEnd[property].length === 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Create a local copy of the Array with the start value at the front
|
||||
_valuesEnd[property] = [_object[property]].concat(_valuesEnd[property]);
|
||||
|
||||
}
|
||||
|
||||
_valuesStart[property] = _object[property];
|
||||
|
||||
if ((_valuesStart[property] instanceof Array) === false) {
|
||||
_valuesStart[property] *= 1.0; // Ensures we're using numbers, not strings
|
||||
}
|
||||
|
||||
_valuesStartRepeat[property] = _valuesStart[property] || 0;
|
||||
|
||||
}
|
||||
|
||||
return this;
|
||||
|
||||
};
|
||||
|
||||
this.stop = function () {
|
||||
|
||||
if (!_isPlaying) {
|
||||
return this;
|
||||
}
|
||||
|
||||
TWEEN.remove(this);
|
||||
_isPlaying = false;
|
||||
|
||||
if (_onStopCallback !== null) {
|
||||
_onStopCallback.call(_object);
|
||||
}
|
||||
|
||||
this.stopChainedTweens();
|
||||
return this;
|
||||
|
||||
};
|
||||
|
||||
this.stopChainedTweens = function () {
|
||||
|
||||
for (var i = 0, numChainedTweens = _chainedTweens.length; i < numChainedTweens; i++) {
|
||||
_chainedTweens[i].stop();
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
this.delay = function (amount) {
|
||||
|
||||
_delayTime = amount;
|
||||
return this;
|
||||
|
||||
};
|
||||
|
||||
this.repeat = function (times) {
|
||||
|
||||
_repeat = times;
|
||||
return this;
|
||||
|
||||
};
|
||||
|
||||
this.yoyo = function (yoyo) {
|
||||
|
||||
_yoyo = yoyo;
|
||||
return this;
|
||||
|
||||
};
|
||||
|
||||
|
||||
this.easing = function (easing) {
|
||||
|
||||
_easingFunction = easing;
|
||||
return this;
|
||||
|
||||
};
|
||||
|
||||
this.interpolation = function (interpolation) {
|
||||
|
||||
_interpolationFunction = interpolation;
|
||||
return this;
|
||||
|
||||
};
|
||||
|
||||
this.chain = function () {
|
||||
|
||||
_chainedTweens = arguments;
|
||||
return this;
|
||||
|
||||
};
|
||||
|
||||
this.onStart = function (callback) {
|
||||
|
||||
_onStartCallback = callback;
|
||||
return this;
|
||||
|
||||
};
|
||||
|
||||
this.onUpdate = function (callback) {
|
||||
|
||||
_onUpdateCallback = callback;
|
||||
return this;
|
||||
|
||||
};
|
||||
|
||||
this.onComplete = function (callback) {
|
||||
|
||||
_onCompleteCallback = callback;
|
||||
return this;
|
||||
|
||||
};
|
||||
|
||||
this.onStop = function (callback) {
|
||||
|
||||
_onStopCallback = callback;
|
||||
return this;
|
||||
|
||||
};
|
||||
|
||||
this.update = function (time) {
|
||||
|
||||
var property;
|
||||
var elapsed;
|
||||
var value;
|
||||
|
||||
if (time < _startTime) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (_onStartCallbackFired === false) {
|
||||
|
||||
if (_onStartCallback !== null) {
|
||||
_onStartCallback.call(_object);
|
||||
}
|
||||
|
||||
_onStartCallbackFired = true;
|
||||
|
||||
}
|
||||
|
||||
elapsed = (time - _startTime) / _duration;
|
||||
elapsed = elapsed > 1 ? 1 : elapsed;
|
||||
|
||||
value = _easingFunction(elapsed);
|
||||
|
||||
for (property in _valuesEnd) {
|
||||
|
||||
var start = _valuesStart[property] || 0;
|
||||
var end = _valuesEnd[property];
|
||||
|
||||
if (end instanceof Array) {
|
||||
|
||||
_object[property] = _interpolationFunction(end, value);
|
||||
|
||||
} else {
|
||||
|
||||
// Parses relative end values with start as base (e.g.: +10, -3)
|
||||
if (typeof (end) === 'string') {
|
||||
end = start + parseFloat(end, 10);
|
||||
}
|
||||
|
||||
// Protect against non numeric properties.
|
||||
if (typeof (end) === 'number') {
|
||||
_object[property] = start + (end - start) * value;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (_onUpdateCallback !== null) {
|
||||
_onUpdateCallback.call(_object, value);
|
||||
}
|
||||
|
||||
if (elapsed === 1) {
|
||||
|
||||
if (_repeat > 0) {
|
||||
|
||||
if (isFinite(_repeat)) {
|
||||
_repeat--;
|
||||
}
|
||||
|
||||
// Reassign starting values, restart by making startTime = now
|
||||
for (property in _valuesStartRepeat) {
|
||||
|
||||
if (typeof (_valuesEnd[property]) === 'string') {
|
||||
_valuesStartRepeat[property] = _valuesStartRepeat[property] + parseFloat(_valuesEnd[property], 10);
|
||||
}
|
||||
|
||||
if (_yoyo) {
|
||||
var tmp = _valuesStartRepeat[property];
|
||||
|
||||
_valuesStartRepeat[property] = _valuesEnd[property];
|
||||
_valuesEnd[property] = tmp;
|
||||
}
|
||||
|
||||
_valuesStart[property] = _valuesStartRepeat[property];
|
||||
|
||||
}
|
||||
|
||||
if (_yoyo) {
|
||||
_reversed = !_reversed;
|
||||
}
|
||||
|
||||
_startTime = time + _delayTime;
|
||||
|
||||
return true;
|
||||
|
||||
} else {
|
||||
|
||||
if (_onCompleteCallback !== null) {
|
||||
_onCompleteCallback.call(_object);
|
||||
}
|
||||
|
||||
for (var i = 0, numChainedTweens = _chainedTweens.length; i < numChainedTweens; i++) {
|
||||
// Make the chained tweens start exactly at the time they should,
|
||||
// even if the `update()` method was called way past the duration of the tween
|
||||
_chainedTweens[i].start(_startTime + _duration);
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
|
||||
TWEEN.Easing = {
|
||||
|
||||
Linear: {
|
||||
|
||||
None: function (k) {
|
||||
|
||||
return k;
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
Quadratic: {
|
||||
|
||||
In: function (k) {
|
||||
|
||||
return k * k;
|
||||
|
||||
},
|
||||
|
||||
Out: function (k) {
|
||||
|
||||
return k * (2 - k);
|
||||
|
||||
},
|
||||
|
||||
InOut: function (k) {
|
||||
|
||||
if ((k *= 2) < 1) {
|
||||
return 0.5 * k * k;
|
||||
}
|
||||
|
||||
return - 0.5 * (--k * (k - 2) - 1);
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
Cubic: {
|
||||
|
||||
In: function (k) {
|
||||
|
||||
return k * k * k;
|
||||
|
||||
},
|
||||
|
||||
Out: function (k) {
|
||||
|
||||
return --k * k * k + 1;
|
||||
|
||||
},
|
||||
|
||||
InOut: function (k) {
|
||||
|
||||
if ((k *= 2) < 1) {
|
||||
return 0.5 * k * k * k;
|
||||
}
|
||||
|
||||
return 0.5 * ((k -= 2) * k * k + 2);
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
Quartic: {
|
||||
|
||||
In: function (k) {
|
||||
|
||||
return k * k * k * k;
|
||||
|
||||
},
|
||||
|
||||
Out: function (k) {
|
||||
|
||||
return 1 - (--k * k * k * k);
|
||||
|
||||
},
|
||||
|
||||
InOut: function (k) {
|
||||
|
||||
if ((k *= 2) < 1) {
|
||||
return 0.5 * k * k * k * k;
|
||||
}
|
||||
|
||||
return - 0.5 * ((k -= 2) * k * k * k - 2);
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
Quintic: {
|
||||
|
||||
In: function (k) {
|
||||
|
||||
return k * k * k * k * k;
|
||||
|
||||
},
|
||||
|
||||
Out: function (k) {
|
||||
|
||||
return --k * k * k * k * k + 1;
|
||||
|
||||
},
|
||||
|
||||
InOut: function (k) {
|
||||
|
||||
if ((k *= 2) < 1) {
|
||||
return 0.5 * k * k * k * k * k;
|
||||
}
|
||||
|
||||
return 0.5 * ((k -= 2) * k * k * k * k + 2);
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
Sinusoidal: {
|
||||
|
||||
In: function (k) {
|
||||
|
||||
return 1 - Math.cos(k * Math.PI / 2);
|
||||
|
||||
},
|
||||
|
||||
Out: function (k) {
|
||||
|
||||
return Math.sin(k * Math.PI / 2);
|
||||
|
||||
},
|
||||
|
||||
InOut: function (k) {
|
||||
|
||||
return 0.5 * (1 - Math.cos(Math.PI * k));
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
Exponential: {
|
||||
|
||||
In: function (k) {
|
||||
|
||||
return k === 0 ? 0 : Math.pow(1024, k - 1);
|
||||
|
||||
},
|
||||
|
||||
Out: function (k) {
|
||||
|
||||
return k === 1 ? 1 : 1 - Math.pow(2, - 10 * k);
|
||||
|
||||
},
|
||||
|
||||
InOut: function (k) {
|
||||
|
||||
if (k === 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (k === 1) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if ((k *= 2) < 1) {
|
||||
return 0.5 * Math.pow(1024, k - 1);
|
||||
}
|
||||
|
||||
return 0.5 * (- Math.pow(2, - 10 * (k - 1)) + 2);
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
Circular: {
|
||||
|
||||
In: function (k) {
|
||||
|
||||
return 1 - Math.sqrt(1 - k * k);
|
||||
|
||||
},
|
||||
|
||||
Out: function (k) {
|
||||
|
||||
return Math.sqrt(1 - (--k * k));
|
||||
|
||||
},
|
||||
|
||||
InOut: function (k) {
|
||||
|
||||
if ((k *= 2) < 1) {
|
||||
return - 0.5 * (Math.sqrt(1 - k * k) - 1);
|
||||
}
|
||||
|
||||
return 0.5 * (Math.sqrt(1 - (k -= 2) * k) + 1);
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
Elastic: {
|
||||
|
||||
In: function (k) {
|
||||
|
||||
var s;
|
||||
var a = 0.1;
|
||||
var p = 0.4;
|
||||
|
||||
if (k === 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (k === 1) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!a || a < 1) {
|
||||
a = 1;
|
||||
s = p / 4;
|
||||
} else {
|
||||
s = p * Math.asin(1 / a) / (2 * Math.PI);
|
||||
}
|
||||
|
||||
return - (a * Math.pow(2, 10 * (k -= 1)) * Math.sin((k - s) * (2 * Math.PI) / p));
|
||||
|
||||
},
|
||||
|
||||
Out: function (k) {
|
||||
|
||||
var s;
|
||||
var a = 0.1;
|
||||
var p = 0.4;
|
||||
|
||||
if (k === 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (k === 1) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!a || a < 1) {
|
||||
a = 1;
|
||||
s = p / 4;
|
||||
} else {
|
||||
s = p * Math.asin(1 / a) / (2 * Math.PI);
|
||||
}
|
||||
|
||||
return (a * Math.pow(2, - 10 * k) * Math.sin((k - s) * (2 * Math.PI) / p) + 1);
|
||||
|
||||
},
|
||||
|
||||
InOut: function (k) {
|
||||
|
||||
var s;
|
||||
var a = 0.1;
|
||||
var p = 0.4;
|
||||
|
||||
if (k === 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (k === 1) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!a || a < 1) {
|
||||
a = 1;
|
||||
s = p / 4;
|
||||
} else {
|
||||
s = p * Math.asin(1 / a) / (2 * Math.PI);
|
||||
}
|
||||
|
||||
if ((k *= 2) < 1) {
|
||||
return - 0.5 * (a * Math.pow(2, 10 * (k -= 1)) * Math.sin((k - s) * (2 * Math.PI) / p));
|
||||
}
|
||||
|
||||
return a * Math.pow(2, -10 * (k -= 1)) * Math.sin((k - s) * (2 * Math.PI) / p) * 0.5 + 1;
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
Back: {
|
||||
|
||||
In: function (k) {
|
||||
|
||||
var s = 1.70158;
|
||||
|
||||
return k * k * ((s + 1) * k - s);
|
||||
|
||||
},
|
||||
|
||||
Out: function (k) {
|
||||
|
||||
var s = 1.70158;
|
||||
|
||||
return --k * k * ((s + 1) * k + s) + 1;
|
||||
|
||||
},
|
||||
|
||||
InOut: function (k) {
|
||||
|
||||
var s = 1.70158 * 1.525;
|
||||
|
||||
if ((k *= 2) < 1) {
|
||||
return 0.5 * (k * k * ((s + 1) * k - s));
|
||||
}
|
||||
|
||||
return 0.5 * ((k -= 2) * k * ((s + 1) * k + s) + 2);
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
Bounce: {
|
||||
|
||||
In: function (k) {
|
||||
|
||||
return 1 - TWEEN.Easing.Bounce.Out(1 - k);
|
||||
|
||||
},
|
||||
|
||||
Out: function (k) {
|
||||
|
||||
if (k < (1 / 2.75)) {
|
||||
return 7.5625 * k * k;
|
||||
} else if (k < (2 / 2.75)) {
|
||||
return 7.5625 * (k -= (1.5 / 2.75)) * k + 0.75;
|
||||
} else if (k < (2.5 / 2.75)) {
|
||||
return 7.5625 * (k -= (2.25 / 2.75)) * k + 0.9375;
|
||||
} else {
|
||||
return 7.5625 * (k -= (2.625 / 2.75)) * k + 0.984375;
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
InOut: function (k) {
|
||||
|
||||
if (k < 0.5) {
|
||||
return TWEEN.Easing.Bounce.In(k * 2) * 0.5;
|
||||
}
|
||||
|
||||
return TWEEN.Easing.Bounce.Out(k * 2 - 1) * 0.5 + 0.5;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
TWEEN.Interpolation = {
|
||||
|
||||
Linear: function (v, k) {
|
||||
|
||||
var m = v.length - 1;
|
||||
var f = m * k;
|
||||
var i = Math.floor(f);
|
||||
var fn = TWEEN.Interpolation.Utils.Linear;
|
||||
|
||||
if (k < 0) {
|
||||
return fn(v[0], v[1], f);
|
||||
}
|
||||
|
||||
if (k > 1) {
|
||||
return fn(v[m], v[m - 1], m - f);
|
||||
}
|
||||
|
||||
return fn(v[i], v[i + 1 > m ? m : i + 1], f - i);
|
||||
|
||||
},
|
||||
|
||||
Bezier: function (v, k) {
|
||||
|
||||
var b = 0;
|
||||
var n = v.length - 1;
|
||||
var pw = Math.pow;
|
||||
var bn = TWEEN.Interpolation.Utils.Bernstein;
|
||||
|
||||
for (var i = 0; i <= n; i++) {
|
||||
b += pw(1 - k, n - i) * pw(k, i) * v[i] * bn(n, i);
|
||||
}
|
||||
|
||||
return b;
|
||||
|
||||
},
|
||||
|
||||
CatmullRom: function (v, k) {
|
||||
|
||||
var m = v.length - 1;
|
||||
var f = m * k;
|
||||
var i = Math.floor(f);
|
||||
var fn = TWEEN.Interpolation.Utils.CatmullRom;
|
||||
|
||||
if (v[0] === v[m]) {
|
||||
|
||||
if (k < 0) {
|
||||
i = Math.floor(f = m * (1 + k));
|
||||
}
|
||||
|
||||
return fn(v[(i - 1 + m) % m], v[i], v[(i + 1) % m], v[(i + 2) % m], f - i);
|
||||
|
||||
} else {
|
||||
|
||||
if (k < 0) {
|
||||
return v[0] - (fn(v[0], v[0], v[1], v[1], -f) - v[0]);
|
||||
}
|
||||
|
||||
if (k > 1) {
|
||||
return v[m] - (fn(v[m], v[m], v[m - 1], v[m - 1], f - m) - v[m]);
|
||||
}
|
||||
|
||||
return fn(v[i ? i - 1 : 0], v[i], v[m < i + 1 ? m : i + 1], v[m < i + 2 ? m : i + 2], f - i);
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
Utils: {
|
||||
|
||||
Linear: function (p0, p1, t) {
|
||||
|
||||
return (p1 - p0) * t + p0;
|
||||
|
||||
},
|
||||
|
||||
Bernstein: function (n, i) {
|
||||
|
||||
var fc = TWEEN.Interpolation.Utils.Factorial;
|
||||
|
||||
return fc(n) / fc(i) / fc(n - i);
|
||||
|
||||
},
|
||||
|
||||
Factorial: (function () {
|
||||
|
||||
var a = [1];
|
||||
|
||||
return function (n) {
|
||||
|
||||
var s = 1;
|
||||
|
||||
if (a[n]) {
|
||||
return a[n];
|
||||
}
|
||||
|
||||
for (var i = n; i > 1; i--) {
|
||||
s *= i;
|
||||
}
|
||||
|
||||
a[n] = s;
|
||||
return s;
|
||||
|
||||
};
|
||||
|
||||
})(),
|
||||
|
||||
CatmullRom: function (p0, p1, p2, p3, t) {
|
||||
|
||||
var v0 = (p2 - p0) * 0.5;
|
||||
var v1 = (p3 - p1) * 0.5;
|
||||
var t2 = t * t;
|
||||
var t3 = t * t2;
|
||||
|
||||
return (2 * p1 - 2 * p2 + v0 + v1) * t3 + (- 3 * p1 + 3 * p2 - 2 * v0 - v1) * t2 + v0 * t + p1;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
// UMD (Universal Module Definition)
|
||||
(function (root) {
|
||||
|
||||
if (typeof define === 'function' && define.amd) {
|
||||
|
||||
// AMD
|
||||
define([], function () {
|
||||
return TWEEN;
|
||||
});
|
||||
|
||||
} else if (typeof exports === 'object') {
|
||||
|
||||
// Node.js
|
||||
module.exports = TWEEN;
|
||||
|
||||
} else {
|
||||
|
||||
// Global variable
|
||||
root.TWEEN = TWEEN;
|
||||
|
||||
}
|
||||
|
||||
})(this);
|
||||
|
||||
loadTween = function(){
|
||||
return TWEEN
|
||||
}
|
Loading…
Reference in a new issue