Ejemplo n.º 1
0
        public bool Set(TableCoords pos, BlockType type)
        {
            if (OutOfBounds(pos)) return false;

            field[posToIdx(pos)] = type;

            return true;
        }
Ejemplo n.º 2
0
 public MoveAnimation(Vector2 _start, Vector2 _shift, float _duration, GameField.BlockType _type, TableCoords _destination)
     : base(_duration, _type)
 {
     start = _start;
     timeElapsed = 0;
     shift = _shift;
     duration = _duration;
     destination = _destination;
     type = _type;
 }
Ejemplo n.º 3
0
 public bool OutOfBounds(TableCoords pos)
 {
     return outOfSize(pos.col) || outOfSize(pos.row);
 }
Ejemplo n.º 4
0
 public bool IsEmpty(TableCoords pos)
 {
     return Get(pos) == BlockType.Empty;
 }
Ejemplo n.º 5
0
 public BlockType Get(TableCoords pos)
 {
     return OutOfBounds(pos) ? BlockType.Empty : field[posToIdx(pos)];
 }
Ejemplo n.º 6
0
 private int posToIdx(TableCoords pos)
 {
     return pos.row * size + pos.col;
 }
Ejemplo n.º 7
0
        private bool canSwap(TableCoords block1, TableCoords block2)
        {
            int dcol = Math.Abs(block2.col - block1.col);
            int drow = Math.Abs(block2.row - block1.row);

            return dcol * drow == 0 && (dcol == 1 || drow == 1);
        }
Ejemplo n.º 8
0
 public DestroyAnimation(float _duration, TableCoords _block, GameField.BlockType _type)
     : base(_duration, _type)
 {
     block = _block;
 }
Ejemplo n.º 9
0
        private Vector2 getBlockCoords(TableCoords pos)
        {
            float x = FIELD_SHIFT_BY_X + pos.col * BLOCK_TEXTURE_SIZE;
            float y = FIELD_SHIFT_BY_Y + pos.row * BLOCK_TEXTURE_SIZE;

            return new Vector2(x, y);
        }
Ejemplo n.º 10
0
        private void drawGameScreenGameRunning(GameTime gameTime)
        {
            for (int i = 0; i < FIELD_SIZE; ++i)
            {
                for (int j = 0; j < FIELD_SIZE; ++j)
                {
                    TableCoords pos = new TableCoords(j, i);

                    if (field.IsEmpty(pos)) continue;

                    int typeIdx = (int)field.Get(pos);

                    if (isBlockSelected(pos))
                    {
                        typeIdx += (int)GameField.BlockType.BlocksCount;
                    }

                    Texture2D blockTexture = blocksTextures[typeIdx];

                    int x = FIELD_SHIFT_BY_X + j * BLOCK_TEXTURE_SIZE;
                    int y = FIELD_SHIFT_BY_Y + i * BLOCK_TEXTURE_SIZE;

                    spriteBatch.Draw(blockTexture, new Vector2(x, y), Color.White);
                }
            }

            if (!gameEnded())
            {
                info.gameTimeMillisecondsElapsed += (int)gameTime.ElapsedGameTime.TotalMilliseconds;
            }

            int timeRest = 60 - info.gameTimeMillisecondsElapsed / 1000;
            string leftSide = Convert.ToString(timeRest / 60);
            string rightSide = Convert.ToString(timeRest % 60);

            if (rightSide.Length < 2)
                rightSide = "0" + rightSide;

            spriteBatch.DrawString(common, "Timer: " + leftSide + ":" + rightSide, new Vector2(0, 0), Color.White);
            spriteBatch.DrawString(common, "Score: " + Convert.ToString(info.score), new Vector2(0, 30), Color.White);
        }
Ejemplo n.º 11
0
        private void destroyOneBlock(TableCoords block)
        {
            info.score += 25;

            runningAnimations.Add(new DestroyAnimation(DESTROY_ANIMATION_DURATION, block, field.Get(block)));

            field.SetEmpty(block);
        }
Ejemplo n.º 12
0
        private MoveAnimation createOneDropDownAnimation(Vector2 start, TableCoords destination, GameField.BlockType type)
        {
            float duration = Math.Abs(getBlockCoords(destination).Y - start.Y) / BLOCK_TEXTURE_SIZE / BLOCK_DROP_DOWN_VELOCITY;
            Vector2 shift = new Vector2(0, BLOCK_TEXTURE_SIZE * BLOCK_DROP_DOWN_VELOCITY);

            return new MoveAnimation(start, shift, duration, type, destination);
        }
Ejemplo n.º 13
0
        private void createDropDownAnimations()
        {
            for (int i = 0; i < FIELD_SIZE; ++i)
            {
                int firstEmptyRow = -1;

                for (int j = FIELD_SIZE - 1; j >= 0 && firstEmptyRow == -1; --j)
                {
                    if (field.IsEmpty(new TableCoords(i, j)))
                    {
                        firstEmptyRow = j;
                    }
                }

                if (firstEmptyRow != -1)
                {
                    int[] fallHeight = new int[FIELD_SIZE];
                    Vector2 shift = new Vector2(0, BLOCK_TEXTURE_SIZE * BLOCK_DROP_DOWN_VELOCITY);

                    fallHeight[firstEmptyRow] = 1;

                    for (int j = firstEmptyRow - 1; j >= 0; --j)
                    {
                        TableCoords pos = new TableCoords(i, j);
                        bool isEmpty = field.IsEmpty(pos);

                        fallHeight[j] = fallHeight[j + 1] + Convert.ToInt32(isEmpty);

                        if (!isEmpty)
                        {
                            TableCoords destination = new TableCoords(i, j + fallHeight[j]);

                            runningAnimations.Add(createOneDropDownAnimation(getBlockCoords(pos), destination, field.Get(pos)));

                            field.SetEmpty(pos);
                        }
                    }

                    int newBlocksCount = fallHeight[0];
                    float startY = FIELD_SHIFT_BY_Y - BLOCK_TEXTURE_SIZE;
                    float startX = getBlockCoords(new TableCoords(i, 0)).X;

                    for (int j = 0; j < newBlocksCount; ++j)
                    {
                        Vector2 start = new Vector2(startX, startY);
                        TableCoords destination = new TableCoords(i, newBlocksCount - j - 1);

                        runningAnimations.Add(createOneDropDownAnimation(start, destination, GameField.RandomType()));

                        startY -= BLOCK_TEXTURE_SIZE;
                    }
                }
            }
        }
Ejemplo n.º 14
0
        private int countBlocks(GameField.BlockType type, TableCoords start, TableCoords shift)
        {
            int counter = 0;
            TableCoords pos = start + shift;

            while (type == field.Get(pos))
            {
                ++counter;

                pos += shift;
            }

            return counter;
        }
Ejemplo n.º 15
0
 public void SetEmpty(TableCoords pos)
 {
     if (!OutOfBounds(pos))
     {
         field[posToIdx(pos)] = BlockType.Empty;
     }
 }
Ejemplo n.º 16
0
        public void Swap(TableCoords pos1, TableCoords pos2)
        {
            int idx1 = posToIdx(pos1);
            int idx2 = posToIdx(pos2);

            BlockType tmp = field[idx1];
            field[idx1] = field[idx2];
            field[idx2] = tmp;
        }
Ejemplo n.º 17
0
 private bool isBlockSelected(TableCoords pos)
 {
     return info.blockSelected() && info.curSelectedBlock.Value == pos;
 }
Ejemplo n.º 18
0
        private void updateGame()
        {
            updateField();

            if (info.previousTurn != null)
            {
                addSwapAnimation(info.previousTurn.Value.block1, info.previousTurn.Value.block2);

                info.previousTurn = null;
            }

            int gameFieldSideLength = FIELD_SIZE * BLOCK_TEXTURE_SIZE;
            Point mp = getMousePosition();
            Rectangle gameFieldRect = new Rectangle(FIELD_SHIFT_BY_X, FIELD_SHIFT_BY_Y, gameFieldSideLength, gameFieldSideLength);

            if (leftKeyClick())
            {
                if (gameFieldRect.Contains(mp))
                {
                    int selectedCol = (mp.X - FIELD_SHIFT_BY_X) / BLOCK_TEXTURE_SIZE;
                    int selectedRow = (mp.Y - FIELD_SHIFT_BY_Y) / BLOCK_TEXTURE_SIZE;
                    TableCoords selectedBlock = new TableCoords(selectedCol, selectedRow);

                    if (info.blockSelected())
                    {
                        if (canSwap(info.curSelectedBlock.Value, selectedBlock))
                        {
                            info.previousTurn = new Turn(info.curSelectedBlock.Value, selectedBlock);

                            addSwapAnimation(info.curSelectedBlock.Value, selectedBlock);
                        }

                        info.dropSelection();
                    }
                    else
                    {
                        info.curSelectedBlock = selectedBlock;
                    }
                }
                else
                {
                    info.dropSelection();
                }
            }
        }
Ejemplo n.º 19
0
        private void addSwapAnimation(TableCoords block1, TableCoords block2)
        {
            int dcol = block2.col - block1.col;
            int drow = block2.row - block1.row;

            Vector2 shift = new Vector2(dcol * BLOCK_TEXTURE_SIZE / SWAP_ANIM_DURATION, drow * BLOCK_TEXTURE_SIZE / SWAP_ANIM_DURATION);

            MoveAnimation block1Movement = new MoveAnimation(
                getBlockCoords(block1),
                shift,
                SWAP_ANIM_DURATION,
                field.Get(block1),
                block2
            );

            MoveAnimation block2Movement = new MoveAnimation(
                getBlockCoords(block2),
                -shift,
                SWAP_ANIM_DURATION,
                field.Get(block2),
                block1
            );

            runningAnimations.Add(block1Movement);
            runningAnimations.Add(block2Movement);

            field.SetEmpty(block1);
            field.SetEmpty(block2);
        }