public PuzzleCell CheckLeftAmmount(int times)
 {
     if (times == 0)
     {
         return(this);
     }
     else
     {
         return(cellLeft.CheckLeftAmmount(times - 1));
     }
 }
Esempio n. 2
0
    public void MoveHorizontal(float curPos, float prevPos)
    {
        if (lastCell.CheckRight().goalCell)
        {
            maxX = iniMaxX + cellDistance;
        }
        else
        {
            maxX = iniMaxX;
        }
        if (firstCell.CheckLeft().goalCell)
        {
            minX = iniMinX - cellDistance;
        }
        else
        {
            minX = iniMinX;
        }
        if (!selected)
        {
            iniPos   = this.gameObject.transform.position;
            selected = true;
            nextPos  = iniPos;
        }
        float Diff = Mathf.Abs(curPos - prevPos);

        if (Diff > maxDiff)
        {
            Diff = maxDiff;
        }
        if (curPos > prevPos)
        {
            //moving right
            nextPos.x    += Diff;
            pushingPos.x += Diff;
            if (nextPos.x > iniPos.x)
            {
                //Is moving right from the starting point
                if (!directionController)
                {
                    directionController = true;
                    SetPushedBaguette();
                    pushing = false;
                    if (movingToGoal)
                    {
                        movingToGoal = false;
                    }
                }
                if (!lastCell.edgeRight)
                {
                    //no board edge right
                    if (!lastCell.cellRight.occupied)
                    {
                        //no baguette in the next right cell
                        canMove = true;
                        if (lastCell.cellRight.goalCell)
                        {
                            //code for checking goal
                            if (lastCell.cellRight.gameObject.GetComponent <BakeryGoalCell>().myColor.ToString() == myColor.ToString())
                            {
                                canMove      = true;
                                movingToGoal = true;
                            }
                            else
                            {
                                canMove = false;
                            }
                        }
                    }
                    else
                    {
                        if (!baguetteToPush)
                        {
                            baguetteToPush = lastCell.cellRight.gameObject.GetComponent <BakeryCellConn>().mybaguette;
                            BTPcurrentPos  = baguetteToPush.transform.position;
                            float adjustedX = nextPos.x + ((myCells.Length * 0.5f) + 0.5f);
                            baguetteToPush.transform.position = new Vector3(adjustedX, baguetteToPush.transform.position.y, baguetteToPush.transform.position.z);
                            pushingPos = baguetteToPush.transform.position;
                        }
                        if (baguetteToPush.CheckAllRight())
                        {
                            //can push right
                            pushing = true;
                            canMove = true;
                        }
                        else
                        {
                            // cant push right
                            SetPushedBaguette();
                            baguetteToPush = null;
                            maxX           = iniPos.x;
                            canMove        = false;
                            this.gameObject.transform.position = iniPos;
                        }
                    }
                }
                else
                {
                    maxX    = iniPos.x;
                    canMove = false;
                    this.gameObject.transform.position = iniPos;
                }
            }
            else if (nextPos.x > this.gameObject.transform.position.x)
            {
                //Is moving right towards the starting point
                canMove = true;
            }
            if (Mathf.Abs(nextPos.x - iniPos.x) >= minDistance && canMove)
            {
                List <PuzzleCell> tempCells = new List <PuzzleCell>();
                if (baguetteToPush)
                {
                    foreach (PuzzleCell cell in baguetteToPush.myCells)
                    {
                        tempCells.Add(cell.CheckRightAmmount(1));
                        cell.occupied = false;
                        cell.gameObject.GetComponent <BakeryCellConn>().mybaguette = null;
                    }
                    for (int i = 0; i < tempCells.Count; i++)
                    {
                        //occupie (hmmm pie I want pie, give me pie X if you are reading this, I know is written occupy but whatever) new baguette cells and assign baguette
                        baguetteToPush.myCells[i]          = tempCells[i];
                        baguetteToPush.myCells[i].occupied = true;
                        baguetteToPush.myCells[i].gameObject.GetComponent <BakeryCellConn>().mybaguette = baguetteToPush;
                    }
                    baguetteToPush.firstCell = baguetteToPush.firstCell.CheckRightAmmount(1);
                    baguetteToPush.lastCell  = baguetteToPush.lastCell.CheckRightAmmount(1);
                    tempCells.Clear();
                    BTPcurrentPos.x          += cellDistance;
                    baguetteToPush.iniPos     = BTPcurrentPos;
                    baguetteToPush.currentPos = BTPcurrentPos;
                }

                foreach (PuzzleCell cell in myCells)
                {
                    //free current baguette cells and get new cells
                    tempCells.Add(cell.CheckRightAmmount(1));
                    cell.occupied = false;
                    cell.gameObject.GetComponent <BakeryCellConn>().mybaguette = null;
                }
                for (int i = 0; i < tempCells.Count; i++)
                {
                    //occupie (hmmm pie I want pie, give me pie X if you are reading this, I know is written occupy but whatever) new baguette cells and assign baguette
                    myCells[i]          = tempCells[i];
                    myCells[i].occupied = true;
                    myCells[i].gameObject.GetComponent <BakeryCellConn>().mybaguette = this;
                }
                //update edge cells
                firstCell = firstCell.CheckRightAmmount(1);
                lastCell  = lastCell.CheckRightAmmount(1);
                tempCells.Clear();
                currentPos.x += cellDistance;
                iniPos        = currentPos;
                if (movingToGoal)
                {
                    foreach (PuzzleCell cell in myCells)
                    {
                        //free current baguette cells and get new cells
                        cell.occupied = false;
                        cell.gameObject.GetComponent <BakeryCellConn>().mybaguette = null;
                        onGoal = true;
                        this.gameObject.SetActive(false);
                        canMove = false;
                    }
                }
            }
        }
        else
        {
            //moving left
            nextPos.x    -= Diff;
            pushingPos.x -= Diff;
            if (nextPos.x < iniPos.x)
            {
                //Is moving left from the starting point
                if (directionController)
                {
                    directionController = false;
                    SetPushedBaguette();
                    pushing = false;
                    if (movingToGoal)
                    {
                        movingToGoal = false;
                    }
                }
                if (!firstCell.edgeLeft)
                {
                    //no board edge up
                    if (!firstCell.cellLeft.occupied)
                    {
                        //no baguette in the next up cell
                        canMove = true;
                        if (firstCell.cellLeft.goalCell)
                        {
                            //code for checking goal
                            if (firstCell.cellLeft.gameObject.GetComponent <BakeryGoalCell>().myColor.ToString() == myColor.ToString())
                            {
                                canMove      = true;
                                movingToGoal = true;
                            }
                            else
                            {
                                canMove = false;
                            }
                        }
                    }
                    else
                    {
                        // next left cell is occupied
                        if (!baguetteToPush)
                        {
                            baguetteToPush = firstCell.cellLeft.gameObject.GetComponent <BakeryCellConn>().mybaguette;
                            BTPcurrentPos  = baguetteToPush.transform.position;
                            float adjustedX = nextPos.x - ((myCells.Length * 0.5f) + 0.5f);
                            baguetteToPush.transform.position = new Vector3(adjustedX, baguetteToPush.transform.position.y, baguetteToPush.transform.position.z);
                            pushingPos = baguetteToPush.transform.position;
                        }
                        if (baguetteToPush.CheckAllLeft())
                        {
                            //can push left
                            pushing = true;
                            canMove = true;
                        }
                        else
                        {
                            // cant push left
                            SetPushedBaguette();
                            baguetteToPush = null;
                            minX           = iniPos.x;
                            canMove        = false;
                            this.gameObject.transform.position = iniPos;
                        }
                    }
                }
                else
                {
                    minX    = iniPos.x;
                    canMove = false;
                    this.gameObject.transform.position = iniPos;
                }
            }
            else if (nextPos.x < this.gameObject.transform.position.x)
            {
                //Is moving left towards the starting point
                canMove = true;
            }

            if (Mathf.Abs(nextPos.x - iniPos.x) >= minDistance && canMove)
            {
                List <PuzzleCell> tempCells = new List <PuzzleCell>();
                if (baguetteToPush)
                {
                    foreach (PuzzleCell cell in baguetteToPush.myCells)
                    {
                        tempCells.Add(cell.CheckLeftAmmount(1));
                        cell.occupied = false;
                        cell.gameObject.GetComponent <BakeryCellConn>().mybaguette = null;
                    }
                    for (int i = 0; i < tempCells.Count; i++)
                    {
                        //occupie (hmmm pie I want pie, give me pie X if you are reading this, I know is written occupy but whatever) new baguette cells and assign baguette
                        baguetteToPush.myCells[i]          = tempCells[i];
                        baguetteToPush.myCells[i].occupied = true;
                        baguetteToPush.myCells[i].gameObject.GetComponent <BakeryCellConn>().mybaguette = baguetteToPush;
                    }
                    baguetteToPush.firstCell = baguetteToPush.firstCell.CheckLeftAmmount(1);
                    baguetteToPush.lastCell  = baguetteToPush.lastCell.CheckLeftAmmount(1);
                    tempCells.Clear();
                    BTPcurrentPos.x          -= cellDistance;
                    baguetteToPush.iniPos     = BTPcurrentPos;
                    baguetteToPush.currentPos = BTPcurrentPos;
                }

                foreach (PuzzleCell cell in myCells)
                {
                    //free current baguette cells and get new cells
                    tempCells.Add(cell.CheckLeftAmmount(1));
                    cell.occupied = false;
                    cell.gameObject.GetComponent <BakeryCellConn>().mybaguette = null;
                }
                for (int i = 0; i < tempCells.Count; i++)
                {
                    //occupie (hmmm pie I want pie, give me pie X if you are reading this, I know is written occupy but whatever) new baguette cells and assign baguette
                    myCells[i]          = tempCells[i];
                    myCells[i].occupied = true;
                    myCells[i].gameObject.GetComponent <BakeryCellConn>().mybaguette = this;
                }
                //update edge cells
                firstCell = firstCell.CheckLeftAmmount(1);
                lastCell  = lastCell.CheckLeftAmmount(1);
                tempCells.Clear();
                currentPos.x -= cellDistance;
                iniPos        = currentPos;
                if (movingToGoal)
                {
                    foreach (PuzzleCell cell in myCells)
                    {
                        //free current baguette cells and get new cells
                        cell.occupied = false;
                        cell.gameObject.GetComponent <BakeryCellConn>().mybaguette = null;
                        onGoal = true;
                        this.gameObject.SetActive(false);
                        canMove = false;
                    }
                }
            }
        }

        if (canMove)
        {
            if (nextPos.x > maxX)
            {
                nextPos.x = maxX;
            }
            if (nextPos.x < minX)
            {
                nextPos.x = minX;
            }
            this.gameObject.transform.position = nextPos;
            if (baguetteToPush)
            {
                baguetteToPush.gameObject.transform.position = pushingPos;
            }
        }
    }
 public void MoveItemLeft()
 {
     myItem.nextCell = myCell.CheckLeftAmmount(1);
     myItem.moving   = true;
 }