Exemplo n.º 1
0
    public LevelSolver Move(InputHandler.MoveDirection move)
    {
        switch (move)
        {
        case InputHandler.MoveDirection.Up: return(Move(-1, 0));

        case InputHandler.MoveDirection.down: return(Move(1, 0));

        case InputHandler.MoveDirection.left: return(Move(0, -1));

        case InputHandler.MoveDirection.right: return(Move(0, 1));

        default: return(Move(0, 0));    // never happens
        }
    }
Exemplo n.º 2
0
    public IEnumerator MovePieces(InputHandler.MoveDirection md)
    {
        List <MovablePiece> piecesToMove = new List <MovablePiece>();

        if (piecesAreMoving)
        {
            yield break;
        }

        int size = pieceBoard.GetLength(0);

        switch (md)
        {
        // starts ignoring the first line
        case InputHandler.MoveDirection.down:
            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    if (pieceBoard[i, j] != null)
                    {
                        for (int k = j - 1; k >= 0; k--)
                        {
                            if (pieceBoard[i, k] == null || pieceBoard[i, k].GetComponent <ObjectivePiece>())
                            {
                                if (pieceBoard[i, j].GetComponent <MovablePiece>())
                                {
                                    pieceBoard[i, k] = pieceBoard[i, j];
                                    pieceBoard[i, j] = null;
                                    pieceBoard[i, k].GetComponent <MovablePiece>().SetDesiredPosition(new Vector2(i, k));
                                    if (!piecesToMove.Contains(pieceBoard[i, k] as MovablePiece))
                                    {
                                        piecesToMove.Add(pieceBoard[i, k] as MovablePiece);
                                    }
                                    j--;
                                }
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }
            }
            break;

        case InputHandler.MoveDirection.Up:
            for (int i = 0; i < size; i++)
            {
                for (int j = size - 2; j >= 0; j--)
                {
                    if (pieceBoard[i, j] != null)
                    {
                        for (int k = j + 1; k <= size - 1; k++)
                        {
                            if (pieceBoard[i, k] == null || pieceBoard[i, k].GetComponent <ObjectivePiece>())
                            {
                                if (pieceBoard[i, j].GetComponent <MovablePiece>())
                                {
                                    pieceBoard[i, k] = pieceBoard[i, j];
                                    pieceBoard[i, j] = null;
                                    pieceBoard[i, k].GetComponent <MovablePiece>().SetDesiredPosition(new Vector2(i, k));
                                    if (!piecesToMove.Contains(pieceBoard[i, k] as MovablePiece))
                                    {
                                        piecesToMove.Add(pieceBoard[i, k] as MovablePiece);
                                    }
                                    j++;
                                }
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }
            }
            break;

        case InputHandler.MoveDirection.left:
            for (int i = 1; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    if (pieceBoard[i, j] != null)
                    {
                        for (int k = i - 1; k >= 0; k--)
                        {
                            if (pieceBoard[k, j] == null || pieceBoard[k, j].GetComponent <ObjectivePiece>())
                            {
                                if (pieceBoard[i, j].GetComponent <MovablePiece>())
                                {
                                    pieceBoard[k, j] = pieceBoard[i, j];
                                    pieceBoard[i, j] = null;
                                    pieceBoard[k, j].GetComponent <MovablePiece>().SetDesiredPosition(new Vector2(k, j));
                                    if (!piecesToMove.Contains(pieceBoard[k, j] as MovablePiece))
                                    {
                                        piecesToMove.Add(pieceBoard[k, j] as MovablePiece);
                                    }
                                    i--;
                                }
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }
            }
            break;

        case InputHandler.MoveDirection.right:
            for (int i = size - 2; i >= 0; i--)
            {
                for (int j = 0; j < size; j++)
                {
                    if (pieceBoard[i, j] != null)
                    {
                        for (int k = i + 1; k <= pieceBoard.GetLength(0) - 1; k++)
                        {
                            if (pieceBoard[k, j] == null || pieceBoard[k, j].GetComponent <ObjectivePiece>())
                            {
                                if (pieceBoard[i, j].GetComponent <MovablePiece>())
                                {
                                    pieceBoard[k, j] = pieceBoard[i, j];
                                    pieceBoard[i, j] = null;
                                    pieceBoard[k, j].GetComponent <MovablePiece>().SetDesiredPosition(new Vector2(k, j));
                                    if (!piecesToMove.Contains(pieceBoard[k, j] as MovablePiece))
                                    {
                                        piecesToMove.Add(pieceBoard[k, j] as MovablePiece);
                                    }
                                    i++;
                                }
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }
            }
            break;
        }

        piecesAreMoving = true;

        piecesToMoveCount = piecesToMove.Count;

        if (piecesToMoveCount == 0)
        {
            EndedPiecesMovement();
            yield break;
        }
        EditorManager.Instance.IncreasePlay();

        for (int i = 0; i < piecesToMoveCount; i++)
        {
            piecesToMove[i].StartCoroutine("Move");
        }
    }