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);
        }
    }
Esempio n. 2
0
    void MoveDown()
    {
        NumberedBrick brick = field[currentBrick.x, currentBrick.y];

        if (currentBrick.y > 0 && field[currentBrick.x, currentBrick.y - 1] == null)
        {
            field[currentBrick.x, currentBrick.y] = null;
            currentBrick.y--;
            field[currentBrick.x, currentBrick.y] = brick;

            brick.GetComponent <RectTransform> ().anchoredPosition =
                GetBrickPosition(new Vector2(currentBrick.x, currentBrick.y));

            SaveGame();
        }
        else
        {
            isAnimating = true;
            landingSfx.Play();
            brick.DoLandingAnimation(
                () => {
                isAnimating = false;
                Merge(
                    new List <Vector2Int> {
                    currentBrick
                },
                    () => {
                    isFalling = false;

                    currentBrick = new Vector2Int(bricksCount.x / 2, bricksCount.y - 1);

                    if (field[currentBrick.x, currentBrick.y] != null)
                    {
                        isAnimating = true;

                        gameState.SetField(new int[0]);
                        UserProgress.Current.SaveGameState(name);

                        OnGameOver();
                        return;
                    }

                    Spawn(currentBrick, nextBrick.Number);
                    nextBrick.Number     = GetRandomNumber();
                    nextBrick.ColorIndex = GetColorIndex(nextBrick.Number);

                    SaveGame();
                }
                    );
            }
                );
        }
    }
    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();
    }