def perform(self) -> None: ''' - If not in the player's vision, wait until next turn. - If the player is right next to the enemy, attack the player. - If the player sees the enemy, but the enemy is too far away to attack, move closer to the player. ''' target = self.engine.player dx = target.x - self.entity.x dy = target.y - self.entity.y distance = max(abs(dx), abs(dy)) # "Chevyshev" distance if self.engine.game_map.visible[self.entity.x, self.entity.y]: if distance <= 1: return MeleeAction(self.entity, dx, dy).perform() # Update the enemy's path to chase after the player self.path = self.get_path_to(target.x, target.y) if self.path: dest_x, dest_y = self.path.pop(0) return MovementAction(self.entity, dest_x - self.entity.x, dest_y - self.entity.y).perform() # Wait until the next turn return WaitAction(self.entity).perform()
def get_melee_action(self, dx, dy): """ Return the action this ai will perform when its melee attacking something. If the ai has any sort of special effects to its melee attack, its passed as a parameter. """ return MeleeAction(self.parent, dx, dy, self.melee_effects, self.melee_effects_var).perform()
def perform(self) -> None: target = self.engine.player dx = target.x - self.entity.x dy = target.y - self.entity.y distance = max(abs(dx), abs(dy)) # Chebyshev distance. if self.engine.game_map.visible[self.entity.x, self.entity.y]: if distance <= 1: return MeleeAction(self.entity, dx, dy).perform() self.path = self.get_path_to(target.x, target.y) if self.path: dest_x, dest_y = self.path.pop(0) return MovementAction(self.entity, dest_x - self.entity.x, dest_y - self.entity.y).perform() return WaitAction(self.entity).perform()
def perform(self) -> None: target = self.engine.player dx = target.x - self.entity.x dy = target.y - self.entity.y distance = max(abs(dx), abs(dy)) # Расстояние Чебышёва (максимум модуля разности компонент n-мерных числовых векторов) if self.engine.game_map.visible[self.entity.x, self.entity.y]: if distance <= 1: return MeleeAction(self.entity, dx, dy).perform() self.path = self.get_path_to(target.x, target.y) if self.path: dest_x, dest_y = self.path.pop(0) return MovementAction( self.entity, dest_x - self.entity.x, dest_y - self.entity.y, ).perform() return WaitAction(self.entity).perform()
def take_turn(self, engine: Engine, target: Entity) -> Action: dx = target.x - self.parent.x dy = target.y - self.parent.y distance = math.sqrt(dx**2 + dy**2) dx = int(round(dx / distance)) dy = int(round(dy / distance)) action: Action = WaitAction(engine, self.parent) if engine.game_map.visible[self.parent.x, self.parent.y]: if distance >= 2: destination_x, destination_y = self.parent.get_first_step_towards_destination( target.x, target.y, engine.game_map) action = MovementAction(engine, self.parent, destination_x - self.parent.x, destination_y - self.parent.y) else: action = MeleeAction(engine, self.parent, dx, dy) return action
def perform(self) -> None: target = self.engine.player dx = target.x - self.entity.x dy = target.y - self.entity.y distance = max(abs(dx), abs(dy)) # Chebyshev distance. # when the player is visible: if near player, attack!, if not, move towards it. if self.engine.game_map.visible[self.entity.x, self.entity.y]: if distance <= 1: return MeleeAction(self.entity, dx, dy).perform() self.path = self.get_path_to(target.x, target.y) # if there is a path, move one step. if self.path: dest_x, dest_y = self.path.pop(0) return MovementAction( self.entity, dest_x - self.entity.x, dest_y - self.entity.y, ).perform() return WaitAction(self.entity).perform()
def perform(self) -> None: """Does what an hostile enemy does: when you see the player, move towards it and attack when in range""" target = self.engine.player dx = target.x - self.entity.x dy = target.y - self.entity.y distance = max(abs(dx), abs(dy)) # Chebyshev distance. if self.engine.game_map.visible[self.entity.x, self.entity.y]: if distance <= 1: return MeleeAction(self.entity, dx, dy).perform() self.path = self.get_path_to(target.x, target.y) if self.path: dest_x, dest_y = self.path.pop(0) return MovementAction( self.entity, dest_x - self.entity.x, dest_y - self.entity.y, ).perform() return WaitAction(self.entity).perform()
def perform(self): target = self.engine.player dir_x = target.x - self.entity.x dir_y = target.y - self.entity.y distance = max( abs(dir_x), abs(dir_y)) # https://en.wikipedia.org/wiki/Chebyshev_distance # Prøve at angribe if self.engine.game_map.visible[self.entity.x, self.entity.y]: if distance <= 1: # Hvis distancen mellem entity og target er 1 eller mindre (de rører hinanden) return MeleeAction(self.entity, dir_x, dir_y).perform() self.path = self.get_path_to(target.x, target.y) if self.path: # Hvis entity har en path dest_x, dest_y = self.path.pop(0) # Fjern den ældste entry return MovementAction( self.entity, dest_x - self.entity.x, dest_y - self.entity.y, ).perform() return WaitAction(self.entity).perform()