Exemple #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();
    }
Exemple #2
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;
            }

            Brick             brick = field[coords.x, coords.y];
            List <Vector2Int> area  = WaveAlgorithm.GetArea(
                field,
                coords,
                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,
                        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.DoMergingAnimation(
                    () =>
                {
                    if (newCoords.Count > 0)
                    {
                        Normalize(
                            normalized =>
                        {
                            newCoords.AddRange(normalized);
                            Merge(newCoords, onComplete);
                        }
                            );
                    }
                }
                    );

                UserProgress.Current.Score += brick.Number;
            }
                );
        }

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

        isAnimating = false;
        onComplete.Invoke();
    }