@@ -8,8 +8,11 @@ var DiagonalMovement = require('./DiagonalMovement');
8
8
* @param {number } height Number of rows of the grid.
9
9
* @param {Array.<Array.<(number|boolean)>> } [matrix] - A 0-1 matrix
10
10
* representing the walkable status of the nodes(0 or false for walkable).
11
- * If the matrix is not supplied, all the nodes will be walkable. */
12
- function Grid ( width_or_matrix , height , matrix ) {
11
+ * If the matrix is not supplied, all the nodes will be walkable.
12
+ * @param {Array.<Array.<(number)>> } [costs] - A matrix
13
+ * representing the cost of walking the node.
14
+ * If the costs is not supplied, all the nodes will cost 0. */
15
+ function Grid ( width_or_matrix , height , matrix , costs ) {
13
16
var width ;
14
17
15
18
if ( typeof width_or_matrix !== 'object' ) {
@@ -34,7 +37,7 @@ function Grid(width_or_matrix, height, matrix) {
34
37
/**
35
38
* A 2D array of nodes.
36
39
*/
37
- this . nodes = this . _buildNodes ( width , height , matrix ) ;
40
+ this . nodes = this . _buildNodes ( width , height , matrix , costs ) ;
38
41
}
39
42
40
43
/**
@@ -44,9 +47,11 @@ function Grid(width_or_matrix, height, matrix) {
44
47
* @param {number } height
45
48
* @param {Array.<Array.<number|boolean>> } [matrix] - A 0-1 matrix representing
46
49
* the walkable status of the nodes.
50
+ * @param {Array.<Array.<number>> } [costs] - A matrix representing
51
+ * the costs to walk the nodes.
47
52
* @see Grid
48
53
*/
49
- Grid . prototype . _buildNodes = function ( width , height , matrix ) {
54
+ Grid . prototype . _buildNodes = function ( width , height , matrix , costs ) {
50
55
var i , j ,
51
56
nodes = new Array ( height ) ,
52
57
row ;
@@ -58,7 +63,6 @@ Grid.prototype._buildNodes = function(width, height, matrix) {
58
63
}
59
64
}
60
65
61
-
62
66
if ( matrix === undefined ) {
63
67
return nodes ;
64
68
}
@@ -67,13 +71,20 @@ Grid.prototype._buildNodes = function(width, height, matrix) {
67
71
throw new Error ( 'Matrix size does not fit' ) ;
68
72
}
69
73
74
+ if ( costs !== undefined && ( costs . length !== height || costs [ 0 ] . length !== width ) ) {
75
+ throw new Error ( 'Costs size does not fit' ) ;
76
+ }
77
+
70
78
for ( i = 0 ; i < height ; ++ i ) {
71
79
for ( j = 0 ; j < width ; ++ j ) {
72
80
if ( matrix [ i ] [ j ] ) {
73
81
// 0, false, null will be walkable
74
82
// while others will be un-walkable
75
83
nodes [ i ] [ j ] . walkable = false ;
76
84
}
85
+ if ( costs !== undefined ) {
86
+ nodes [ i ] [ j ] . cost = costs [ i ] [ j ] ;
87
+ }
77
88
}
78
89
}
79
90
@@ -98,6 +109,19 @@ Grid.prototype.isWalkableAt = function(x, y) {
98
109
} ;
99
110
100
111
112
+ /**
113
+ * Get cost to walk the node at the given position.
114
+ * (Also returns false if the position is outside the grid.)
115
+ * @param {number } x - The x coordinate of the node.
116
+ * @param {number } y - The y coordinate of the node.
117
+ * @return {number } - Cost to walk node.
118
+ */
119
+ Grid . prototype . getCostAt = function ( x , y ) {
120
+ if ( ! this . isInside ( x , y ) ) return false ;
121
+ return this . nodes [ y ] [ x ] . cost ;
122
+ } ;
123
+
124
+
101
125
/**
102
126
* Determine whether the position is inside the grid.
103
127
* XXX: `grid.isInside(x, y)` is wierd to read.
@@ -124,6 +148,18 @@ Grid.prototype.setWalkableAt = function(x, y, walkable) {
124
148
} ;
125
149
126
150
151
+ /**
152
+ * Set cost of the node on the given position
153
+ * NOTE: throws exception if the coordinate is not inside the grid.
154
+ * @param {number } x - The x coordinate of the node.
155
+ * @param {number } y - The y coordinate of the node.
156
+ * @param {number } cost - Cost to walk the node.
157
+ */
158
+ Grid . prototype . setCostAt = function ( x , y , cost ) {
159
+ this . nodes [ y ] [ x ] . cost = cost ;
160
+ } ;
161
+
162
+
127
163
/**
128
164
* Get the neighbors of the given node.
129
165
*
@@ -235,7 +271,7 @@ Grid.prototype.clone = function() {
235
271
for ( i = 0 ; i < height ; ++ i ) {
236
272
newNodes [ i ] = new Array ( width ) ;
237
273
for ( j = 0 ; j < width ; ++ j ) {
238
- newNodes [ i ] [ j ] = new Node ( j , i , thisNodes [ i ] [ j ] . walkable ) ;
274
+ newNodes [ i ] [ j ] = new Node ( j , i , thisNodes [ i ] [ j ] . walkable , thisNodes [ i ] [ j ] . cost ) ;
239
275
}
240
276
}
241
277
0 commit comments