Beispiel #1
0
        void Start()
        {
            //Prepare two dimentional GridPoint Array for easy access
            GridPoint.All = new GridPoint[Size.x, Size.y];
            for (int i = 0; i < GridPoints.Length; i++)
            {
                GridPoint.All[GridPoints[i].X, GridPoints[i].Y] = GridPoints[i];
            }

            //Initialize GridJunctions
            GridJunctions = new GridJunction[(Size.x - 1) * 2, (Size.y - 1)];
            for (int x = 0; x < GridJunctions.GetLength(0); x++)
            {
                for (int y = 0; y < GridJunctions.GetLength(1); y++)
                {
                    GridJunctions[x, y] = new GridJunction(this, x, y);
                }
            }

            //Deactivate All Pieces but preserve their GridPoints and invoke activation.
            for (int i = 0; i < Pieces.Length; i++)
            {
                Pieces[i].Deactivate(true);
                Pieces[i].ActivateInSeconds(PieceActivationInterval * (i + 1));
            }

            if (StartByCheckingExplosions)
            {
                ExplosionOccurred = true;
            }
        }
Beispiel #2
0
        public bool CheckForExplosion(GridJunction gridJunction = null)
        {
            //Assign values for default loop
            int xStart  = 0;
            int yStart  = 0;
            int xLength = GridJunctions.GetLength(0);
            int yLength = GridJunctions.GetLength(1);

            //This was disabled because it created a bug where sometimes explosions wasn't detected correctly

            /*
             * //Modify loop values to only check for the given junction and its neighbors to avoid unnecessary calculations
             * if (gridJunction != null)
             * {
             *  xStart = gridJunction.X > 1 ? gridJunction.X - 2 : xStart;
             *  yStart = gridJunction.Y > 1 ? gridJunction.Y - 2 : yStart;
             *  xLength = gridJunction.X < xLength - 2 ? gridJunction.X + 3 : xLength;
             *  yLength = gridJunction.Y < yLength - 2 ? gridJunction.Y + 3 : yLength;
             * }
             */

            for (int y = yStart; y < yLength; y++)
            {
                for (int x = xStart; x < xLength; x++)
                {
                    if (GridJunctions[x, y].GridPoints[0].Piece.ColorIndex == GridJunctions[x, y].GridPoints[1].Piece.ColorIndex &&
                        GridJunctions[x, y].GridPoints[0].Piece.ColorIndex == GridJunctions[x, y].GridPoints[2].Piece.ColorIndex)
                    {
                        GameReady = false;
                        int colorIndex = GridJunctions[x, y].GridPoints[0].Piece.ColorIndex;

                        #region DEBUG
#if DEBUG
                        Debug.Log("Explosion found at " + x + ":" + y);
#endif
                        #endregion
                        int[] numRemoved  = new int[GridPoint.All.GetLength(0)];
                        int[] lastRemoved = new int[GridPoint.All.GetLength(0)];
                        for (int i = 0; i < GridJunctions[x, y].GridPoints.Length; i++)
                        {
                            numRemoved[GridJunctions[x, y].GridPoints[i].X]++;
                            lastRemoved[GridJunctions[x, y].GridPoints[i].X] = GridJunctions[x, y].GridPoints[i].Y;
                            GridJunctions[x, y].GridPoints[i].Piece.Deactivate();
                        }

                        //Also need to check surrounding pieces to detect more than 3
                        GridPoint neighbor;
                        GridPoint gridPointA = GridJunctions[x, y].GridPoints[0];
                        GridPoint gridPointB = GridJunctions[x, y].GridPoints[1];
                        GridPoint gridPointC = GridJunctions[x, y].GridPoints[2];

                        if (GridPoint.GetCommonNeighbor(gridPointA, gridPointB, gridPointC, out neighbor))
                        {
                            if (neighbor != null && neighbor.Piece != null && neighbor.Piece.Activated && neighbor.Piece.ColorIndex == colorIndex)
                            {
                                numRemoved[neighbor.X]++;
                                lastRemoved[neighbor.X] = neighbor.Y > lastRemoved[neighbor.X] ? neighbor.Y : lastRemoved[neighbor.X];
                                neighbor.Piece.Deactivate();
                            }
                        }

                        gridPointA = GridJunctions[x, y].GridPoints[1];
                        gridPointB = GridJunctions[x, y].GridPoints[2];
                        gridPointC = GridJunctions[x, y].GridPoints[0];

                        if (GridPoint.GetCommonNeighbor(gridPointA, gridPointB, gridPointC, out neighbor))
                        {
                            if (neighbor != null && neighbor.Piece != null && neighbor.Piece.Activated && neighbor.Piece.ColorIndex == colorIndex)
                            {
                                numRemoved[neighbor.X]++;
                                lastRemoved[neighbor.X] = neighbor.Y > lastRemoved[neighbor.X] ? neighbor.Y : lastRemoved[neighbor.X];
                                neighbor.Piece.Deactivate();
                            }
                        }

                        gridPointA = GridJunctions[x, y].GridPoints[2];
                        gridPointB = GridJunctions[x, y].GridPoints[0];
                        gridPointC = GridJunctions[x, y].GridPoints[1];

                        if (GridPoint.GetCommonNeighbor(gridPointA, gridPointB, gridPointC, out neighbor))
                        {
                            if (neighbor != null && neighbor.Piece != null && neighbor.Piece.Activated && neighbor.Piece.ColorIndex == colorIndex)
                            {
                                numRemoved[neighbor.X]++;
                                lastRemoved[neighbor.X] = neighbor.Y > lastRemoved[neighbor.X] ? neighbor.Y : lastRemoved[neighbor.X];
                                neighbor.Piece.Deactivate();
                            }
                        }

                        ShiftGridPoints(ref numRemoved, ref lastRemoved);

                        //Add the score
                        int totalRemoved = 0;
                        for (int i = 0; i < numRemoved.Length; i++)
                        {
                            totalRemoved += numRemoved[i];
                        }
                        Menu.Instance.Score += totalRemoved * 5;

                        //Play Audio
                        AudioSource.PlayOneShot(AC_PieceExplosion);

                        return(true);
                    }
                }
            }
            return(false);
        }