示例#1
0
    def followBestPath(self):
        """ 
        This function tells the NPC to continue following the best path 
        
        Basically, it checks the currentTarget to determine if we're already seeking to the correct waypoint.
        When we finally reach the currentTarget, we pop it off the bestPath list and set the currentTarget
        to the next waypoint in bestPath.
        
        At this point, we also need to re-run AStar from our new currentTarget to the destination, which is
        bestPath[-1]. We store as our new bestPath and continue from there.
        """

        assert self.bestPath, "self.bestPath must be valid before calling followBestPath"

        if self.currentTarget is not self.bestPath[0]:
            self.currentTarget = self.bestPath[0]

        #Comment out next two lines to disable path smoothening.
        if (self.pathSmoothening):
            #attempting to smoothen path
            #print("Checking if there is a clear path to next target")
            while (len(self.bestPath) > 1
                   and PathFinder.waypointIsReachable(self, self.bestPath[1])):
                #print("Next waypoint is reachable, skipping to next")
                self.bestPath.pop(0)
                self.currentTarget = self.bestPath[0]
        # have we reached our currentTarget?
        if PathFinder.distance(
                self, self.currentTarget
        ) < 2:  #This number must be greater than distance in seek()
            assert self.currentTarget == self.bestPath.pop(
                0
            ), "We've reached our currentTarget, but it's not in our bestPath"
            # Are there any waypoints left to follow?
            if self.bestPath:
                self.currentTarget = self.bestPath[0]
            if len(self.bestPath) > 1:
                self.bestPath = PathFinder.AStar(self.bestPath[0],
                                                 self.bestPath[-1],
                                                 self.waypoints)
示例#2
0
 def start(self):
     self.bestPath = PathFinder.AStar(self, self.mainTarget, self.waypoints)