Beispiel #1
0
    void Merge(List <Vector2Int> toMerge, Action onComplete)
    {
        isAnimating = true;

        List <Vector2Int> newCoords = new List <Vector2Int> ();

        int animationsLeft = 0;

        foreach (Vector2Int coords in toMerge)
        {
            if (field[coords.x, coords.y] == null)
            {
                continue;
            }

            NumberedBrick     brick = field[coords.x, coords.y];
            List <Vector2Int> area  = WaveAlgorithm.GetArea(
                field,
                coords,
                GetAdjacentCoords,
                b => b != null && b.Number == brick.Number
                );

            if (area.Count < 2)
            {
                continue;
            }

            newCoords.AddRange(area);

            List <BrickPath> paths = new List <BrickPath> ();
            foreach (Vector2Int toMove in area)
            {
                if (toMove == coords)
                {
                    continue;
                }

                BrickPath brickPath = new BrickPath {
                    brick = field[toMove.x, toMove.y],
                    path  = WaveAlgorithm.GetPath(
                        field,
                        toMove,
                        coords,
                        GetAdjacentCoords,
                        b => b != null && b.Number == brick.Number
                        )
                };
                brickPath.path.RemoveAt(0);
                paths.Add(brickPath);
            }

            foreach (Vector2Int toMove in area)
            {
                if (toMove != coords)
                {
                    field[toMove.x, toMove.y] = null;
                }
            }

            animationsLeft++;

            int areaSize = area.Count;
            AnimateMerge(
                paths,
                () => {
                animationsLeft--;

                if (animationsLeft > 0)
                {
                    return;
                }

                mergingSfx.Play();

                brick.Number    *= Mathf.ClosestPowerOfTwo(areaSize);
                brick.ColorIndex = GetColorIndex(brick.Number);
                brick.DoMergingAnimation(
                    () => {
                    if (Random.Range(0f, 1f) < coinProbability)
                    {
                        UserProgress.Current.Coins++;

                        GameObject vfx = Resources.Load <GameObject> ("CoinVFX");
                        vfx            = Instantiate(vfx, fieldTransform.parent);

                        vfx.transform.position = brick.transform.position;

                        Destroy(vfx, 1.5f);
                    }

                    if (newCoords.Count > 0)
                    {
                        Normalize(
                            normalized => {
                            newCoords.AddRange(normalized);
                            Merge(newCoords, onComplete);
                        }
                            );
                    }
                }
                    );

                gameState.Score += brick.Number;
                speed            = GetSpeed();
                // Debug.Log(speed);
            }
                );
        }

        if (newCoords.Count > 0)
        {
            return;
        }

        isAnimating = false;
        onComplete.Invoke();
    }
Beispiel #2
0
    void CheckLines()
    {
        List <Vector2Int> bricksToDestroy = new List <Vector2Int>();

        for (int x = 0; x < bricksCount.x; x++)
        {
            bool line = true;

            for (int y = 0; y < bricksCount.y; y++)
            {
                if (field[x, y] != null)
                {
                    continue;
                }

                line = false;
                break;
            }

            if (!line)
            {
                continue;
            }

            for (int y = 0; y < bricksCount.y; y++)
            {
                Vector2Int coords = new Vector2Int(x, y);
                if (bricksToDestroy.Contains(coords))
                {
                    continue;
                }

                bricksToDestroy.Add(coords);
            }
        }

        for (int y = 0; y < bricksCount.y; y++)
        {
            bool line = true;

            for (int x = 0; x < bricksCount.x; x++)
            {
                if (field[x, y] != null)
                {
                    continue;
                }

                line = false;
                break;
            }

            if (!line)
            {
                continue;
            }

            for (int x = 0; x < bricksCount.x; x++)
            {
                Vector2Int coords = new Vector2Int(x, y);
                if (bricksToDestroy.Contains(coords))
                {
                    continue;
                }

                bricksToDestroy.Add(coords);
            }
        }

        if (bricksToDestroy.Count > 0)
        {
            mergingSfx.Play();
        }

        foreach (Vector2Int c in bricksToDestroy)
        {
            NumberedBrick brick = field[c.x, c.y];
            brick.DoMergingAnimation(() => Destroy(brick.gameObject));

            field[c.x, c.y] = null;

            gameState.Score++;
        }
    }