Game Development Reference

In-Depth Information

public class Tunnel extends Sprite {

protected var _radius:Number;

protected var _sides:int, _depth:int;

protected var _tileWidth:Number, _tileHeight:Number;

protected var _tunnelTiles:Array;

protected var _highlightIndex:int = -1;

There are some basic properties we will need to track during

and after creation of the tunnel. Even though it is not a circle, the

radius will keep track of the distance of each tile from the center of

the tunnel. We also need to know the number of sides the tunnel

has, as well as how many tiles deep it extends. The
_tunnelTiles

array will keep track of all the tiles so they can be referenced later.

Finally, the
_highlightIndex
property will be used later when we

want to light up a set of tiles.

public function Tunnel(radius:Number, depth:int=10, sides:int=8)

{

_radius = radius;

_sides = sides;

_depth = depth;

createTunnel();

}

In the constructor, we pass the radius of the tunnel, as well as

how many tiles deep and around the tunnel are. After that we call

createTunnel
, which we will look at next.

protected function createTunnel():void {

_tunnelTiles = new Array();

var tempTile:TunnelTile = new TunnelTile();

_tileHeight = tempTile.height;

_tileWidth = (_radius * Math.tan(Math.PI/_sides)) * 2;

var angle:Number = (Math.PI * 2) / _sides;

for (var j:int = 0; j
<
_depth; j++) {

var tileSet:Array = new Array();

for (var i:int = 0; i
<
_sides; i++) {

tempTile = new TunnelTile();

tempTile.width = _tileWidth;

tempTile.x = Math.cos(i*angle) * _radius;

tempTile.y = Math.sin(i*angle) * _radius;

tempTile.z = j * _tileHeight;

tempTile.rotationX = 90;

tempTile.rotationZ = i * Math.round(radiansToDegrees

(angle)) + 90;

var ct:ColorTransform = tempTile.transform.

colorTransform;

ct.redMultiplier *= (_depth - j)/_depth;