def decorator(character, target, characters_sight=None, max_prediction=1.0, *args, **kwargs): global LEVEL # First calculate the target to delegate the seek if hasattr(target, "position"): distance = (target.position - character.position).length else: distance = (target - character.position).length if characters_sight is not None and distance > characters_sight: return None if character.velocity.length <= distance / max_prediction: prediction = max_prediction else: prediction = distance / character.velocity.length # Now we tell seek to look after a target that have a # position = real_target.velocity * prediction ## ALERT: small modification, if the target.velocity == 0 we are ## pursuing/evading the target itself, not some delegate target which ## doesn't exists. if hasattr(target, "velocity") and target.velocity.length != 0: target_node = graph_quantization(target.position + (target.velocity * prediction)) else: if hasattr(target, "position"): target_node = graph_quantization(target.position) else: target_node = graph_quantization(target) a_result = LEVEL["graph"].path[character.node_position.id, target_node.id] # print 'pursuing', a_result['path'], target_node.id try: node = LEVEL["nodes"][a_result["path"][1]] except IndexError: return None return basic_behavior(character, node.location, **kwargs)
def update_position(self, game): """ Updates the position/area of a character. checks for: 0. resets all the forces applied to the character 1. collisions with some specific elements of the game. 2. hit with bullets 3. negative Y position 4. we're finally dead """ # Reset forces self.hitting_acceleration.length = 0 self.bullet_acceleration.length = 0 self.area.center = (self.position.x, self.position.z) if not game.stage.floor.area.contains(self.area): self.reset_velocity(game=game, wall=True) # For every stage obstacle for obstacle in game.stage.obstacles: distance = (self.position - obstacle.position).length if distance < self.radius + obstacle.radius and \ self.position.y < obstacle.height: self.reset_velocity(game=game, obj=obstacle) # For every other character check if they are colliding for ch in game.characters: if ch == self: continue distance = (self.position - ch.position).length if distance < self.radius + ch.radius and \ self.position.y < ch.position.y + ch.height and \ ch.position.y < self.position.y + self.height: self.reset_velocity(game=game, obj=ch) # Check for bullets hitting me self.check_for_bullets(game) # Check for negative position.y and corrects it if self.position.y < 0 and not self.dying: self.position.y = 0. self.velocity.y = 0. self.acceleration.y = 0. # Updates node_position using quantization self.node_position = graph_quantization(self.position) return self
def update(self, game): """ Updates the character's state and behavior. """ # Check if the character is dizzy if self.character.is_dizzy: if self._still_dizzy(): self.dizzy = True return self else: self.dizzy = False self.character.dizzy = None # Check if the character is hitted by some sound wave if self._check_sound_wave_collision(game.sound_wave): # Set dizzynes # First we need to calculate the level of dizzynes if game.sound_wave.intensity > self.character.hearing_umbral: dizzyness_level = 16000 * game.sound_wave.intensity / 100 self.character.dizzy = (pygame.time.get_ticks(), dizzyness_level) return self # 1. Updates character's behavior self._fuzzy_life(game) # Check if the enemy is super powerfull if game.main_character.is_kicking_asses: self.character.not_so_smart = True # Was I attacking before? if self.scared_state == S_STATE.attacking: self._fighting_tree(game) if self.fighting_state != F_STATE.hold: return self # Do I have a waypoint asigned? if self.character.waypoint_taken is None: # I have to cover quickly self.scared_state = S_STATE.uncovered return self # Am I covered yet? localization_node = graph_quantization(self.character.position) if self.character.waypoint_taken.main_node.id == localization_node.id and \ (self.character.waypoint_taken.main_node.location - \ self.character.position).length < 1: # Yes i'm covered self.scared_state = S_STATE.shaking # Do I hear steps that came from an uncover location? for step in game.main_character.steps: if self._check_sound_wave_collision(step['wave']) and \ step['wave'].intensity > self.character.hearing_umbral: node = graph_quantization(step['wave'].position) if node.id in self.character.waypoint_taken.uncover_from: # Yes I hear them self.scared_state = S_STATE.attacking return self else: # No, i'm not! # Is m enemy near to me? if (self.character.position - game.main_character.position).length < 7: self.scared_state = S_STATE.attacking else: self.scared_state = S_STATE.uncovered return self else: self.scared_state = S_STATE.none self.character.not_so_smart = False try: self.character.waypoint_taken.taken = None self.character.waypoint_taken = None except AttributeError: pass # NORMAL BEHAVIOR # 2. Fighting check self._fighting_tree(game) # 3. Moving support self._moving_support() return self