Example #1
0
    private bool DestroyMatchesAt(int column, int row)
    {
        AnimalTile testAnimal = allAnimals[column, row].GetComponent <AnimalTile>();

        if (testAnimal.isMatched)
        {
            //how many elements are in the matched pieces list
            if (findMatches.currentMatches.Count == 4 && currentAnimal != null && !(testAnimal.isColumnBomb || testAnimal.isRowBomb))
            {
                if (findMatches.CheckBombs())
                {
                    findMatches.formedBombs.Add(currentAnimal);
                }
            }
            findMatches.currentMatches.Remove(allAnimals[column, row]);
            GameObject explode = Instantiate(explosion, allAnimals[column, row].transform.position, Quaternion.identity);
            Destroy(explode, 1f);
            if (testAnimal.isMatched)
            {
                Destroy(allAnimals[column, row]);
                allAnimals[column, row] = null;
            }
            return(true);
        }
        else if (findMatches.formedBombs.Contains(testAnimal))
        {
            return(true);
        }
        else
        {
            return(false);
        }
    }
Example #2
0
    /// <summary>
    /// Checks in every direction around the centerPos for atleast
    /// 4 animal tiles of the same type in a row. Animal rows that are found
    /// are marked matched at the tile level.
    /// </summary>
    /// <param name="centerPos"></param>
    public void CheckForMatches(IntVector2 centerPos)
    {
        if (!IsValidIndex(centerPos))
        {
            return;
        }
        AnimalTile centerAnimal = GetAnimal(centerPos);

        if (AnimalTile.IsNullOrMoving(centerAnimal))
        {
            return;
        }

        //Match count for each direction.
        int[] matchCounts = { 1, 1, 1, 1, 0, 0, 0, 0 };

        //Count matches found extending out from (x,y) in each direction.
        foreach (SwipeDirection direction in Enum.GetValues(typeof(SwipeDirection)))
        {
            IntVector2 currPos    = GetNextNeighbor(centerPos, direction);
            AnimalTile currAnimal = GetAnimal(currPos);

            //While neighbor in direction is the same as center. Add to count and move to next
            while (!AnimalTile.IsNullOrMoving(currAnimal) && AnimalTile.IsSameType(centerAnimal, currAnimal))
            {
                matchCounts[(int)direction]++;

                currPos    = GetNextNeighbor(currPos, direction);
                currAnimal = GetAnimal(currPos);
            }
        }

        //Only iterate: North, NorthEast, East, SouthEast. The rest can be considered part of these.
        for (int i = 0; i < 4; i++)
        {
            IntVector2     currStartPos  = new IntVector2(centerPos.x, centerPos.y);
            SwipeDirection currDirection = (SwipeDirection)i;
            int            currLength    = 0;

            //Center is not start of match
            SwipeDirection invertedDirection = InputController.InvertDirection(currDirection);

            //Find correct start, and how long it really is.
            currStartPos = GetNextNeighbor(currStartPos, invertedDirection, matchCounts[i + 4]);
            currLength  += matchCounts[i + 4] + matchCounts[i];

            //If the row is 4 or longer and has an unmatched animal in it. Remove it.
            if (currLength >= MinMatch && DoesRowHaveNonMatched(currStartPos, currLength, currDirection))
            {
                IsReady = false;
                //Set them marked in the board. And give player points.
                MarkRowMatched(currStartPos, currLength, currDirection);
                playerScore.AddPoints(currStartPos, currLength, currDirection);
            }
        }
    }
Example #3
0
    /// <summary>
    /// Move the animal and remove it's reference. That way
    /// during lerping it wont have an incorrect reference.
    /// </summary>
    private void MoveAnimal(AnimalTile animal, IntVector2 targetPos)
    {
        if (AnimalTile.IsNullOrMoving(animal))
        {
            return;
        }

        animal.LerpToPosition(targetPos);
        SetAnimalInBoard(animal, targetPos);
    }
Example #4
0
 private IEnumerator ActivateRowBombCo(int row)
 {
     for (int i = 0; i < board.width; i++)
     {
         GameObject testGameObject = board.allAnimals[i, row];
         AnimalTile testAnimalTile = testGameObject.GetComponent <AnimalTile>();
         testAnimalTile.isMatched = true;
         currentMatches.Add(testGameObject);
     }
     yield return(null);
 }
Example #5
0
    /// <summary>
    /// Switch the animals at the two specific locations. Their
    /// references in the array are set to null until
    /// they both finish lerping to them.
    /// </summary>
    public void SwitchAnimals(IntVector2 posA, IntVector2 posB)
    {
        AnimalTile animalA = GetAnimal(posA);
        AnimalTile animalB = GetAnimal(posB);

        if (!AnimalTile.IsNullOrMoving(animalA) && !AnimalTile.IsNullOrMoving(animalB))
        {
            MoveAnimal(animalA, posB);
            MoveAnimal(animalB, posA);
        }
    }
Example #6
0
    /// <summary>
    /// Spawns a new gameobject and stores its reference in the board.
    /// </summary>
    private void SpawnAnimal()
    {
        AnimalBluePrint bluePrint     = animalsToSpawn.Dequeue();
        GameObject      newTile       = Instantiate(animalPrefabs[(int)bluePrint.Type], this.transform.position, Quaternion.identity, animalStorage);
        AnimalTile      newAnimalTile = newTile.GetComponent <AnimalTile>();

        //Start lerping towards it's target, and store it's reference in the board.
        newAnimalTile.animalBoard = animalBoard;
        newAnimalTile.LerpToPosition(bluePrint.TargetPos);
        animalBoard.SetAnimalInBoard(newAnimalTile, bluePrint.TargetPos);
    }
Example #7
0
    // Update is called once per frame
    void Update()
    {
        //If mouse was clicked.
        if (Input.GetMouseButtonDown(0))
        {
            isClicking    = true;
            startMousePos = new Vector2(gameCamera.ScreenToWorldPoint(Input.mousePosition).x, gameCamera.ScreenToWorldPoint(Input.mousePosition).y);

            //Shoot a ray out from cursor.
            RaycastHit2D hit = Physics2D.Raycast(startMousePos, new Vector2(), 0f);

            //If not animal was hit. End the click so nothing funky happens.
            if (hit.collider != null)
            {
                animalHit = hit.collider.GetComponent <AnimalTile>();
            }
            else
            {
                isClicking = false;
            }
        }

        //Mouse was released.
        if (Input.GetMouseButtonUp(0))
        {
            endMousePos = new Vector2(gameCamera.ScreenToWorldPoint(Input.mousePosition).x, gameCamera.ScreenToWorldPoint(Input.mousePosition).y);

            //User is finishing up a click.
            if (isClicking)
            {
                //Prevent double click from doing anything
                float distance = Vector2.Distance(startMousePos, endMousePos);
                if (distance < 0.1f)
                {
                    isClicking = false;
                    return;
                }

                //Get direction from clicks, and figure out where it started.
                float          swipeAngle     = AngleBetweenVector2(animalHit.transform.position, endMousePos);
                SwipeDirection swipeDirection = GetSwipeDirection(swipeAngle);

                IntVector2 startIndex = new IntVector2(animalHit.transform.position.x, animalHit.transform.position.y);

                //Verify it was valid. Get end swipe position and switch the two.
                if (IsValidMove(startIndex, swipeDirection))
                {
                    IntVector2 endIndex = GetNeighborPos(startIndex, swipeDirection);
                    animalBoard.SwitchAnimals(startIndex, endIndex);
                }
            }
            isClicking = false;
        }
    }
Example #8
0
 /// <summary>
 /// Checks if the animalTile is moving or null.
 /// </summary>
 public static bool IsNullOrMoving(AnimalTile animal)
 {
     if (animal == null || animal.state == AnimalState.Moving)
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
Example #9
0
    public void Switcharoo(AnimalTile a)
    {
        previousTargetX = targetX;
        previousTargetY = targetY;
        float tempX = a.targetX;
        float tempY = a.targetY;

        a.targetX = targetX;
        a.targetY = targetY;
        targetX   = tempX;
        targetY   = tempY;
    }
Example #10
0
 /// <summary>
 /// Store the inputted animal in the array at the specific
 /// location. Position is checked to ensure it's valid. This
 /// is called from AnimalTile.cs when the lerp position
 /// reaches it's destination.
 /// </summary>
 public bool SetAnimalInBoard(AnimalTile animal, IntVector2 pos)
 {
     if (IsValidIndex(pos))
     {
         animals[pos.x, pos.y] = animal;
         return(true);
     }
     else
     {
         return(false);
     }
 }
Example #11
0
    /// <summary>
    /// Move the animal and remove it's reference. That way
    /// during lerping it wont have an incorrect reference.
    /// </summary>
    private void MoveAnimal(IntVector2 animalPos, IntVector2 targetPos)
    {
        AnimalTile animalToMove = GetAnimal(animalPos);

        if (AnimalTile.IsNullOrMoving(animalToMove))
        {
            return;
        }

        animalToMove.LerpToPosition(targetPos);
        animals[animalPos.x, animalPos.y] = null;
        SetAnimalInBoard(animalToMove, targetPos);
    }
Example #12
0
    /// <summary>
    /// Safe way to retrieve an animal tile from the board. This
    /// will prevent any invalid index errors.
    /// </summary>
    /// <param name="index"></param>
    /// <returns></returns>
    public AnimalTile GetAnimal(IntVector2 index)
    {
        if (IsValidIndex(index))
        {
            AnimalTile animal = animals[index.x, index.y];

            if (animal != null)
            {
                return(animals[index.x, index.y]);
            }
        }
        return(null);
    }
Example #13
0
    /// <summary>
    /// Checks to see if every animal has finally reached its target.
    /// </summary>
    /// <returns></returns>
    private bool IsBoardFull()
    {
        for (int x = 0; x < Width; x++)
        {
            for (int y = 0; y < Height; y++)
            {
                AnimalTile currAnimal = GetAnimal(new IntVector2(x, y));

                if (AnimalTile.IsNullOrMoving(currAnimal))
                {
                    return(false);
                }
            }
        }
        return(true);
    }
Example #14
0
    /// <summary>
    /// Checks if two animals share the same type.
    /// </summary>
    /// <returns></returns>
    public static bool IsSameType(AnimalTile a, AnimalTile b)
    {
        if (a == null || b == null)
        {
            return(false);
        }

        if (a.Type == b.Type)
        {
            return(true);
        }
        else
        {
            return(false);
        }
    }
Example #15
0
    /// <summary>
    /// Sets all the animals in the row as matched.
    /// </summary>
    /// <param name="startPos"></param>
    /// <param name="length"></param>
    /// <param name="rowDirection"></param>
    private void MarkRowMatched(IntVector2 startPos, int length, SwipeDirection rowDirection)
    {
        IntVector2 currPos = new IntVector2(startPos.x, startPos.y);

        //Iterate through the row setting each animal to matched.
        for (int i = 0; i < length; i++)
        {
            AnimalTile toMark = GetAnimal(currPos);

            if (!AnimalTile.IsNullOrMoving(toMark))
            {
                toMark.State = AnimalState.Matched;
            }

            currPos = GetNextNeighbor(currPos, rowDirection);
        }
    }
Example #16
0
 public bool CheckBombs()
 {
     //Did the player move something?
     if (board.currentAnimal != null)
     {
         // is the piece they moved matched?
         if (board.currentAnimal.isMatched && !(board.currentAnimal.isColumnBomb || board.currentAnimal.isRowBomb))
         {
             //make it unmatched
             board.currentAnimal.isMatched = false;
             //decide what kind of bomb to make
             int typeOfBomb = Random.Range(0, 100);
             if (typeOfBomb < 50)
             {
                 board.currentAnimal.MakeRowBomb();
             }
             else if (typeOfBomb >= 50)
             {
                 board.currentAnimal.MakeColumnBomb();
             }
             return(true);
         }
         else if (board.currentAnimal.otherAnimal != null)
         {
             AnimalTile otherAnimal = board.currentAnimal.otherAnimal.GetComponent <AnimalTile>();
             if (otherAnimal.isMatched)
             {
                 //make it unmatched
                 otherAnimal.isMatched = false;
                 //decide what kind of bomb to make
                 int typeOfBomb = Random.Range(0, 100);
                 if (typeOfBomb < 50)
                 {
                     otherAnimal.MakeRowBomb();
                 }
                 else if (typeOfBomb >= 50)
                 {
                     otherAnimal.MakeColumnBomb();
                 }
                 return(true);
             }
         }
     }
     return(false);
 }
Example #17
0
    /// <summary>
    /// Squeeze the column back together.
    /// </summary>
    private int UpdateColumn(int x)
    {
        int emptyY = -1;

        //Start at bottom of column and squeeze it down.
        for (int y = 0; y < Height; y++)
        {
            IntVector2 currPos    = new IntVector2(x, y);
            AnimalTile currAnimal = GetAnimal(currPos);

            //if empty tile, store y coord if none yet.
            if (AnimalTile.IsNullOrMoving(currAnimal))
            {
                emptyY = (emptyY == -1) ? y : emptyY;
            }
            //Tile was not empty. If we need to fill a lower one do so.
            else
            {
                //We have an empty spot to fill.
                if (emptyY != -1)
                {
                    //Move current animal to last empty slot found.
                    IntVector2 newPos = new IntVector2(x, emptyY);
                    MoveAnimal(currAnimal, newPos);
                    KillAnimalAt(currPos);
                    //Debug.Log("Moving " + currAnimal + " from: " + currPos + " to " + newPos);

                    //If we jumped more than 1 empty slot, set it to current y.
                    int yDelta = emptyY - y;
                    emptyY = (yDelta > 1) ? y : emptyY + 1;
                }
            }
        }

        for (int y = 0; y < Height; y++)
        {
            IntVector2 currPos = new IntVector2(x, y);
            if (GetAnimal(currPos) == null)
            {
                animalSpawners[x].AddAnimalToSpawnQueue(currPos);
            }
        }

        return(emptyY);
    }
Example #18
0
    /// <summary>
    /// Checks if any of the animal tiles in the row
    /// are not matched. If so, match them.
    /// </summary>
    private bool DoesRowHaveNonMatched(IntVector2 startPos, int length, SwipeDirection rowDirection)
    {
        IntVector2 currPos        = new IntVector2(startPos.x, startPos.y);
        bool       unMatchedFound = false;
        AnimalTile currAnimal;

        for (int i = 0; i < length; i++)
        {
            currAnimal = GetAnimal(currPos);

            if (!AnimalTile.IsNullOrMoving(currAnimal) && currAnimal.State != AnimalState.Matched)
            {
                unMatchedFound = true;
            }
            currPos = GetNextNeighbor(currPos, rowDirection);
        }

        return(unMatchedFound);
    }
Example #19
0
    public IEnumerator DestroyMatches()
    {
        yield return(new WaitForSeconds(.20f));

        int scoreUpdate = 0;

        for (int i = 0; i < height; i++)
        {
            for (int j = 0; j < width; j++)
            {
                if (DestroyMatchesAt(j, i))
                {
                    scoreUpdate += 100;
                    if (!activatedPower)
                    {
                        powerBar.IncreasePowerLevel(1.5f);
                    }
                }
            }
        }
        if (activatedColumnBomb)
        {
            AudioManager.instance.PlayColumnBombSound();
        }
        else if (activatedRowBomb)
        {
            AudioManager.instance.PlayRowBombSound();
        }
        else if (activatedPower)
        {
            AudioManager.instance.PlayPowerUse();
        }
        else
        {
            AudioManager.instance.PlayHitSound();
        }
        activatedColumnBomb = false;
        activatedRowBomb    = false;
        activatedPower      = false;
        dataTracker.UpdateScore(scoreUpdate);
        currentAnimal = null;
        yield return(StartCoroutine(DecreaseRowCo()));
    }
Example #20
0
    private IEnumerator FindAllMatchesCo()
    {
        yield return(new WaitForSeconds(.2f));

        for (int i = 0; i < board.width; i++)
        {
            for (int j = 0; j < board.height; j++)
            {
                GameObject currentAnimal = board.allAnimals[i, j];
                if (currentAnimal != null)
                {
                    if (i > 0 && i < board.width - 1)
                    {
                        GameObject leftAnimal  = board.allAnimals[i - 1, j];
                        GameObject rightAnimal = board.allAnimals[i + 1, j];
                        // null check
                        if (leftAnimal != null && rightAnimal != null)
                        {
                            if (leftAnimal.tag == currentAnimal.tag && rightAnimal.tag == currentAnimal.tag)
                            {
                                AnimalTile currentAnimalTileComponent = currentAnimal.GetComponent <AnimalTile>();
                                AnimalTile leftAnimalTileComponent    = leftAnimal.GetComponent <AnimalTile>();
                                AnimalTile rightAnimalTileComponent   = rightAnimal.GetComponent <AnimalTile>();

                                if (!currentMatches.Contains(leftAnimal))
                                {
                                    currentMatches.Add(leftAnimal);
                                }
                                leftAnimalTileComponent.isMatched = true;

                                if (!currentMatches.Contains(rightAnimal))
                                {
                                    currentMatches.Add(rightAnimal);
                                }
                                rightAnimalTileComponent.isMatched = true;

                                if (!currentMatches.Contains(currentAnimal))
                                {
                                    currentMatches.Add(currentAnimal);
                                }
                                currentAnimalTileComponent.isMatched = true;
                            }
                        }
                    }
                    if (j > 0 && j < board.height - 1)
                    {
                        GameObject upAnimal   = board.allAnimals[i, j + 1];
                        GameObject downAnimal = board.allAnimals[i, j - 1];
                        if (upAnimal != null && downAnimal != null)
                        {
                            if (upAnimal.tag == currentAnimal.tag && downAnimal.tag == currentAnimal.tag)
                            {
                                AnimalTile currentAnimalTileComponent = currentAnimal.GetComponent <AnimalTile>();
                                AnimalTile upAnimalTileComponent      = upAnimal.GetComponent <AnimalTile>();
                                AnimalTile downAnimalTileComponent    = downAnimal.GetComponent <AnimalTile>();

                                if (!currentMatches.Contains(upAnimal))
                                {
                                    currentMatches.Add(upAnimal);
                                }
                                upAnimalTileComponent.isMatched = true;

                                if (!currentMatches.Contains(downAnimal))
                                {
                                    currentMatches.Add(downAnimal);
                                }
                                downAnimalTileComponent.isMatched = true;

                                if (!currentMatches.Contains(currentAnimal))
                                {
                                    currentMatches.Add(currentAnimal);
                                }
                                currentAnimalTileComponent.isMatched = true;
                            }
                        }
                    }
                }
            }
        }
    }