@@ -8,8 +8,11 @@ var DiagonalMovement = require('./DiagonalMovement');
88 * @param {number } height Number of rows of the grid.
99 * @param {Array.<Array.<(number|boolean)>> } [matrix] - A 0-1 matrix
1010 * 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 ) {
1316 var width ;
1417
1518 if ( typeof width_or_matrix !== 'object' ) {
@@ -34,7 +37,7 @@ function Grid(width_or_matrix, height, matrix) {
3437 /**
3538 * A 2D array of nodes.
3639 */
37- this . nodes = this . _buildNodes ( width , height , matrix ) ;
40+ this . nodes = this . _buildNodes ( width , height , matrix , costs ) ;
3841}
3942
4043/**
@@ -44,9 +47,11 @@ function Grid(width_or_matrix, height, matrix) {
4447 * @param {number } height
4548 * @param {Array.<Array.<number|boolean>> } [matrix] - A 0-1 matrix representing
4649 * the walkable status of the nodes.
50+ * @param {Array.<Array.<number>> } [costs] - A matrix representing
51+ * the costs to walk the nodes.
4752 * @see Grid
4853 */
49- Grid . prototype . _buildNodes = function ( width , height , matrix ) {
54+ Grid . prototype . _buildNodes = function ( width , height , matrix , costs ) {
5055 var i , j ,
5156 nodes = new Array ( height ) ,
5257 row ;
@@ -58,7 +63,6 @@ Grid.prototype._buildNodes = function(width, height, matrix) {
5863 }
5964 }
6065
61-
6266 if ( matrix === undefined ) {
6367 return nodes ;
6468 }
@@ -67,13 +71,20 @@ Grid.prototype._buildNodes = function(width, height, matrix) {
6771 throw new Error ( 'Matrix size does not fit' ) ;
6872 }
6973
74+ if ( costs !== undefined && ( costs . length !== height || costs [ 0 ] . length !== width ) ) {
75+ throw new Error ( 'Costs size does not fit' ) ;
76+ }
77+
7078 for ( i = 0 ; i < height ; ++ i ) {
7179 for ( j = 0 ; j < width ; ++ j ) {
7280 if ( matrix [ i ] [ j ] ) {
7381 // 0, false, null will be walkable
7482 // while others will be un-walkable
7583 nodes [ i ] [ j ] . walkable = false ;
7684 }
85+ if ( costs !== undefined ) {
86+ nodes [ i ] [ j ] . cost = costs [ i ] [ j ] ;
87+ }
7788 }
7889 }
7990
@@ -98,6 +109,19 @@ Grid.prototype.isWalkableAt = function(x, y) {
98109} ;
99110
100111
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+
101125/**
102126 * Determine whether the position is inside the grid.
103127 * XXX: `grid.isInside(x, y)` is wierd to read.
@@ -124,6 +148,18 @@ Grid.prototype.setWalkableAt = function(x, y, walkable) {
124148} ;
125149
126150
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+
127163/**
128164 * Get the neighbors of the given node.
129165 *
@@ -235,7 +271,7 @@ Grid.prototype.clone = function() {
235271 for ( i = 0 ; i < height ; ++ i ) {
236272 newNodes [ i ] = new Array ( width ) ;
237273 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 ) ;
239275 }
240276 }
241277
0 commit comments