def move(self, destination, callback=None, destination_in_building=False, action='move', blocked_callback=None, path=None): """Moves unit to destination @param destination: Point or Rect @param callback: a parameter supported by WeakMethodList. Gets called when unit arrives. @param action: action as string to use for movement @param blocked_callback: a parameter supported by WeakMethodList. Gets called when unit gets blocked. @param path: a precalculated path (return value of FindPath()()) """ if not path: # calculate the path move_possible = self.path.calc_path(destination, destination_in_building) self.log.debug("%s: move to %s; possible: %s; is_moving: %s", self, destination, move_possible, self.is_moving()) if not move_possible: raise MoveNotPossible else: self.path.move_on_path(path, destination_in_building=destination_in_building) self.move_callbacks = WeakMethodList(callback) self.blocked_callbacks = WeakMethodList(blocked_callback) self._conditional_callbacks = {} self._setup_move(action) # start moving by regular ticking (only if next tick isn't scheduled) if not self.is_moving(): self.__is_moving = True # start moving in 1 tick # this assures that a movement takes at least 1 tick, which is sometimes subtly # assumed e.g. in the collector code Scheduler().add_new_object(self._move_tick, self)
def __init(self): self.__listeners = WeakMethodList() self.__remove_listeners = WeakMethodList() # number of event calls # if any event is triggered increase the number, after all callbacks are executed decrease it # if it reaches 0 it means that in the current object all event callbacks were executed self.__event_call_number = 0 self.__hard_remove = True
def stop(self, callback=None): """Stops a unit with currently no possibility to continue the movement. The unit actually stops moving when current move (to the next coord) is finished. @param callback: a parameter supported by WeakMethodList. is executed immediately if unit isn't moving """ if not self.is_moving(): WeakMethodList(callback).execute() return self.move_callbacks = WeakMethodList(callback) self.path.end_move()
def __init(self, x, y): self.position = Point(x, y) self.last_position = Point(x, y) self._next_target = Point(x, y) self.move_callbacks = WeakMethodList() self.blocked_callbacks = WeakMethodList() self._conditional_callbacks = {} self.__is_moving = False self.path = self.pather_class(self, session=self.session) self._exact_model_coords1 = fife.ExactModelCoordinate() # save instance since construction is expensive (no other purpose) self._exact_model_coords2 = fife.ExactModelCoordinate() # save instance since construction is expensive (no other purpose) self._fife_location1 = None self._fife_location2 = None
def clear_change_listeners(self): """Removes all change listeners""" self.__listeners = WeakMethodList()