def _process_ingredient(self, ingredients, on_done): player = self.engine.player sprite = Sprite(ingredients[0]) sprite.move_to(player.rect.left, player.rect.centery) self.layer_map["fg"].add(sprite) sprite.velocity = (0, 2) sprite.collidable = False sprite.moved.connect(lambda dx, dy: self._on_ingredient_moved(sprite, ingredients[1:], on_done)) Timer(ms=500, one_shot=True, cb=sprite.start)
def add_item(self, name, text): self.has_items[name] = False item = Sprite(name) item.move_to(*self.eventboxes[name].rects[0].topleft) self.layer_map['items'].add(item) self.connect_eventbox_enter( name, lambda: self._on_item_entered(name, text, item), True)
def show_exclamation(self, exclamation_type='exclamation', on_done=None): self.stop_wandering() self.stop_moving() self.exclamation = Sprite(exclamation_type) self.layer.add(self.exclamation) self.exclamation.move_to( self.rect.centerx - self.exclamation.rect.width / 2, self.rect.y - self.exclamation.rect.height) self.exclamation.start() Timer(ms=self.EXCLAMATION_MS, cb=lambda: self._on_exclamation_done(on_done), one_shot=True)
class ChaseMixin(object): APPROACH_DISTANCE = 250 CHASE_SPEED = 3 STOP_FOLLOWING_DISTANCE = 1000 SHOW_EXCLAMATION = True EXCLAMATION_MS = 700 FOLLOWING_KEY_NAME = "walking" def __init__(self, *args, **kwargs): super(ChaseMixin, self).__init__(*args, **kwargs) self.following = False self.exclamation = None def stop(self): super(ChaseMixin, self).stop() self.stop_following() def tick(self): super(ChaseMixin, self).tick() if self.started: # Figure out how close we are to the player. player = get_engine().player distance_x = abs(player.rect.x - self.rect.x) distance_y = abs(player.rect.y - self.rect.y) if (self.following and (distance_x >= self.STOP_FOLLOWING_DISTANCE or distance_y >= self.STOP_FOLLOWING_DISTANCE)): self.stop_following() if (self.following or (distance_x <= self.APPROACH_DISTANCE and distance_y <= self.APPROACH_DISTANCE and not self.following and (not self.SHOW_EXCLAMATION or not self.exclamation))): if not self.following and not self.exclamation: if self.SHOW_EXCLAMATION: # They haven't noticed the player before, but they # do now! self.show_exclamation(on_done=self.start_following) return else: self.start_following() x_dir = None y_dir = None if player.rect.x > self.rect.x: x = 1 x_dir = Direction.EAST elif player.rect.x < self.rect.x: x = -1 x_dir = Direction.WEST else: x = 0 if player.rect.y > self.rect.y: y = 1 y_dir = Direction.SOUTH elif player.rect.y < self.rect.y: y = -1 y_dir = Direction.NORTH else: y = 0 self.velocity = (x * self.CHASE_SPEED, y * self.CHASE_SPEED) if distance_x > distance_y: self.direction = x_dir elif distance_y > distance_x: self.direction = y_dir self.update_image() super(ChaseMixin, self).tick() def start_following(self): self.following = True self.stop_wandering() self.velocity = (0, 0) self.start_animation(self.FOLLOWING_KEY_NAME) def stop_following(self): self.following = False self.stop_moving() self.wander() def wander(self): pass def stop_wandering(self): pass def show_exclamation(self, exclamation_type='exclamation', on_done=None): self.stop_wandering() self.stop_moving() self.exclamation = Sprite(exclamation_type) self.layer.add(self.exclamation) self.exclamation.move_to( self.rect.centerx - self.exclamation.rect.width / 2, self.rect.y - self.exclamation.rect.height) self.exclamation.start() Timer(ms=self.EXCLAMATION_MS, cb=lambda: self._on_exclamation_done(on_done), one_shot=True) def _on_exclamation_done(self, on_done): if self.exclamation: self.exclamation.remove() self.exclamation = None if on_done: on_done()