public void SetDestinationPoint(CellBase point)
 {
     if (DestinationPoint != null && CellBase.CompareStates(DestinationPoint.GetState(), CellBase.State.DestinationPoint))
     {
         DestinationPoint.SetFree();
     }
     DestinationPoint = point;
 }
 public void SetStartPoint(CellBase point)
 {
     if (StartPoint != null && CellBase.CompareStates(StartPoint.GetState(), CellBase.State.StartPoint))
     {
         StartPoint.SetFree();
     }
     StartPoint = point;
 }
Beispiel #3
0
    public IList <ICell> FindPathOnMap(ICell cellStart, ICell cellEnd)
    {
        if (cellStart == null || cellEnd == null)
        {
            return(null);
        }

        List <ICell> OpenList   = new List <ICell>();
        List <ICell> ClosedList = new List <ICell>();

        OpenList.Add(cellStart);

        while (OpenList.Count > 0)
        {
            ICell CurrentNode = OpenList[0];
            for (int i = 1; i < OpenList.Count; i++)
            {
                CellNode _target  = OpenList[i].GetNode();
                CellNode _current = CurrentNode.GetNode();

                if (_target.FCost < _current.FCost || _target.FCost == _current.FCost && _target.ihCost < _current.ihCost)
                {
                    CurrentNode = OpenList[i];
                }
            }

            OpenList.Remove(CurrentNode);
            ClosedList.Add(CurrentNode);

            if (CurrentNode == cellEnd)
            {
                return(GetFinalPath(cellStart, cellEnd));
            }

            foreach (ICell NeighborNode in CurrentNode.GetNearestCells())
            {
                CellNode _target  = NeighborNode.GetNode();
                CellNode _current = CurrentNode.GetNode();

                if (CellBase.CompareStates(NeighborNode.GetState(), CellBase.State.Obstacle) ||
                    ClosedList.Contains(NeighborNode))
                {
                    continue;
                }

                int MoveCost = _current.igCost + GetManhattenDistance(_current, _target);

                if (MoveCost < _target.igCost || !OpenList.Contains(NeighborNode))
                {
                    NeighborNode.GetNode().igCost     = MoveCost;
                    NeighborNode.GetNode().ihCost     = GetManhattenDistance(_target, cellEnd.GetNode());
                    NeighborNode.GetNode().ParentNode = CurrentNode;

                    if (!OpenList.Contains(NeighborNode))
                    {
                        OpenList.Add(NeighborNode);
                    }
                }
            }
        }

        return(null);
    }