예제 #1
0
    /// <summary>
    /// Меняет местами клетки
    /// </summary>
    /// <param name="target">Клетка</param>
    public void ExchangeCrystal(Cell target)
    {
        if (target.crystal != null && crystal != null)
        {
            isCrystalMove        = true;
            target.isCrystalMove = true;

            if (crystal.bonus != null && target.crystal.bonus != null)
            {
                if (crystal.bonus.GetType() == typeof(LineBonus) && target.crystal.bonus.GetType() == typeof(LineBonus))
                {
                    crystal.transform.DOMove(target.transform.position, 0.2f).SetEase(Ease.InSine).OnComplete(delegate { DestroyCrystal(); target.DestroyCrystal(); });
                    if (target.x == x)
                    {
                        crystal.bonus.type        = TypeLineBonus.Verical;
                        target.crystal.bonus.type = TypeLineBonus.Horizontal;
                    }
                    else
                    {
                        target.crystal.bonus.type = TypeLineBonus.Verical;
                        crystal.bonus.type        = TypeLineBonus.Horizontal;
                    }
                    return;
                }

                if ((crystal.bonus.GetType() == typeof(BoxBonus) && target.crystal.bonus.GetType() == typeof(LineBonus)) || (crystal.bonus.GetType() == typeof(LineBonus) && target.crystal.bonus.GetType() == typeof(BoxBonus)))
                {
                    Destroy(crystal.bonus);
                    Destroy(target.crystal.bonus);

                    List <Cell> destroyCellList = new List <Cell>();
                    destroyCellList.Add(this);

                    crystal.spriteRenderer.sortingOrder        = 0;
                    target.crystal.spriteRenderer.sortingOrder = 0;

                    destroyCellList.Add(target);
                    if (target.x == x)
                    {
                        LineBonus lineBonus = crystal.gameObject.AddComponent <LineBonus>();
                        lineBonus.Line(TypeLineBonus.Verical, gameField, crystal);
                        lineBonus.SetEffect(crystal.InitLineEffect(), crystal.lineSprite);
                        lineBonus.OffAnim();
                        crystal.bonus = lineBonus;

                        lineBonus = target.crystal.gameObject.AddComponent <LineBonus>();
                        lineBonus.Line(TypeLineBonus.Horizontal, gameField, target.crystal);
                        lineBonus.SetEffect(target.crystal.InitLineEffect(), target.crystal.lineSprite);
                        lineBonus.OffAnim();
                        target.crystal.bonus = lineBonus;
                    }
                    else
                    {
                        LineBonus lineBonus = crystal.gameObject.AddComponent <LineBonus>();
                        lineBonus.Line(TypeLineBonus.Horizontal, gameField, crystal);
                        lineBonus.SetEffect(crystal.InitLineEffect(), crystal.lineSprite);
                        lineBonus.OffAnim();
                        crystal.bonus = lineBonus;

                        lineBonus = target.crystal.gameObject.AddComponent <LineBonus>();
                        lineBonus.Line(TypeLineBonus.Verical, gameField, target.crystal);
                        lineBonus.SetEffect(target.crystal.InitLineEffect(), target.crystal.lineSprite);
                        lineBonus.OffAnim();
                        target.crystal.bonus = lineBonus;
                    }

                    if (target.x > 0)
                    {
                        LineBonus.DestroyCell(target.x - 1, target.y, TypeLineBonus.Verical);
                    }

                    if (target.x < Field.size - 1)
                    {
                        LineBonus.DestroyCell(target.x + 1, target.y, TypeLineBonus.Verical);
                    }

                    if (target.y > 0)
                    {
                        LineBonus.DestroyCell(target.x, target.y - 1, TypeLineBonus.Horizontal);
                    }

                    if (target.y < Field.size - 1)
                    {
                        LineBonus.DestroyCell(target.x, target.y + 1, TypeLineBonus.Horizontal);
                    }
                    crystal.transform.DOMove(target.transform.position, 0.2f).SetEase(Ease.InSine).OnComplete(
                        delegate
                    {
                        crystal.spriteRenderer.sortingOrder        = 0;
                        target.crystal.spriteRenderer.sortingOrder = 0;

                        foreach (Cell cellDestroy in destroyCellList)
                        {
                            cellDestroy.DestroyCrystal();
                        }
                    });
                    return;
                }
                if (crystal.bonus.GetType() == typeof(BoxBonus) && target.crystal.bonus.GetType() == typeof(BoxBonus))
                {
                    Destroy(crystal.bonus);
                    Destroy(target.crystal.bonus);

                    BoxBonus lineBonus = target.crystal.gameObject.AddComponent <BoxBonus>();
                    lineBonus.Box(gameField, target.crystal);
                    lineBonus.SetEffect(target.crystal.InitBoxEffect(), target.crystal.colorsBoxBonus[target.crystal.colorID]);
                    lineBonus.AreaSize   = 2;
                    target.crystal.bonus = lineBonus;
                    target.crystal.spriteRenderer.sortingOrder += 2;
                    target.crystal.spriteRenderer.sprite        = target.crystal.spriteOfBoxEffect[target.crystal.colorID];
                    crystal.transform.DOMove(target.transform.position, 0.2f).SetEase(Ease.InSine).OnComplete(target.DestroyCrystal);
                    return;
                }
            }
            else
            {
                if (crystal.bonus != null)
                {
                    if (crystal.bonus.GetType() == typeof(StarBonus))
                    {
                        crystal.GetComponent <StarBonus>().SetType(target.crystal.type);
                        crystal.transform.DOMove(target.transform.position, 0.2f).SetEase(Ease.InSine).OnComplete(DestroyCrystal);
                        return;
                    }
                }
                if (target.crystal.bonus != null)
                {
                    if (target.crystal.bonus.GetType() == typeof(StarBonus))
                    {
                        target.crystal.GetComponent <StarBonus>().SetType(crystal.type);
                        crystal.transform.DOMove(target.transform.position, 0.2f).SetEase(Ease.InSine).OnComplete(target.DestroyCrystal);
                        return;
                    }
                }
            }



            if (gameField.CheckNearCombination(x, y, target) || gameField.CheckNearCombination(target.x, target.y, this))
            {
                crystal.transform.DOMove(target.transform.position, 0.2f).SetEase(Ease.InSine).OnComplete(EndMove);
                target.crystal.transform.DOMove(transform.position, 0.2f).SetEase(Ease.InSine).OnComplete(target.EndMove);

                Crystal obm;
                obm                         = crystal;
                crystal                     = target.crystal;
                target.crystal              = obm;
                target.crystal.cell         = this;
                target.crystal.previousCell = this;
                crystal.cell                = target;
                crystal.previousCell        = target;

                if (y == target.y)
                {
                    target.crystal.typeOfLine = TypeLineBonus.Horizontal;
                    crystal.typeOfLine        = TypeLineBonus.Horizontal;
                }
            }
            else
            {
                crystal.transform.DOMove(target.transform.position, 0.2f).SetEase(Ease.InSine).OnComplete(() => ReturnCrystal(target));
                target.crystal.transform.DOMove(transform.position, 0.2f).SetEase(Ease.InSine);
            }
        }
    }
예제 #2
0
    /// <summary>
    /// Поиск пути движения для кристалов
    /// </summary>
    private void FindWayFromCrystals()
    {
        bool move = false;

        if (!CheckCellFromBonusActive())
        {
            return;
        }

        if (moveComplite)
        {
            if (combinations.Count != 0)
            {
                SortCombination();

                foreach (Combination combo in combinations)
                {
                    switch (combo.cellsInCombination.Count)
                    {
                    case 3:
                    {
                        foreach (Cell cell in combo.cellsInCombination)
                        {
                            cell.DestroyCrystal();
                        }
                        break;
                    }

                    case 4:
                    {
                        if (combo.activeCell.crystal.bonus == null)
                        {
                            LineBonus lineBonus = combo.activeCell.crystal.gameObject.AddComponent <LineBonus>();
                            lineBonus.Line(combo.activeCell.crystal.typeOfLine, this, combo.activeCell.crystal);
                            lineBonus.SetEffect(combo.activeCell.crystal.InitLineEffect(), combo.activeCell.crystal.lineSprite);
                            combo.activeCell.crystal.bonus = lineBonus;
                            combo.activeCell.destroyEffect.Activate(combo.activeCell.gameObject, false, 250);
                            foreach (Cell cell in combo.cellsInCombination)
                            {
                                if (cell != combo.activeCell)
                                {
                                    cell.DestroyCrystal();
                                }
                            }
                            combo.activeCell.cellInCombination.Clear();
                        }
                        else
                        {
                            foreach (Cell cell in combo.cellsInCombination)
                            {
                                cell.DestroyCrystal();
                            }
                            break;
                        }
                        break;
                    }

                    default:
                    {
                        int x      = combo.activeCell.x;
                        int y      = combo.activeCell.y;
                        int xCount = 0;
                        int yCount = 0;
                        for (int i = 0; i < combo.cellsInCombination.Count; i++)
                        {
                            if (combo.cellsInCombination[i].x != x)
                            {
                                xCount++;
                            }
                            if (combo.cellsInCombination[i].y != y)
                            {
                                yCount++;
                            }
                        }
                        if (xCount < 4 && yCount < 4)
                        {
                            if (combo.activeCell.crystal.bonus == null)
                            {
                                BoxBonus lineBonus = combo.activeCell.crystal.gameObject.AddComponent <BoxBonus>();
                                lineBonus.Box(this, combo.activeCell.crystal);
                                lineBonus.SetEffect(combo.activeCell.crystal.InitBoxEffect(), combo.activeCell.crystal.colorsBoxBonus[combo.activeCell.crystal.colorID]);
                                combo.activeCell.crystal.bonus = lineBonus;
                                combo.activeCell.crystal.spriteRenderer.sortingOrder += 2;
                                combo.activeCell.crystal.spriteRenderer.sprite        = combo.activeCell.crystal.spriteOfBoxEffect[combo.activeCell.crystal.colorID];
                                combo.activeCell.destroyEffect.Activate(combo.activeCell.gameObject, false, 500);
                                foreach (Cell cell in combo.cellsInCombination)
                                {
                                    if (cell != combo.activeCell)
                                    {
                                        cell.DestroyCrystal();
                                    }
                                }
                                combo.activeCell.cellInCombination.Clear();
                            }
                            else
                            {
                                foreach (Cell cell in combo.cellsInCombination)
                                {
                                    cell.DestroyCrystal();
                                }
                                break;
                            }
                        }
                        else
                        {
                            if (combo.activeCell.crystal.bonus == null)
                            {
                                StarBonus starBonus = combo.activeCell.crystal.gameObject.AddComponent <StarBonus>();
                                combo.activeCell.crystal.bonus = starBonus;
                                starBonus.Star(this, combo.activeCell.crystal);
                                starBonus.SetType(combo.activeCell.crystal.type);
                                starBonus.SetEffect(combo.activeCell.crystal.lizerPrefab, combo.activeCell.crystal.InitStarArialEffect(), combo.activeCell.crystal.InitStarArialEffect(), combo.activeCell.crystal.firstColorArial, combo.activeCell.crystal.secondColorArial);
                                combo.activeCell.crystal.spriteRenderer.sprite       = combo.activeCell.crystal.starSprite;
                                combo.activeCell.crystal.spriteRenderer.sortingOrder = 1;
                                combo.activeCell.crystal.type = TypeOfCrystal.star;
                                combo.activeCell.destroyEffect.Activate(combo.activeCell.gameObject, false, 1000);
                                foreach (Cell cell in combo.cellsInCombination)
                                {
                                    if (cell != combo.activeCell)
                                    {
                                        cell.DestroyCrystal();
                                    }
                                }
                                combo.activeCell.cellInCombination.Clear();
                            }
                            else
                            {
                                foreach (Cell cell in combo.cellsInCombination)
                                {
                                    cell.DestroyCrystal();
                                }
                                break;
                            }
                        }
                        break;
                    }
                    }
                }

                combinations.Clear();

                if (!CheckCellFromBonusActive())
                {
                    return;
                }
            }

            for (int j = 0; j < size; j++)
            {
                for (int i = 0; i < size; i++)
                {
                    if (cells[i, j].crystal != null)
                    {
                        if (cells[i, j].isCrystalMove)
                        {
                            return;
                        }

                        if (j < size - 1)
                        {
                            if (cells[i, j + 1].crystal == null && !cells[i, j + 1].isBarrier)
                            {
                                move = true;
                                if (!AddWayPoint(i, j, 0, 1))
                                {
                                    findComplite = !move && moveCrystals.Count == 0;
                                    return;
                                }
                            }
                            else
                            {
                                if (i > 0)
                                {
                                    if (cells[i - 1, j].crystal == null && cells[i - 1, j + 1].crystal == null && !cells[i - 1, j + 1].isBarrier && !cells[i - 1, j].isCellGenerate)
                                    {
                                        move = true;
                                        if (!AddWayPoint(i, j, -1, 1))
                                        {
                                            findComplite = !move && moveCrystals.Count == 0;
                                            return;
                                        }
                                    }
                                    else
                                    {
                                        if (i < size - 1)
                                        {
                                            if (cells[i + 1, j].crystal == null && cells[i + 1, j + 1].crystal == null && !cells[i + 1, j + 1].isBarrier && !cells[i + 1, j].isCellGenerate)
                                            {
                                                move = true;
                                                if (!AddWayPoint(i, j, 1, 1))
                                                {
                                                    findComplite = !move && moveCrystals.Count == 0;
                                                    return;
                                                }
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    if (i < size - 1)
                                    {
                                        if (cells[i + 1, j].crystal == null && cells[i + 1, j + 1].crystal == null && !cells[i + 1, j + 1].isBarrier && !cells[i + 1, j].isCellGenerate)
                                        {
                                            move = true;
                                            if (!AddWayPoint(i, j, 1, 1))
                                            {
                                                findComplite = !move && moveCrystals.Count == 0;
                                                return;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        if (!AddWayPoint(i, j, 0, 0))
                        {
                            findComplite = !move && moveCrystals.Count == 0;
                            return;
                        }
                    }
                }
            }
        }

        findComplite = !move && moveCrystals.Count == 0;

        if (!move)
        {
            moveComplite = false;
            if (moveCrystals.Count == 0)
            {
                moveComplite = true;
                return;
            }
            for (int i = 0; i < moveCrystals.Count; i++)
            {
                if (moveCrystals[i].moveWayPoints.Count == 0)
                {
                    moveCrystals.RemoveAt(i);
                    break;
                }
                moveCrystals[i].MoveOnWay();
            }
        }
    }