Exemplo n.º 1
0
    bool CheckCanRotate()
    {
        bool    canRotate    = true;
        int     nextRotation = lastPieceData.rotationVariation;
        Vector2 pos          = GlobalPos2MathModelPos(GetPieceTopLeft(lastPiece));

        nextRotation += 1;
        if (nextRotation % lastPieceData.type.rotations.Length == 0)
        {
            lastPieceData.rotationVariation = 0;
        }
        PieceRotation pNextR = lastPieceData.type.pieceRotations[nextRotation];

        for (int i = 0; i < pNextR.cells.GetLength(0); i++)
        {
            for (int j = 0; j < pNextR.cells.GetLength(1); j++)
            {
                if (pNextR.cells[i, j] != 0)
                {
                    float y = pos.y + (int)(pNextR.topLeftOffset.y / cellSize) + i;
                    int   x = (int)pos.x + (int)(pNextR.topLeftOffset.x / cellSize) + j;

                    if (mathModel[Mathf.CeilToInt(y)][x] != null || mathModel[Mathf.FloorToInt(y)][x] != null)
                    {
                        return(false);
                    }
                }
            }
        }

        return(canRotate);
    }
Exemplo n.º 2
0
    void MovePieceHorizontally(float horizontalInput)
    {
        Vector2       mathModelPos = GlobalPos2MathModelPos(GetPieceTopLeft(lastPiece));
        PieceType     pt           = lastPieceData.type;
        PieceRotation pr           = lastPieceData.type.pieceRotations[lastPieceData.rotationVariation];
        bool          canMove      = true;

        // left
        if (Mathf.Sign(horizontalInput) < 0)
        {
            canMove = mathModelPos.x > 0;

            for (int i = 0; i < pr.leftCells.Length; i++)
            {
                canMove = canMove && mathModel[Mathf.FloorToInt(mathModelPos.y) + i][(int)mathModelPos.x - pr.leftCells[i].x] == null;
                canMove = canMove && mathModel[Mathf.CeilToInt(mathModelPos.y) + i][(int)mathModelPos.x - pr.leftCells[i].x] == null;
            }
        }
        // right
        else
        {
            canMove = mathModelPos.x < playAreaSize.x;

            for (int i = 0; i < pr.rightCells.Length; i++)
            {
                canMove = canMove && mathModel[Mathf.FloorToInt(mathModelPos.y) + i][(int)mathModelPos.x - pr.rightCells[i].x] == null;
                canMove = canMove && mathModel[Mathf.CeilToInt(mathModelPos.y) + i][(int)mathModelPos.x - pr.rightCells[i].x] == null;
            }
        }

        if (canMove)
        {
            lastPiece.transform.Translate(Mathf.Sign(horizontalInput) * horizontalSpeed, 0, 0, Space.World);
        }
    }
Exemplo n.º 3
0
    void FindPieceRotationRightCells(PieceRotation pr)
    {
        pr.rightCells = new Vector2Int[pr.cells.GetLength(0)];

        for (int i = 0; i < pr.cells.GetLength(0); i++)
        {
            for (int j = pr.cells.GetLength(1) - 1; j > -1; j--)
            {
                if (pr.cells[i, j] != 0)
                {
                    pr.rightCells[i] = new Vector2Int(j, pr.cells[i, j]);
                    break;
                }
            }
        }
    }
Exemplo n.º 4
0
    void FindPieceRotationLeftCells(PieceRotation pr)
    {
        pr.leftCells = new Vector2Int[pr.cells.GetLength(0)];

        for (int i = 0; i < pr.cells.GetLength(0); i++)
        {
            for (int j = 0; j < pr.cells.GetLength(1); j++)
            {
                if (pr.cells[i, j] != 0)
                {
                    pr.leftCells[i] = new Vector2Int(j, pr.cells[i, j]);
                    break;
                }
            }
        }
    }
Exemplo n.º 5
0
    void FindPieceRotationBottomCells(PieceRotation pr)
    {
        pr.bottomCells = new Vector2Int[pr.cells.GetLength(1)];

        for (int i = 0; i < pr.cells.GetLength(1); i++)
        {
            for (int j = pr.cells.GetLength(0) - 1; j > -1; j--)
            {
                if (pr.cells[j, i] != 0)
                {
                    pr.bottomCells[i] = new Vector2Int(j, pr.cells[j, i]);
                    break;
                }
            }
        }
    }
Exemplo n.º 6
0
    void MovePieceToFarthestFreePos(float speed)
    {
        int     obstacleY        = 10000;
        int     closestObstacleY = obstacleY;
        Vector2 pieceMathPos;
        int     bottomCellY = 0;

        pieceMathPos = GlobalPos2MathModelPos(GetPieceTopLeft(lastPiece));

        PieceRotation r = lastPieceData.type.pieceRotations[lastPieceData.rotationVariation];

        for (int i = 0; i < r.bottomCells.Length; i++)
        {
            obstacleY = FindFathestFreeCellY(pieceMathPos, r.bottomCells[i].x, i, speed);
            if (closestObstacleY > obstacleY)
            {
                closestObstacleY = obstacleY;
                bottomCellY      = r.bottomCells[i].x;
            }
        }

        if (closestObstacleY != 10000)
        {
            lastPiece.transform.position = GetPieceTopLeftToPosCorrection() + MathModelPos2GlobalPos((int)pieceMathPos.x, closestObstacleY - bottomCellY);

            for (int i = 0; i < r.cells.GetLength(0); i++)
            {
                for (int j = 0; j < r.cells.GetLength(1); j++)
                {
                    if (r.cells[i, j] != 0)
                    {
                        mathModel[closestObstacleY - bottomCellY + i][(int)pieceMathPos.x + j] = lastPiece.GetChild(r.cells[i, j] - 1);
                    }
                }
            }

            lastPiece     = null;
            lastPieceData = null;

            DestroyRows();
        }
        else
        {
            lastPiece.transform.Translate(0, -speed, 0, Space.World);
        }
    }
Exemplo n.º 7
0
        public static Piece GetPiece(PieceType type, PieceRotation rotation, bool isNextPiece = true)
        {
            Piece returnPiece = null;

            switch (type)
            {
            case PieceType.Indigo:
                returnPiece = new IndigoPiece();
                break;

            case PieceType.Oscar:
                returnPiece = new OscarPiece();
                break;

            case PieceType.Tango:
                returnPiece = new TangoPiece();
                break;

            case PieceType.Sierra:
                returnPiece = new SierraPiece();
                break;

            case PieceType.Zulu:
                returnPiece = new ZuluPiece();
                break;

            case PieceType.Juliet:
                returnPiece = new JulietPiece();
                break;

            case PieceType.Lima:
                returnPiece = new LimaPiece();
                break;
            }

            returnPiece.Rotation    = rotation;
            returnPiece.IsNextPiece = isNextPiece;

            return(returnPiece);
        }
Exemplo n.º 8
0
 public static bool[,] GetSprite(TetrisPieceKind kind, PieceRotation rotation) =>
 GetSprite(Lookup[kind][rotation]);