-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpathfinder.js
149 lines (118 loc) · 4.22 KB
/
pathfinder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*This software is released under MIT License. Texts for license are listed below:
* Aventura do Saber , a educational fantasy action RPG
* Copyright (c) 2012-2013, ITSimples Games
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.
*/
/**
* pathFinder.
* @class
* @extends
* @constructor
* @param
* @return path
*/
adsGame.PathFinder = Object.extend({
"init" : function init() {
console.log('Init pathfinder class...');
},
"getPath" : function getPath( start, end , layerName){
// console.log('Start:', start , 'End:', end , 'layerName:', layerName);
// Get layer object
var layer = me.game.currentLevel.getLayerByName("collision");
// set array to layer
var myLayerArray = new Array(layer.cols);
// parse all the layer tiles
for ( var x = 0; x < layer.cols; x++)
{
// create multidimensional array
myLayerArray[x] = new Array(layer.rows);
for ( var y = 0; y < layer.rows; y++)
{
var testTile = layer.layerData[y][x];
// if null not collide then 0 free path with Astar algoritm
if (testTile == null){
myLayerArray[x][y] = 0;
}else { // 1 block the path
myLayerArray[x][y] = 1;
}
}
}
var result = AStar(myLayerArray, start, end, "Manhattan");
/*
Get the result and transform to get only the start and end points of a line.
to apply the Bresenham algorithm
*/
var pathArray = [];
var countArray = 1;
var equal ='';
var x;
var y;
var nextX;
var nextY;
//first point
pathArray[0] = start;
$.each ( result, function (i, results ){
if (i != result.length - 1){
x = result[i][0];
y = result[i][1];
nextX = result[i + 1][0];
nextY = result[i + 1][1];
//if x is not equal to x+1 then y equal to y+1
if ( x == nextX){
if (equal == 'y') countArray++;
pathArray[countArray] = [nextX,nextY];
equal ='x';
}else { // y equal
if (equal == 'x') countArray++;
pathArray[countArray] = [nextX,nextY];
equal ='y';
}
}
// console.log ("X: " , x , ' Y:', y);
});
// $.each ( pathArray, function (i, point ){
// console.log('pathArray:', i , '-' , point);
// });
return pathArray;
},
"getPathTest" : function getPathTest( start, end){
// Get layer object
var layer = me.game.currentLevel.getLayerByName("collision");
// set array to layer
var myLayerArray = new Array(layer.cols);
// parse all the layer tiles
for ( var x = 0; x < layer.cols; x++)
{
// create multidimensional array
myLayerArray[x] = new Array(layer.rows);
for ( var y = 0; y < layer.rows; y++)
{
var testTile = layer.layerData[y][x];
// if null not collide then 0 free path with Astar algoritm
if (testTile == null){
myLayerArray[x][y] = 0;
}else { // 1 block the path
myLayerArray[x][y] = 1;
}
}
}
var grid = new PF.Grid(50, 50, myLayerArray);
var finder = new PF.AStarFinder();
var path = finder.findPath(start[0], start[1], end[0], end[1], grid);
return path;
}
});