var Dungeon = function(map,width,height,wallSize,texture)
{
	this.init();

	this.setMaterial(1.000, 1.000, 1.000, 1.000, 0.000, 0.000, 1.000, 0.000, 5.00, initTexture(texture));

	for ( var x = 1; x < width-1; x++ )
	{
		for ( var z = 1; z < height-1; z++ )
		{
			switch ( map[z*width+x] )
			{
			case 1:
				//floor
				this.setVertex(x*wallSize    , 0, z*wallSize    , 0, -1, 0, 0);
				this.setVertex((x+1)*wallSize, 0, z*wallSize    , 0, -1, 0, 0);
				this.setVertex((x+1)*wallSize, 0, (z+1)*wallSize, 0, -1, 0, 0);
				this.setVertex((x+1)*wallSize, 0, (z+1)*wallSize, 0, -1, 0, 0);
				this.setVertex(x*wallSize    , 0, (z+1)*wallSize, 0, -1, 0, 0);
				this.setVertex(x*wallSize    , 0, z*wallSize    , 0, -1, 0, 0);
				this.setUV(0, 0);
				this.setUV(0.5, 0);
				this.setUV(0.5, 0.5);
				this.setUV(0.5, 0.5);
				this.setUV(0, 0.5);
				this.setUV(0, 0);
				//Ceiling
				this.setVertex(x*wallSize    , 200, (z+1)*wallSize, 0, -1, 0, 0);
				this.setVertex((x+1)*wallSize, 200, (z+1)*wallSize, 0, -1, 0, 0);
				this.setVertex((x+1)*wallSize, 200, z*wallSize    , 0, -1, 0, 0);
				this.setVertex((x+1)*wallSize, 200, z*wallSize    , 0, -1, 0, 0);
				this.setVertex(x*wallSize    , 200, z*wallSize    , 0, -1, 0, 0);
				this.setVertex(x*wallSize    , 200, (z+1)*wallSize, 0, -1, 0, 0);
				this.setUV(0, 0);
				this.setUV(0.5, 0);
				this.setUV(0.5, 0.5);
				this.setUV(0.5, 0.5);
				this.setUV(0, 0.5);
				this.setUV(0, 0);
				//North
				if ( map[(z+1)*width+x] == 0 )
				{
					this.setVertex((x+1)*wallSize,   0, (z+1)*wallSize, 0, -1, 0, 0);
					this.setVertex((x+1)*wallSize, 200, (z+1)*wallSize, 0, -1, 0, 0);
					this.setVertex(x*wallSize    , 200, (z+1)*wallSize, 0, -1, 0, 0);
					this.setVertex(x*wallSize    , 200, (z+1)*wallSize, 0, -1, 0, 0);
					this.setVertex(x*wallSize    ,   0, (z+1)*wallSize, 0, -1, 0, 0);
					this.setVertex((x+1)*wallSize,   0, (z+1)*wallSize, 0, -1, 0, 0);
					this.setUV(0, 0.5);
					this.setUV(0.5, 0.5);
					this.setUV(0.5, 1);
					this.setUV(0.5, 1);
					this.setUV(0, 1);
					this.setUV(0, 0.5);
				}
				//South
				if ( map[(z-1)*width+x] == 0 )
				{
					this.setVertex(x*wallSize    ,   0, z*wallSize, 0, -1, 0, 0);
					this.setVertex(x*wallSize    , 200, z*wallSize, 0, -1, 0, 0);
					this.setVertex((x+1)*wallSize, 200, z*wallSize, 0, -1, 0, 0);
					this.setVertex((x+1)*wallSize, 200, z*wallSize, 0, -1, 0, 0);
					this.setVertex((x+1)*wallSize,   0, z*wallSize, 0, -1, 0, 0);
					this.setVertex(x*wallSize    ,   0, z*wallSize, 0, -1, 0, 0);
					this.setUV(0, 0.5);
					this.setUV(0.5, 0.5);
					this.setUV(0.5, 1);
					this.setUV(0.5, 1);
					this.setUV(0, 1);
					this.setUV(0, 0.5);
				}
				//West
				if ( map[z*width+(x-1)] == 0 )
				{
					this.setVertex(x*wallSize,   0, (z+1)*wallSize, 0, -1, 0, 0);
					this.setVertex(x*wallSize, 200, (z+1)*wallSize, 0, -1, 0, 0);
					this.setVertex(x*wallSize, 200, z*wallSize    , 0, -1, 0, 0);
					this.setVertex(x*wallSize, 200, z*wallSize    , 0, -1, 0, 0);
					this.setVertex(x*wallSize,   0, z*wallSize    , 0, -1, 0, 0);
					this.setVertex(x*wallSize,   0, (z+1)*wallSize, 0, -1, 0, 0);
					this.setUV(0.5, 0);
					this.setUV(0.5, 0.5);
					this.setUV(1, 0.5);
					this.setUV(1, 0.5);
					this.setUV(0.5, 0.5);
					this.setUV(0.5, 0);
				}
				//East
				if ( map[z*width+(x+1)] == 0 )
				{
					this.setVertex((x+1)*wallSize,   0, z*wallSize    , 0, -1, 0, 0);
					this.setVertex((x+1)*wallSize, 200, z*wallSize    , 0, -1, 0, 0);
					this.setVertex((x+1)*wallSize, 200, (z+1)*wallSize, 0, -1, 0, 0);
					this.setVertex((x+1)*wallSize, 200, (z+1)*wallSize, 0, -1, 0, 0);
					this.setVertex((x+1)*wallSize,   0, (z+1)*wallSize, 0, -1, 0, 0);
					this.setVertex((x+1)*wallSize,   0, z*wallSize    , 0, -1, 0, 0);
					this.setUV(0.5, 0);
					this.setUV(0.5, 0.5);
					this.setUV(1, 0.5);
					this.setUV(1, 0.5);
					this.setUV(0.5, 0.5);
					this.setUV(0.5, 0);
				}
				break;
			}
		}
	}

	this.initBuffers();
};
Dungeon.prototype = new Model3D();
