public/js-roguelike-skeleton/src/array-2d.js
function(x, y, settings) {
            settings = settings || {};

            var radius          = settings.radius           || 1,
                filter          = settings.filter           || false,
                withCoords      = settings.withCoords       || false,
                includeTarget   = settings.includeTarget    || false;

            var tileX = x,
                tileY = y;
            var minX = tileX - radius,
                maxX = tileX + radius,
                minY = tileY - radius,
                maxY = tileY + radius,
                output = [],
                val;

            if (minX < 0) {
                minX = 0;
            }
            if (minY < 0) {
                minY = 0;
            }
            if (maxX > this.width - 1) {
                maxX = this.width - 1;
            }
            if (maxY > this.height - 1) {
                maxY = this.height - 1;
            }
            for (x = minX; x <= maxX; x++) {
                for (y = minY; y <= maxY; y++) {
                    if (!includeTarget && tileX === x && tileY === y) {
                        continue;
                    }
                    val = this.data[x][y];

                    if (filter === false || filter(val, x, y)) {
                        if (withCoords) {
                            output.push({
                                x: x,
                                y: y,
                                value: val
                            });
                        } else {
                            output.push(val);
                        }
                    }
                }
            }
            return output;
        }
function(x, y, settings) {
            settings = settings || {};

            var radius          = settings.radius           || 1,
                filter          = settings.filter           || false,
                withCoords      = settings.withCoords       || false,
                includeTarget   = settings.includeTarget    || false;

            var tileX = x,
                tileY = y;
            var minX = tileX - radius,
                maxX = tileX + radius,
                minY = tileY - radius,
                maxY = tileY + radius,
                output = [],
                val;

            if (minX < 0) {
                minX = 0;
            }
            if (minY < 0) {
                minY = 0;
            }
            if (maxX > this.width - 1) {
                maxX = this.width - 1;
            }
            if (maxY > this.height - 1) {
                maxY = this.height - 1;
            }
            for (x = minX; x <= maxX; x++) {
                for (y = minY; y <= maxY; y++) {
                    if (!includeTarget && tileX === x && tileY === y) {
                        continue;
                    }
                    val = this.data[x][y];

                    if (filter === false || filter(val, x, y)) {
                        if (withCoords) {
                            output.push({
                                x: x,
                                y: y,
                                value: val
                            });
                        } else {
                            output.push(val);
                        }
                    }
                }
            }
            return output;
        }
function ( x , y , settings ) { settings = settings || { } ; var radius = settings . radius || 1 , filter = settings . filter || false , withCoords = settings . withCoords || false , includeTarget = settings . includeTarget || false ; var tileX = x , tileY = y ; var minX = tileX - radius , maxX = tileX + radius , minY = tileY - radius , maxY = tileY + radius , output = [ ] , val ; if ( minX < 0 ) { minX = 0 ; } if ( minY < 0 ) { minY = 0 ; } if ( maxX > this . width - 1 ) { maxX = this . width - 1 ; } if ( maxY > this . height - 1 ) { maxY = this . height - 1 ; } for ( x = minX ; x <= maxX ; x ++ ) { for ( y = minY ; y <= maxY ; y ++ ) { if ( ! includeTarget && tileX === x && tileY === y ) { continue ; } val = this . data [ x ] [ y ] ; if ( filter === false || filter ( val , x , y ) ) { if ( withCoords ) { output . push ( { x : x , y : y , value : val } ) ; } else { output . push ( val ) ; } } } } return output ; }
Retrieves an array of values of coords within a given radius.
@method getWithinSquareRadius
@param {Number} x - Map tile x coord at the center of the radius.
@param {Number} y - Map tile x coord at the center of the radius.
@param {Object} [settings] -
@param {Number} [settings.radius=1] - Radius of the area to retrieve tiles from.
@param {Function} [settings.filter=false] - A function to filter the values returned (function(value, x, y){ return true;})
@param {Bool} [settings.withCoords=false] - If true the returned array will include the coords of each value ([{x: 0, y: 0, value: 1}, ...])
@param {Bool} [settings.includeTarget=false] - If true the value of the coordinates given will be included in the returned array.
@return {Array} An array of coord values within a square radius of the given coords.
Retrieves an array of values of coords within a given radius .