コード例 #1
0
 public bool MoveTo(Node n)
 {
     if (n == null || n.traverseCost >= 100)
     {
         return(false);
     }
     if (!(transform.tag == "Player" && Player.Instance.GetState() == Player.PlayerState.Ghosting))
     {
         if (n.occupied)
         {
             return(false);
         }
         if (myNode != null)
         {
             myNode.occupied = false;
         }
         else
         {
             grid.NodeFromPosition(transform.position).occupied = false;
         }
         n.occupied = true;
     }
     StartCoroutine(SmoothMove(this.transform.position, n.gameObject.transform.position));
     myNode = n;
     return(true);
 }
コード例 #2
0
    public Node FindStep(Vector3 a_MyPos, Vector3 a_TargetPos)
    {
        StartNode  = grid.NodeFromPosition(a_MyPos);
        TargetNode = grid.NodeFromPosition(a_TargetPos);

        List <Node>    OpenList   = new List <Node>();
        HashSet <Node> ClosedList = new HashSet <Node>();

        OpenList.Add(StartNode);

        while (OpenList.Count > 0)
        {
            Node CurrentNode = OpenList[0];
            for (int i = 1; i < OpenList.Count; i++)
            {
                if (OpenList[i].FCost < CurrentNode.FCost || OpenList[i].FCost == CurrentNode.FCost && OpenList[i].hCost < CurrentNode.hCost)
                {
                    CurrentNode = OpenList[i];
                }
            }
            OpenList.Remove(CurrentNode);
            ClosedList.Add(CurrentNode);

            if (CurrentNode == TargetNode)
            {
                List <Node> bestPath = GetFinalPath(StartNode, TargetNode);
                if (bestPath.Count > 0)
                {
                    return(bestPath[0]);
                }
                else
                {
                    return(null);
                }
            }
            foreach (Node NeighborNode in grid.GetNeighboringNodes(CurrentNode))
            {
                if ((NeighborNode.occupied && !NeighborNode == TargetNode) || NeighborNode.traverseCost >= 100 || ClosedList.Contains(NeighborNode))
                {
                    continue;
                }
                float MoveCost = CurrentNode.gCost + NeighborNode.traverseCost + GetManhattenDistance(CurrentNode, NeighborNode);
                if (MoveCost < NeighborNode.gCost || !OpenList.Contains(NeighborNode))
                {
                    NeighborNode.gCost  = MoveCost;
                    NeighborNode.hCost  = GetManhattenDistance(NeighborNode, TargetNode);
                    NeighborNode.Parent = CurrentNode;

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