import json import pygame import random from client_local.algo import aStar from client_local.const import game as G from client_local.const import color as C from client_local.level import level from client_local.sound import sound_player from client_local.sprite.npc import Npc from client_local.sprite.player import Player, PlayerState from client_local.file_loader.fileLoader import load_json, load_png # 常驻地图的怪 class Wanderer(Npc): def __init__(self, npc_name, xy, color=C.CHARACTER_RED, patrol_path=None): super(Wanderer, self).__init__(npc_name, xy, color, patrol_path=patrol_path or None) def update(self): super(Npc, self).update() if not self.is_dead: current_time = pygame.time.get_ticks() self.update_npc_blood_display(current_time) if self.has_patrol: cl = level.current_level self.patrol(((0, 0), (cl.map_x - 1, cl.map_y - 1))) else: self.wander_and_detect(current_time) self.chase_hero() self.contact_damage() self.try_using_skills() self.throw() def wander_and_detect(self, current_time): me = level.current_level.me swallow = me.swallow_attack if ((self.resentful or self.mocking) and not self.friendly and (swallow is None or (not swallow.require_aggro and swallow.user is not self))): return if current_time - self.npc_time_init < 300: return if (current_time - self.last_motion) >= 50 * self.last_random: if self.speed >= (5.8 * G.GAME_SQUARE) / 1000: self.last_random = random.randint(5, 15) if 0 <= (current_time % 1200) <= 300 or (random.randint(1, 100) >= 85): self.last_motion = current_time if random.randint(1, 100) >= 25: motion = self.idx_motion[random.randint(0, 3)] self.set_motion(motion) else: self.set_motion(self.orientation) else: self.set_motion(self.orientation) else: self.set_motion(self.orientation) if swallow is not None and swallow.user is self: return # 技能初始时间更新 for i in range(len(self.skill_names)): # 游荡状态下,技能初始时间永远等于当前时刻加init值 self.skill_init_times[i] = self.npc_json["skills"][i].get("init", 0) + current_time self.is_skill_init = True if me.state == PlayerState.NORMAL: if abs(me.x_pos - self.x_pos) < self.resent_dist and abs(me.y_pos - self.y_pos) < self.resent_dist and not me.is_hide_sight(): # 如果玩家活着且距离小于临界值 且不被障碍阻碍视野 则产生玩家仇恨 self.resentful = True level.current_level.recal_npc_paths = True def chase_hero(self): if (not self.resentful and not self.mocking) or self.friendly or self.imitating or (self.speed == 0 and self.original_speed == 0): return hero = level.current_level.me if hero.swallow_attack is not None and not (self.free_chasing or self.chasing_monster): if hero.swallow_attack.user is not self: self.set_motion() return me = (hero.x, hero.y) if self.free_chasing: me = self.chasing_point if self.chasing_monster and self.chasing_monster_target is not None: me = (self.chasing_monster_target.x, self.chasing_monster_target.y) # 玩家在糖泡中 定身Npc if len(level.current_level.get_bomb_instance(*me)) > 0 and not self.is_ghost and not self.free_chasing and not self.chasing_monster: self.set_motion() return now = (self.x, self.y) # 如果重新计算路径标志为True则重新追踪玩家 if level.current_level.recal_npc_paths: self.chase_path = aStar.cal_path( now, me, (max(0, level.current_level.me.x - 15), max(0, level.current_level.me.y - 15)), (min(G.CURRENT_MAP_WIDTH-1, level.current_level.me.x + 15), min(G.CURRENT_MAP_HEIGHT-1, level.current_level.me.y + 15)), is_ghost=self.is_ghost ) if now in self.chase_path.keys(): _next = self.chase_path[now] if _next is None: self.set_motion() elif _next[0] > self.x: self.set_motion("R") elif _next[1] < self.y: self.set_motion("U") elif _next[0] < self.x: self.set_motion("L") elif _next[1] > self.y: self.set_motion("D") else: self.set_motion() def die(self): super(Npc, self).die() sound_player.play("npc_dead") if self.blood_bar_pos != 0: try: level.current_level.ui_instances_top.remove(self.blood_bar) level.current_level.ui_instances_top.remove(self.profile) level.current_level.boss_blood_bar_dic[self.blood_bar_pos] = 0 except Exception as e: print(e) try: print("血条错误", self.chs_name, self.blood_bar_pos) except Exception as e: print(f"npc.py: die(): {e}") self.is_dead = True def die_quietly(self): super(Npc, self).die() if self.blood_bar_pos != 0: try: level.current_level.ui_instances_top.remove(self.blood_bar) level.current_level.ui_instances_top.remove(self.profile) level.current_level.boss_blood_bar_dic[self.blood_bar_pos] = 0 except: try: print("血条错误", self.chs_name, self.blood_bar_pos) except: pass self.is_dead = True self.remain_blood = -1 def handle_direction_movement(self, direction, speed): right, right_grid, top, top_grid, left, left_grid, bottom, bottom_grid = self.get_ruld_block() cl = level.current_level block = cl.block if direction == "R": self.movement_right(False, speed, top, bottom, right, block, top_grid, bottom_grid, cl) elif direction == "U": self.movement_up(False, speed, top, left, right, block, left_grid, right_grid, cl) elif direction == "L": self.movement_left(False, speed, top, bottom, left, block, top_grid, bottom_grid, cl) elif direction == "D": self.movement_down(False, speed, bottom, left, right, block, left_grid, right_grid, cl)