Exemplo n.º 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;
     }
 }
Exemplo n.º 2
0
    //this function randomizes the typeOfFood and sprite that appears in the Fruitcell.
    private void randomFoodSelection(FruitCell myCell)
    {
        int whatTypeOfFood = Random.Range(0, foodSprites.Length);

        myCell.typeOfFood        = (FruitCell.FoodType)whatTypeOfFood;
        myCell.foodSprite.sprite = foodSprites[whatTypeOfFood];
    }
Exemplo n.º 3
0
 private void LevelLoader()
 {
     for (int i = 0; i < widthOfBoard; i++)
     {
         for (int j = 0; j < heightOfBoard; j++)
         {
             //(Vector2)transform.position casts transform.position as a vector2 instead of vector3.
             Vector2    position = (Vector2)transform.position + new Vector2(i * cellWidth, j * cellHight);
             GameObject tile     = Instantiate(myTile, position, Quaternion.identity) as GameObject;
             tile.transform.parent = this.transform;
             FruitCell fruit = tile.GetComponent <FruitCell>();
             tiles[i, j]   = fruit;
             fruit.gridPos = new Vector2(i, j);
             tile.name     = "(" + i + "," + j + ")";
             int whatTypeOfFood = Random.Range(0, foodSprites.Length);
             tiles[i, j].typeOfFood        = (FruitCell.FoodType)whatTypeOfFood;
             tiles[i, j].foodSprite.sprite = foodSprites[whatTypeOfFood];
         }
     }
 }
Exemplo n.º 4
0
 //get's the fruit cell when the mouse is clicked down.
 private void onMouseDown()
 {
     fruitCellDown = GetFruiteCellFromMousePos(Input.mousePosition);
 }
Exemplo n.º 5
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;
             }
         }
     }
 }