/// <summary>
    /// With match element, clone mock grids to simulate the situation if the match is valid or not.
    /// Search mock grids to find a match with specified rules. If there is match, add matches to the explode events, send turn numbe rof the mock grid and bomb events.
    /// </summary>
    /// <param name="matchElement">Threee coord values for the match.</param>
    /// <param name="simulate">If match is valid, make it a valid simulation by changing original grid with the mock one.</param>
    /// <returns>Explode event</returns>
    public ExplodeEvent Explode(MatchElement?matchElement, bool isTurnClockwise = false, bool simulate = true)
    {
        // Create mock grid to simulate request
        List <HexagonGrid> mockGrids = new List <HexagonGrid>();

        for (int i = 0; i < 2; i++)
        {
            mockGrids.Add((HexagonGrid)this.grid.Clone());
        }

        // Turn mock grids. First one with 120 degree, secone one with 240 degree.
        if (matchElement != null && matchElement?.coords != null)
        {
            int firstIndex = isTurnClockwise ? 0 : 2;
            int lastIndex  = isTurnClockwise ? 2 : 0;
            for (int mockIndex = 0; mockIndex < 2; mockIndex++)
            {
                for (int turn = 0; turn < mockIndex + 1; turn++)
                {
                    HexagonElement element = (HexagonElement)mockGrids[mockIndex].GetElement((int)matchElement?.coords[firstIndex].x, (int)matchElement?.coords[firstIndex].y);
                    mockGrids[mockIndex].grid[(int)matchElement?.coords[firstIndex].x, (int)matchElement?.coords[firstIndex].y] = mockGrids[mockIndex].grid[(int)matchElement?.coords[1].x, (int)matchElement?.coords[1].y];
                    mockGrids[mockIndex].grid[(int)matchElement?.coords[1].x, (int)matchElement?.coords[1].y] = mockGrids[mockIndex].grid[(int)matchElement?.coords[lastIndex].x, (int)matchElement?.coords[lastIndex].y];
                    mockGrids[mockIndex].grid[(int)matchElement?.coords[lastIndex].x, (int)matchElement?.coords[lastIndex].y] = element;
                }
            }
        }

        // Check explode events on the mock grids to see if the action is valid or not.
        // Every rule has to be tried to be sure the match.
        List <ExplodeElement> explodeElements = new List <ExplodeElement>();
        int selectedMockGridNo = 0;

        CheckGridWithrulesToFindMatch(mockGrids, explodeElements, ref selectedMockGridNo);

        // If action is valid and not for game over check
        if (explodeElements.Count > 0 && simulate)
        {
            this.grid = mockGrids[selectedMockGridNo];
        }

        ExplodeEvent explodeEvent = new ExplodeEvent(explodeElements.Count > 0, selectedMockGridNo + 1, explodeElements);

        // If action is valid and not for game over check
        if (simulate)
        {
            scoring.SetScore(explodeEvent);
        }

        // If action is valid and done by user then check for bomb timers and explosion.
        if (matchElement != null && matchElement?.coords != null && matchElement?.coords.Count > 0 && simulate)
        {
            BombEvent bombEvent = scoring.BombCheck(ref this.grid);
            explodeEvent.bombEvent = bombEvent;
        }


        return(explodeEvent);
    }
 /// <summary>
 /// Set bomb texts of the all bombs in the scene.
 /// </summary>
 /// <param name="explodeEvent"></param>
 private void SetBombTexts(ExplodeEvent explodeEvent)
 {
     if (explodeEvent.bombEvent.bombElements.Count > 0)
     {
         Debug.Log("There is at least one bomb in the scene and its timer is running out!");
     }
     foreach (BombElement element in explodeEvent.bombEvent.bombElements)
     {
         ActivateBombText(element.bombInfo, gridComponentHolder[element.coords]);
     }
 }
Beispiel #3
0
 private void Awake()
 {
     gameOverEvent      = new GameOverEvent();
     addScoreEvent      = new AddScoreEvent();
     powerPickupEvent   = new PowerPickupEvent();
     stickToPaddleEvent = new StickToPaddleEvent();
     coinPickUpEvent    = new CoinPickUpEvent();
     explodeEvent       = new ExplodeEvent();
     ballHitEvent       = new BallHitEvent();
     brickDamageEvent   = new BrickDamageEvent();
 }
    /// <summary>
    /// Used for non player match element input.
    /// First set board untouchable. Send null match element and get explode event.
    /// If there wont be any explosion then we can set board touchable and return. If there will be any explosion then set these explosions.
    /// </summary>
    public void SetMatchElement()
    {
        SetBoardUnuouchable?.Invoke();
        MatchElement matchElement = new MatchElement();
        ExplodeEvent explodeEvent = hexagonLogic.Explode(matchElement);

        if (!explodeEvent.IsActionValid)
        {
            SetBoardTouchable?.Invoke();
            return;
        }
        SetExplosion(explodeEvent);
    }
    /// <summary>
    /// Calulates the number of exploded block and the score.
    /// </summary>
    /// <param name="explodeEvent"></param>
    public void SetScore(ExplodeEvent explodeEvent)
    {
        if (!explodeEvent.IsActionValid)
        {
            return;
        }
        int explosionCount = 0;

        foreach (ExplodeElement explodeElement in explodeEvent.explodeElements)
        {
            foreach (Vector2 block in explodeElement.positions)
            {
                explosionCount++;
            }
        }
        this.score += explosionCount * this.explosionBlockScore;
    }
    /// <summary>
    /// Explodes all the explode element in the explode event, add explodede gameobjects to object pool to reuse them.
    /// Invoke increase score after all explosions and start fill event for vacancies of exploded gameobjects.
    /// </summary>
    /// <param name="explodeEvent"></param>
    /// <returns></returns>
    private IEnumerator Explode(ExplodeEvent explodeEvent)
    {
        yield return(new WaitForSeconds(0.3f));

        foreach (ExplodeElement explodeElement in explodeEvent.explodeElements)
        {
            foreach (Vector2 coord in explodeElement.positions)
            {
                gridComponentHolder[coord].explode.SetExplode();
                itemComponentsPool.Add(gridComponentHolder[coord]);
                gridComponentHolder[coord] = null;
            }
        }
        yield return(new WaitForSeconds(1f));

        IncreaseScore?.Invoke(hexagonLogic.GetCurrentScore());
        FillEvent fillEvent = hexagonLogic.Fill();

        SetFill(fillEvent);
        yield return(null);
    }
    /// <summary>
    /// Player match element input.
    /// Set boarch untouchable. create match element with selected coords and send it to the logic to see if match request is available or not.
    /// With return explode event set bomb texts. If any bomb explodes then game over.
    /// If action is valid turn hexagon gameobjects, explode them and deselect previous selected coords.
    /// If action is not valid, calculate the destinations of all hexagon gameobject to make it rotation move.
    /// </summary>
    /// <param name="isTurnClockWise"></param>
    public void SetMacthElement(bool isTurnClockWise)
    {
        if (selectedCoords == null)
        {
            return;
        }
        SetBoardUnuouchable?.Invoke();

        MatchElement matchElement = new MatchElement(selectedCoords);

        ExplodeEvent explodeEvent = hexagonLogic.Explode(matchElement, isTurnClockwise: isTurnClockWise);

        SetBombTexts(explodeEvent);

        if (explodeEvent.bombEvent.isBombeExploded)
        {
            Debug.Log("Bomb exploded! Game Over!");
            GameOver?.Invoke();
        }

        if (explodeEvent.IsActionValid)
        {
            Debug.Log("Player action is valid. Explosion and fill events are starting.");
            ChangeElementsCoordsAndPosiitons(selectedCoords, explodeEvent.turnNo, isTurnClockWise);
            SetExplosion(explodeEvent);
            DeselectHexagons();
        }
        else
        {
            Debug.Log("Player action is not valid. Turn selected coords and return to their coords.");
            List <List <Vector2> > destinations = TurnClockwise(selectedCoords, 3, isTurnClockWise);
            TurnGridComponents(destinations);
            StopAllCoroutines();
            StartCoroutine(Move());
        }
    }
 /// <summary>
 /// Start explosion coroutine.
 /// </summary>
 /// <param name="explodeEvent">Explode event</param>
 private void SetExplosion(ExplodeEvent explodeEvent)
 {
     StopAllCoroutines();
     StartCoroutine(Explode(explodeEvent));
 }