public/js-roguelike-skeleton/src/array-2d.js
function(x, y, settings) {
            settings = settings || {};
            var filter          = settings.filter           !== void 0 ? settings.filter        : false,
                withCoords      = settings.withCoords       !== void 0 ? settings.withCoords    : false,
                withDiagonals   = settings.withDiagonals    !== void 0 ? settings.withDiagonals : true;

            var _this = this,
                out = [],
                ax, ay;

            var add = function(x, y) {
                var val = _this.get(x, y);
                if (filter === false || (filter(val, x, y))) {
                    if (withCoords) {
                        out.push({
                            x: x,
                            y: y,
                            value: val
                        });
                    } else {
                        out.push(val);
                    }
                }
            };

            // top
            ax = x;
            ay = y - 1;
            add(ax, ay);

            // bottom
            ax = x;
            ay = y + 1;
            add(ax, ay);

            // left
            ax = x - 1;
            ay = y;
            add(ax, ay);

            // right
            ax = x + 1;
            ay = y;
            add(ax, ay);

            if(withDiagonals){
                // top left
                ax = x - 1;
                ay = y - 1;
                add(ax, ay);

                // top right
                ax = x + 1;
                ay = y - 1;
                add(ax, ay);

                // bottom left
                ax = x - 1;
                ay = y + 1;
                add(ax, ay);

                // bottom right
                ax = x + 1;
                ay = y + 1;
                add(ax, ay);
            }

            return out;
        }
function(x, y, settings) {
            settings = settings || {};
            var filter          = settings.filter           !== void 0 ? settings.filter        : false,
                withCoords      = settings.withCoords       !== void 0 ? settings.withCoords    : false,
                withDiagonals   = settings.withDiagonals    !== void 0 ? settings.withDiagonals : true;

            var _this = this,
                out = [],
                ax, ay;

            var add = function(x, y) {
                var val = _this.get(x, y);
                if (filter === false || (filter(val, x, y))) {
                    if (withCoords) {
                        out.push({
                            x: x,
                            y: y,
                            value: val
                        });
                    } else {
                        out.push(val);
                    }
                }
            };

            // top
            ax = x;
            ay = y - 1;
            add(ax, ay);

            // bottom
            ax = x;
            ay = y + 1;
            add(ax, ay);

            // left
            ax = x - 1;
            ay = y;
            add(ax, ay);

            // right
            ax = x + 1;
            ay = y;
            add(ax, ay);

            if(withDiagonals){
                // top left
                ax = x - 1;
                ay = y - 1;
                add(ax, ay);

                // top right
                ax = x + 1;
                ay = y - 1;
                add(ax, ay);

                // bottom left
                ax = x - 1;
                ay = y + 1;
                add(ax, ay);

                // bottom right
                ax = x + 1;
                ay = y + 1;
                add(ax, ay);
            }

            return out;
        }
function ( x , y , settings ) { settings = settings || { } ; var filter = settings . filter !== void 0 ? settings . filter : false , withCoords = settings . withCoords !== void 0 ? settings . withCoords : false , withDiagonals = settings . withDiagonals !== void 0 ? settings . withDiagonals : true ; var _this = this , out = [ ] , ax , ay ; var add = function ( x , y ) { var val = _this . get ( x , y ) ; if ( filter === false || ( filter ( val , x , y ) ) ) { if ( withCoords ) { out . push ( { x : x , y : y , value : val } ) ; } else { out . push ( val ) ; } } } ; // top ax = x ; ay = y - 1 ; add ( ax , ay ) ; // bottom ax = x ; ay = y + 1 ; add ( ax , ay ) ; // left ax = x - 1 ; ay = y ; add ( ax , ay ) ; // right ax = x + 1 ; ay = y ; add ( ax , ay ) ; if ( withDiagonals ) { // top left ax = x - 1 ; ay = y - 1 ; add ( ax , ay ) ; // top right ax = x + 1 ; ay = y - 1 ; add ( ax , ay ) ; // bottom left ax = x - 1 ; ay = y + 1 ; add ( ax , ay ) ; // bottom right ax = x + 1 ; ay = y + 1 ; add ( ax , ay ) ; } return out ; }
Retrieves an array of values of adjacent coords.
@method getAdjacent
@param {Number} x - Map tile x coord to get adjacent values of.
@param {Number} y - Map tile y coord to get adjacent values of.
@param {Object} [settings] -
@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.withDiagonals=true] - If true diagonals will be included.
@param {Function} [settings.filter=false] - A function to filter the values returned (function(value, x, y){ return true;})
@return {Array} An array of adjacent coord values.
Retrieves an array of values of adjacent coords .