// $Id: tile.js,v 1.5 2005/05/18 15:00:17 kai Exp $
// $Log: tile.js,v $
// Revision 1.5  2005/05/18 15:00:17  kai
// Fehler bei validtiles behoben
//
// Revision 1.4  2004/07/10 21:13:57  kai
// Refaktorisiert und HTML-Element-Stuktur überarbeitet
//
// Revision 1.3  2004/07/10 15:04:36  kai
// Erste Version von Anzeige des nächsten Tiles
//
// Revision 1.2  2004/07/09 23:20:31  kai
// Dummy Hiscoreliste eingefügt, fertige Reihen beschleunigen die Steine
//
// Revision 1.1.1.1  2004/07/09 22:36:28  kai
// Erster Import
//

// myplayfield = playfield which belongs to tile
// left,top = position in playfield
// objname = name of the object for setTimeout()
function tile(myplayfield, left, top, objname)
{
	// Members
	this.plfield;
	this.left;
	this.top;
	this.leftdefault;
	this.topdefault;
	this.realleft;
	this.rot;
	this.tarea;
	this.tmparea;
	this.brickname;
	this.width;
	this.height;
	this.delay;
	this.objectname;
	this.paused;
	this.realdelay;
	this.timeouthandle;
	this.thistile;
	this.nexttile;
	this.nexttileobj;

	// Member functions
	this.draw = tile_draw;
	this.move = tile_move;
	this.rotate = tile_rotate;
	this.newtimeout = tile_newtimeout;
	this.timehandle = tile_timehandle;
	this.newtile = tile_newtile;
	this.validposition = tile_validposition;
	this.getrandomtile = tile_getrandomtile;

	// Init
	this.objectname = objname;
	this.plfield = myplayfield;
	this.left = this.leftdefault = this.realleft = left;
	this.top = this.topdefault = top;
	this.rot = 0;
	this.paused = false;
	this.delay = 1000;
	this.realdelay = this.delay;
	this.tshape = new Array(7);
	this.brickname = new String();
	this.width = new Array(4);
	this.height = new Array(4);
	this.tarea = new Array(4);
	this.tmparea = new Array(4);
	this.nexttile = this.getrandomtile();
	this.nexttileobj = new nexttile(this.plfield.brickwidth, this.plfield.brickheight);

	for(i = 0; i < 4; i++)
	{
		this.tarea[i] = new Array(4);
		this.tmparea[i] = new Array(4);
		for(j = 0; j < 4; j++)
		{
			this.tarea[i][j] = new Array(4);
		}
	}

	for(i = 0; i < 7; i++)
	{
		this.tshape[i] = new Array(4);
		for(j = 0; j < 4; j++)
		{
			this.tshape[i][j] = new Array(4);
			for(k = 0; k < 4; k++)
			{
				this.tshape[i][j][k] = new String();
			}
		}
	}

	this.tshape[0][0][0] = "*..."; this.tshape[0][1][0] = "***."; this.tshape[0][2][0] = "**.."; this.tshape[0][3][0] = "..*.";
	this.tshape[0][0][1] = "*..."; this.tshape[0][1][1] = "*..."; this.tshape[0][2][1] = ".*.."; this.tshape[0][3][1] = "***.";
	this.tshape[0][0][2] = "**.."; this.tshape[0][1][2] = "...."; this.tshape[0][2][2] = ".*.."; this.tshape[0][3][2] = "....";
	this.tshape[0][0][3] = "...."; this.tshape[0][1][3] = "...."; this.tshape[0][2][3] = "...."; this.tshape[0][3][3] = "....";

	this.tshape[1][0][0] = ".*.."; this.tshape[1][1][0] = "*..."; this.tshape[1][2][0] = "**.."; this.tshape[1][3][0] = "***.";
	this.tshape[1][0][1] = ".*.."; this.tshape[1][1][1] = "***."; this.tshape[1][2][1] = "*..."; this.tshape[1][3][1] = "..*.";
	this.tshape[1][0][2] = "**.."; this.tshape[1][1][2] = "...."; this.tshape[1][2][2] = "*..."; this.tshape[1][3][2] = "....";
	this.tshape[1][0][3] = "...."; this.tshape[1][1][3] = "...."; this.tshape[1][2][3] = "...."; this.tshape[1][3][3] = "....";

	this.tshape[2][0][0] = "**.."; this.tshape[2][1][0] = "**.."; this.tshape[2][2][0] = "**.."; this.tshape[2][3][0] = "**..";
	this.tshape[2][0][1] = "**.."; this.tshape[2][1][1] = "**.."; this.tshape[2][2][1] = "**.."; this.tshape[2][3][1] = "**..";
	this.tshape[2][0][2] = "...."; this.tshape[2][1][2] = "...."; this.tshape[2][2][2] = "...."; this.tshape[2][3][2] = "....";
	this.tshape[2][0][3] = "...."; this.tshape[2][1][3] = "...."; this.tshape[2][2][3] = "...."; this.tshape[2][3][3] = "....";

	this.tshape[3][0][0] = ".**."; this.tshape[3][1][0] = "*..."; this.tshape[3][2][0] = ".**."; this.tshape[3][3][0] = "*...";
	this.tshape[3][0][1] = "**.."; this.tshape[3][1][1] = "**.."; this.tshape[3][2][1] = "**.."; this.tshape[3][3][1] = "**..";
	this.tshape[3][0][2] = "...."; this.tshape[3][1][2] = ".*.."; this.tshape[3][2][2] = "...."; this.tshape[3][3][2] = ".*..";
	this.tshape[3][0][3] = "...."; this.tshape[3][1][3] = "...."; this.tshape[3][2][3] = "...."; this.tshape[3][3][3] = "....";

	this.tshape[4][0][0] = "**.."; this.tshape[4][1][0] = ".*.."; this.tshape[4][2][0] = "**.."; this.tshape[4][3][0] = ".*..";
	this.tshape[4][0][1] = ".**."; this.tshape[4][1][1] = "**.."; this.tshape[4][2][1] = ".**."; this.tshape[4][3][1] = "**..";
	this.tshape[4][0][2] = "...."; this.tshape[4][1][2] = "*..."; this.tshape[4][2][2] = "...."; this.tshape[4][3][2] = "*...";
	this.tshape[4][0][3] = "...."; this.tshape[4][1][3] = "...."; this.tshape[4][2][3] = "...."; this.tshape[4][3][3] = "....";

	this.tshape[5][0][0] = "...."; this.tshape[5][1][0] = "*..."; this.tshape[5][2][0] = "...."; this.tshape[5][3][0] = "*...";
	this.tshape[5][0][1] = "****"; this.tshape[5][1][1] = "*..."; this.tshape[5][2][1] = "****"; this.tshape[5][3][1] = "*...";
	this.tshape[5][0][2] = "...."; this.tshape[5][1][2] = "*..."; this.tshape[5][2][2] = "...."; this.tshape[5][3][2] = "*...";
	this.tshape[5][0][3] = "...."; this.tshape[5][1][3] = "*..."; this.tshape[5][2][3] = "...."; this.tshape[5][3][3] = "*...";

	this.tshape[6][0][0] = ".*.."; this.tshape[6][1][0] = ".*.."; this.tshape[6][2][0] = "*..."; this.tshape[6][3][0] = "***.";
	this.tshape[6][0][1] = "**.."; this.tshape[6][1][1] = "***."; this.tshape[6][2][1] = "**.."; this.tshape[6][3][1] = ".*..";
	this.tshape[6][0][2] = ".*.."; this.tshape[6][1][2] = "...."; this.tshape[6][2][2] = "*..."; this.tshape[6][3][2] = "....";
	this.tshape[6][0][3] = "...."; this.tshape[6][1][3] = "...."; this.tshape[6][2][3] = "...."; this.tshape[6][3][3] = "....";

	this.newtile();
	this.draw(1);
	this.newtimeout();
	return;
}

function tile_getrandomtile()
{
	return(parseInt(7 * Math.random()));
}

function tile_newtile()
{
	this.realdelay -= this.plfield.removelines() * 25;
	if(this.realdelay < 200) this.realdelay = 200;
	this.delay = this.realdelay;

	this.thistile = this.nexttile;
	this.nexttile = this.getrandomtile();
	this.nexttileobj.settile(this);
	this.brickname = brickgfx[this.thistile].src;

	for(i1=0;i1<4;i1++)
	{
		this.width[i1]=0;
		this.height[i1]=0;
		for(i2=0;i2<4;i2++)
		{
			for(i3=0;i3<4;i3++)
			{
				if(this.tshape[this.thistile][i1][i2].substring(i3,i3+1)=="*")
				{
					this.tarea[i1][i2][i3]=1;
					if(this.width[i1]<=i3)this.width[i1]=i3+1;
					if(this.height[i1]<=i2)this.height[i1]=i2+1;
				}
				else
				{
					this.tarea[i1][i2][i3]=0;
				}
			}
		}
	}
	return;
}

function tile_timehandle(objname)
{
	if(!this.paused)
	{
		if(this.top + this.height[this.rot] < this.plfield.brickrows)
		{
			if(!this.move(0,1))
			{
				this.newtile();
				this.left = this.realleft = this.leftdefault;
				this.top = this.topdefault;
				if(!this.validposition())
				{
					this.plfield.myscore.show();
					alert("Spielende");
					return;
				}
				this.draw(1);
			}
		}
		else
		{
			this.newtile();
			this.left = this.realleft = this.leftdefault;
			this.top = this.topdefault;
			this.draw(1);
		}
	}
	this.newtimeout();
	return;
}

function tile_newtimeout()
{
	clearTimeout(this.timeouthandle);
	this.timeouthandle = setTimeout(this.objectname + ".timehandle(\"" + this.objectname + "\")", this.delay);
}

function tile_rotate()
{
	this.draw(0);
	left = this.left;
	if(++this.rot == 4) this.rot = 0;
	this.left = this.realleft;
	while(this.plfield.brickcols < this.left + this.width[this.rot])
	{
		this.left--;
	}
	if(!this.validposition())
	{
		this.left = left;
		if(--this.rot == -1) this.rot=3;
	}
	this.draw(1);
	return;
}

function tile_draw(show)
{
	for(i = 0; i < 4; i++)
	{
		for(j = 0; j < 4; j++)
		{
			if(this.tarea[this.rot][i][j]) this.plfield.showbrick(j + this.left, i + this.top, show, this.brickname);
		}
	}
	return;
}

function tile_validposition()
{
	for(i = 0; i < 4; i++)
	{
		for(j = 0; j < 4; j++)
		{
			if(this.tarea[this.rot][i][j] && (this.plfield.playarea[this.left + j][this.top + i] || this.top + i >= this.plfield.brickrows)) return false;
		}
	}
	return true;
}

function tile_move(movX, movY)
{
	this.draw(0);
	this.left += movX;
	this.realleft += movX;
	this.top += movY;
	if(!this.validposition())
	{
		this.left -= movX;
		this.realleft -= movX;
		this.top -= movY;
		this.draw(1);
		return false;
	}
	this.draw(1);
	return true;
}
