import json import random import os import pygame from client_local.algo import aStar from client_local.const import color as C from client_local.const import game as G from client_local.frame import bomb, item from client_local.level import level from client_local.sound import sound_player from client_local.sprite.bomb_instance import BombInstance from client_local.sprite.ghost import Ghost from client_local.sprite.hero import Hero from client_local.sprite.item_instance import ItemInstance from client_local.sprite.player import Player, PlayerState from client_local.sprite.throwable import Throwable from client_local.file_loader.fileLoader import load_json, load_png import logging from datetime import datetime class Pet(Player, Throwable): def __init__(self, npc_name, xy, color=C.CHARACTER_RED): super(Pet, self).__init__(npc_name, xy, color) self.npc_json = None self.chs_name = None # Npc中文名 self.stats_name = npc_name self.idx_motion = {0: "R", 1: "U", 2: "L", 3: "D"} self.contact = 0 # Npc接触伤害 self.resent_dist = None # Npc仇恨范围(几格以内) self.resentful = False # Npc玩家仇恨 self.mocking = False # Npc受到嘲笑药水的作用暂时仇恨 self.friendly = False # Npc受到玫瑰药水的作用暂时解除仇恨 self.refresh = 0 self.is_ghost = True # 幽灵模式 self.fake_blood = 0 self.energy_amount = 0 # 充能格数 self.max_energy_amount = 0 self.has_energy = False self.wall_walking = True # 穿墙 self.flying = True # 飞行(免疫墙体减速) self.sliding = False # 踩技能滑行中 self.sliding_direction = None # 踩技能后滑的方向 self.witch_protected_remove = False # 巫师无防护状态 self.fire_load_time = 0 self.slime = False self.last_in_water = 0 self.slime_time = 0 self.free_chasing = False self.chasing_point = (1,1) self.imitating = False self.combo_num = 1 self.is_combo_target = False self.bomb_shield_num = 0 # 防糖泡电磁护盾 self.bomb_shield_protect = False self.is_target = False self.light = 0 # 鬼屋模式发光大小 0不发光,1发光20像素,2发光40像素 最大9 self.sharing_damage = False self.obstacle_can_push = False self.npc_time_init = None # Npc初始化时刻 self.wander_random = 10 # Npc在无仇恨时的闲逛时间 self.district_locked = True self.chase_path = None # Npc追踪路径 self.npc_skill_time = pygame.time.get_ticks() # npc施放技能时的时刻 设为成员变量是为了统一多个技能的时间 self.chs_name_bg = load_png("res/img/ui/game/misc172.png") # Npc姓名牌背景 self.blood_bar = None self.blood_bar_pos = 0 self.profile = None self.chain = False self.chase_rad = 2 self.chs_name_font = pygame.font.Font("res/font/simsun.ttc", 13) # Npc姓名牌字体 self.chs_name_text = None # Npc姓名牌前景文字 self.chs_name_text_shadow = None # Npc姓名牌背景文字 self.chs_name_text_half_width = None # Npc姓名牌文字的半宽 用于居中显示文字 self.chs_name_show = True # 是否显示名牌 self.bomb_skin = bomb.get_bomb("bomb1") self.gifts = None # 爆装备 self.death = None # 死亡状态 self.last_motion = 0 self.last_random = 10 self.energy_num = None self.load_npc(npc_name, color) def load_npc(self, npc_name, color): # 加载npc的json文件 self.npc_json = load_json("game/pet/" + npc_name + ".json") self.chs_name = self.npc_json["chs_name"] self.blood = self.npc_json["blood"] self.light = self.npc_json.get("light",self.light) if "self_damage_blood" in self.npc_json.keys(): self.self_damage_blood = self.npc_json["self_damage_blood"] self.speed = (max(2.5, self.npc_json["speed"]) * G.GAME_SQUARE) / 1000 self.contact = self.npc_json["contact"] self.defense = self.npc_json["defense"] if "slime" in self.npc_json.keys(): self.slime = self.npc_json["slime"] if "gifts" in self.npc_json.keys(): self.gifts = self.npc_json["gifts"] if "death" in self.npc_json.keys(): self.death = self.npc_json["death"] if "show_name" in self.npc_json.keys(): self.chs_name_show = self.npc_json["show_name"] if "light" in self.npc_json.keys(): self.light = self.npc_json["light"] if "chain" in self.npc_json.keys(): self.chain = self.npc_json["chain"] if "chase_rad" in self.npc_json.keys(): self.chase_rad = self.npc_json["chase_rad"] self.resent_dist = self.npc_json["resent_dist"] * G.GAME_SQUARE # 仇恨距离(像素) for n, s in enumerate(self.npc_json["skills"]): self.skill_names.append(s["name"]) self.skill_init_times.append(s["init"]) self.skill_intervals.append(s["interval"]) self.skill_remains.append(s["max"]) self.skill_params.append(s["params"] if "params" in s else []) self.skill_params_order.append(n) self.skill_spawns.append(s["spawns"] if "spawns" in s else []) self.skill_spawn_order.append(s["spawn_order"] if "spawn_order" in s else n) self.remain_blood = self.blood self.chase_path = dict() self.chs_name_text = self.chs_name_font.render(self.chs_name, True, (255, 128, 192)) self.chs_name_text_shadow = self.chs_name_font.render(self.chs_name, True, (0, 0, 0)) self.chs_name_text_half_width = self.chs_name_text.get_width() // 2 self.character = self.load_character(self.npc_json["character"], color, dict()) self.npc_time_init = pygame.time.get_ticks() def update(self): super().update() current_time = pygame.time.get_ticks() # self.update_npc_blood_display(current_time) self.chase_hero() # self.try_using_skills() def update_npc_blood_display(self, current_time): if G.DISPLAY_NPC_NAME_CARD and G.DISPLAY_NPC_BLOOD: self.chs_name_text = self.chs_name_font.render(str(self.remain_blood-self.fake_blood), True, (255, 128, 192)) self.chs_name_text_shadow = self.chs_name_font.render(str(self.remain_blood-self.fake_blood), True, (0, 0, 0)) self.chs_name_text_half_width = self.chs_name_text.get_width() // 2 else: self.chs_name_text = self.chs_name_font.render(self.chs_name, True, (255, 128, 192)) self.chs_name_text_shadow = self.chs_name_font.render(self.chs_name, True, (0, 0, 0)) self.chs_name_text_half_width = self.chs_name_text.get_width() // 2 def chase_hero(self): me = (level.current_level.me.x, level.current_level.me.y) if abs(self.x - me[0]) + abs(self.y - me[1]) <= self.chase_rad: self.set_motion() return now = (self.x, self.y) # 追踪玩家 self.chase_path = aStar.cal_path( now, me, (level.current_level.district_square_grid["x1"], level.current_level.district_square_grid["y1"]), (level.current_level.district_square_grid["x2"], level.current_level.district_square_grid["y2"]), 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 half_body_damage(self, point, cl, current_time): self.try_damage(cl.grid_damage_blood[point]) def die(self): super().die() sound_player.play("npc_dead") def try_push(self, direction, offset=(0, 0)): pass def try_using_skills(self): pass def use_skill(self, idx: int): cl = level.current_level if self.npc_skill_time < self.skill_init_times[idx] or self.skill_remains[idx] == 0: # 未到初始时间 或技能冷却中 或次数已用完(-1则可以无限使用) return def set_restoration(self, to_x, to_y): self.x = to_x self.y = to_y def draw(self, screen: pygame.Surface): super().draw(screen) if G.DISPLAY_NPC_NAME_CARD and self.chs_name_show: self.chs_name_text_shadow.set_alpha(self.temporary_alpha) screen.blit(self.chs_name_text_shadow, (self.x_pos + 3 - self.chs_name_text_half_width, self.y_pos - 75)) self.chs_name_text.set_alpha(self.temporary_alpha) screen.blit(self.chs_name_text, (self.x_pos + 2 - self.chs_name_text_half_width, self.y_pos - 75)) if self.chain: pygame.draw.line(screen, (155, 155, 155), (level.current_level.me.x_pos, level.current_level.me.y_pos), (self.x_pos, self.y_pos), 1)