예제 #1
0
 private void onMouseUp()
 {
     fruitCellUp = GetFruiteCellFromMousePos(Input.mousePosition);
     if (fruitCellUp == null)
     {
         return;
     }
     if (fruitCellUp.IsNeiborCell(fruitCellDown.gridPos))
     {
         if (fruitCellDown == null)
         {
             return;
         }
         //swaps the foodtype.
         FruitCell.FoodType temp = fruitCellDown.typeOfFood;
         fruitCellDown.typeOfFood = fruitCellUp.typeOfFood;
         fruitCellUp.typeOfFood   = temp;
         //swaps the sprites.
         Sprite tempSprite = fruitCellDown.foodSprite.sprite;
         fruitCellDown.foodSprite.sprite = fruitCellUp.foodSprite.sprite;
         fruitCellUp.foodSprite.sprite   = tempSprite;
         //sets the picked up fruit sprite back into it's cell.
         fruitCellDown.foodSprite.gameObject.transform.localPosition = Vector3.zero;
     }
     else
     {
         fruitCellDown.foodSprite.gameObject.transform.localPosition = Vector3.zero;
     }
 }
예제 #2
0
 //this function solves the grid but looping using for loops to cheak every cell and cheak weather the cells beside or on the bottom or top of the cell match the cell.
 private void SolveGrid()
 {
     Debug.Log("solveGrid");
     //two for loops used to cheak through every grid cell
     for (int i = 0; i < widthOfBoard; i++)
     {
         for (int j = 0; j < heightOfBoard; j++)
         {
             Debug.Log("cheaking cell " + i + "," + j);
             FruitCell.FoodType tempFood                = tiles[i, j].typeOfFood;
             FruitCell          currentFruitCell        = tiles[i, j];
             List <FruitCell>   matchingCellsToTheRight = new List <FruitCell>();
             List <FruitCell>   matchingCellsUp         = new List <FruitCell>();
             FruitCell          neiborFruitCell         = currentFruitCell.getNeiborRight(tiles);
             //uses a while loop to cheak keep cheaking for matches as long as their is a neiboring Cell to the one being cheaked.
             while (neiborFruitCell != null)
             {
                 //if statment used to cheak if cell being cheaked has a match with a horizontal cell.
                 if (currentFruitCell.typeOfFood == neiborFruitCell.typeOfFood)
                 {
                     Debug.Log("H-match- " + neiborFruitCell.gridPos);
                     matchingCellsToTheRight.Add(neiborFruitCell);
                     neiborFruitCell = neiborFruitCell.getNeiborRight(tiles);
                 }
                 //breaks out of the stament if their is no match.
                 else
                 {
                     break;
                 }
             }
             neiborFruitCell = currentFruitCell.getNeiborUp(tiles);
             while (neiborFruitCell != null)
             {
                 if (currentFruitCell.typeOfFood == neiborFruitCell.typeOfFood)
                 {
                     //if statment used to cheak if cell being cheaked has a match with a vertical cell.
                     Debug.Log("V-match- " + neiborFruitCell.gridPos);
                     matchingCellsUp.Add(neiborFruitCell);
                     neiborFruitCell = neiborFruitCell.getNeiborUp(tiles);
                 }
                 else
                 {
                     break;
                 }
             }
             //uses bools to tell game if the food in a cell needs to be replaced by another food when a match is found
             bool horizontalSolved = false;
             bool verticalSolved   = false;
             if (matchingCellsToTheRight.Count >= 2)
             {
                 horizontalSolved = true;
                 //iterate over this list and replace the food type with a new food type.
                 for (int b = 0; b < matchingCellsToTheRight.Count; b++)
                 {
                     FruitCell fruitCellToSolve = matchingCellsToTheRight[b];
                     Debug.Log("cells to the right match");
                     randomFoodSelection(fruitCellToSolve);
                 }
             }
             if (matchingCellsUp.Count >= 2)
             {
                 verticalSolved = true;
                 //iterate over this list and replace the food type with a new food type.
                 for (int b = 0; b < matchingCellsUp.Count; b++)
                 {
                     FruitCell fruitCellToSolve = matchingCellsUp[b];
                     Debug.Log("cells up match");
                     randomFoodSelection(fruitCellToSolve);
                 }
             }
             //if we solved any fruit cells above or to the right thne break out of this
             if (horizontalSolved || verticalSolved)
             {
                 //randomFoodSelection called to change food that matches.
                 randomFoodSelection(currentFruitCell);
                 //resterts the solving prosses.
                 StartCoroutine(WaitAndSolveGrid());
                 break;
             }
         }
     }
 }