Grid_Range.js

/* 
 * Allows for mass-changing of grid tile properties for a given range of index values
 * that are contained within the grid object.
 * 
 */

// the edit tile functions
// these will all return false if the given X/Y is invalid
// and true if the tile was able to be edited

/**
 * Change the texture displayed by the Tile at the given index.
 * @param {number} x - The x coordinate of the tile in the Grid index coordinate system.
 * @param {number} y - The y coordinate of the tile in the Grid index coordinate system.
 * @param {String} texture - The filepath to the texture to set the tile to display. Assumes texture is already loaded.
 * @return {boolean} True if the texture was successfully set, false otherwise.
 */
Grid.prototype.editTileTexture = function(x, y, texture) {
   if (x < 0 || y < 0 || x >= this.mGridW || y >= this.mGridH) {
       return false; // invalid coordinates
   } else {
       this.mTiles[y][x].setTexture(texture);
       return true;
   }
};

/**
 * Change the collision property of the Tile at the given index.
 * @param {number} x - The x coordinate of the tile in the Grid index coordinate system.
 * @param {number} y - The y coordinate of the tile in the Grid index coordinate system.
 * @param {boolean} toSet - The value either enabling or disabling the collision of the desired tile.
 * @return {boolean} True if the collision was successfully set, false otherwise.
 */
Grid.prototype.editTileCollision = function(x, y, toSet) {
  if (x < 0 || y < 0 || x >= this.mGridW || y >= this.mGridH) {
       return false; // invalid coordinates
   } else {
       this.mTiles[y][x].setCollision(toSet);
       return true;
   }
};

/**
 * Change the visibility property of the Tile at the given index.
 * @param {number} x - The x coordinate of the tile in the Grid index coordinate system.
 * @param {number} y - The y coordinate of the tile in the Grid index coordinate system.
 * @param {boolean} toSet - The value either enabling or disabling the visibility of the desired tile.
 * @return {boolean} True if the visibility was successfully set, false otherwise.
 */
Grid.prototype.editTileVisible = function(x, y, toSet) {
  if (x < 0 || y < 0 || x >= this.mGridW || y >= this.mGridH) {
       return false; // invalid coordinates
   } else {
       this.mTiles[y][x].setVisible(toSet);
       return true;
   }
};

// Now, the edit range functions
// All of these include basic handling for unexpected range input
// if a number is lower than 0, it will be set to 0
// if a number is higher than the grid height/width, will be set to that

/**
 * Change the texture of a given range of Tiles within the Grid system. If given range is invalid, the function 
 * will round the given range to the nearest valid range.
 * @param {number} x1 - The x coordinate in the Grid index of the beginning Tile in the range.
 * @param {number} y1 - The y coordinate in the Grid index of the beginning Tile in the range.
 * @param {number} x2 - The x coordinate in the Grid index of the last Tile in the range.
 * @param {number} y2 - The y coordinate in the Grid index of the last Tile in the range.
 * @param {String} texture - The filepath to the texture to set the tiles to display. Assumes texture is already loaded.
 */
Grid.prototype.editRangeTexture = function(x1, y1, x2, y2, texture) {
  // error handling
  x1 = Math.max(0, Math.min(x1, this.mGridW - 1));
  x2 = Math.max(0, Math.min(x2, this.mGridW - 1));
  y1 = Math.max(0, Math.min(y1, this.mGridH - 1));
  y2 = Math.max(0, Math.min(y2, this.mGridH - 1));
  
  for (var i = y1; i <= y2; i++) {
      for (var j = x1; j <= x2; j++) {
          this.mTiles[i][j].setTexture(texture);
      }
  }
};

/**
 * Change the collision property of a given range of Tiles within the Grid system. 
 * If given range is invalid, the function will round the given range to the nearest valid range.
 * @param {number} x1 - The x coordinate in the Grid index of the beginning Tile in the range.
 * @param {number} y1 - The y coordinate in the Grid index of the beginning Tile in the range.
 * @param {number} x2 - The x coordinate in the Grid index of the last Tile in the range.
 * @param {number} y2 - The y coordinate in the Grid index of the last Tile in the range.
 * @param {String} toSet - The value either enabling or disabling the collision of the desired tiles in the range.
 */
Grid.prototype.editRangeCollision = function(x1, y1, x2, y2, toSet) {
  // error handling
  x1 = Math.max(0, Math.min(x1, this.mGridW - 1));
  x2 = Math.max(0, Math.min(x2, this.mGridW - 1));
  y1 = Math.max(0, Math.min(y1, this.mGridH - 1));
  y2 = Math.max(0, Math.min(y2, this.mGridH - 1));
  
  for (var i = y1; i <= y2; i++) {
      for (var j = x1; j <= x2; j++) {
          this.mTiles[i][j].setCollision(toSet);
      }
  }
};

/**
 * Change the visibility property of a given range of Tiles within the Grid system. 
 * If given range is invalid, the function will round the given range to the nearest valid range.
 * @param {number} x1 - The x coordinate in the Grid index of the beginning Tile in the range.
 * @param {number} y1 - The y coordinate in the Grid index of the beginning Tile in the range.
 * @param {number} x2 - The x coordinate in the Grid index of the last Tile in the range.
 * @param {number} y2 - The y coordinate in the Grid index of the last Tile in the range.
 * @param {String} toSet - The value either enabling or disabling the visibility of the desired tiles in the range.
 */
Grid.prototype.editRangeVisible = function(x1, y1, x2, y2, toSet) {
  // error handling
  x1 = Math.max(0, Math.min(x1, this.mGridW - 1));
  x2 = Math.max(0, Math.min(x2, this.mGridW - 1));
  y1 = Math.max(0, Math.min(y1, this.mGridH - 1));
  y2 = Math.max(0, Math.min(y2, this.mGridH - 1));
  
  for (var i = y1; i <= y2; i++) {
      for (var j = x1; j <= x2; j++) {
          this.mTiles[i][j].setVisible(toSet);
      }
  }
};

/**
 * Add walls to a given range of tiles of a single specified direction.
 * If given range is invalid, the function will round the given range to the nearest valid range.
 * @param {number} x1 - The x coordinate in the Grid index of the beginning Tile in the range.
 * @param {number} y1 - The y coordinate in the Grid index of the beginning Tile in the range.
 * @param {number} x2 - The x coordinate in the Grid index of the last Tile in the range.
 * @param {number} y2 - The y coordinate in the Grid index of the last Tile in the range.
 * @param {Direction} direction - The direction of the walls being placed.
 */
Grid.prototype.addRangeWalls = function(x1, y1, x2, y2, direction) {
  // error handling
  x1 = Math.max(0, Math.min(x1, this.mGridW - 1));
  x2 = Math.max(0, Math.min(x2, this.mGridW - 1));
  y1 = Math.max(0, Math.min(y1, this.mGridH - 1));
  y2 = Math.max(0, Math.min(y2, this.mGridH - 1));
  
  for (var i = y1; i <= y2; i++) {
      for (var j = x1; j <= x2; j++) {
          this.mTiles[i][j].addWall(direction);
      }
  }
};

/**
 * Add a wall of given direction to the Tile at the given index.
 * @param {number} x - The x coordinate of the tile in the Grid index coordinate system.
 * @param {number} y - The y coordinate of the tile in the Grid index coordinate system.
 * @param {} direction - The desired direction to add a wall in.
 * @return {boolean} True if the wall was successfully added, false otherwise.
 */
Grid.prototype.addTileWall = function(x, y, direction) {
    if (x > -1 && x < this.mGridW) {
        if (y > -1 && y < this.mGridH) {
            this.mTiles[y][x].addWall(direction);
            return true;
        }
    }
    return false;
};


/**
 * Add walls to a given range of tiles of a single specified direction.
 * If given range is invalid, the function will round the given range to the nearest valid range.
 * NOTE: This function also adds a corresponding wall in the opposite direction to the tile on
 * the other side of the given wall.
 * @param {number} x1 - The x coordinate in the Grid index of the beginning Tile in the range.
 * @param {number} y1 - The y coordinate in the Grid index of the beginning Tile in the range.
 * @param {number} x2 - The x coordinate in the Grid index of the last Tile in the range.
 * @param {number} y2 - The y coordinate in the Grid index of the last Tile in the range.
 * @param {Direction} direction - The direction of the walls being placed.
 */
Grid.prototype.addWallRange = function (x1, y1, x2, y2, direction) {
    var minX, maxX;
    if (x1 < x2) {
        minX = x1;
        maxX = x2;
    } else {
        minX = x2;
        maxX = x2;
    }
    var minY, maxY;
    if (y1 < y2) {
        minY = y1;
        maxY = y2;
    } else {
        minY = y2;
        maxY = y1;
    }
    var oppoDirection = [0 - direction[0], 0 - direction[1]];
    
    for (var i = minY; i <= maxY; i++) {
        for (var j = minX; j <= maxX; j++) {
            this.addTileWall(j, i, direction);
            this.addTileWall(j + direction[0], i + direction[1], oppoDirection);
        }
    }
};


/**
 * Remove a wall of given direction from the Tile at the given index.
 * @param {number} x - The x coordinate of the tile in the Grid index coordinate system.
 * @param {number} y - The y coordinate of the tile in the Grid index coordinate system.
 * @param {} direction - The desired direction to remove a wall from.
 * @return {boolean} True if the wall was successfully removed, false otherwise.
 */
Grid.prototype.removeTileWall = function(x, y, direction) {
    if (x > -1 && x < this.mGridW) {
        if (y > -1 && y < this.mGridH) {
            this.mTiles[y][x].removeWall(direction);
            return true;
        }
    }
    return false;
};

/**
 * removes walls from a given range of tiles of a single specified direction.
 * If given range is invalid, the function will round the given range to the nearest valid range.
 * NOTE: This function also removes any corresponding walls in the opposite direction to the tile on
 * the other side of the given wall.
 * @param {number} x1 - The x coordinate in the Grid index of the beginning Tile in the range.
 * @param {number} y1 - The y coordinate in the Grid index of the beginning Tile in the range.
 * @param {number} x2 - The x coordinate in the Grid index of the last Tile in the range.
 * @param {number} y2 - The y coordinate in the Grid index of the last Tile in the range.
 * @param {Direction} direction - The direction of the walls being removed.
 */
Grid.prototype.removeWallRange = function (x1, y1, x2, y2, direction) {
    var minX, maxX;
    if (x1 < x2) {
        minX = x1;
        maxX = x2;
    } else {
        minX = x2;
        maxX = x2;
    }
    var minY, maxY;
    if (y1 < y2) {
        minY = y1;
        maxY = y2;
    } else {
        minY = y2;
        maxY = y1;
    }
    var oppoDirection = [0 - direction[0], 0 - direction[1]];
    
    for (var i = minY; i <= maxY; i++) {
        for (var j = minX; j <= maxX; j++) {
            this.removeTileWall(j, i, direction);
            this.removeTileWall(j + direction[0], i + direction[1], oppoDirection);
        }
    }
};