public/js-roguelike-skeleton/src/array-2d.js
function(x0, y0, x1, y1, condition, withCoords) {
            withCoords = withCoords || false;
            condition = condition || false;
            var output = [],
                dx = Math.abs(x1 - x0),
                dy = Math.abs(y1 - y0),
                sx = (x0 < x1) ? 1 : -1,
                sy = (y0 < y1) ? 1 : -1,
                err = dx - dy,
                e2, val;

            while (true) {
                if (x0 < 0 || x0 >= this.width || y0 < 0 || y0 >= this.height) {
                    break;
                }

                val = this.get(x0, y0);
                if (withCoords) {
                    output.push({
                        x: x0,
                        y: y0,
                        value: val
                    });
                } else {
                    output.push(val);
                }

                if (condition !== false && condition(val, x0, y0)) {
                    break;
                }

                e2 = 2 * err;
                if (e2 > -dy) {
                    err -= dy;
                    x0 += sx;
                }
                if (e2 < dx) {
                    err += dx;
                    y0 += sy;
                }
            }
            return output;
        }
function(x0, y0, x1, y1, condition, withCoords) {
            withCoords = withCoords || false;
            condition = condition || false;
            var output = [],
                dx = Math.abs(x1 - x0),
                dy = Math.abs(y1 - y0),
                sx = (x0 < x1) ? 1 : -1,
                sy = (y0 < y1) ? 1 : -1,
                err = dx - dy,
                e2, val;

            while (true) {
                if (x0 < 0 || x0 >= this.width || y0 < 0 || y0 >= this.height) {
                    break;
                }

                val = this.get(x0, y0);
                if (withCoords) {
                    output.push({
                        x: x0,
                        y: y0,
                        value: val
                    });
                } else {
                    output.push(val);
                }

                if (condition !== false && condition(val, x0, y0)) {
                    break;
                }

                e2 = 2 * err;
                if (e2 > -dy) {
                    err -= dy;
                    x0 += sx;
                }
                if (e2 < dx) {
                    err += dx;
                    y0 += sy;
                }
            }
            return output;
        }
function ( x0 , y0 , x1 , y1 , condition , withCoords ) { withCoords = withCoords || false ; condition = condition || false ; var output = [ ] , dx = Math . abs ( x1 - x0 ) , dy = Math . abs ( y1 - y0 ) , sx = ( x0 < x1 ) ? 1 : - 1 , sy = ( y0 < y1 ) ? 1 : - 1 , err = dx - dy , e2 , val ; while ( true ) { if ( x0 < 0 || x0 >= this . width || y0 < 0 || y0 >= this . height ) { break ; } val = this . get ( x0 , y0 ) ; if ( withCoords ) { output . push ( { x : x0 , y : y0 , value : val } ) ; } else { output . push ( val ) ; } if ( condition !== false && condition ( val , x0 , y0 ) ) { break ; } e2 = 2 * err ; if ( e2 > - dy ) { err -= dy ; x0 += sx ; } if ( e2 < dx ) { err += dx ; y0 += sy ; } } return output ; }
Retrieves an array of values of coords along a line starting at point 0 and crossing point 1 until it hits the edge of the 2d array or a coord value returning true when passed to the condtion function.
@method getLineThrough
@param {Number} x0 - Map tile x coord of start.
@param {Number} y0 - Map tile y coord of start.
@param {Number} x1 - Map tile x coord of crossing.
@param {Number} y1 - Map tile y coord of crossing.
@param {Function} [condition=false] - A function to determine when to end the line. A coord value returning true when passed to the function will end the line. (function(value, x, y){ return true;})
@param {Bool} [withCoords=false] - If true the returned array will include the coords of each value ([{x: 0, y: 0, value: 1}, ...])
@return {Array} An array of coord values.
Retrieves an array of values of coords along a line starting at point 0 and crossing point 1 until it hits the edge of the 2d array or a coord value returning true when passed to the condtion function .