Game Development Reference
In-Depth Information
Let us look at the top level pseudo code for the gen method:
• For each piece in the column:
° For each piece in the row
° TopEdge = topEdge()
° BottomEdge = bottomEdge()
° LeftEdge = leftEdge()
° RightEdge = rightEdge()
° Shape = TopEdge + BottomEdge + LeftEdge + RightEdge
To figure the top edge, we know that if it is the top-most row (row = 0), the edge
must be flat (binary 00). Otherwise, we will make the top edge compatible with
the bottom edge of the piece on top. Similarly, to figure out the bottom edge, if the
piece is on the last row, the edge will be flat; if not, we choose convex or concave
randomly. We do a similar thing for the right and the left edges. Finally, we combine
the edge values to come up with the shape ID.
shapeID = ((topData << TOP_SIDE_BIT_SHIFT) |
(bottomData << BOTTOM_SIDE_BIT_SHIFT) |
(leftData << LEFT_SIDE_BIT_SHIFT) |
(rightData << RIGHT_SIDE_BIT_SHIFT));
Here, topData , bottomData , leftData , and rightData are the edge values as 00
for flat, 10 for concave, 01 for convex. The final shape ID is obtained by bit or-ing the
values after shifting them by the right amount as show next:
private static const TOP_SIDE_BIT_SHIFT:int = 6;
private static const BOTTOM_SIDE_BIT_SHIFT:int = 4;
private static const LEFT_SIDE_BIT_SHIFT:int = 2;
private static const RIGHT_SIDE_BIT_SHIFT:int = 0;
Here is a complete listing of the gen method:
public static function gen(numCols:int, numRows:int):Array {
var workspaceNumRowDiv:int = 1;
var workspaceNumColDiv:int = 1;
var shapeIDs:Array = new Array();
// init the array
var i:int, j:int;
for ( i=0; i<numCols; i++ ) {
shapeIDs[i] = new Array();
for ( j=0; j<numRows; j++ ) {
shapeIDs[i].push(0);
 
Search WWH ::




Custom Search