本文整理汇总了Python中horizons.util.python.weakmethodlist.WeakMethodList.execute方法的典型用法代码示例。如果您正苦于以下问题:Python WeakMethodList.execute方法的具体用法?Python WeakMethodList.execute怎么用?Python WeakMethodList.execute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类horizons.util.python.weakmethodlist.WeakMethodList
的用法示例。
在下文中一共展示了WeakMethodList.execute方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: MovingObject
# 需要导入模块: from horizons.util.python.weakmethodlist import WeakMethodList [as 别名]
# 或者: from horizons.util.python.weakmethodlist.WeakMethodList import execute [as 别名]
class MovingObject(ComponentHolder, ConcreteObject):
"""This class provides moving functionality and is to be inherited by Unit.
Its purpose is to provide a cleaner division of the code.
It provides:
*attributes:
- position, last_position: Point
- path: Pather
*moving methods:
- move
- stop
- add_move_callback
*getters/checkers:
- check_move
- get_move_target
- is_moving
"""
movable = True
log = logging.getLogger("world.units")
# overwrite this with a descendant of AbstractPather
pather_class = None # type: Type[AbstractPather]
def __init__(self, x, y, **kwargs):
super().__init__(x=x, y=y, **kwargs)
self.__init(x, y)
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 check_move(self, destination):
"""Tries to find a path to destination
@param destination: destination supported by pathfinding
@return: object that can be used in boolean expressions (the path in case there is one)
"""
return self.path.calc_path(destination, check_only=True)
def is_moving(self):
"""Returns whether unit is currently moving"""
return self.__is_moving
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 _setup_move(self, action='move'):
"""Executes necessary steps to begin a movement. Currently only the action is set."""
# try a number of actions and use first existent one
for action_iter in (action, 'move', self._action):
if self.has_action(action_iter):
self._move_action = action_iter
return
# this case shouldn't happen, but no other action might be available (e.g. ships)
self._move_action = 'idle'
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)
#.........这里部分代码省略.........
示例2: MovingObject
# 需要导入模块: from horizons.util.python.weakmethodlist import WeakMethodList [as 别名]
# 或者: from horizons.util.python.weakmethodlist.WeakMethodList import execute [as 别名]
class MovingObject(ComponentHolder, ConcreteObject):
"""This class provides moving functionality and is to be inherited by Unit.
Its purpose is to provide a cleaner division of the code.
It provides:
*attributes:
- position, last_position: Point
- path: Pather
*moving methods:
- move
- stop
- add_move_callback
*getters/checkers:
- check_move
- get_move_target
- is_moving
"""
movable = True
log = logging.getLogger("world.units")
pather_class = None # overwrite this with a descendant of AbstractPather
def __init__(self, x, y, **kwargs):
super(MovingObject, self).__init__(x=x, y=y, **kwargs)
self.__init(x, y)
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_coords = fife.ExactModelCoordinate() # save instance since construction is expensive (no other purpose)
self._fife_location = None
def check_move(self, destination):
"""Tries to find a path to destination
@param destination: destination supported by pathfinding
@return: object that can be used in boolean expressions (the path in case there is one)
"""
return self.path.calc_path(destination, check_only = True)
def is_moving(self):
"""Returns whether unit is currently moving"""
return self.__is_moving
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 _setup_move(self, action='move'):
"""Executes necessary steps to begin a movement. Currently only the action is set."""
# try a number of actions and use first existent one
for action_iter in (action, 'move', self._action):
if self.has_action(action_iter):
self._move_action = action_iter
return
# this case shouldn't happen, but no other action might be available (e.g. ships)
self._move_action = 'idle'
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)
#.........这里部分代码省略.........