def perform(self): moves = (1, 0), (-1, 0), (0, 1), (0, -1) number_of_moves = random.randint(1, 3) batched_move = action.BatchedAction(self.performer) for _ in range(number_of_moves): move_action = moveaction.MoveAction(self.performer, moves[random.randint(0, 3)]) move_action.parent = batched_move self.performer.brain.add_action(move_action) self.performer.brain.add_action(batched_move)
def tick(self, tick): if self.threat and self.threat.position: # Neighboring tiles possible_tiles = [utils.math.add(self.owner.position, d) for d in helpers.DirectionHelper.directions] # Possible tiles possible_tiles = [d for d in possible_tiles if instances.scene_root.check_collision(*d)] # Determine furthest tiles possible_tiles = sorted(possible_tiles, key=lambda x: utils.math.distance(x, self.threat.position), reverse=True) direction = utils.math.sub(possible_tiles[0], self.owner.position) act = moveaction.MoveAction(self.owner, direction) self.brain.add_action(act)
def tick(self, tick): if self.owner.weapon.isinstance('Spear'): # Are we lined up for a throw? if self.current_target.x == self.owner.x or self.current_target.y == self.owner.y: # Are we in range? dist = utils.math.distance(self.owner.position, self.current_target.position) if dist <= self.owner.weapon.throw_distance and dist > 1: dir = utils.math.sub(self.current_target.position, self.owner.position) dir = utils.math.normalize(dir) dir = tuple(map(lambda i: int(i), dir)) s = self.owner.weapon s.remove() s.position = utils.math.add(self.owner.position, dir) instances.scene_root.append(s) self.owner.equip_weapon(fist.Fist()) act = throwaction.ThrowAction(self.owner, s) self.brain.add_action(act) self.brain.add_action(action.IdleAction(self.owner)) return # Attempt to line up a throw elif not self.brain.actions: dir = utils.math.sub(self.current_target.position, self.owner.position) if abs(dir[0]) < abs(dir[1]): dir = dir[0], 0 else: dir = 0, dir[1] dir = utils.math.normalize(dir) dir = tuple(map(lambda i: int(i), dir)) act = moveaction.MoveAction(self.owner, dir) self.brain.add_action(act) return # Attack enemy if self.aggro_counter <= 0: self.brain.add_action( movetoaction.MoveToAction(self.owner, self.current_target.position, self.brain.owner.sight_radius)) self.aggro_counter = self.aggro_cooldown self.aggro_counter -= 1 return # Go get our spear elif not self.brain.actions: s = [ e for e in self.owner.visible_entities if e.isinstance('Spear') ] if s: s = sorted(s, key=lambda t: utils.math.distance( self.owner.position, t.position))[0] act = movetoaction.MoveToAction(self.owner, s.position) self.brain.add_action(act) self.brain.add_action(action.IdleAction(self.owner)) return # Attack enemy if self.aggro_counter <= 0: self.brain.add_action( movetoaction.MoveToAction(self.owner, self.current_target.position, self.brain.owner.sight_radius)) self.aggro_counter = self.aggro_cooldown self.aggro_counter -= 1
def move_to_action(performer, move): if move in DirectionHelper.dir_map: return moveaction.MoveAction( performer, DirectionHelper.get_direction(move.upper())) return None