// Piece.js

// Class for a tetris piece

// Author:  Gabe Greve



// constructor

function Piece(pieceNum)

{

	this.id = pieceNum

	this.rotationID = 0;

	switch(pieceNum)

	{

	case 0:

		this.positionArray = new Array(new Position(0, 4), new Position(1, 4), 

						new Position(2, 4), new Position(3, 4));

		this.color = "purple";

		break;

	case 1:

		this.positionArray = new Array(new Position(1, 4), new Position(2, 4),

						new Position(3, 4), new Position(3, 5));

		this.color = "yellow";

		break;

	case 2:

		this.positionArray = new Array(new Position(1, 5), new Position(2, 5),

						new Position(3, 4), new Position(3, 5));

		this.color = "red";

		break;

	case 3:

		this.positionArray = new Array(new Position(1, 4), new Position(2, 4),

						new Position(2, 5), new Position(3, 5));

		this.color = "blue";

		break;

	case 4:

		this.positionArray = new Array(new Position(1, 5), new Position(2, 4),

						new Position(2, 5), new Position(3, 4));

		this.color = "green";

		break;

	case 5:

		this.positionArray = new Array(new Position(1, 4), new Position(2, 4),

						new Position(2, 5), new Position(3, 4));

		this.color = "orange";

		break;

	case 6:

		this.positionArray = new Array(new Position(2, 4), new Position(2, 5),

						new Position(3, 4), new Position(3, 5));

		this.color = "white";

		break;

	default:

		alert("Error could not construct piece number: " + i);

		break;

	}

}



Piece.prototype.positionArray;

Piece.prototype.color;

Piece.prototype.id;

Piece.prototype.rotationID;



Piece.prototype.rotate = function()

{

    var offsetArray;

	switch(this.id)

	{

	case 0:

		if (this.rotationID == 0)

		{

			offsetArray = new Array(new Position(2, -1), new Position(1, 0), 

						new Position(0, 1), new Position(-1, 2));

			if (this.checkAndDoRotation(offsetArray))

			{

				this.rotationID++;

			}

		}

		else if (this.rotationID == 1)

		{

			offsetArray = new Array(new Position(-2, 1), new Position(-1, 0), 

						new Position(0, -1), new Position(1, -2));

			if (this.checkAndDoRotation(offsetArray))

			{

				this.rotationID--;

			}

		}

		else 

		{

			alert("Error bad rotation ID (" + this.rotationID + ") for piece " + i);

		}

		break;

	case 1:

		switch(this.rotationID)

		{

		case 0:

			offsetArray = new Array(new Position(1, 1), new Position(0, 0), 

						new Position(0, 0), new Position(-1, 1));

			if (this.checkAndDoRotation(offsetArray))

			{

				this.rotationID++;

			}

			break;

		case 1:

			offsetArray = new Array(new Position(0, 0), new Position(-1, 0), 

						new Position(-2, 1), new Position(1, -1));

			if (this.checkAndDoRotation(offsetArray))

			{

				this.rotationID++;

			}

			break;

		case 2:

			offsetArray = new Array(new Position(0, 1), new Position(2, 2), 

						new Position(2, -1), new Position(0, 0));

			if (this.checkAndDoRotation(offsetArray))

			{

				this.rotationID++;

			}

			break;

		case 3:

			offsetArray = new Array(new Position(-1, -2), new Position(-1, -2), 

						new Position(0, 0), new Position(0, 0));

			if (this.checkAndDoRotation(offsetArray))

			{

				this.rotationID = 0;

			}

			break;

		default:

			alert("Error bad rotation ID (" + this.rotationID + ") for piece " + i);

			break;

		}

		break;

	case 2:

		switch(this.rotationID)

		{

		case 0:

			offsetArray = new Array(new Position(1, -1), new Position(1, 1), 

						new Position(0, 0), new Position(0, 0));

			if (this.checkAndDoRotation(offsetArray))

			{

				this.rotationID++;

			}

			break;

		case 1:

			offsetArray = new Array(new Position(0, 0), new Position(-2, -1), 

						new Position(0, 0), new Position(-2, -1));

			if (this.checkAndDoRotation(offsetArray))

			{

				this.rotationID++;

			}

			break;

		case 2:

			offsetArray = new Array(new Position(0, 0), new Position(1, 0), 

						new Position(-1, 2), new Position(2, 2));

			if (this.checkAndDoRotation(offsetArray))

			{

				this.rotationID++;

			}

			break;

		case 3:

			offsetArray = new Array(new Position(-1, 1), new Position(0, 0), 

						new Position(1, -2), new Position(0, -1));

			if (this.checkAndDoRotation(offsetArray))

			{

				this.rotationID = 0;

			}

			break;

		default:

			alert("Error bad rotation ID (" + this.rotationID + ") for piece " + i);

			break;

		}

		break;

	case 3:

		if (this.rotationID == 0)

		{

			offsetArray = new Array(new Position(1, 2), new Position(1, 0), 

						new Position(0, 0), new Position(0, 0));

			if (this.checkAndDoRotation(offsetArray))

			{

				this.rotationID++;

			}

		}

		else if (this.rotationID == 1)

		{

			offsetArray = new Array(new Position(-1, -2), new Position(-1, 0), 

						new Position(0, 0), new Position(0, 0));

			if (this.checkAndDoRotation(offsetArray))

			{

				this.rotationID--;

			}

		}

		else 

		{

			alert("Error bad rotation ID (" + this.rotationID + ") for piece " + i);

		}

		break;

	case 4:

		if (this.rotationID == 0)

		{

			offsetArray = new Array(new Position(1, -2), new Position(0, 0), 

						new Position(1, 0), new Position(0, 0));

			if (this.checkAndDoRotation(offsetArray))

			{

				this.rotationID++;

			}

		}

		else if (this.rotationID == 1)

		{

			offsetArray = new Array(new Position(-1, 2), new Position(0, 0), 

						new Position(-1, 0), new Position(0, 0));

			if (this.checkAndDoRotation(offsetArray))

			{

				this.rotationID--;

			}

		}

		else 

		{

			alert("Error bad rotation ID (" + this.rotationID + ") for piece " + i);

		}

		break;

	case 5:

		switch(this.rotationID)

		{

		case 0:

			offsetArray = new Array(new Position(1, 2), new Position(0, 0), 

						new Position(0, 0), new Position(0, 1));

			if (this.checkAndDoRotation(offsetArray))

			{

				this.rotationID++;

			}

			break;

		case 1:

			offsetArray = new Array(new Position(-1, -1), new Position(0, 0), 

						new Position(0, 0), new Position(0, 0));

			if (this.checkAndDoRotation(offsetArray))

			{

				this.rotationID++;

			}

			break;

		case 2:

			offsetArray = new Array(new Position(2, 1), new Position(1, 0), 

						new Position(0, 0), new Position(0, 0));

			if (this.checkAndDoRotation(offsetArray))

			{

				this.rotationID++;

			}

			break;

		case 3:

			offsetArray = new Array(new Position(-2, -2), new Position(-1, 0), 

						new Position(0, 0), new Position(0, -1));

			if (this.checkAndDoRotation(offsetArray))

			{

				this.rotationID = 0;

			}

			break;

		default:

			alert("Error bad rotation ID (" + this.rotationID + ") for piece " + i);

			break;

		}

		break;

	case 6: // this piece cannot rotate

		break;

	default:

		alert("Error could not rotate piece number: " + i);

		break;

	}

}



Piece.prototype.checkAndDoRotation = function(offsetArray)

{

    var newPositionArray = new Array();

	// check if piece can be placed

    var pos;

    var element;

	for (var i = 0; i < 4; i++) // 4 blocks per piece

	{

		pos = this.positionArray[i];

		var x = pos.getX() + offsetArray[i].getX();

		var y = pos.getY() + offsetArray[i].getY();

		element = document.getElementById("index"+x+"_"+y);

		if (y < 0 || y > 9 || 

			"url(images/background.png)" != element.style.backgroundImage &&

			"url(images/"+this.color+".png)" != element.style.backgroundImage)

		{

			return false;

		}

		newPositionArray[i] = new Position(x, y);

	}

	

	// clear old piece positions

	for (var j = 0; j < 4; j++) // 4 blocks per piece

	{

		pos = this.positionArray[j];

		element = document.getElementById("index"+pos.getX()+"_"+pos.getY());

		element.style.backgroundImage="url(images/background.png)";

	}

	// rotate piece

	for (var k = 0; k < 4; k++) // 4 blocks per piece

	{

		pos = newPositionArray[k];

		element = document.getElementById("index"+pos.getX()+"_"+pos.getY());

		element.style.backgroundImage="url(images/"+this.color+".png)";

	}

	this.positionArray = newPositionArray;

	return true;

}



Piece.prototype.getPositionArray = function()

{

	return this.positionArray;

}



Piece.prototype.setPositionArray = function(positionArray)

{

	this.positionArray = positionArray;

}



Piece.prototype.getColor = function()

{

	return this.color;

}


