Exemple #1
0
 void SpawnNextBrick()
 {
     nextBrick = Instantiate(brickPrefab, nextBrickPoint);
     nextBrick.GetComponent <RectTransform> ().anchorMin        = new Vector2(0.5f, 0.5f);
     nextBrick.GetComponent <RectTransform> ().anchorMax        = new Vector2(0.5f, 0.5f);
     nextBrick.GetComponent <RectTransform> ().anchoredPosition = Vector2.zero;
 }
Exemple #2
0
    void Spawn(Vector2Int coords)
    {
        NumberedBrick brick = Instantiate(brickPrefab, fieldTransform);

        brick.transform.SetParent(fieldTransform, false);
        brick.GetComponent <RectTransform>().anchorMin        = Vector2.zero;
        brick.GetComponent <RectTransform>().anchorMax        = Vector2.zero;
        brick.GetComponent <RectTransform>().anchoredPosition = GetBrickPosition(coords);

        brick.ColorIndex = Random.Range(0, 6);

        field[coords.x, coords.y] = brick;
    }
Exemple #3
0
    void Spawn(Vector2Int coords, int number)
    {
        NumberedBrick brick = Instantiate(brickPrefab, fieldTransform);

        brick.transform.SetParent(fieldTransform, false);
        brick.GetComponent <RectTransform> ().anchorMin        = Vector2.zero;
        brick.GetComponent <RectTransform> ().anchorMax        = Vector2.zero;
        brick.GetComponent <RectTransform> ().anchoredPosition = GetBrickPosition(new Vector2(coords.x, coords.y));

        brick.Number     = number;
        brick.ColorIndex = GetColorIndex(number);

        field[coords.x, coords.y] = brick;
    }
Exemple #4
0
    void SpawnFigure(FigureController figureController, int figureIndex, float rotation)
    {
        figureController.transform.localRotation = Quaternion.identity;

        int colorIndex = Random.Range(0, 6);

        int[,] figure = Figures1010.Figures[figureIndex];
        for (int i = 0; i < figure.GetLength(0); i++)
        {
            for (int j = 0; j < figure.GetLength(1); j++)
            {
                if (figure[figure.GetLength(0) - i - 1, j] == 0)
                {
                    continue;
                }

                NumberedBrick brick = Instantiate(brickPrefab, figureController.transform);
                figureController.bricks.Add(brick);

                RectTransform brickRectTransform = brick.GetComponent <RectTransform>();

                brickRectTransform.anchorMin = new Vector2(0.5f, 0.5f);
                brickRectTransform.anchorMax = new Vector2(0.5f, 0.5f);

                Rect    rect      = figureController.GetComponent <RectTransform>().rect;
                Vector2 brickSize = new Vector2
                {
                    x = rect.width / 4,
                    y = rect.height / 4
                };

                Vector2 coords        = new Vector2(j - figure.GetLength(1) / 2f, i - figure.GetLength(0) / 2f);
                Vector2 brickPosition = Vector2.Scale(coords, brickSize);
                brickPosition += Vector2.Scale(brickSize, brickRectTransform.pivot);
                brick.GetComponent <RectTransform>().anchoredPosition = brickPosition;

                brick.ColorIndex = colorIndex;
            }
        }

        figureController.transform.localRotation = Quaternion.Euler(0f, 0f, rotation);
    }
Exemple #5
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();
    }
Exemple #7
0
    void Move(int value)
    {
        if (value < 0 || value >= field.GetLength(0) || field[value, currentBrick.y] != null)
        {
            return;
        }

        NumberedBrick brick = field[currentBrick.x, currentBrick.y];

        field[currentBrick.x, currentBrick.y] = null;
        currentBrick.x = value;
        field[currentBrick.x, currentBrick.y] = brick;

        brick.GetComponent <RectTransform> ().anchoredPosition =
            GetBrickPosition(new Vector2(currentBrick.x, currentBrick.y));
    }
Exemple #8
0
    protected virtual Vector2 GetBrickPosition(Vector2 coords)
    {
        Rect    rect      = fieldTransform.rect;
        Vector2 brickSize = new Vector2
        {
            x = rect.width / bricksCount.x,
            y = rect.height / bricksCount.y
        };

        RectTransform brickTransform = brickPrefab.GetComponent <RectTransform>();

        Vector2 brickPosition = Vector2.Scale(coords, brickSize);

        brickPosition += Vector2.Scale(brickSize, brickTransform.pivot);

        return(brickPosition);
    }