/* This object implements a background-- a board The board has features. It is tiled; which tile determines which feature. The board's tiles are in one array, tiles. There are functions to call to determine 1) which square you're in 2) where its edges are 3) what is in a square */ package { import flash.display.Sprite; public class board extends Sprite { // concerning the board, where the tiles lie private var numRows:int; private var numColumns:int; private var numSquares:int; private var squareSize:int; // size of a square in pixels private var halfSquareSize:int; // size of a square in pixels private var boardContents:Array; // what tile is in each square // concerning the tiles-- what is shown private var tiles:Array; // the set of tiles private var numTiles; // # different tiles public function board(nr, nc, ss) { numRows = nr; numColumns = nc; numSquares = nr*nc; squareSize = ss; halfSquareSize = ss/2; numTiles = 1; // just those blocks, for now.. boardContents = new Array(numRows*numColumns); fillBoard(); makeTiles(); arrangeTiles(); } /////////////////////// these routines are called only by the constructor // this function sets what tiles are where // modify this to load a level... public function fillBoard() { var i, blockCtr:int; for (i=0; i<(numSquares); ++i) { if (Math.random()>0.75) { boardContents[i] = blockCtr; ++blockCtr; } else { boardContents[i] = -1; } } } // make enough sprites of the tiles // to fill all the squares of the board public function makeTiles() { var i:int; tiles = new Array(numTiles*numSquares); for (i=0; i<(numTiles*numSquares); ++i) { tiles[i] = new block_sm_sym(); tiles[i].x = -1000.0; // hide this tile tiles[i].y = 0.0; addChild(tiles[i]); } } // now that the tiles are available, // move them to where "boardContents" says they should be. public function arrangeTiles() { var i, pn:int; for (i=0; i<(numRows*numColumns); ++i) { pn = boardContents[i]; // is there a piece here? if (pn!=-1) { tiles[pn].x = squareLeftX(i); // move the symbol to here tiles[pn].y = squareHighY(i); addChild(tiles[pn]); } } } //////////////////////////// these are called by plat at runtime // return true if the given position is in a block, false otherwise public function inBlock(xin:Number, yin:Number):Boolean { if (contents(xin, yin)!=-1) { return true; } return false; } // given a position, can you stand there? // that is, is there a block under you? // return 0 for no, 1 for yes public function onBlock(xin:Number, yin:Number):Boolean { if (contentsBelow(xin, yin)==-1) { return false; } // no floor! if (rowAltitude(yin)>20) { return false; } return true; } // given two successive positions, have you hit // the side of a block? if so, return 1, OW 0 // return 0 for no, 1 for horizontal collision, 10 for error public function sideCollision(x1, y1, x2, y2:Number):int { var res:int; // are we illegally embedded in a block? that's an error if (inBlock(x1, y1)) { res =10; } else { // we're in space, ok. if (!inBlock(x2, y2)) { res=0; // nothing to hit! } else { // something to hit if (columnNumber(x1)!=columnNumber(x2)) { //trace(x1, columnNumber(x1), x2, columnNumber(x2)); // x1 & x2 in different columns? you hit something res = 1; } } } return res; } // same, but for vertical: 0 none, 1 hit bottom of cube, 2 hit top, 10 error public function vertCollision(x1, y1, x2, y2:Number):int { var res:int; // are we illegally embedded in a block? that's an error if (inBlock(x1, y1)) { res =10; } else { // we're in space, ok. if (!inBlock(x2, y2)) { res=0; // nothing to hit! } else { // if y1 and y2 are in different rows; crossed bounds if (rowNumber(y1)!=rowNumber(y2)) { if (y1>y2) { // going up? +y is down, remember res = 1; // bottom hit } else { // going down? res = 2; // top hit } } } } return res; } /////////////////////////// helper routines, called only by board members // given an x pixel coordinate, tell which column you're in private function columnNumber(xIn:int):int { return int(Math.floor(xIn/squareSize)); } // given a y pixel coordinate, tell which row you're in private function rowNumber(yIn:int):int { return int(Math.floor(yIn/squareSize)); } // given a y pixel coordinate, give its distance from the nearest // square bottom (remembering that +y is up, of course) private function rowAltitude(yIn:int):int { var rowNum:int; var n:Number; rowNum = rowNumber(yIn); n = (rowNumber+1)*squareSize; return n - yIn; } // given location on screen, return which box it's in, or -1 // this number is an index into the "boardContents" array private function squareIndex(xIn:int, yIn:int):int { return (rowNumber(yIn)*numColumns)+columnNumber(xIn); } // given a box number, return x or y coords of // positions on the boxes // upper left corners private function squareLeftX(which:int): int { return (which%numColumns)*squareSize; } private function squareHighY(which:int): int { return int(Math.floor(which/numColumns))*squareSize; } // middles private function squareMidX(which:int): int { return squareLeftX(which)+halfSquareSize; } private function squareMidY(which:int): int { return squareHighY(which)+halfSquareSize; } // contents of the square you're in private function contents(xIn:int, yIn:int):int { if (columnNumber(xIn)<0) { return 1; } if (columnNumber(xIn)>=numColumns) { return 1; } if (rowNumber(yIn)<0) { return 1; } if (rowNumber(yIn)>=numRows) { return 1; } return boardContents[squareIndex(xIn, yIn)]; } // contents of the square below you private function contentsBelow(xIn:int, yIn:int):int { return contents(xIn, yIn+squareSize-10); } }}