Beispiel #1
0
        public GameScreen(ContentManager content, GraphicsDeviceManager graphics)
            : base(content, graphics)
        {
            offSet = new Vector2((Graphics.GraphicsDevice.Viewport.Width - grid.GetLength(1) * Game1.GridCellSize) / 2, 0);

            if (!Game1.isDebugMode)
            {
                for (int i = 0; i < 3; i++)
                {
                    var newPiece = GeneratePiece(i);
                    for (int j = 0; j < nextTiles.Count; j++)
                    {
                        if (nextTiles[j].PieceType == newPiece.PieceType)
                        {
                            newPiece = GeneratePiece(i);
                            j        = 0;
                        }
                    }
                    nextTiles.Add(newPiece);
                }

                current = GeneratePiece(0);
                current.GridPosition = new Point(4, -3);
            }
        }
Beispiel #2
0
        private BaseTetromino GeneratePiece(int i)
        {
            var           pieceTypeToCreate = (PieceTypes)Game1.Random.Next(0, 7);
            var           startPoint        = new Point(-4, i * 4);
            BaseTetromino newPiece          = null;


            switch (pieceTypeToCreate)
            {
            case PieceTypes.LL:
                newPiece = new LeftL(Content.Load <Texture2D>("NewPieces/LPiece"), startPoint, Color.Orange, Vector2.One, RotationOptions.NoRotation);
                break;

            case PieceTypes.RL:
                newPiece = new RightL(Content.Load <Texture2D>("NewPieces/RPiece"), startPoint, Color.Blue, Vector2.One, RotationOptions.NoRotation);
                break;

            case PieceTypes.T:
                newPiece = new TPiece(Content.Load <Texture2D>("NewPieces/TPiece"), startPoint, Color.Purple, Vector2.One, RotationOptions.NoRotation);
                break;

            case PieceTypes.LZZ:
                newPiece = new LeftZigZag(Content.Load <Texture2D>("NewPieces/LeftZigZag"), startPoint, Color.Green, Vector2.One, RotationOptions.NoRotation);
                break;

            case PieceTypes.RZZ:
                newPiece = new RightZigZag(Content.Load <Texture2D>("NewPieces/RightZigZag"), startPoint, Color.Red, Vector2.One, RotationOptions.NoRotation);
                break;

            case PieceTypes.Square:
                newPiece = new Square(Content.Load <Texture2D>("NewPieces/Square"), startPoint, Color.Yellow, Vector2.One, RotationOptions.NoRotation);
                break;

            case PieceTypes.Straight:
                newPiece = new StraightPiece(Content.Load <Texture2D>("NewPieces/StraightPiece"), startPoint, Color.LightBlue, Vector2.One, RotationOptions.NoRotation);
                break;
            }


            return(newPiece);
        }
Beispiel #3
0
        public override void Update(GameTime gameTime)
        {
            /* This is debug to see that the grid matches with the visualization */
            if (InputManager.KeyboardState.IsKeyDown(Keys.P))
            {
                isPaused = !isPaused;
                string content = "";
                for (int i = 0; i < grid.GetLength(0); i++)
                {
                    for (int j = 0; j < grid.GetLength(1); j++)
                    {
                        if (grid[i, j] == true)
                        {
                            content += "1, ";
                        }
                        else
                        {
                            content += "0, ";
                        }
                    }
                    content += '\n';
                }
                File.WriteAllText("gridRepresentation.txt", content);
            }

            //check for receive

            if (isPaused == true || Game1.isDebugMode)
            {
                return;
            }

            if (InputManager.KeyboardState.IsKeyDown(Keys.H) && InputManager.OldKeyboardState.IsKeyUp(Keys.H))
            {
                if (holdPiece == null)
                {
                    holdPiece = current;
                    holdPiece.shouldProject = false;
                    holdPiece.GridPosition  = new Point(11, 0);

                    current = nextTiles[0];
                    current.GridPosition = new Point(4, -3);

                    nextTiles.RemoveAt(0);

                    foreach (var tile in nextTiles)
                    {
                        tile.GridPosition.Y -= 4;
                    }

                    nextTiles.Add(GeneratePiece(2));
                }
                else
                {
                    var temp = current;

                    current = holdPiece;
                    current.GridPosition  = new Point(MathHelper.Clamp(temp.GridPosition.X, 0, grid.GetLength(1) - current.Shape[current.RotationOption].GetLength(1)), temp.GridPosition.Y);
                    current.shouldProject = false;

                    holdPiece = temp;
                    holdPiece.GridPosition  = new Point(11, 0);
                    holdPiece.shouldProject = false;
                }
            }

            if (current.GridPosition.Y > 0 && current.GridPosition.X > 0 && current.GridPosition.X < grid.GetLength(1))
            {
                current.shouldProject = true;
            }

            if (InputManager.KeyboardState.IsKeyDown(Keys.Space) && InputManager.OldKeyboardState.IsKeyUp(Keys.Space) &&
                current.GridPosition.Y > 0)
            {
                current.elapsedMoveDownTime = TimeSpan.Zero;

                current.GridPosition.Y = current.LowestGridPosition();
            }


            current.Update(gameTime);


            if (current.IsEnabled == false)
            {
                var       spots        = current.GetMarkedSpots(current.RotationOption, current.Shape);
                Texture2D textureToUse = null;
                Color     colorToUse   = Color.White;
                switch (current.PieceType)
                {
                case PieceTypes.LL:
                    textureToUse = Content.Load <Texture2D>("Cells/LCell");
                    colorToUse   = Color.Orange;
                    break;

                case PieceTypes.RL:
                    textureToUse = Content.Load <Texture2D>("Cells/LCell");
                    colorToUse   = Color.Blue;
                    break;

                case PieceTypes.T:
                    textureToUse = Content.Load <Texture2D>("Cells/TPieceCell");
                    colorToUse   = Color.Purple;
                    break;

                case PieceTypes.LZZ:
                    textureToUse = Content.Load <Texture2D>("Cells/ZigZagCell");
                    colorToUse   = Color.Green;
                    break;

                case PieceTypes.RZZ:
                    textureToUse = Content.Load <Texture2D>("Cells/ZigZagCell");
                    colorToUse   = Color.Red;
                    break;

                case PieceTypes.Square:
                    textureToUse = Content.Load <Texture2D>("Cells/SquareCell");
                    colorToUse   = Color.Yellow;
                    break;

                case PieceTypes.Straight:
                    textureToUse = Content.Load <Texture2D>("Cells/StraightPieceCell");
                    colorToUse   = Color.LightBlue;
                    break;
                }
                for (int i = 0; i < spots[0].Count; i++)
                {
                    var point = new Point(current.GridPosition.X + spots[0][i], current.GridPosition.Y + spots[1][i]);
                    boomers.Add(new Cell(textureToUse, point, colorToUse, Vector2.One));
                }

                current = nextTiles[0];
                current.GridPosition = new Point(4, -3);

                nextTiles.RemoveAt(0);

                foreach (var tile in nextTiles)
                {
                    tile.GridPosition.Y -= 4;
                }

                var newPiece = GeneratePiece(2);
                for (int i = 0; i < nextTiles.Count; i++)
                {
                    if (nextTiles[i].PieceType == newPiece.PieceType)
                    {
                        newPiece = GeneratePiece(2);
                        i        = 0;
                    }
                }
                nextTiles.Add(newPiece);
            }


            List <int> rowFilledYs = new List <int>();

            for (int y = 0; y < grid.GetLength(0); y++)
            {
                int count = 0;
                for (int x = 0; x < grid.GetLength(1); x++)
                {
                    if (grid[y, x] == true)
                    {
                        count++;
                    }
                }
                if (count >= 10)
                {
                    rowFilledYs.Add(y);
                }
            }

            if (rowFilledYs.Count > 0 && amountOfFades == 0)
            {
                highestRowAffected = rowFilledYs.OrderByDescending(x => x).First();
                amountOfRowsToDrop = rowFilledYs.Count;

                for (int i = 0; i < rowFilledYs.Count; i++)
                {
                    for (int j = 0; j < boomers.Count; j++)
                    {
                        if (boomers[j].GridPosition.Y == rowFilledYs[i])
                        {
                            boomers[j].isFadingOut = true;
                            amountOfFades++;
                        }
                    }
                }
            }

            int doneCount = 0;

            for (int i = 0; i < boomers.Count; i++)
            {
                boomers[i].Update(gameTime);
                if (boomers[i].TravelPercentage >= 1f)
                {
                    doneCount++;
                }
            }

            if (doneCount == amountOfFades && amountOfFades != 0)
            {
                //reset amountOfFades to 0
                for (int i = 0; i < boomers.Count; i++)
                {
                    if (boomers[i].isFadingOut == true)
                    {
                        boomers.RemoveAt(i);
                        i--;
                        continue;
                    }

                    if (boomers[i].GridPosition.Y <= highestRowAffected)
                    {
                        boomers[i].GridPosition.Y += amountOfRowsToDrop;
                    }
                }

                amountOfFades = 0;

                for (int w = 0; w < grid.GetLength(0); w++)
                {
                    for (int z = 0; z < grid.GetLength(1); z++)
                    {
                        grid[w, z] = false;
                        for (int i = 0; i < boomers.Count; i++)
                        {
                            if (boomers[i].GridPosition.X == z &&
                                boomers[i].GridPosition.Y == w)
                            {
                                grid[w, z] = true;
                            }
                        }
                    }
                }
            }

            base.Update(gameTime);
        }