private void HoldPiece()
        {
            TetreminoModel temp;

            if (heldTetremino == null && !swappedThisTurn)
            {
                ClearTetremino();
                heldTetremino    = new TetreminoModel(currentTetremino.GetType());
                currentTetremino = nextTetremino;
                nextTetremino    = SpawnTetromino();
                swappedThisTurn  = true;
                DrawTetremino();
                myEvents.getAggregator().PublishOnUIThread(new NextPieceEvent(nextTetremino));
                myEvents.getAggregator().PublishOnUIThread(new HeldPieceEvent(heldTetremino));
            }
            else
            {
                if (MoveIsLegal(Move.SPAWN, heldTetremino) && !swappedThisTurn)
                {
                    temp = currentTetremino;
                    ClearTetremino();
                    currentTetremino = new TetreminoModel(heldTetremino.GetType());
                    heldTetremino    = new TetreminoModel(temp.GetType());
                    DrawTetremino();
                    swappedThisTurn = true;
                    myEvents.getAggregator().PublishOnUIThread(new HeldPieceEvent(heldTetremino));
                }
            }
        }
 private void MoveDown()
 {
     if (MoveIsLegal(Move.DOWN, currentTetremino))
     {
         ClearTetremino();
         currentTetremino.MovePoint(Move.DOWN);
         DrawTetremino();
     }
     else
     {
         currentTetremino = nextTetremino;
         nextTetremino    = SpawnTetromino();
         swappedThisTurn  = false;
         if (nextTetremino != null)
         {
             //this check is to reduce the overall amounts of duplicates
             if (currentTetremino.GetType() == nextTetremino.GetType())
             {
                 nextTetremino = SpawnTetromino();
             }
             DrawTetremino();
             myEvents.getAggregator().PublishOnUIThread(new NextPieceEvent(nextTetremino));
         }
     }
 }
 private void StartGame()
 {
     gameStarted      = true;
     currentTetremino = SpawnTetromino();
     nextTetremino    = SpawnTetromino();
     heldTetremino    = null;
     myEvents.getAggregator().PublishOnUIThread(new NextPieceEvent(nextTetremino));
     myEvents.getAggregator().PublishOnUIThread(new HeldPieceEvent(heldTetremino));
     myEvents.getAggregator().PublishOnUIThread(new ScoreEvent(score));
     myEvents.getAggregator().PublishOnUIThread(new LevelEvent(level));
     DrawTetremino();
     audioManager.PlayTheme();
 }
        private TetreminoModel SpawnTetromino()
        {
            CheckRows();
            CalculateScore();
            CalculateLevel();
            TetreminoModel tempTetremino = new TetreminoModel((Tetremino)tetreminoTypes.GetValue(rand.Next(tetreminoTypes.Length)));

            if (MoveIsLegal(Move.SPAWN, tempTetremino))
            {
                return(tempTetremino);
            }
            else
            {
                EndGame();
                return(null);
            }
        }
        private void SetNext(TetreminoModel nextTetremino)
        {
            ClearCells(nextCell);

            for (int i = 0; i < nextTetremino.GetShape().Length; i++)
            {
                if (nextTetremino.GetType() == Tetremino.BLUE_I || nextTetremino.GetType() == Tetremino.YELLOW_O)
                {
                    nextCell[(int)(nextTetremino.GetShape()[i].X - 4),
                             (int)(nextTetremino.GetShape()[i].Y)].Background = nextTetremino.GetBrush();
                }
                else
                {
                    nextCell[(int)(nextTetremino.GetShape()[i].X - 3),
                             (int)(nextTetremino.GetShape()[i].Y)].Background = nextTetremino.GetBrush();
                }
            }
        }
        private void SetHeld(TetreminoModel heldTetremino)
        {
            ClearCells(heldCell);

            for (int i = 0; i < heldTetremino.GetShape().Length; i++)
            {
                if (heldTetremino.GetType() == Tetremino.BLUE_I || heldTetremino.GetType() == Tetremino.YELLOW_O)
                {
                    heldCell[(int)(heldTetremino.GetShape()[i].X - 4),
                             (int)(heldTetremino.GetShape()[i].Y)].Background = heldTetremino.GetBrush();
                }
                else
                {
                    heldCell[(int)(heldTetremino.GetShape()[i].X - 3),
                             (int)(heldTetremino.GetShape()[i].Y)].Background = heldTetremino.GetBrush();
                }
            }
        }
 private void ResetGame()
 {
     ClearBoard();
     score               = 0;
     level               = 1;
     levelThreshhold     = 500;
     gameOver            = false;
     countDown           = 4;
     interval            = 1000;
     eventTimer.Interval = interval;
     currentTetremino    = null;
     myEvents.getAggregator().PublishOnUIThread(new NextPieceEvent(null));
     myEvents.getAggregator().PublishOnUIThread(new HeldPieceEvent(null));
     myEvents.getAggregator().PublishOnUIThread(new GameOverEvent(gameOver));
     myEvents.getAggregator().PublishOnUIThread(new ScoreEvent(score));
     myEvents.getAggregator().PublishOnUIThread(new LevelEvent(level));
     eventTimer.Start();
     audioManager.PauseTheme();
 }
示例#8
0
 /// <summary>Initializes a new instance of the <see cref="NextPieceEvent" /> class.</summary>
 /// <param name="nextPiece">The next piece.</param>
 public NextPieceEvent(TetreminoModel nextPiece)
 {
     _nextPiece = nextPiece;
 }
 /// <summary>Initializes a new instance of the <see cref="HeldPieceEvent" /> class.</summary>
 /// <param name="heldPiece">The held piece type.</param>
 public HeldPieceEvent(TetreminoModel heldPiece)
 {
     _heldPiece = heldPiece;
 }
        private Boolean MoveIsLegal(Move direction, TetreminoModel checkingTetremino)
        {
            if (direction == Move.SPAWN)
            {
                for (int i = 0; i < checkingTetremino.GetShape().Length; i++)
                {
                    if (cell[(int)(checkingTetremino.GetShape()[i].X + checkingTetremino.GetPosition().X),
                             (int)(checkingTetremino.GetShape()[i].Y + checkingTetremino.GetPosition().Y)].Background != BACKGROUND_TILE)
                    {
                        if (currentTetremino == null)
                        {
                            return(false);
                        }
                        DrawTetremino();
                        return(false);
                    }
                }
                return(true);
            }
            else
            {
                ClearTetremino();

                switch (direction)
                {
                case Move.DOWN:
                    for (int i = 0; i < checkingTetremino.GetShape().Length; i++)
                    {
                        if (cell[(int)(checkingTetremino.GetShape()[i].X + checkingTetremino.GetPosition().X),
                                 (int)(checkingTetremino.GetShape()[i].Y + checkingTetremino.GetPosition().Y + 1)].Background != BACKGROUND_TILE)
                        {
                            DrawTetremino();
                            return(false);
                        }
                    }
                    break;

                case Move.RIGHT:
                    for (int i = 0; i < checkingTetremino.GetShape().Length; i++)
                    {
                        if (cell[(int)(checkingTetremino.GetShape()[i].X + checkingTetremino.GetPosition().X + 1),
                                 (int)(checkingTetremino.GetShape()[i].Y + checkingTetremino.GetPosition().Y)].Background != BACKGROUND_TILE)
                        {
                            DrawTetremino();
                            return(false);
                        }
                    }
                    break;

                case Move.LEFT:
                    for (int i = 0; i < checkingTetremino.GetShape().Length; i++)
                    {
                        if (cell[(int)(checkingTetremino.GetShape()[i].X + checkingTetremino.GetPosition().X - 1),
                                 (int)(checkingTetremino.GetShape()[i].Y + checkingTetremino.GetPosition().Y)].Background != BACKGROUND_TILE)
                        {
                            DrawTetremino();
                            return(false);
                        }
                    }
                    break;

                case Move.ROTATE:

                    Point[] tempShape = checkingTetremino.GetShape();
                    Point   tempPoint;

                    if (checkingTetremino.GetType() == Tetremino.BLUE_I && checkingTetremino.GetPosition().Y <= 1)
                    {
                        DrawTetremino();
                        return(false);
                    }
                    else
                    {
                        for (int i = 0; i < checkingTetremino.GetShape().Length; i++)
                        {
                            tempPoint = checkingTetremino.RotatePoint(tempShape[i], tempShape[2]);
                            if (cell[(int)(tempPoint.X + checkingTetremino.GetPosition().X),
                                     (int)(tempPoint.Y + checkingTetremino.GetPosition().Y)].Background != BACKGROUND_TILE)
                            {
                                DrawTetremino();
                                return(false);
                            }
                        }
                    }
                    break;

                default:
                    DrawTetremino();
                    return(false);
                }
                DrawTetremino();
                return(true);
            }
        }