예제 #1
0
 def start (self):
     """
      Called when the schedule is first assigned
     """
     # -- get map assigned to this schedule
     map = world.area_manager.get_map ()
     # -- get random x coordinate
     x = random.randint (20, map.length () - 20)
     # -- get random y coordinate
     y = random.randint (20, map.height () - 20)
     # -- create target vector
     target = world.vector3i (x, y, 0)
     # -- get pathfinder instance
     pathfinder = world.area_manager.get_pathfinder()
     # -- start path finding task
     self.task = pathfinder.add_task (self.chr, target)
     # -- get notification when goal has been reached
     pathfinder.set_callback (self.task, self.on_arrived)
예제 #2
0
 def start(self):
     """
      Called when the schedule is first assigned
     """
     # -- get map assigned to this schedule
     map = world.area_manager.get_map()
     # -- get random x coordinate
     x = random.randint(20, map.length() - 20)
     # -- get random y coordinate
     y = random.randint(20, map.height() - 20)
     # -- create target vector
     target = world.vector3i(x, y, 0)
     # -- get pathfinder instance
     pathfinder = world.area_manager.get_pathfinder()
     # -- start path finding task
     self.task = pathfinder.add_task(self.chr, target)
     # -- get notification when goal has been reached
     pathfinder.set_callback(self.task, self.on_arrived)
     sys.stdout.write("Walking ")
     sys.stdout.flush()
예제 #3
0
def get_objects (chr, radius, arc, type):
    """
     Return a sorted list of objects of the given type that
     are in the given radius and arc from the given characters center
     and the direction the character is facing. The objects in the
     list are of type world.chunk_info, which allows access to object
     coordinate, unique id and actual object instance.
     
     @param chr the character whose surroundings to check
     @param radius the distance to check for objects
     @param arc an angle between 0 and 360 degree
     @param type the type(s) of objects to return
     
     @return list of objects in given location.
    """
    # -- center of the character
    chr_x = chr.x() + chr.length()/2
    chr_y = chr.y() + chr.width()/2

    # -- get bbox enclosing area of effect around the character
    min = world.vector3i (chr_x - radius, chr_y - radius, chr.z())
    max = world.vector3i (chr_x + radius, chr_y + radius, chr.z() + chr.height())
        
    # -- get list of characters and items enclosed by bbox
    object_list = chr.map().objects_in_bbox (min, max, type)

    # -- get start and end of the area of effect
    end = arc/2 + _angle_of_direction[chr.heading ()]
    start = end - arc

    result_list = []

    # -- get uid to exclude this character from result list
    name = chr.uid()

    # -- find objects actually in the area of effect
    for chunk_data in object_list:
        # -- get the map entity
        entity = chunk_data.get_entity()
        # -- do not consider ourselves
        if entity.id() == name: continue
        
        # -- the actual object represented by the entity
        object = entity.get_object()
        
        # -- line between object and character
        px = chunk_data.Min.x() + object.length()/2 - chr_x
        py = chunk_data.Min.y() + object.width()/2  - chr_y
        
        # -- calculate angle between line and x-axis
        angle = math.degrees (math.atan2 (py, px))

        # -- if facing west, angle might be around 180/-180 
        if chr.heading() == world.character.WEST:
            angle = abs(angle)
        
        # -- check if angle is in given arc
        if angle >= start and angle <= end:
            # -- the distance between character and object
            distance = math.hypot (px, py)
            # -- proximity of object to the direction the character is facing
            direction = math.fabs ((start + end)/2 - angle)
            # -- add tuple of sorting criteria and object to result list
            result_list.append ((distance * direction, direction, chunk_data))

    # -- sort result list by sorting criteria
    result_list.sort()
    # -- strip sorting criteria from result
    return [chunk_data for ignore, ignore, chunk_data in result_list]