private void Spawn(Utility.Timer timer)
        {
            if (nextPiece == null)
            {
                nextPiece = getRandomTetrisPiece();
            }

            currentPiece = nextPiece;
            currentPieceMaxLen = Math.Max(currentPiece.shape.GetLength(0), currentPiece.shape.GetLength(1));
            currentPiece.body.Position = getSpawnPosition();
            currentPieceCollide = new OnCollisionEventHandler(currentPieceCollision);
            currentPieceSeparate = new OnSeparationEventHandler(currentPieceSeparation);
            currentPiece.body.OnCollision += currentPieceCollide;
            currentPiece.body.OnSeparation += currentPieceSeparate;
            currentPieceRotation = JointFactory.CreateFixedAngleJoint(_world, currentPiece.body);
            pieces.Add(currentPiece);
            activePieces.Add(currentPiece);

            currentCheat = null;
            --countdownToCheat;
            if (countdownToCheat < 0)
            {
                currentCheat = new TetrisPiece(_world, tetrisTextures[2], tetrisShapes[2], currentPiece.body.WorldCenter + new Vector2(1, -2));
                currentCheat.body.FixedRotation = true;
                currentCheat.body.Rotation = (float)Math.PI / 2;
                currentCheat.body.OnCollision += currentPieceCollide;
                currentCheat.body.OnSeparation += currentPieceSeparate;
                JointFactory.CreateRevoluteJoint(_world, currentCheat.body, currentPiece.body, currentPiece.body.LocalCenter);
                pieces.Add(currentCheat);
                activePieces.Add(currentCheat);
                countdownToCheat = 5;
            }

            currentPiece.body.Enabled = true;
            if(currentCheat!=null) currentCheat.body.Enabled = true;

            nextPiece = getRandomTetrisPiece();

            //Debug.Print("Spawn new tetris piece at: {0}, {1}", currentPiece.body.Position.X, currentPiece.body.Position.Y);
        }
        public void DrawTetrisPiece(TetrisPiece piece, Color color)
        {
            Matrix mat = Matrix.CreateRotationZ(piece.body.Rotation) * Matrix.CreateTranslation(new Vector3(piece.body.Position, 0.0f));

            GraphicsDevice.SamplerStates[0] = SamplerState.AnisotropicClamp;
            GraphicsDevice.BlendState = BlendState.AlphaBlend;

            effect.Parameters["Projection"].SetValue(Matrix.CreateOrthographicOffCenter(0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, 0, 0, 1));
            //effect.Parameters["View"].SetValue(mat);
            effect.Parameters["World"].SetValue(Matrix.Identity);
            effect.Parameters["BasicTexture"].SetValue(piece.texture);
            effect.CurrentTechnique.Passes[0].Apply();

            bool wrapL = piece.body.Position.X < Game1.worldWidthInBlocks/2 + Game1.worldDuplicateBorder;
            bool wrapR = piece.body.Position.X > Game1.worldWidthInBlocks/2 - Game1.worldDuplicateBorder;

            for (int i = -1; i <= 1; i++)
            {
                if (i == -1 && !wrapL) continue;
                if (i == 1 && !wrapR) continue;

                Matrix off = Matrix.CreateTranslation(new Vector3(-0.5f, -0.5f, 0));
                effect.Parameters["View"].SetValue(off * mat * Matrix.CreateTranslation(new Vector3(-i * Game1.worldWidthInBlocks, 0, 0)) * cameraMatrix);
                effect.CurrentTechnique.Passes[0].Apply();

                int maxX = piece.shape.GetLength(1) + 1;
                int maxY = piece.shape.GetLength(0) + 1;
                Vertices v = new Vertices(new Vector2[] { new Vector2(0, 0), new Vector2(maxX, 0), new Vector2(maxX, maxY), new Vector2(0, maxY) });
                DrawPolygon(color, 1.0f / 5.0f, v);

                Flush();
            }
        }
        private void dropCurrentPiece()
        {
            if (currentPiece != null)
            {
                if (isCurrentPieceBlocked())
                {
                    currentPiece.body.Position = getSpawnPosition();
                }
                else
                {
                    currentPiece.body.LinearVelocity = Vector2.Zero;
                    currentPiece.body.ResetDynamics();
                    currentPiece.body.OnCollision -= currentPieceCollide;
                    currentPiece.body.OnSeparation -= currentPieceSeparate;
                    if (currentCheat != null)
                    {
                        currentCheat.body.OnCollision -= currentPieceCollide;
                        currentCheat.body.OnSeparation -= currentPieceSeparate;
                    }

                    if (currentPieceRotation != null)
                    {
                        _world.RemoveJoint(currentPieceRotation);
                        currentPieceRotation = null;
                    }

                    currentPiece = null;
                    currentPieceCollide = null;
                    currentCheat = null;

                    Game1.Timers.Create(SPAWN_TIME, false, Spawn);
                }
            }
        }
 public void DrawTetrisPiece(TetrisPiece piece)
 {
     DrawTetrisPiece(piece, Color.White);
 }