Exemplo n.º 1
0
        public int Cost(IngredientGridCell cell, IngredientGridCell cellParent)
        {
            if (cell.Ingredient.Category == IngredientCategory.Bread)
                return int.MinValue;
            else if (cell.Ingredient == cellParent.Ingredient)
                return int.MinValue;
            else
            {
                List<int> listScore = new List<int>();
                List<IngredientGridCell> listIngredientGridCell = _listCellClose.ConvertAll<IngredientGridCell>(p => p.Cell);

                int scoreTotalWithoutCell = GamePlayManager.Instance.CalcScore(listIngredientGridCell, ref listScore);

                listIngredientGridCell.Add(cell);

                int scoreTotalWithCell = GamePlayManager.Instance.CalcScore(listIngredientGridCell, ref listScore);

                //Debug.Log("Score cell : " + (scoreTotalWithCell - scoreTotalWithoutCell).ToString());

                return scoreTotalWithCell - scoreTotalWithoutCell;
            }
        }
Exemplo n.º 2
0
        public int Distance(IngredientGridCell cellA, IngredientGridCell cellB)
        {
            if (cellA == null || cellB == null)
                return 0;

            int distance = Mathf.Abs(cellA.m_xIndex - cellB.m_xIndex) + Mathf.Abs(cellA.m_yIndex - cellB.m_yIndex);

            return distance;
        }
Exemplo n.º 3
0
        private List<IngredientGridCell> CalcPath(IngredientGridCell cellStart, IngredientGridCell cellEnd, ref int score)
        {
            List<IngredientGridCell> listCellPath = new List<IngredientGridCell>();

            //--- Ajout de la case de départ
            AStarCell aStarCell = new AStarCell();
            aStarCell.Cell = cellStart;
            //---

            //---
            _listCellClose.Add(aStarCell);

            bool pathFound = CalcPathFromCell(aStarCell, cellStart, cellEnd);
            score = 0;

            if (!pathFound)
                return listCellPath;
            //---

            //--- Reconstitue le chemin
            bool pathCompleted = false;
            IngredientGridCell cell = cellEnd;

            while (!pathCompleted)
            {
                if (_listCellClose.Count == 0)
                    pathCompleted = true;

                foreach (AStarCell aStarCellChild in _listCellClose)
                {
                    if (aStarCellChild.Cell == cell)
                    {
                        listCellPath.Add(aStarCellChild.Cell);

                        cell = aStarCellChild.ParentCell;

                        if (aStarCellChild.Cell == cellStart)
                        {
                            pathCompleted = true;
                        }
                    }
                }
            }
            //---

            List<int> listScore = new List<int>();
            //Debug.Log("==>" + cellStart.Ingredient.Name + " length : " + listCellPath.Count);

            score = GamePlayManager.Instance.CalcScore(listCellPath, ref listScore);

            //for (int i = 0; i < listCellPath.Count; i++)
            //{
            //    Debug.Log(listCellPath[i].Ingredient.Name + " : " + listScore[i] + " points");
            //}

            return listCellPath;
        }
Exemplo n.º 4
0
        private bool CalcPathFromCell(AStarCell cellParent, IngredientGridCell cellStart, IngredientGridCell cellEnd)
        {
            foreach (IngredientGridCell cell in cellParent.Cell.GetNeighbour(cellStart))
            {
                //---> La cellule n'est pas dans la liste fermée
                if (!_listCellClose.Exists(c => c.Cell == cell))
                {
                    AStarCell aStarCellPrev = _listCellOpen.Find(c => c.Cell == cell);

                    AStarCell aStarCell = new AStarCell();
                    aStarCell.Cell = cell;
                    aStarCell.ParentCell = cellParent.Cell;
                    aStarCell.G = cellParent.G + Distance(cell, cellParent.Cell);
                    aStarCell.H = Distance(cell, cellEnd);
                    aStarCell.F = aStarCell.G + aStarCell.H;

                    int cost = Cost(cell, cellParent.Cell);

                    //if (cost > 0)
                    {
                        aStarCell.F = (int)((float)aStarCell.F * cost);
                    }

                    if (aStarCellPrev != null && aStarCell.G > aStarCellPrev.G)
                    {
                        aStarCellPrev.ParentCell = cellParent.Cell;
                        aStarCellPrev.G = aStarCell.G;
                        aStarCellPrev.H = aStarCell.H;
                        aStarCellPrev.F = aStarCell.F;
                    }
                    else if (aStarCellPrev == null)
                    {
                        _listCellOpen.Add(aStarCell);
                    }
                }
            }

            AStarCell aStarChoosenCell = null;

            foreach (AStarCell aStarCell in _listCellOpen)
            {
                if (aStarChoosenCell == null)
                    aStarChoosenCell = aStarCell;

                if ((aStarCell.Cell.Ingredient.Category != IngredientCategory.Bread) &&
                    (aStarCell.Cell.Ingredient != cellParent.Cell.Ingredient) &&
                    aStarCell.F > aStarChoosenCell.F)
                {
                    //Debug.Log(this.name + " : " + aStarCell.Cell.Ingredient.Category);
                    aStarChoosenCell = aStarCell;
                }
            }

            //---
            if (aStarChoosenCell != null)
            {
                _listCellOpen.Remove(aStarChoosenCell);
                _listCellClose.Add(aStarChoosenCell);
            }
            //---

            //---
            if (aStarChoosenCell == null)
                return false;
            else if (aStarChoosenCell.Cell == cellEnd)
                return true;
            else
                return CalcPathFromCell(aStarChoosenCell, cellStart, cellEnd);
            //---
        }
    public void Start()
    {
        m_cells = new IngredientGridCell[HorizontalCells, VerticalCells];
        m_ui = GameObject.Find("UIToolkit").GetComponent<UIToolkit>();

        for (int x = 0; x < HorizontalCells; x++)
        {
            for (int y = 0; y < VerticalCells; y++)
            {
                m_cells[x, y] = new IngredientGridCell(this, x, y);
            }
        }

        Fill();

        Layout();
    }