예제 #1
0
        public static void Process(AStarAgent agent)
        {
            foreach (GridElement element in agent.MyGrid.Elements)
            {
                element.PathParentField = null;
            }

            _openElements.Clear();
            _closedElements.Clear();

            if (!_targetElement.Walkable)
            {
                Debug.LogWarning("Target isn't Walkable!");
                return;
            }

            _startElement.PathParentField = null;

            bool complete = false;

            if (_startElement != null)
            {
                _openElements.Add(_startElement);
                _currentElement = _startElement;
                _currentElement.CalculateHeuristic(_targetElement);
            }
            while (_openElements.Count > 0)
            {
                int   openSize   = _openElements.Count;
                float lowestCost = float.MaxValue;
                for (int i = 0; i < openSize; ++i)
                {
                    if (_openElements[i].TotalFieldMoveCost < lowestCost)
                    {
                        _currentElement = _openElements[i];
                        lowestCost      = _currentElement.TotalFieldMoveCost;
                    }
                }
                _openElements.Remove(_currentElement);
                _closedElements.Add(_currentElement);
                if (_currentElement.Equals(_targetElement))
                {
                    complete = true;
                    break;
                }
                CheckNeighbours(agent.MyGrid);
            }

            if (complete)
            {
                GridElement step = _currentElement;
                agent.Path.Clear();
                do
                {
                    agent.Path.Add(step);
                    step = step.PathParentField;
                } while (step != null);
                agent.Path.Reverse();
            }
        }
예제 #2
0
 private static void CheckNeighbourHelper(Grid grid, IntVector3 index)
 {
     if (IsIndexInGrid(grid, index))
     {
         GridElement element = grid.Elements[index.x, index.y, index.z];
         if (element.Walkable && !_closedElements.Contains(element))
         {
             if (!_openElements.Contains(element))
             {
                 element.PathParentField = _currentElement;
                 _openElements.Add(element);
             }
             else
             {
                 element.CheckNewParent(_currentElement);
             }
             element.CalculateHeuristic(_targetElement);
         }
     }
 }