Exemple #1
0
    public void Move(UtilityTools.Directions dir)
    {
        //after checkups

        if (isRotating || isMoving)
        {
            print("already moving/rotating");
            return;
        }

        if (!canMove(dir))
        {
            return;
        }

        Tile[] destinations = { null, null, null };

        bool shouldEmptyPreviousTile = false;

        switch (dir)
        {
        case UtilityTools.Directions.up:
            if (isVertical)
            {
                destinations[0] = getDirectionMostBox(dir).getNeighbourTile(dir); // the uppermost is gonna go to his up neighbour
                destinations[1] = getDirectionMostBox(dir).GetTile();             // the center is gonna go to current uppermost
                destinations[2] = getCenterBox().GetTile();                       // lowest is going to go to center

                shouldEmptyPreviousTile = true;
            }
            else
            {
                for (int i = 0; i < destinations.Length && i < boxes.Length; i++)
                {
                    destinations[i] = boxes[i].getNeighbourTile(dir);
                }
            }

            break;

        case UtilityTools.Directions.right:
            if (!isVertical)
            {
                destinations[0]         = getCenterBox().GetTile();                       // leftmost is going to go to center
                destinations[1]         = getDirectionMostBox(dir).GetTile();             // the center is gonna go to current rightmost
                destinations[2]         = getDirectionMostBox(dir).getNeighbourTile(dir); // the rightmost is gonna go to his right neighbour
                shouldEmptyPreviousTile = true;
            }
            else
            {
                for (int i = 0; i < destinations.Length && i < boxes.Length; i++)
                {
                    destinations[i] = boxes[i].getNeighbourTile(dir);
                }
            }

            break;

        case UtilityTools.Directions.down:
            if (isVertical)
            {
                destinations[0]         = getCenterBox().GetTile();                       // highest is going to go to center
                destinations[1]         = getDirectionMostBox(dir).GetTile();             // the center is gonna go to current downmost
                destinations[2]         = getDirectionMostBox(dir).getNeighbourTile(dir); // the downmost is gonna go to his down neighbour
                shouldEmptyPreviousTile = true;
            }
            else
            {
                for (int i = 0; i < destinations.Length && i < boxes.Length; i++)
                {
                    destinations[i] = boxes[i].getNeighbourTile(dir);
                }
            }

            break;

        case UtilityTools.Directions.left:
            if (!isVertical)
            {
                destinations[0]         = getDirectionMostBox(dir).getNeighbourTile(dir); // the leftmost is gonna go to his left neighbour
                destinations[1]         = getDirectionMostBox(dir).GetTile();             // the center is gonna go to current leftmost
                destinations[2]         = getCenterBox().GetTile();                       // rightmost is going to go to center
                shouldEmptyPreviousTile = true;
            }
            else
            {
                for (int i = 0; i < destinations.Length && i < boxes.Length; i++)
                {
                    destinations[i] = boxes[i].getNeighbourTile(dir);
                }
            }

            break;

        default:
            return;

            break;
        }

        // all destinations are ok
        if (!System.Array.FindAll(destinations, x => x == null).Any())
        {
            isMoving = true;

            transform.DOMove(transform.position + UtilityTools.getDirectionVector(dir) * 1, TheGrid.moveTime).OnComplete(() =>
            {
                isMoving = false;
                grid_ref.spawnTriBox();
                grid_ref.Match(getBoxMatches());
            });

            for (int i = 0; i < destinations.Length && i < boxes.Length; i++)
            {
                //move sprites

                //update tiles  isVertical && dir == UtilityTools.Directions.down && i > 0
                //fix when moving down it should not set previous tiles as empties
                if (shouldEmptyPreviousTile)
                {
                    boxes[i].setTile(destinations[i], Tile.Status.box);
                }
                else
                {
                    boxes[i].setTile(destinations[i]);
                }
            }
        }
        else
        {
            return;
        }

        //print("first box: " + boxes.First() + boxes.First().GetPoint().print() + " is moving to position" + destination1.GetPoint().Clone().print());
        rearrangeBoxesArray();
    }