コード例 #1
0
ファイル: Pathfinding.cs プロジェクト: Nemus91/AsciiShooter
 public void InitNeighbours()
 {
     for (int i = 0; i < 8; i++)
     {
         Neighbours[i] = new Node(GetNeighbourPosition((NeighbourPositions)i), null, Target);
     }
 }
コード例 #2
0
ファイル: Pathfinding.cs プロジェクト: Nemus91/AsciiShooter
        public static Vector2 GetNextField(Vector2 start, Vector2 target, Map map)
        {
            Node StartNode = new Node(start, null, target);
            StartNode.G = 0;
            Openlist.Add(StartNode);

            Node NextNode = StartNode;

            while (Openlist.Count > 0)
            {
                //Get Node with smallest heuristic Value
                Node closestNode = StartNode;
                foreach (Node NodesToCheck in Openlist)
                {
                    if (NodesToCheck.F < closestNode.F)
                        closestNode = NodesToCheck;
                }
                //Check if target has been reached
                if (closestNode.NodePosition == target)
                    break;
                //Transfer Node to ClosedList
                Openlist.Remove(closestNode);
                ClosedList.Add(closestNode);

                //CheckNeighbours
                closestNode.InitNeighbours();
                foreach (Node OpenListCandidate in closestNode.Neighbours)
                {
                    bool iswalkable = map.Field[OpenListCandidate.NodePosition.X, OpenListCandidate.NodePosition.Y] == ' ';
                    if (iswalkable && !ClosedList.Contains(OpenListCandidate))
                    {
                        if (!Openlist.Contains(OpenListCandidate))
                        {
                            Openlist.Add(OpenListCandidate);
                            OpenListCandidate.PreviousNode = closestNode;
                            OpenListCandidate.G = closestNode.G + 1;
                        }
                        else
                        {
                            if (closestNode.G + 1 < OpenListCandidate.G)
                            {
                                OpenListCandidate.PreviousNode = closestNode;
                                OpenListCandidate.G = closestNode.G + 1;
                            }
                        }
                    }
                }
            }

            //Follow Path
            while (NextNode.PreviousNode != null)
            {
                NextNode = NextNode.PreviousNode;
            }

            //ClearLists
            Openlist.Clear();
            ClosedList.Clear();

            //return NextField
            Vector2 NextField = NextNode.NodePosition;
            return NextField;
        }
コード例 #3
0
ファイル: Pathfinding.cs プロジェクト: Nemus91/AsciiShooter
 public Node(Vector2 Position, Node Previous, Vector2 TargetPosition)
 {
     NodePosition = Position;
     PreviousNode = Previous;
     Target = TargetPosition;
     H = Math.Abs((Position.X - TargetPosition.X) + (Position.Y - TargetPosition.Y));
 }