Esempio n. 1
0
    void Normalize(Action <List <Vector2Int> > onComplete)
    {
        List <Vector2Int> normalized = new List <Vector2Int> ();

        for (int x = 0; x < field.GetLength(0); x++)
        {
            for (int y = 0; y < field.GetLength(1); y++)
            {
                NumberedBrick brick = field[x, y];

                if (brick == null)
                {
                    continue;
                }

                int yEmpty = y;
                while (yEmpty > 0 && field[x, yEmpty - 1] == null)
                {
                    yEmpty--;
                }

                if (yEmpty == y)
                {
                    continue;
                }

                field[x, y]      = null;
                field[x, yEmpty] = brick;
                Vector2Int brickCoords = new Vector2Int(x, yEmpty);

                normalized.Add(brickCoords);

                bool isFirst = normalized.Count == 1;
                brick.DoLocalMove(
                    GetBrickPosition(brickCoords),
                    () => {
                    if (isFirst)
                    {
                        brick.DoLandingAnimation(() => onComplete.Invoke(normalized));
                        landingSfx.Play();
                    }
                    else
                    {
                        brick.DoLandingAnimation(null);
                    }
                }
                    );
            }
        }

        if (normalized.Count == 0)
        {
            onComplete.Invoke(normalized);
        }
    }
    void SpawnNewBricks(Action onComplete)
    {
        isAnimating = true;

        bool spawned = false;
        int  yMin    = int.MaxValue;

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

                yMin = Mathf.Min(yMin, y);

                Vector2Int    coords = new Vector2Int(x, y);
                NumberedBrick brick  = Spawn(coords, GetRandomNumber());
                brick.GetComponent <RectTransform>().anchoredPosition =
                    GetBrickPosition(new Vector2Int(x, bricksCount.y + y - yMin));
                brick.DoLocalMove(
                    GetBrickPosition(coords),
                    () => brick.DoLandingAnimation(
                        () =>
                {
                    isAnimating = false;

                    if (onComplete != null)
                    {
                        onComplete.Invoke();
                    }
                }
                        )
                    );

                spawned = true;
            }
        }

        if (!spawned)
        {
            isAnimating = false;

            if (onComplete != null)
            {
                onComplete.Invoke();
            }
        }

        SaveGame();
    }