npc.py 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  1. import random
  2. import pygame
  3. from game.skill.skill_map import skill_map
  4. from game.skill.skill import Protect005s, Protect3s
  5. from game.skill.npc_skill import Skill21018
  6. from game.algo import aStar
  7. from game.const import color as C
  8. from game.const import game as G
  9. from game.const import rouge as R
  10. from game.const import rouge_theme as T
  11. from game.frame import bomb, item
  12. from game.level import level
  13. from game.ui.game.blood_bar import BossBloodBar, BossHead
  14. from game.sound import sound_player
  15. from game.sprite.bomb_instance import BombInstance
  16. from game.sprite.ghost import Ghost
  17. from game.sprite.hero import Hero
  18. from game.sprite.item_instance import ItemInstance
  19. from game.sprite.player import Player, PlayerState
  20. from game.sprite.throwable import Throwable
  21. from game.file_loader.fileLoader import load_json, load_png
  22. # 配置logging基础配置并设置日志级别为INFO
  23. # logging.basicConfig(filename='npc.log', level=logging.INFO)
  24. origin_attr = {"blood": 1, "defense": 0, "contact": 1, "speed": 1}
  25. variant_attr = {"blood": 1.6, "defense": 2000, "contact": 1.2, "speed": 1.1}
  26. def get_variant_attr(is_var, a_name):
  27. if is_var:
  28. return variant_attr.get(a_name)
  29. return origin_attr.get(a_name)
  30. def render_digit(screen, number, x_pos, y_pos, alpha, offset=0):
  31. num_img = G.NUMBER_IMG[int(max(min(number, 9), 0))] # 获取对应的数字图片
  32. num_img.set_alpha(alpha) # 设置透明度
  33. screen.blit(num_img, (x_pos + offset, y_pos - 110)) # 渲染到屏幕
  34. class Npc(Player, Throwable):
  35. def __init__(self, npc_name, xy, color=C.CHARACTER_RED, variant=False):
  36. super(Npc, self).__init__(npc_name, xy, color)
  37. self.npc_json = None
  38. self.chs_name = None # Npc中文名
  39. self.stats_name = npc_name
  40. self.idx_motion = {0: "R", 1: "U", 2: "L", 3: "D"}
  41. self.contact = 0 # Npc接触伤害
  42. self.original_contact = 0
  43. self.maria_contact = 0
  44. self.resent_dist = None # Npc仇恨范围(几格以内)
  45. self.resentful = False # Npc玩家仇恨
  46. self.mocking = False # Npc受到嘲笑药水的作用暂时仇恨
  47. self.friendly = False # Npc受到玫瑰药水的作用暂时解除仇恨
  48. self.boss_mode = False # Boss模式
  49. self.refresh = 0
  50. self.original_speed = 0
  51. self.variant = variant
  52. self.isElite = False
  53. self.isBoss = False
  54. self.isSpawned = False
  55. self.isTreasure = False
  56. self.is_ghost = False # 幽灵模式
  57. self.fake_blood = 0
  58. self.energy_amount = 0 # 充能格数
  59. self.max_energy_amount = 0
  60. self.has_energy = False
  61. self.wall_walking = False # 穿墙
  62. self.flying = False # 飞行(免疫墙体减速)
  63. self.sliding = False # 踩技能滑行中
  64. self.sliding_direction = None # 踩技能后滑的方向
  65. self.witch_protected_remove = False # 巫师无防护状态
  66. self.fire_load_time = 0
  67. self.slime = False
  68. self.last_in_water = 0
  69. self.slime_time = 0
  70. self.free_chasing = False
  71. self.chasing_point = (1, 1)
  72. self.imitating = False
  73. self.combo_num = 1
  74. self.is_combo_target = False
  75. self.bomb_shield_num = 0 # 防糖泡电磁护盾
  76. self.bomb_shield_protect = False
  77. self.is_target = True
  78. self.light = 0 # 鬼屋模式发光大小 0不发光,1发光20像素,2发光40像素 最大9
  79. self.sharing_damage = False
  80. self.obstacle_can_push = False
  81. self.npc_time_init = None # Npc初始化时刻
  82. self.wander_random = 10 # Npc在无仇恨时的闲逛时间
  83. self.district_locked = True
  84. self.chase_path = None # Npc追踪路径
  85. self.npc_skill_time = pygame.time.get_ticks() # npc施放技能时的时刻 设为成员变量是为了统一多个技能的时间
  86. self.chs_name_bg = load_png(G.GAME_PATH + "\\" + "res/img/ui/game/misc171.png") # Npc姓名牌背景
  87. # self.chs_name_bg = pygame.image.load("res/img/ui/game/misc171.png").convert_alpha() # Npc姓名牌背景
  88. self.blood_bar = None
  89. self.blood_bar_pos = 0
  90. self.profile = None
  91. # self.head_cx = -100000
  92. # self.head_cy = -100000
  93. self.chs_name_font = pygame.font.Font(G.GAME_PATH + "\\" + "res/font/simsun.ttc", 13) # Npc姓名牌字体
  94. self.chs_name_text = None # Npc姓名牌前景文字
  95. self.chs_name_text_shadow = None # Npc姓名牌背景文字
  96. self.chs_name_text_half_width = None # Npc姓名牌文字的半宽 用于居中显示文字
  97. self.chs_name_show = True # 是否显示名牌
  98. self.bomb_skin = bomb.get_bomb("bomb1")
  99. self.gifts = None # 爆装备
  100. self.death = None # 死亡状态
  101. self.last_motion = 0
  102. self.last_random = 10
  103. self.energy_num = None
  104. self.load_npc(npc_name, color)
  105. def load_npc(self, npc_name, color):
  106. # 加载npc的json文件
  107. # print(self.stats_name)
  108. if not R.test_mode:
  109. # self.npc_json = load_json(G.GAME_PATH + "\\" + "game/npc/" + npc_name + ".json")
  110. self.npc_json = load_json("game/npc/" + npc_name + ".json")
  111. else:
  112. try:
  113. self.npc_json = load_json(G.GAME_PATH + "\\" + "gtest/test_npc/" + npc_name + ".json")
  114. except Exception as e:
  115. print(e)
  116. self.npc_json = load_json(G.GAME_PATH + "\\" + "game/npc/" + npc_name + ".json")
  117. self.chs_name = self.npc_json["chs_name"]
  118. if self.variant:
  119. self.chs_name = "(异变)" + self.chs_name
  120. self.blood = int(
  121. self.npc_json["blood"] * R.monster_HP_rate * R.diff_HP[R.difficulty] * get_variant_attr(self.variant,
  122. "blood"))
  123. self.light = self.npc_json.get("light", self.light)
  124. if "self_damage_blood" in self.npc_json.keys():
  125. self.self_damage_blood = self.npc_json["self_damage_blood"] * get_variant_attr(self.variant, "blood")
  126. self.speed = (self.npc_json["speed"] * G.GAME_SQUARE * R.diff_speed[R.difficulty] * get_variant_attr(
  127. self.variant, "speed")) / 1000
  128. self.original_speed = self.speed
  129. self.contact = int(
  130. max(self.npc_json["contact"] * R.diff_contact[R.difficulty] * get_variant_attr(self.variant, "contact"),
  131. self.npc_json["contact"]))
  132. self.original_contact = self.contact
  133. self.defense = self.npc_json["defense"] + R.monster_defense_delta + R.diff_Delta_Defense[
  134. R.difficulty] + get_variant_attr(self.variant, "defense")
  135. if "boss_mode" in self.npc_json.keys():
  136. self.boss_mode = bool(self.npc_json["boss_mode"])
  137. if "elite" in self.npc_json.keys():
  138. if self.npc_json["elite"]:
  139. try:
  140. self.chs_name_bg = load_png(G.GAME_PATH + "\\" + "res/img/ui/game/misc170.png")
  141. # self.chs_name_bg = pygame.image.load("res/img/ui/game/misc170.png").convert_alpha() # Npc姓名牌背景
  142. except:
  143. pass
  144. if "boss" in self.npc_json.keys():
  145. if self.npc_json["boss"]:
  146. self.boss_mode = True
  147. try:
  148. self.chs_name_bg = load_png(G.GAME_PATH + "\\" + "res/img/ui/game/misc169.png")
  149. # self.chs_name_bg = pygame.image.load("res/img/ui/game/misc169.png").convert_alpha() # Npc姓名牌背景
  150. except:
  151. pass
  152. if "ally" in self.npc_json.keys():
  153. if self.npc_json["ally"]:
  154. try:
  155. self.chs_name_bg = load_png(G.GAME_PATH + "\\" + "res/img/ui/game/misc172.png")
  156. # self.chs_name_bg = pygame.image.load("res/img/ui/game/misc172.png").convert_alpha() # Npc姓名牌背景
  157. except:
  158. pass
  159. if "spawned" in self.npc_json.keys():
  160. if self.npc_json["spawned"]:
  161. self.isSpawned = True
  162. if "treasure" in self.npc_json.keys():
  163. if self.npc_json["treasure"]:
  164. self.isTreasure = True
  165. if "max_energy_amount" in self.npc_json.keys():
  166. if self.npc_json["max_energy_amount"] > 0:
  167. self.has_energy = True
  168. self.max_energy_amount = self.npc_json["max_energy_amount"] + R.diff_power[R.difficulty]
  169. self.energy_amount = self.npc_json["max_energy_amount"] + R.diff_power[R.difficulty]
  170. else:
  171. pass
  172. if "flying" in self.npc_json.keys():
  173. self.flying = self.npc_json["flying"]
  174. if "slime" in self.npc_json.keys():
  175. self.slime = self.npc_json["slime"]
  176. if "gifts" in self.npc_json.keys():
  177. self.gifts = self.npc_json["gifts"]
  178. if "death" in self.npc_json.keys():
  179. self.death = self.npc_json["death"]
  180. if "show_name" in self.npc_json.keys():
  181. self.chs_name_show = self.npc_json["show_name"]
  182. if "is_target" in self.npc_json.keys():
  183. self.is_target = self.npc_json["is_target"]
  184. if "light" in self.npc_json.keys():
  185. self.light = self.npc_json["light"]
  186. self.resent_dist = self.npc_json["resent_dist"] * G.GAME_SQUARE # 仇恨距离(像素)
  187. for n, s in enumerate(self.npc_json["skills"]):
  188. self.skill_names.append(s["name"])
  189. self.skill_init_times.append(s["init"])
  190. self.skill_intervals.append(s["interval"])
  191. self.skill_remains.append(s["max"])
  192. self.skill_params.append(s["params"] if "params" in s else [])
  193. self.skill_params_order.append(n)
  194. self.skill_spawns.append(s["spawns"] if "spawns" in s else [])
  195. self.skill_spawn_order.append(s["spawn_order"] if "spawn_order" in s else n)
  196. self.remain_blood = self.blood
  197. self.chase_path = dict()
  198. self.chs_name_text = self.chs_name_font.render(self.chs_name, True, (255, 255, 0))
  199. self.chs_name_text_shadow = self.chs_name_font.render(self.chs_name, True, (0, 0, 0))
  200. self.chs_name_text_half_width = self.chs_name_text.get_width() // 2
  201. self.character = self.load_character(self.npc_json["character"], color, dict())
  202. self.npc_time_init = pygame.time.get_ticks()
  203. if "head_pos" in self.npc_json.keys():
  204. self.head_cx, self.head_cy = self.npc_json["head_pos"][0], self.npc_json["head_pos"][1]
  205. if "blood_bar" in self.npc_json.keys():
  206. if self.npc_json["blood_bar"]:
  207. for bar_pos in range(1, 7):
  208. if level.current_level.boss_blood_bar_dic[bar_pos] == 0:
  209. level.current_level.boss_blood_bar_dic[bar_pos] = 1
  210. self.blood_bar_pos = bar_pos
  211. self.blood_bar = BossBloodBar(self, bar_pos)
  212. level.current_level.ui_instances_top.append(self.blood_bar)
  213. self.profile = BossHead(self, bar_pos)
  214. level.current_level.ui_instances_top.append(self.profile)
  215. break
  216. else:
  217. if self.boss_mode:
  218. for bar_pos in range(1, 7):
  219. if level.current_level.boss_blood_bar_dic[bar_pos] == 0:
  220. level.current_level.boss_blood_bar_dic[bar_pos] = 1
  221. self.blood_bar_pos = bar_pos
  222. self.blood_bar = BossBloodBar(self, bar_pos)
  223. level.current_level.ui_instances_top.append(self.blood_bar)
  224. self.profile = BossHead(self, bar_pos)
  225. level.current_level.ui_instances_top.append(self.profile)
  226. break
  227. def update(self):
  228. super().update()
  229. current_time = pygame.time.get_ticks()
  230. self.update_npc_blood_display(current_time)
  231. self.wander_and_detect(current_time)
  232. self.chase_hero()
  233. self.contact_damage()
  234. self.try_using_skills()
  235. self.throw()
  236. def update_npc_blood_display(self, current_time):
  237. if G.DISPLAY_NPC_NAME_CARD and G.DISPLAY_NPC_BLOOD:
  238. self.chs_name_text = self.chs_name_font.render(str(self.remain_blood - self.fake_blood), True,
  239. (255, 255, 0))
  240. self.chs_name_text_shadow = self.chs_name_font.render(str(self.remain_blood - self.fake_blood), True,
  241. (0, 0, 0))
  242. self.chs_name_text_half_width = self.chs_name_text.get_width() // 2
  243. else:
  244. self.chs_name_text = self.chs_name_font.render(self.chs_name, True, (255, 255, 0))
  245. self.chs_name_text_shadow = self.chs_name_font.render(self.chs_name, True, (0, 0, 0))
  246. self.chs_name_text_half_width = self.chs_name_text.get_width() // 2
  247. def wander_and_detect(self, current_time):
  248. # 游走并检测玩家距离
  249. if (self.resentful or self.mocking) and not self.friendly:
  250. return
  251. # 初始化时刻小于0.3s
  252. if current_time - self.npc_time_init < 300:
  253. return
  254. # 随机运动时间
  255. if level.current_level.in_district:
  256. if (current_time - self.last_motion) >= 50 * self.last_random:
  257. if self.speed >= (5.8 * G.GAME_SQUARE) / 1000:
  258. self.last_random = random.randint(5, 15)
  259. if 0 <= (current_time % 1200) <= 300 or (random.randint(1, 100) >= 85):
  260. self.last_motion = current_time
  261. if random.randint(1, 100) >= 25:
  262. motion = self.idx_motion[random.randint(0, 3)]
  263. self.set_motion(motion)
  264. else:
  265. self.set_motion(self.orientation)
  266. else:
  267. self.set_motion(self.orientation)
  268. else:
  269. self.set_motion(self.orientation)
  270. # 技能初始时间更新
  271. for i in range(len(self.skill_names)):
  272. # 游荡状态下,技能初始时间永远等于当前时刻加init值
  273. self.skill_init_times[i] = self.npc_json["skills"][i]["init"] + current_time
  274. # 检测与玩家的距离
  275. me: Hero = level.current_level.me
  276. if me.district_locked and me.state == PlayerState.NORMAL:
  277. if abs(me.x_pos - self.x_pos) < self.resent_dist and abs(me.y_pos - self.y_pos) < self.resent_dist:
  278. # 如果玩家活着、已进入区域锁,并且距离小于临界值 则产生玩家仇恨
  279. self.resentful = True
  280. level.current_level.recal_npc_paths = True
  281. def chase_hero(self):
  282. if not self.resentful and not self.mocking or self.friendly or self.imitating or (
  283. self.speed == 0 and self.original_speed == 0):
  284. return
  285. me = (level.current_level.me.x, level.current_level.me.y)
  286. if self.free_chasing:
  287. me = self.chasing_point
  288. # 玩家在糖泡中 定身Npc
  289. if len(level.current_level.get_bomb_instance(*me)) > 0 and self.is_ghost == False and not self.free_chasing:
  290. self.set_motion()
  291. return
  292. now = (self.x, self.y)
  293. path_map = level.current_level.path_map
  294. # 如果重新计算路径标志为True则重新追踪玩家
  295. if level.current_level.recal_npc_paths:
  296. if now not in path_map.keys():
  297. self.chase_path = path_map[now] = aStar.cal_path(
  298. now, me,
  299. (level.current_level.district_square_grid["x1"], level.current_level.district_square_grid["y1"]),
  300. (level.current_level.district_square_grid["x2"], level.current_level.district_square_grid["y2"]),
  301. is_ghost=self.is_ghost,
  302. path_cache=path_map
  303. )
  304. else:
  305. self.chase_path = path_map[now]
  306. if now in self.chase_path.keys():
  307. _next = self.chase_path[now]
  308. if _next is None:
  309. self.set_motion()
  310. elif _next[0] > self.x:
  311. self.set_motion("R")
  312. elif _next[1] < self.y:
  313. self.set_motion("U")
  314. elif _next[0] < self.x:
  315. self.set_motion("L")
  316. elif _next[1] > self.y:
  317. self.set_motion("D")
  318. else:
  319. self.set_motion()
  320. def half_body_damage(self, point, cl, current_time):
  321. if self.bomb_shield_num > 0:
  322. if self.protected > 0:
  323. return
  324. if self.bomb_shield_protect:
  325. self.try_damage(cl.grid_damage_blood[point], bomb_damage=True)
  326. Protect3s(self, self.skill_instances)
  327. self.energy_amount = int(max(0, self.bomb_shield_num - 1))
  328. return
  329. self.try_damage(cl.grid_damage_blood[point], bomb_damage=True)
  330. self.energy_amount = int(max(0, self.bomb_shield_num - cl.grid_damage_multiples[point]))
  331. Protect005s(self, self.skill_instances)
  332. return
  333. self.try_damage(cl.grid_damage_blood[point], bomb_damage=True)
  334. def contact_damage(self):
  335. # 检查npc与人物的接触伤害
  336. if level.current_level.me.x == self.x and level.current_level.me.y == self.y:
  337. level.current_level.me.try_damage(self.contact, "C")
  338. def check_task(self):
  339. if self.stats_name == "RougeTreasureDestinyElite":
  340. R.task_RougeTreasureDestinyElite += 1
  341. elif self.stats_name == "MineCFireDragonBoss":
  342. R.task_MineCFireDragonBoss += 1
  343. elif self.stats_name == "RougeTreasureDestinyWizard":
  344. R.task_RougeTreasureDestinyWizard += 1
  345. def die(self):
  346. super().die()
  347. sound_player.play("npc_dead")
  348. self.check_task()
  349. if R.pass_status[
  350. R.difficulty] == 2 and not R.story_destiny and R.rouge_story != "重塑光影" and R.rouge_story != "渊海之息":
  351. if self.stats_name in ["RougeTreasureDestiny", "RougeTreasureDestinyWizard", "RougeTreasureDestinyMachine"]:
  352. R.rouge_destiny[self.stats_name] = 1
  353. if R.rouge_destiny["RougeTreasureDestiny"] + R.rouge_destiny["RougeTreasureDestinyWizard"] + \
  354. R.rouge_destiny["RougeTreasureDestinyMachine"] == 3:
  355. level.current_level.message_entry.new_message(sender='命运', message='时空法则开始崩溃,因果维度相互纠缠')
  356. level.current_level.message_entry.new_message(sender='*获得永久效果(坍缩)',
  357. message='玩家每次被攻击时随机受到1~800点伤害,生命值+1000')
  358. R.random_dmg = True
  359. R.current_standard_HP += 1000
  360. del R.se_district[15:20]
  361. level.current_level.me.blood += 1000
  362. level.current_level.me.remain_blood += 1000
  363. level.current_level.game.map_set_json = level.current_level.game.map_file = R.central_mapset = {
  364. "maps": ["rogueMap1", "rogueAttMap1", "rogueMap2", "rogueAttMap2", "rogueMap3", "rogueAttMap3",
  365. "rogueMap4", "rogueAttMap4", "rogueMap5", "rogueAttMap5",
  366. "rogueMap6", "rogueAttMap6", "rogueMap7", "rogueAttMap7", "rogueMap8", "rogueAttMap8",
  367. "rogueMap9", "rogueAttMap9", "rogueMap10", "rogueAttMap10",
  368. "rogueMap11", "rogueAttMap11", "rogueMap12", "rogueAttMap12", "rogueMap13",
  369. "rogueAttMap13", "rogueMap14", "rogueAttMap14", "rogueMap15", "rogueAttMap15_continue",
  370. "rogueMap16_destiny", "rogueAttMap16_destiny", "rogueMap17_destiny",
  371. "rogueAttMap17_destiny", "rogueMap18_destiny", "rogueAttMap18_destiny",
  372. "rogueMap19_destiny", "rogueAttMap19_destiny", "rogueMap20_destiny",
  373. "rogueAttMap20_destiny"]}
  374. R.rouge_story = "命运乐章"
  375. R.rouge_theme_4 = "重构秩序"
  376. R.story_destiny = True
  377. G.TOTAL_LEVEL = 40
  378. for key, value in T.theme_destiny_mon_dic.items():
  379. if value['type'] == 'normal':
  380. R.general_mon_dic["4"]["normal"].append(key)
  381. if value['type'] == 'elite':
  382. R.general_mon_dic["4"]["elite"].append(key)
  383. R.general_mon_dic["4"]["elite"].append("RogueDestinyASanGrail")
  384. if not self.isSpawned:
  385. if G.CURRENT_LEVEL <= 10:
  386. if self.stats_name in T.boss_list:
  387. R.coin += random.randint(int(4 * R.coin_lv(R.rouge_lv)), int(8 * R.coin_lv(R.rouge_lv)))
  388. R.rouge_exp += random.randint(30, 50)
  389. elif self.stats_name in T.elite_list:
  390. # R.coin += random.randint(0, int(2*R.coin_lv(R.rouge_lv)))
  391. R.coin += random.choices([0, 1, 2], weights=[1, R.coin_lv(R.rouge_lv) - 0.6, R.coin_lv(R.rouge_lv) - 0.8], k=1)[0]
  392. R.rouge_exp += random.randint(3, 5)
  393. else:
  394. # R.coin += random.randint(0, int(1*R.coin_lv(R.rouge_lv))) # random.choices([0, 1], weights=[70, 30], k=1)[0]
  395. R.coin += random.choices([0, 1], weights=[1.6, R.coin_lv(R.rouge_lv) - 0.6], k=1)[0]
  396. R.rouge_exp += random.randint(1, 3)
  397. elif 11 <= G.CURRENT_LEVEL <= 20:
  398. if self.stats_name in T.boss_list:
  399. R.coin += random.randint(int(6 * R.coin_lv(R.rouge_lv)), int(10 * R.coin_lv(R.rouge_lv)))
  400. R.rouge_exp += random.randint(50, 80)
  401. elif self.stats_name in T.elite_list:
  402. # R.coin += random.randint(0, int(3*R.coin_lv(R.rouge_lv)))
  403. R.coin += random.choices([0, 1, 2, 3],
  404. weights=[1, R.coin_lv(R.rouge_lv) - 0.4, R.coin_lv(R.rouge_lv) - 0.6,
  405. R.coin_lv(R.rouge_lv) - 0.8], k=1)[0]
  406. R.rouge_exp += random.randint(4, 6)
  407. else:
  408. # R.coin += random.randint(0, int(2*R.coin_lv(R.rouge_lv)))
  409. R.coin += random.choices([0, 1, 2], weights=[1.6, R.coin_lv(R.rouge_lv) - 0.4, R.coin_lv(R.rouge_lv) - 0.6], k=1)[0]
  410. R.rouge_exp += random.randint(2, 4)
  411. elif 21 <= G.CURRENT_LEVEL <= 30:
  412. if self.stats_name in T.boss_list:
  413. R.coin += random.randint(int(8 * R.coin_lv(R.rouge_lv)), int(12 * R.coin_lv(R.rouge_lv)))
  414. R.rouge_exp += random.randint(100, 150)
  415. elif self.stats_name in T.elite_list:
  416. # R.coin += random.randint(0, int(3*R.coin_lv(R.rouge_lv)))
  417. R.coin += random.choices([0, 1, 2, 3, 4],
  418. weights=[1, R.coin_lv(R.rouge_lv) - 0.2, R.coin_lv(R.rouge_lv) - 0.4,
  419. R.coin_lv(R.rouge_lv) - 0.6, R.coin_lv(R.rouge_lv) - 0.8], k=1)[0]
  420. R.rouge_exp += random.randint(5, 8)
  421. else:
  422. # R.coin += random.randint(0, int(2*R.coin_lv(R.rouge_lv)))
  423. R.coin += random.choices([0, 1, 2, 3],
  424. weights=[1.6, R.coin_lv(R.rouge_lv) - 0.2, R.coin_lv(R.rouge_lv) - 0.4,
  425. R.coin_lv(R.rouge_lv) - 0.6], k=1)[0]
  426. R.rouge_exp += random.randint(3, 5)
  427. else:
  428. if self.stats_name in T.boss_list:
  429. R.coin += random.randint(int(30 * R.coin_lv(R.rouge_lv)), int(50 * R.coin_lv(R.rouge_lv)))
  430. R.rouge_exp += random.randint(150, 250)
  431. elif self.stats_name in T.elite_list:
  432. R.coin += random.randint(1, 2) * random.choices(
  433. [2, 3, 4],
  434. weights=[
  435. R.coin_lv(R.rouge_lv),
  436. R.coin_lv(R.rouge_lv) - 0.2,
  437. R.coin_lv(R.rouge_lv) - 0.4
  438. ],
  439. k=1)[0]
  440. R.rouge_exp += random.randint(8, 12)
  441. else:
  442. R.coin += random.randint(1, 2) * random.choices([2, 3], weights=[R.coin_lv(R.rouge_lv), R.coin_lv(R.rouge_lv) - 0.2], k=1)[0]
  443. R.rouge_exp += random.randint(5, 7)
  444. if self.blood_bar_pos != 0:
  445. try:
  446. level.current_level.ui_instances_top.remove(self.blood_bar)
  447. level.current_level.ui_instances_top.remove(self.profile)
  448. level.current_level.boss_blood_bar_dic[self.blood_bar_pos] = 0
  449. except Exception as e:
  450. print(e)
  451. try:
  452. print("血条错误", self.chs_name, self.blood_bar_pos)
  453. except Exception as e:
  454. print(f"npc.py: die(): {e}")
  455. self.hell_plus_death_soldier_spawn()
  456. self.gene_gifts()
  457. self.gene_ghost()
  458. def hell_plus_death_soldier_spawn(self):
  459. if self.chs_name in ["RogueEdenAGhostWizard", "RogueEdenAGhost"]:
  460. return
  461. if not self.isSpawned and not self.isTreasure and R.difficulty == "Hell+" and random.randint(0, 100) >= 97:
  462. n_name = random.choice(["RogueEdenAGhostWizard", "RogueEdenAGhost"])
  463. an_npc = Npc(n_name, (self.x, self.y))
  464. Protect3s(an_npc, an_npc.skill_instances)
  465. level.current_level.npcs.append(an_npc)
  466. Skill21018(an_npc, level.current_level.me, level.current_level.skill_instances)
  467. def gene_gifts(self):
  468. if not self.boss_mode:
  469. return
  470. if self.stats_name not in ["RougeTreasureDestinyWizard", "RogueForestAWizardKing"]:
  471. return
  472. cl = level.current_level
  473. gx = int((cl.map_x_pos + 1) / 40 - 1)
  474. gy = int((cl.map_y_pos + 1) / 40 - 1)
  475. x_range = sorted(range(max(0, self.x - 2), min(self.x + 3, gx)), key=lambda k: abs(k))
  476. y_range = sorted(range(max(0, self.y - 2), min(self.y + 3, gy)), key=lambda k: abs(k))
  477. gift = ""
  478. prob = 0
  479. diff_add = {"Easy": 0, "Normal": 0.03, "Hell": 0.06, "Hell+": 0.08}
  480. if self.stats_name == "RougeTreasureDestinyWizard":
  481. print("RougeTreasureDestinyWizard")
  482. gift = "item_bomb26001"
  483. prob = 0.03 + diff_add[R.difficulty]
  484. if self.stats_name == "RogueForestAWizardKing":
  485. print("RogueForestAWizardKing")
  486. gift = "item_bomb26002"
  487. prob = 0.03 + diff_add[R.difficulty]
  488. if prob - random.uniform(0, 1) > 0:
  489. grids = []
  490. for x in x_range:
  491. for y in y_range:
  492. if (x, y) not in cl.obstacle_instances.keys() and (x, y) not in cl.item_instances.keys() and len(
  493. cl.get_bomb_instance(x, y)) == 0:
  494. grids.append((x, y))
  495. if len(grids) >= 3:
  496. break
  497. if len(grids) >= 3:
  498. break
  499. if len(grids) == 0:
  500. print("哦豁没位置")
  501. return
  502. random.shuffle(grids)
  503. point = grids[0]
  504. try:
  505. print(gift)
  506. an_item = ItemInstance(self.x, self.y, level.current_level.item_instances, item.get_item(gift))
  507. an_item.throw_to(point[0], point[1], an_item.get_direction(point[0], point[1]))
  508. except Exception as e:
  509. print(e)
  510. def gene_ghost(self):
  511. if self.death is None:
  512. return
  513. for gene in self.death:
  514. if "ghost" in gene.keys():
  515. # 生成幽灵Ghost,指定生存时间,速度,接触伤害(自身无敌 + 穿墙穿泡、无视区域)
  516. level.current_level.ghosts.append(
  517. Ghost(gene["name"], (self.x, self.y), gene["ghost"]["time"],
  518. gene["ghost"]["speed"], gene["ghost"]["contact"]))
  519. elif "prob" in gene.keys():
  520. if float(gene["prob"]) - random.uniform(0, 1) > 0:
  521. # 生成普通Npc,并给予无敌防护
  522. an_npc = Npc(gene["name"], (self.x, self.y))
  523. Protect3s(an_npc, an_npc.skill_instances)
  524. level.current_level.npcs.append(an_npc)
  525. if "inherit" in gene.keys():
  526. an_npc.remain_blood = self.remain_blood
  527. break
  528. else:
  529. pass
  530. else:
  531. # 生成普通Npc,并给予无敌防护
  532. an_npc = Npc(gene["name"], (self.x, self.y))
  533. Protect3s(an_npc, an_npc.skill_instances)
  534. level.current_level.npcs.append(an_npc)
  535. if "inherit" in gene.keys():
  536. an_npc.remain_blood = self.remain_blood
  537. def collide_wall(self):
  538. # npc 游走时撞墙 则改变方向
  539. if self.speed >= (5.8 * G.GAME_SQUARE) / 1000:
  540. if self.orientation == "U":
  541. if random.randint(1, 100) >= 20:
  542. self.set_motion("D")
  543. else:
  544. if random.randint(1, 100) >= 40:
  545. self.set_motion("L")
  546. else:
  547. self.set_motion("R")
  548. elif self.orientation == "D":
  549. if random.randint(1, 100) >= 20:
  550. self.set_motion("U")
  551. else:
  552. if random.randint(1, 100) >= 40:
  553. self.set_motion("R")
  554. else:
  555. self.set_motion("L")
  556. elif self.orientation == "L":
  557. if random.randint(1, 100) >= 20:
  558. self.set_motion("R")
  559. else:
  560. if random.randint(1, 100) >= 40:
  561. self.set_motion("D")
  562. else:
  563. self.set_motion("U")
  564. elif self.orientation == "R":
  565. if random.randint(1, 100) >= 20:
  566. self.set_motion("L")
  567. else:
  568. if random.randint(1, 100) >= 40:
  569. self.set_motion("U")
  570. else:
  571. self.set_motion("D")
  572. else:
  573. if self.orientation == "U":
  574. self.set_motion("D")
  575. if self.orientation == "D":
  576. self.set_motion("U")
  577. if self.orientation == "L":
  578. self.set_motion("R")
  579. if self.orientation == "R":
  580. self.set_motion("L")
  581. def try_push(self, direction, offset=(0, 0)):
  582. pass
  583. def try_using_skills(self):
  584. # npc 循环使用技能
  585. if not self.resentful and not self.mocking or self.friendly:
  586. return
  587. # npc 技能时间更新
  588. self.npc_skill_time = pygame.time.get_ticks()
  589. # 每一帧都尝试使用全部技能
  590. for i in range(len(self.skill_names)):
  591. self.use_skill(i)
  592. def use_skill(self, idx: int):
  593. if self.npc_skill_time < self.skill_init_times[idx] or self.skill_remains[idx] == 0:
  594. # 未到初始时间 或技能冷却中 或次数已用完(-1则可以无限使用)
  595. return
  596. name = self.skill_names[idx]
  597. param = self.skill_params[idx]
  598. spawn_order = self.skill_spawn_order[idx]
  599. try:
  600. self.using_skill_map(name, param, spawn_order)
  601. except Exception as e:
  602. print(f"{name}: {e}")
  603. self.skill_remains[idx] -= 1
  604. self.skill_init_times[idx] += self.skill_intervals[idx]
  605. def set_bomb(self):
  606. # npc尝试放置一个糖泡
  607. if self.state != PlayerState.NORMAL:
  608. return
  609. p = (self.x, self.y)
  610. bis = level.current_level.bomb_instances
  611. bs = level.current_level.get_bomb_instance(*p)
  612. if len(bs) > 0: # 当前位置有糖泡
  613. return
  614. BombInstance(*p, bis, self.bomb_skin, 3, 1000)
  615. def set_general_bomb(self, power, damage, skin=False):
  616. # npc尝试放置一个糖泡
  617. if self.state != PlayerState.NORMAL:
  618. return
  619. p = (self.x, self.y)
  620. bis = level.current_level.bomb_instances
  621. bs = level.current_level.get_bomb_instance(*p)
  622. if len(bs) > 0: # 当前位置有糖泡
  623. return
  624. sound_player.play("bomb")
  625. if not skin:
  626. BombInstance(*p, bis, self.bomb_skin, power, damage)
  627. else:
  628. BombInstance(*p, bis, bomb.get_bomb(skin), power, damage)
  629. def set_custom_bomb(self, x, y, power, damage):
  630. # npc尝试放置一个糖泡
  631. if self.state != PlayerState.NORMAL:
  632. return
  633. p = (x, y)
  634. bis = level.current_level.bomb_instances
  635. bs = level.current_level.get_bomb_instance(*p)
  636. if len(bs) > 0: # 当前位置有糖泡
  637. return
  638. BombInstance(*p, bis, self.bomb_skin, power, damage)
  639. def set_restoration(self, to_x, to_y):
  640. self.x = to_x
  641. self.y = to_y
  642. def draw(self, screen: pygame.Surface):
  643. super().draw(screen)
  644. if G.DISPLAY_NPC_NAME_CARD and self.chs_name_show:
  645. self.chs_name_bg.set_alpha(self.temporary_alpha)
  646. screen.blit(self.chs_name_bg, (self.x_pos - 40, self.y_pos - 100))
  647. self.chs_name_text_shadow.set_alpha(self.temporary_alpha)
  648. screen.blit(self.chs_name_text_shadow, (self.x_pos + 3 - self.chs_name_text_half_width, self.y_pos - 95))
  649. self.chs_name_text.set_alpha(self.temporary_alpha)
  650. screen.blit(self.chs_name_text, (self.x_pos + 2 - self.chs_name_text_half_width, self.y_pos - 95))
  651. self.render_energy(screen)
  652. def render_energy(self, screen):
  653. if not self.has_energy:
  654. return
  655. energy_str = str(self.energy_amount)
  656. max_len = len(energy_str)
  657. if max_len > 5:
  658. energy_str = '99999'
  659. x_pos_offset = -5 * max(0, max_len - 1) # 初始偏移量
  660. for i, digit in enumerate(energy_str):
  661. render_digit(screen, int(digit), self.x_pos, self.y_pos, self.temporary_alpha, x_pos_offset + 10 * i)
  662. def using_skill_map(self, skill_name, param, spawn_order):
  663. cl = level.current_level
  664. if skill_name not in skill_map.keys():
  665. return
  666. try:
  667. skill_attributes = skill_map[skill_name]
  668. skill_class = skill_attributes["skill"]
  669. skill_target = skill_attributes.get("target")
  670. skill_target_map = {"me": cl.me, "npcs": cl.npcs, "listed_me": [cl.me]}
  671. final_target = None
  672. if skill_target_map is not None:
  673. final_target = skill_target_map.get(skill_target)
  674. effect_target_map = {"self": self.skill_instances, "cl": cl.skill_instances}
  675. effect_target = effect_target_map.get(skill_attributes.get("effect"))
  676. if skill_attributes.get("old_spawn"):
  677. skill_class(self, final_target, self.skill_spawns, spawn_order, effect_target) # 旧版召唤
  678. elif skill_target is not None:
  679. if not skill_attributes.get("param"):
  680. skill_class(self, final_target, effect_target) # 有目标无参数
  681. else:
  682. skill_class(self, final_target, param, effect_target) # 有目标有参数
  683. else:
  684. if not skill_attributes.get("param"):
  685. skill_class(self, effect_target) # 无目标无参数
  686. else:
  687. skill_class(self, param, effect_target) # 无目标有参数
  688. except Exception as e:
  689. print(f"{skill_name}: {e}")