Exemple #1
0
 private void DiselectCurrentCell()
 {
     currentCell.SwitchSelection();
     selectedCell = null;
 }
Exemple #2
0
 private void SelectCurrentCell()
 {
     currentCell.SwitchSelection();
     selectedCell?.SwitchSelection();
     selectedCell = currentCell;
 }
Exemple #3
0
 internal void UnswapBlocks()
 {
     currentCell.SwapWith(selectedCell, true);
     selectedCell = null;
 }
Exemple #4
0
        /// <summary>
        /// Process user interactions. Highlight hovered over cells and selects clicked cells. Checks for swap action.
        /// </summary>
        /// <returns>Returns true if user swapped blocks</returns>
        internal bool UserInput()
        {
            MouseState mouseState = Mouse.GetState();

            if (Rectangle.Contains(mouseState.Position))
            {
                int i = (mouseState.Position.Y - Rectangle.Y) / CellSize.Y;
                int j = (mouseState.Position.X - Rectangle.X) / CellSize.X;

                if (currentCell != null && cells[i, j] != currentCell)
                {
                    currentCell.State = GuiElementState.Normal;
                }
                currentCell = cells[i, j];

                if (mouseState.LeftButton == ButtonState.Released && currentCell.State == GuiElementState.Pressed)
                {
                    currentCell.State = GuiElementState.Hover;
                    if (currentCell.IsSelected)
                    {
                        DiselectCurrentCell();
                    }
                    else if (selectedCell != null && currentCell.IsCloseTo(selectedCell))
                    {
                        //Swap condition reached
                        return true;
                    }
                    else
                    {
                        SelectCurrentCell();
                    }
                }
                else if (mouseState.LeftButton == ButtonState.Pressed)
                {
                    currentCell.State = GuiElementState.Pressed;
                }
                else
                {
                    currentCell.State = GuiElementState.Hover;
                }
            }
            else
            {
                if (currentCell != null)
                {
                    currentCell.State = GuiElementState.Normal;
                }
            }
            return false;
        }
Exemple #5
0
        /// <summary>
        /// Looks for horizontal and vertical matches, destroys matched blocks and spawns bonuses.
        /// </summary>
        /// <returns>Returns score points earned in this destroy cycle. Zero score means no match.</returns>
        internal int MatchAndDestroy()
        {
            int score = 0;
            List<Cell> toDestroy = new List<Cell>(64);
            List<Cell> newBonuses = new List<Cell>();

            for (int i = 0; i < cells.GetLength(0); i++)
            {
                List<Cell> temp = new List<Cell>(8) { cells[i, 0] };
                for (int j = 1; j < cells.GetLength(1); j++)
                {
                    bool stop = false;
                    if (cells[i, j].Shape == temp[0].Shape)
                    {
                        temp.Add(cells[i, j]);
                    }
                    else
                    {
                        stop = true;
                    }
                    if (stop || j == cells.GetLength(1) - 1)
                    {
                        if (temp.Count >= Consts.MATCH_MIN)
                        {
                            if (selectedCell != null)
                            {
                                newBonuses.Add(SpawnBonus(temp, Bonus.LineHorizontal));
                            }
                            toDestroy.AddRange(temp);
                            score += temp.Count * temp.Count * Consts.SCORE_BONUS;
                        }
                        temp.Clear();
                        temp.Add(cells[i, j]);
                    }
                }
            }
            for (int j = 0; j < cells.GetLength(1); j++)
            {
                List<Cell> temp = new List<Cell>(8) { cells[0, j] };
                for (int i = 1; i < cells.GetLength(0); i++)
                {
                    bool stop = false;
                    if (cells[i, j].Shape == temp[0].Shape)
                    {
                        temp.Add(cells[i, j]);
                    }
                    else
                    {
                        stop = true;
                    }
                    if (stop || i == cells.GetLength(0) - 1)
                    {
                        if (temp.Count >= Consts.MATCH_MIN)
                        {
                            if (selectedCell != null)
                            {
                                newBonuses.Add(SpawnBonus(temp, Bonus.LineVertical));
                            }
                            var intersect = toDestroy.Intersect(temp);
                            List<Cell> intersectList = intersect.ToList();
                            foreach (var bonusCell in intersectList)
                            {
                                temp.Remove(bonusCell);
                                toDestroy.Remove(bonusCell);
                                bonusCell.Bonus = Bonus.Bomb;
                            }
                            toDestroy.AddRange(temp);
                            score += temp.Count * temp.Count * Consts.SCORE_BONUS;
                        }
                        temp.Clear();
                        temp.Add(cells[i, j]);
                    }
                }
            }
            newBonuses.ForEach(c => toDestroy.Remove(c));
            toDestroy.ForEach(cell => cell.Destroy());
            if (selectedCell != null && score > 0)
            {
                selectedCell = null;
            }
            return score;
        }
Exemple #6
0
        internal override void LoadContent(GraphicsDeviceManager graphics, ContentManager content)
        {
            shapesAtlas = new ShapesAtlas(content.Load<Texture2D>("shapes"));
            backTexture = new Texture2D(graphics.GraphicsDevice, 1, 1);
            backTexture.SetData(new[] { Color.White });
            fireTexture = content.Load<Texture2D>("fire");

            for (int i = 0; i < cells.GetLength(0); i++)
            {
                for (int j = 0; j < cells.GetLength(1); j++)
                {
                    Cell cell = new Cell(this, Shape.Empty, shapesAtlas, backTexture, i, j);
                    cells[i, j] = cell;
                }
            }
        }
Exemple #7
0
 public bool IsCloseTo(Cell cell)
 {
     return (Column == cell.Column && (Row == cell.Row - 1 || Row == cell.Row + 1)) ||
         (Row == cell.Row && (Column == cell.Column - 1 || Column == cell.Column + 1)) ? true : false;
 }
Exemple #8
0
        internal void SwapWith(Cell cell, bool unswap)
        {
            int swapSpeed = unswap ? SPEED_UNSWAP : SPEED_SWAP;

            State = GuiElementState.Normal;
            cell.State = GuiElementState.Normal;

            Vector2 tempLocation = location;
            location = cell.location;
            cell.location = tempLocation;

            cell.moveDestination = location;
            moveDestination = cell.location;

            Shape tempShape = Shape;
            Shape = cell.Shape;
            cell.Shape = tempShape;

            Bonus tempBonus = Bonus;
            Bonus = cell.Bonus;
            cell.Bonus = tempBonus;

            speed = swapSpeed;
            cell.speed = swapSpeed;
            opacity = 0.5f;
            cell.opacity = 0.5f;
            Animation = Animation.Swap;
            cell.Animation = Animation.Swap;
        }
Exemple #9
0
        internal void FallInto(Cell cell)
        {
            cell.State = GuiElementState.Normal;
            cell.Shape = Shape;
            Shape = Shape.Empty;
            cell.Bonus = Bonus;
            Bonus = Bonus.None;

            cell.moveDestination = cell.location;
            cell.location = location;
            cell.speed = (cell.Row * SPEED_FALL_BASE) + SPEED_FALL_MOD;

            cell.Animation = Animation.Fall;
        }