예제 #1
0
 public PuzzleCell CheckDown(int myNum = 0)
 {
     CheckTimes = myNum;
     if (edgeDown || goalCell)
     {
         return(this);
     }
     else if (cellDown.occupied)
     {
         return(this);
     }
     else
     {
         myNum++;
         return(cellDown.CheckDown(myNum));
     }
 }
예제 #2
0
    public void MoveVertical(float curPos, float prevPos)
    {
        if (lastCell.CheckUp().goalCell)
        {
            maxY = iniMaxY + cellDistance;
        }
        else
        {
            maxY = iniMaxY;
        }
        if (firstCell.CheckDown().goalCell)
        {
            minY = iniMinY - cellDistance;
        }
        else
        {
            minY = iniMinY;
        }

        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 UP
            nextPos.y    += Diff;
            pushingPos.y += Diff;
            if (nextPos.y > iniPos.y)
            {
                //Is moving UP from the starting point
                if (!directionController)
                {
                    directionController = true;
                    SetPushedBaguette();
                    pushing = false;
                    if (movingToGoal)
                    {
                        movingToGoal = false;
                    }
                }
                if (!lastCell.edgeUp)
                {
                    //no board edge up
                    if (!lastCell.cellUp.occupied)
                    {
                        //no baguette in the next up cell
                        canMove = true;
                        if (lastCell.cellUp.goalCell)
                        {
                            //code for checking goal
                            if (lastCell.cellUp.gameObject.GetComponent <BakeryGoalCell>().myColor.ToString() == myColor.ToString())
                            {
                                canMove      = true;
                                movingToGoal = true;
                            }
                            else
                            {
                                canMove = false;
                            }
                        }
                    }
                    else
                    {
                        if (!baguetteToPush)
                        {
                            baguetteToPush = lastCell.cellUp.gameObject.GetComponent <BakeryCellConn>().mybaguette;
                            BTPcurrentPos  = baguetteToPush.transform.position;
                            float adjustedY = nextPos.y + ((myCells.Length * 0.5f) + 0.5f);
                            baguetteToPush.transform.position = new Vector3(baguetteToPush.transform.position.x, adjustedY, baguetteToPush.transform.position.z);
                            pushingPos = baguetteToPush.transform.position;
                        }
                        if (baguetteToPush.CheckAllUp())
                        {
                            //can push UP
                            pushing = true;
                            canMove = true;
                        }
                        else
                        {
                            // cant push UP
                            SetPushedBaguette();
                            baguetteToPush = null;
                            canMove        = false;
                            maxY           = iniPos.y;
                            this.gameObject.transform.position = iniPos;
                        }
                    }
                }
                else
                {
                    canMove = false;
                    maxY    = iniPos.y;
                    this.gameObject.transform.position = iniPos;
                }
            }
            else if (nextPos.y > this.gameObject.transform.position.y)
            {
                //Is moving UP towards the starting point
                canMove = true;
            }
            if (Mathf.Abs(nextPos.y - iniPos.y) >= minDistance && canMove)
            {
                List <PuzzleCell> tempCells = new List <PuzzleCell>();
                if (baguetteToPush)
                {
                    foreach (PuzzleCell cell in baguetteToPush.myCells)
                    {
                        tempCells.Add(cell.CheckUpAmmount(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.CheckUpAmmount(1);
                    baguetteToPush.lastCell  = baguetteToPush.lastCell.CheckUpAmmount(1);
                    tempCells.Clear();
                    BTPcurrentPos.y          += cellDistance;
                    baguetteToPush.iniPos     = BTPcurrentPos;
                    baguetteToPush.currentPos = BTPcurrentPos;
                }

                foreach (PuzzleCell cell in myCells)
                {
                    //free current baguette cells and get new cells
                    tempCells.Add(cell.CheckUpAmmount(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.CheckUpAmmount(1);
                lastCell  = lastCell.CheckUpAmmount(1);
                tempCells.Clear();
                currentPos.y += 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 DOWN
            nextPos.y    -= Diff;
            pushingPos.y -= Diff;
            if (nextPos.y < iniPos.y)
            {
                //Is moving DOWN from the starting point
                if (directionController)
                {
                    directionController = false;
                    SetPushedBaguette();
                    pushing = false;
                    if (movingToGoal)
                    {
                        movingToGoal = false;
                    }
                }
                if (!firstCell.edgeDown)
                {
                    if (!firstCell.cellDown.occupied)
                    {
                        //no baguette in the next down cell
                        canMove = true;
                        if (firstCell.cellDown.goalCell)
                        {
                            //code for checking goal
                            if (firstCell.cellDown.gameObject.GetComponent <BakeryGoalCell>().myColor.ToString() == myColor.ToString())
                            {
                                canMove      = true;
                                movingToGoal = true;
                            }
                            else
                            {
                                canMove = false;
                            }
                        }
                    }
                    else
                    {
                        if (!baguetteToPush)
                        {
                            baguetteToPush = firstCell.cellDown.gameObject.GetComponent <BakeryCellConn>().mybaguette;
                            //baguetteToPush.transform.position = new Vector3(baguetteToPush.transform.position.x,(baguetteToPush.transform.position.y - (this.transform.position.y - iniPos.y)),baguetteToPush.transform.position.z);
                            BTPcurrentPos = baguetteToPush.transform.position;
                            float adjustedY = nextPos.y - ((myCells.Length * 0.5f) + 0.5f);
                            baguetteToPush.transform.position = new Vector3(baguetteToPush.transform.position.x, adjustedY, baguetteToPush.transform.position.z);
                            pushingPos = baguetteToPush.transform.position;
                            //pushingPos.y += (this.transform.position.y - iniPos.y);
                        }
                        if (baguetteToPush.CheckAllDown())
                        {
                            //can push down
                            pushing = true;
                            canMove = true;
                        }
                        else
                        {
                            // cant push down
                            SetPushedBaguette();
                            baguetteToPush = null;
                            minY           = iniPos.y;
                            canMove        = false;
                            this.gameObject.transform.position = iniPos;
                        }
                    }
                }
                else
                {
                    canMove = false;
                    minY    = iniPos.y;
                    this.gameObject.transform.position = iniPos;
                }
            }
            else if (nextPos.y < this.gameObject.transform.position.y)
            {
                //Is moving DOWN towards the starting point
                canMove = true;
            }
            if (Mathf.Abs(nextPos.y - iniPos.y) >= minDistance && canMove)
            {
                List <PuzzleCell> tempCells = new List <PuzzleCell>();
                if (baguetteToPush)
                {
                    foreach (PuzzleCell cell in baguetteToPush.myCells)
                    {
                        tempCells.Add(cell.CheckDownAmmount(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.CheckDownAmmount(1);
                    baguetteToPush.lastCell  = baguetteToPush.lastCell.CheckDownAmmount(1);
                    tempCells.Clear();
                    BTPcurrentPos.y          -= cellDistance;
                    baguetteToPush.iniPos     = BTPcurrentPos;
                    baguetteToPush.currentPos = BTPcurrentPos;
                }

                foreach (PuzzleCell cell in myCells)
                {
                    //free current baguette cells and get new cells
                    tempCells.Add(cell.CheckDownAmmount(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.CheckDownAmmount(1);
                lastCell  = lastCell.CheckDownAmmount(1);
                tempCells.Clear();
                currentPos.y -= 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.y > maxY)
            {
                nextPos.y = maxY;
            }
            if (nextPos.y < minY)
            {
                nextPos.y = minY;
            }
            this.gameObject.transform.position = nextPos;
            if (baguetteToPush)
            {
                baguetteToPush.gameObject.transform.position = pushingPos;
            }
        }
    }