Exemple #1
0
    private void RemoveCombinations(IEnumerable <List <Chuzzle> > combinations)
    {
        //remove combinations
        foreach (var combination in combinations)
        {
            Gamefield.InvokeCombinationDestroyed(combination);

            //count points
            Gamefield.PointSystem.CountForCombinations(combination);

            foreach (var chuzzle in combination)
            {
                chuzzle.Destroy(true);
            }
        }
    }
Exemple #2
0
    private IEnumerator RemoveCombinations()
    {
        var powerUpCombination = GamefieldUtility.FindOnlyOneCombinationWithCondition(Gamefield.Chuzzles,
                                                                                      GamefieldUtility.IsPowerUp);

        //if has any powerups
        if (powerUpCombination.Any())
        {
            //destroy step by step
            PowerUpDestroyManager.Instance.Destroy(powerUpCombination);

            if (!AnimatedChuzzles.Any())
            {
                Gamefield.SwitchStateTo(Gamefield.CreateNewChuzzlesState);
            }
        }
        else
        {
            var combinations = GamefieldUtility.FindCombinations(Gamefield.Chuzzles);
            //remove combinations
            foreach (var combination in combinations)
            {
                Gamefield.InvokeCombinationDestroyed(combination);

                foreach (var chuzzle in combination)
                {
                    chuzzle.Destroy(true);
                }

                if (!AnimatedChuzzles.Any())
                {
                    Gamefield.SwitchStateTo(Gamefield.CreateNewChuzzlesState);
                }
                yield return(new WaitForSeconds(0.05f));
            }
        }

        yield return(new WaitForEndOfFrame());
    }
Exemple #3
0
    public void Destroy(IEnumerable <Chuzzle> chuzzlesToDestroy)
    {
        if (!IsInDestroyState)
        {
            IsInDestroyState = true;
            _firstPowerUp    = chuzzlesToDestroy.FirstOrDefault(x => x.IsPowerUp());
            _firstPowerUp.Destroy(true);
            var destroyedPowerUps = new List <Chuzzle>();
            var tilesToDestroy    = new List <Chuzzle>();
            tilesToDestroy.AddRange(chuzzlesToDestroy);

            IEnumerable <Chuzzle> powerUps = new List <Chuzzle> {
                _firstPowerUp
            };
            do
            {
                foreach (var powerup in powerUps)
                {
                    // Debug.Log("Destroy powerUp: "+powerup);
                    destroyedPowerUps.Add(powerup);

                    if (powerup is BombChuzzle)
                    {
                        var toDestroy = (powerup as IPowerUp).ToDestroy.ToArray();
                        //   Debug.Log("Bomb to destroy: "+toDestroy.Count());
                        PrintCollection(toDestroy);
                        tilesToDestroy.AddUniqRange(toDestroy.Where(x => !x.IsDead));
                    }
                    else
                    {
                        var rowNumber = powerup.Real.y;
                        var row       = GetRow(rowNumber).ToArray();
                        //  Debug.Log("Row:");
                        //  PrintCollection(row);

                        // Debug.Log("Column:");
                        var columnNumber = powerup.Real.x;
                        var column       = GetColumn(columnNumber).ToArray();
                        //  PrintCollection(column);

                        if (powerup is HorizontalLineChuzzle)
                        {
                            //all tiles in row destroyed - destroy column otherwise row
                            tilesToDestroy.AddUniqRange(row.Any(chuzzle => !tilesToDestroy.Contains(chuzzle))
                                ? row
                                : column);
                        }
                        else
                        {
                            if (powerup is VerticalLineChuzzle)
                            {
                                //all tiles in column destroyed - destroy row otherwise - column
                                tilesToDestroy.AddUniqRange(column.Any(chuzzle => !tilesToDestroy.Contains(chuzzle))
                                    ? column
                                    : row);
                            }
                        }
                    }
                }

                powerUps = tilesToDestroy.Where(x => x.IsPowerUp() && !destroyedPowerUps.Contains(x) && !x.IsDead).ToArray();
            } while (powerUps.Any());

            Debug.Log("TtD: " + tilesToDestroy.Count);
            PrintCollection(tilesToDestroy);


            var destructionOrder = new List <List <Chuzzle> >()
            {
                new List <Chuzzle>()
                {
                    _firstPowerUp
                }
            };

            //Debug.Log("First chuzzle: "+_firstPowerUp + " Pos: "+_firstPowerUp.Current.Position);
            int i = 1;
            var currentDistance = new List <Chuzzle>();
            do
            {
                currentDistance.Clear();
                foreach (var chuzzle in tilesToDestroy)
                {
                    var magnitude = (chuzzle.Current.Position - _firstPowerUp.Current.Position).magnitude;
                    //Debug.Log("Chuzzle: " + chuzzle + " magnitude: " + magnitude);
                    if (magnitude > i - 1 && magnitude <= i)
                    {
                        currentDistance.Add(chuzzle);
                    }
                }
                //Debug.Log("CurrentDistance:" + currentDistance.Count + " for i: "+i);

                destructionOrder.Add(new List <Chuzzle>(currentDistance));
                // Debug.Log("Destruction order count: "+destructionOrder.Count);
                i++;
            } while (currentDistance.Any() || i < 10);

/*
 *          foreach (var order in destructionOrder)
 *          {
 *              Debug.Log("---: " + order.Count);
 *              PrintCollection(order);
 *          }
 *          Debug.Log("_______");*/

            var s = destructionOrder.Sum(order => order.Count);
            if (s != tilesToDestroy.Count)
            {
                Debug.LogError("Non equal: " + s + " toD: " + tilesToDestroy.Count);
            }
            _gamefield.InvokeCombinationDestroyed(tilesToDestroy);
            StartCoroutine(DestroyCollection(destructionOrder));
        }
    }