def control_npc(self, game_state: GameState, npc: NonPlayerCharacter, player_entity: WorldEntity, is_player_invisible: bool, time_passed: Millis): if npc.stun_status.is_stunned(): return self._time_since_attack += time_passed self._time_since_updated_path += time_passed self._time_since_reevaluated += time_passed enemy_entity = npc.world_entity target: EnemyTarget = get_target(enemy_entity, game_state) if self._time_since_updated_path > self._update_path_interval: self._time_since_updated_path = 0 if not is_player_invisible: self.pathfinder.update_path_towards_target( enemy_entity, game_state, target.entity) new_next_waypoint = self.pathfinder.get_next_waypoint_along_path( enemy_entity) should_update_waypoint = self.next_waypoint != new_next_waypoint if self._time_since_reevaluated > self._reevaluate_next_waypoint_direction_interval: self._time_since_reevaluated = 0 should_update_waypoint = True if should_update_waypoint: self.next_waypoint = new_next_waypoint if self.next_waypoint: direction = self.pathfinder.get_dir_towards_considering_collisions( game_state, enemy_entity, self.next_waypoint) _move_in_dir(enemy_entity, direction) else: enemy_entity.set_not_moving() if self._time_since_attack > self._attack_interval: if not is_player_invisible: enemy_position = enemy_entity.get_center_position() target_center_pos = target.entity.get_center_position() if is_x_and_y_within_distance(enemy_position, target_center_pos, 200): self._time_since_attack = 0 self.randomize_attack_interval() play_sound(SoundId.ENEMY_ATTACK_ICE_WITCH) damage = random.randint(DAMAGE_MIN, DAMAGE_MAX) deal_npc_damage(damage, DamageType.MAGIC, game_state, enemy_entity, npc, target) game_state.game_world.visual_effects += [ (VisualLine((100, 100, 200), enemy_position, target_center_pos, Millis(120), 3)), (VisualLine((150, 150, 250), enemy_position, target_center_pos, Millis(240), 2)) ] chance_to_resist_slow = game_state.player_state.get_effective_movement_impairing_resist_chance( ) # TODO It's error-prone that we have to check this for every negative debuff that can slow player if random.random() > chance_to_resist_slow: game_state.player_state.gain_buff_effect( get_buff_effect(SLOW_BUFF_TYPE), Millis(1500))
def _apply_ability(game_state: GameState) -> AbilityResult: player_entity = game_state.game_world.player_entity previous_position = player_entity.get_center_position() used_from_stealth = game_state.player_state.has_active_buff( BuffType.STEALTHING) for distance in range(40, 200, 10): new_position = translate_in_direction( (player_entity.x, player_entity.y), player_entity.direction, distance) if game_state.game_world.is_position_within_game_world(new_position) \ and not game_state.game_world.would_entity_collide_if_new_pos(player_entity, new_position): if _would_collide_with_wall(game_state, player_entity, distance): return AbilityFailedToExecute(reason="Wall is blocking") should_regain_mana_and_cd = False enemy_hit = _get_enemy_that_was_hit(game_state, player_entity, distance) if enemy_hit: game_state.camera_shake = CameraShake(Millis(50), Millis(150), 4) deal_player_damage_to_enemy(game_state, enemy_hit, DAMAGE, DamageType.MAGIC) has_reset_upgrade = game_state.player_state.has_upgrade( HeroUpgradeId.ABILITY_DASH_KILL_RESET) enemy_died = enemy_hit.health_resource.is_at_or_below_zero() if has_reset_upgrade and enemy_died: should_regain_mana_and_cd = True player_entity.set_position(new_position) new_center_position = player_entity.get_center_position() color = (250, 140, 80) game_state.game_world.visual_effects.append( VisualCircle(color, previous_position, 17, 35, Millis(150), 1)) game_state.game_world.visual_effects.append( VisualLine(color, previous_position, new_center_position, Millis(250), 2)) game_state.game_world.visual_effects.append( VisualRect(color, previous_position, 37, 46, Millis(150), 1)) game_state.game_world.visual_effects.append( VisualCircle(color, new_center_position, 25, 40, Millis(300), 1, player_entity)) has_speed_upgrade = game_state.player_state.has_upgrade( HeroUpgradeId.ABILITY_DASH_MOVEMENT_SPEED) if has_speed_upgrade: game_state.player_state.gain_buff_effect( get_buff_effect(BUFF_SPEED), BUFF_SPEED_DURATION) if used_from_stealth: game_state.player_state.gain_buff_effect( get_buff_effect(BUFF_FROM_STEALTH), BUFF_FROM_STEALTH_DURATION) return AbilityWasUsedSuccessfully( should_regain_mana_and_cd=should_regain_mana_and_cd) return AbilityFailedToExecute(reason="No space")
def control_npc(self, game_state: GameState, npc: NonPlayerCharacter, player_entity: WorldEntity, _is_player_invisible: bool, time_passed: Millis): if npc.stun_status.is_stunned(): return self._summon_trait.update(npc, game_state, time_passed) self._random_walk_trait.update(npc, game_state, time_passed) self._time_since_healing += time_passed self._time_since_shoot += time_passed if self._time_since_healing > self._healing_cooldown: self._time_since_healing = 0 self._healing_cooldown = self._random_healing_cooldown() necro_center_pos = npc.world_entity.get_center_position() nearby_hurt_enemies = [ e for e in game_state.game_world.non_player_characters if e.is_enemy and is_x_and_y_within_distance( necro_center_pos, e.world_entity.get_center_position(), 200) and e != npc and not e.health_resource.is_at_max() ] if nearby_hurt_enemies: healing_target = nearby_hurt_enemies[0] healing_target.health_resource.gain(5) healing_target_pos = healing_target.world_entity.get_center_position( ) visual_line = VisualLine((80, 200, 150), necro_center_pos, healing_target_pos, Millis(350), 3) game_state.game_world.visual_effects.append(visual_line) play_sound(SoundId.ENEMY_NECROMANCER_HEAL) if self._time_since_shoot > self._shoot_cooldown: self._time_since_shoot = 0 self._shoot_cooldown = self._random_shoot_cooldown() npc.world_entity.direction = get_directions_to_position( npc.world_entity, player_entity.get_position())[0] npc.world_entity.set_not_moving() center_position = npc.world_entity.get_center_position() distance_from_enemy = 35 projectile_pos = translate_in_direction( get_position_from_center_position(center_position, PROJECTILE_SIZE), npc.world_entity.direction, distance_from_enemy) projectile_speed = 0.2 projectile_entity = WorldEntity(projectile_pos, PROJECTILE_SIZE, Sprite.NONE, npc.world_entity.direction, projectile_speed) projectile = Projectile( projectile_entity, create_projectile_controller(PROJECTILE_TYPE)) game_state.game_world.projectile_entities.append(projectile) play_sound(SoundId.ENEMY_ATTACK_NECRO)
def _add_visual_lines_along_path(game_state, path): for i in range(len(path) - 1): current_pos = path[i] next_pos = path[i + 1] game_state.visual_effects.append( VisualRect((100, 150, 150), _get_middle_of_cell_from_position(current_pos), 7, 10, Millis(DEBUG_PATHFINDER_INTERVAL), 1)) game_state.visual_effects.append( VisualLine((250, 250, 250), _get_middle_of_cell_from_position(current_pos), _get_middle_of_cell_from_position(next_pos), Millis(DEBUG_PATHFINDER_INTERVAL), 1))
def strike_enemies(game_state: GameState, num_enemies: int): player_entity = game_state.game_world.player_entity player_center_position = player_entity.get_center_position() close_enemies = game_state.game_world.get_enemies_within_x_y_distance_of( 140, player_center_position) # TODO: sound effect for enemy in close_enemies[0:num_enemies]: damage_amount: float = MIN_DMG + random.random() * (MAX_DMG - MIN_DMG) deal_player_damage_to_enemy(game_state, enemy, damage_amount, DamageType.MAGIC) enemy_center_position = enemy.world_entity.get_center_position() game_state.game_world.visual_effects.append( VisualCircle((250, 250, 0), player_center_position, 50, 140, Millis(100), 1, player_entity)) game_state.game_world.visual_effects.append( VisualLine((250, 250, 0), player_center_position, enemy_center_position, Millis(80), 3))
def apply_middle_effect(self, game_state: GameState, time_passed: Millis): if self.timer.update_and_check_if_ready(time_passed): player_entity = game_state.player_entity player_center_position = player_entity.get_center_position() close_enemies = game_state.get_enemies_within_x_y_distance_of( 140, player_center_position) if close_enemies: damage_amount: float = self.min_dmg + random.random() * ( self.max_dmg - self.min_dmg) deal_player_damage_to_enemy(game_state, close_enemies[0], damage_amount, DamageType.MAGIC) enemy_center_position = close_enemies[ 0].world_entity.get_center_position() game_state.visual_effects.append( VisualCircle((250, 250, 0), player_center_position, 50, 140, Millis(100), 1, player_entity)) game_state.visual_effects.append( VisualLine((250, 250, 0), player_center_position, enemy_center_position, Millis(80), 3))
def control_npc(self, game_state: GameState, npc: NonPlayerCharacter, player_entity: WorldEntity, is_player_invisible: bool, time_passed: Millis): self._time_since_updated_path += time_passed self._time_since_reevaluated += time_passed self._time_since_attack += time_passed summon_entity = npc.world_entity if self._time_since_updated_path > self._update_path_interval: self._time_since_updated_path = 0 nearby_enemies = game_state.game_world.get_enemies_within_x_y_distance_of(300, npc.world_entity.get_position()) if nearby_enemies: target_entity = nearby_enemies[0].world_entity else: target_entity = game_state.game_world.player_entity self.pathfinder.update_path_towards_target(summon_entity, game_state, target_entity) new_next_waypoint = self.pathfinder.get_next_waypoint_along_path(summon_entity) should_update_waypoint = self.next_waypoint != new_next_waypoint if self._time_since_reevaluated > self._reevaluate_next_waypoint_direction_interval: self._time_since_reevaluated = 0 should_update_waypoint = True if should_update_waypoint: self.next_waypoint = new_next_waypoint if self.next_waypoint: direction = self.pathfinder.get_dir_towards_considering_collisions( game_state, summon_entity, self.next_waypoint) _move_in_dir(summon_entity, direction) else: summon_entity.set_not_moving() if self._time_since_attack > self._attack_interval: self._time_since_attack = 0 nearby_enemies = game_state.game_world.get_enemies_within_x_y_distance_of(100, summon_entity.get_position()) if nearby_enemies: damage_amount = 3 target = nearby_enemies[0] deal_npc_damage_to_npc(game_state, target, damage_amount) game_state.game_world.visual_effects.append( VisualLine((220, 0, 0), summon_entity.get_center_position(), target.world_entity.get_center_position(), Millis(100), damage_amount))
def _apply_teleport(game_state: GameState) -> AbilityResult: player_entity = game_state.player_entity previous_position = player_entity.get_center_position() new_position = translate_in_direction((player_entity.x, player_entity.y), player_entity.direction, 140) player_entity.set_position(new_position) new_center_position = player_entity.get_center_position() color = (140, 140, 230) game_state.visual_effects.append( VisualCircle(color, previous_position, 17, 35, Millis(150), 1)) game_state.visual_effects.append( VisualRect(color, previous_position, 37, 50, Millis(150), 1)) game_state.visual_effects.append( VisualLine(color, previous_position, new_center_position, Millis(200), 1)) game_state.visual_effects.append( VisualCircle(color, new_center_position, 25, 50, Millis(300), 2, player_entity)) return AbilityWasUsedSuccessfully()
def control_npc(self, game_state: GameState, npc: NonPlayerCharacter, player_entity: WorldEntity, is_player_invisible: bool, time_passed: Millis): self._time_since_attack += time_passed self._time_since_updated_path += time_passed self._time_since_reevaluated += time_passed self._time_since_shield += time_passed enemy_entity = npc.world_entity if self._time_since_updated_path > self._update_path_interval: self._time_since_updated_path = 0 self.pathfinder.update_path_towards_target( enemy_entity, game_state, game_state.player_entity) new_next_waypoint = self.pathfinder.get_next_waypoint_along_path( enemy_entity) should_update_waypoint = self.next_waypoint != new_next_waypoint if self._time_since_reevaluated > self._reevaluate_next_waypoint_direction_interval: self._time_since_reevaluated = 0 should_update_waypoint = True if should_update_waypoint: self.next_waypoint = new_next_waypoint if self.next_waypoint: direction = self.pathfinder.get_dir_towards_considering_collisions( game_state, enemy_entity, self.next_waypoint) if random.random() < 0.1 and direction: direction = random.choice( get_perpendicular_directions(direction)) _move_in_dir(enemy_entity, direction) else: enemy_entity.set_not_moving() enemy_center_pos = enemy_entity.get_center_position() if self._time_since_attack > self._attack_interval: self._time_since_attack = 0 game_state.visual_effects.append( VisualCircle((100, 100, 100), enemy_center_pos, 180, 180, Millis(self._attack_interval), 1, enemy_entity)) if not is_player_invisible: player_center_pos = game_state.player_entity.get_center_position( ) if is_x_and_y_within_distance(enemy_center_pos, player_center_pos, 160): deal_damage_to_player(game_state, 3, DamageType.MAGIC, npc) game_state.visual_effects += [ VisualCircle((0, 0, 0), enemy_center_pos, 25, 50, Millis(200), 2, enemy_entity), VisualLine((0, 100, 0), enemy_center_pos, player_center_pos, Millis(200), 2), VisualCircle((0, 100, 0), player_center_pos, 20, 40, Millis(150), 2, player_entity), VisualCircle((0, 150, 0), player_center_pos, 25, 50, Millis(200), 2, player_entity), VisualCircle((0, 200, 0), player_center_pos, 30, 60, Millis(300), 2, player_entity), ] if self._time_since_shield > self._shield_interval: self._time_since_shield = 0 game_state.visual_effects.append( VisualCircle((0, 0, 150), enemy_center_pos, 60, 20, Millis(self._shield_duration), 2, enemy_entity)) npc.gain_buff_effect(get_buff_effect(BUFF_TYPE_INVULN), Millis(self._shield_duration))
def _add_visual_line_to_next_waypoint(destination, agent_entity: WorldEntity, game_state: GameState): start = _get_middle_of_cell_from_position(agent_entity.get_position()) end = _get_middle_of_cell_from_position(destination) game_state.visual_effects.append( VisualLine((150, 150, 150), start, end, Millis(100), 2))
def control_npc(self, game_state: GameState, npc: NonPlayerCharacter, player_entity: WorldEntity, _is_player_invisible: bool, time_passed: Millis): self._time_since_decision += time_passed self._time_since_summoning += time_passed self._time_since_healing += time_passed self._time_since_shoot += time_passed if self._time_since_summoning > self._summoning_cooldown: necro_center_pos = npc.world_entity.get_center_position() self._time_since_summoning = 0 self._alive_summons = [summon for summon in self._alive_summons if summon in game_state.non_player_characters] if len(self._alive_summons) < 3: relative_pos_from_summoner = (random.randint(-150, 150), random.randint(-150, 150)) summon_center_pos = sum_of_vectors(necro_center_pos, relative_pos_from_summoner) summon_type = random.choice([NpcType.ZOMBIE, NpcType.MUMMY]) summon_size = NON_PLAYER_CHARACTERS[summon_type].size summon_pos = game_state.get_within_world( get_position_from_center_position(summon_center_pos, summon_size), summon_size) summon_enemy = create_npc(summon_type, summon_pos) is_wall_blocking = game_state.walls_state.does_rect_intersect_with_wall( rect_from_corners(necro_center_pos, summon_center_pos)) is_position_blocked = game_state.would_entity_collide_if_new_pos(summon_enemy.world_entity, summon_pos) if not is_wall_blocking and not is_position_blocked: self._summoning_cooldown = self._random_summoning_cooldown() game_state.add_non_player_character(summon_enemy) self._alive_summons.append(summon_enemy) game_state.visual_effects.append( VisualCircle((80, 150, 100), necro_center_pos, 40, 70, Millis(120), 3)) game_state.visual_effects.append( VisualCircle((80, 150, 100), summon_center_pos, 40, 70, Millis(120), 3)) play_sound(SoundId.ENEMY_NECROMANCER_SUMMON) else: # Failed to summon, so try again without waiting full duration self._summoning_cooldown = 500 else: self._summoning_cooldown = self._random_summoning_cooldown() if self._time_since_healing > self._healing_cooldown: self._time_since_healing = 0 self._healing_cooldown = self._random_healing_cooldown() necro_center_pos = npc.world_entity.get_center_position() nearby_hurt_enemies = [ e for e in game_state.non_player_characters if e.is_enemy and is_x_and_y_within_distance(necro_center_pos, e.world_entity.get_center_position(), 200) and e != npc and not e.health_resource.is_at_max() ] if nearby_hurt_enemies: healing_target = nearby_hurt_enemies[0] healing_target.health_resource.gain(5) healing_target_pos = healing_target.world_entity.get_center_position() visual_line = VisualLine((80, 200, 150), necro_center_pos, healing_target_pos, Millis(350), 3) game_state.visual_effects.append(visual_line) play_sound(SoundId.ENEMY_NECROMANCER_HEAL) if self._time_since_shoot > self._shoot_cooldown: self._time_since_shoot = 0 self._shoot_cooldown = self._random_shoot_cooldown() npc.world_entity.direction = get_directions_to_position(npc.world_entity, player_entity.get_position())[0] npc.world_entity.set_not_moving() center_position = npc.world_entity.get_center_position() distance_from_enemy = 35 projectile_pos = translate_in_direction( get_position_from_center_position(center_position, PROJECTILE_SIZE), npc.world_entity.direction, distance_from_enemy) projectile_speed = 0.2 projectile_entity = WorldEntity(projectile_pos, PROJECTILE_SIZE, Sprite.NONE, npc.world_entity.direction, projectile_speed) projectile = Projectile(projectile_entity, create_projectile_controller(PROJECTILE_TYPE)) game_state.projectile_entities.append(projectile) play_sound(SoundId.ENEMY_ATTACK_NECRO) if self._time_since_decision > self._decision_interval: self._time_since_decision = 0 if random.random() < 0.2: direction = random_direction() npc.world_entity.set_moving_in_dir(direction) else: npc.world_entity.set_not_moving()