This function accepts a String (either "right", "left","up", or "down") to do 4 similar but different things. I'd like a more elegant solution to this problem. I sense that there is one, but I can't think of it. Maybe by using a a few smaller functions? Maybe by using a dictionary?
private function addAndRemoveTiles(pan:String):void
{
var lenRows:int = onScreenRows;
var lenCols:int = onScreenCols;
if (pan == "right"){
onScreenLeftCol++;
// for each row
for (var i:int = 0; i < lenRows; i++){
//////////////////
// remove tile from Left
//////////////////
// t = Tile to be removed from left of row
var t:Tile = onScreenArray[i].shift();
// tileNum = Tile Type
var tileNum:int = tileType(t);
// add tile from onScreenArray to arrGround
arrGround[tileNum].push(t);
// put tile off screen
t.x = -tileWidth;
t.y = -tileHeight;
/////////////
// add tile to Right
/////////////
// c = right tile col
var c:int = onScreenCols + onScreenLeftCol-1;
// cType = right tile Type
var cType:int = complexArray[i+onScreenTopRow][c];
// t = Tile to take from arrGround
var t:Tile = arrGround[cType][arrGround[cType].length - 1];
// farRightTile = Tile on far right of screen
var farRightTile:Tile = onScreenArray[i][onScreenArray[i].length-1];
// add tile from arrGround to onScreeArray
onScreenArray[i].push(t);
// remove tile from arrGround to onScreenArray
arrGround[cType].pop();
// place tile on screen
t.x = farRightTile.x + tileWidth;
t.y = farRightTile.y;
}
} else if (pan == "left"){
onScreenLeftCol--;
for (var i:int = 0; i < lenRows; i++){
// remove tile from Right
var t:Tile = onScreenArray[i].pop();
var tileNum:int = tileType(t);
arrGround[tileNum].push(t);
// place tile off screen
t.x = -tileWidth;
t.y = -tileHeight;
// add tile to Left
var c:int = onScreenLeftCol;
var cType:int = complexArray[i+onScreenTopRow][c];
var t:Tile = arrGround[cType][arrGround[cType].length - 1];
var farLeftTile:Tile = onScreenArray[i][0];
onScreenArray[i].unshift(t);
arrGround[cType].pop();
// place tile on Screen
t.x = farLeftTile.x - tileWidth;
t.y = farLeftTile.y;
}
} else if (pan == "down"){
onScreenTopRow++;
//////////////////
// remove tile from Top
//////////////////
var removedArray:Array = onScreenArray.shift();
for (var i:int = removedArray.length; i > 0; i--){
// t = Tile removed from top of column
var t:Tile = removedArray[removedArray.length-1];
// tileNum = Tile Type
var tileNum:int = tileType(t);
// add tile from onScreenArray to arrGround
arrGround[tileNum].push(t);
// add Tile from top of column
t.x = -tileWidth;
t.y = -tileHeight;
removedArray.pop();
}
//add new Bottom Row
onScreenArray.push([]);
// for each col
for (var i:int = 0; i < lenCols; i++){
/////////////
// add tile to Bottom
/////////////
// botRow = bottom tile row
var botRow:int = onScreenRows-1 + onScreenTopRow;
// tType = bottom tile Type
var tType:int = complexArray[botRow][i+onScreenLeftCol];
// t = Tile to take from arrGround
var t:Tile = arrGround[tType][arrGround[tType].length - 1];
// tile on bottom of pertinent column
var bottomTile:Tile = onScreenArray[onScreenArray.length-2][i];
// add tile from arrGround to onScreeArray
onScreenArray[onScreenArray.length-1].push(t);
// remove tile from arrGround to onScreenArray
arrGround[tType].pop();
// place tile on screen
t.y = bottomTile.y + tileWidth;
t.x = bottomTile.x;
}
} else if (pan == "up"){
onScreenTopRow--;
//////////////////
// remove tile from Bottom
//////////////////
var removedArray:Array = onScreenArray.pop();
for (var i:int = removedArray.length; i > 0; i--){
// t = Tile removed from bottom of column
var t:Tile = removedArray[removedArray.length-1];
// tileNum = Tile Type
var tileNum:int = tileType(t);
// add tile from onScreenArray to arrGround
arrGround[tileNum].push(t);
// add Tile from top of column
t.x = -tileWidth;
t.y = -tileHeight;
removedArray.pop();
}
//add new Top Row
onScreenArray.unshift([]);
// for each col
for (var i:int = 0; i < lenCols; i++){
/////////////
// add tile to Top
/////////////
// topRow = Top tile row
var topRow:int = 0;
// tType = bottom tile Type
var tType:int = complexArray[onScreenTopRow][i+onScreenLeftCol];
// t = Tile to take from arrGround
var t:Tile = arrGround[tType][arrGround[tType].length - 1];
// tile on bottom of pertinent column
var topTile:Tile = onScreenArray[topRow+1][i];
// add tile from arrGround to onScreeArray
onScreenArray[topRow].push(t);
// remove tile from arrGround to onScreenArray
arrGround[tType].pop();
// place tile on screen
t.y = topTile.y - tileWidth;
t.x = topTile.x;
}
}
// put border back on top
addChild(blockingBorder);
}
The basic organization is:
if (right){
remove tile from left
add tile to right
}
if (left){
remove tile from right
add tile to left
}
if (up){
remove tile from bottom
add tile to top
}
if (down){
remove tile from top
add tile to bottom
}
but the complication in simplifying this is in the fact that adding or removing tiles to the bottom and top is implicitly different than adding them to the sides because in the former I'm adding and removing whole arrays in a 2D array, and in the latter, I'm removing or adding just one element in the 2nd level array.
edit
arrGround
is all tiles needed for the whole map
OnScreenArray
is the tiles only visible in camera.
complexArray
is the array of index numbers that tell us where from the tile map bitmap to draw the bitmap data (i.e., 1 is ground... But what type of ground is dependent on what is surrounding it, so the complex array may turn a 1 (ground) into a 13, which is a cliff edge)
The purpose of pushing and popping tiles endlessly in and out of onScreenArray
was so that I can loop through that small array for pertinent functions instead of the potentially huge array of total tiles.