def try_attack_target(self): """ Attacking loop """ if self._target is None: return if not self.attack_in_range(): destination = Annulus(self._target.position.center, self._min_range, self._max_range) not_possible_action = self.stop_attack # if target passes near self, attack! in_range_callback = self.try_attack_target # if executes attack action try to move in 1 second self._move_and_attack(destination, not_possible_action, in_range_callback) else: if self.is_moving() and self._fireable: # stop to shoot self.stop() # finish the move before removing the move tick self._movement_finished() # do not execute the next move tick Scheduler().rem_call(self, self._move_tick) distance = self.position.distance(self._target.position.center) dest = self._target.position.center if self._target.movable and self._target.is_moving(): dest = self._target._next_target fireable_number = len(self._fireable) self.fire_all_weapons(dest) move_closer = False # if no weapon was fired, because of holder positioned in dead range, move closer if self._fired_weapons_number == 0 and fireable_number != 0: # no weapon was fired but i could have fired weapons # check if i have weapons that could be shot from this position move_closer = True distance = self.position.center.distance( self._target.position.center) for weapon in self._weapon_storage: if weapon.check_target_in_range(distance): move_closer = False break if move_closer: destination = Annulus(self._target.position.center, self._min_range, self._min_range) self._move_and_attack(destination) else: Scheduler().add_new_object(self.try_attack_target, self, GAME_SPEED.TICKS_PER_SECOND)
def act_idle(self): """ If an enemy unit approaches move away from it """ unit = self.get_approaching_unit() if unit: try: distance = unit._max_range + self.lookout_distance self.instance.move(Annulus(unit.position.center, distance, distance + 2)) self.state = 'flee' except MoveNotPossible: pass
def _get_annulus(cls, position, range, range_delta): return Annulus(position, max(0, range - range_delta), range + range_delta)