npc.py 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  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 += \
  392. random.choices([0, 1, 2], weights=[1, R.coin_lv(R.rouge_lv) - 0.6, R.coin_lv(R.rouge_lv) - 0.8],
  393. k=1)[0]
  394. R.rouge_exp += random.randint(3, 5)
  395. else:
  396. # R.coin += random.randint(0, int(1*R.coin_lv(R.rouge_lv))) # random.choices([0, 1], weights=[70, 30], k=1)[0]
  397. R.coin += random.choices([0, 1], weights=[1.6, R.coin_lv(R.rouge_lv) - 0.6], k=1)[0]
  398. R.rouge_exp += random.randint(1, 3)
  399. elif 11 <= G.CURRENT_LEVEL <= 20:
  400. if self.stats_name in T.boss_list:
  401. R.coin += random.randint(int(6 * R.coin_lv(R.rouge_lv)), int(10 * R.coin_lv(R.rouge_lv)))
  402. R.rouge_exp += random.randint(50, 80)
  403. elif self.stats_name in T.elite_list:
  404. # R.coin += random.randint(0, int(3*R.coin_lv(R.rouge_lv)))
  405. R.coin += random.choices([0, 1, 2, 3],
  406. weights=[1, R.coin_lv(R.rouge_lv) - 0.4, R.coin_lv(R.rouge_lv) - 0.6,
  407. R.coin_lv(R.rouge_lv) - 0.8], k=1)[0]
  408. R.rouge_exp += random.randint(4, 6)
  409. else:
  410. # R.coin += random.randint(0, int(2*R.coin_lv(R.rouge_lv)))
  411. R.coin += \
  412. random.choices([0, 1, 2],
  413. weights=[1.6, R.coin_lv(R.rouge_lv) - 0.4, R.coin_lv(R.rouge_lv) - 0.6],
  414. k=1)[0]
  415. R.rouge_exp += random.randint(2, 4)
  416. elif 21 <= G.CURRENT_LEVEL <= 30:
  417. if self.stats_name in T.boss_list:
  418. R.coin += random.randint(int(8 * R.coin_lv(R.rouge_lv)), int(12 * R.coin_lv(R.rouge_lv)))
  419. R.rouge_exp += random.randint(100, 150)
  420. elif self.stats_name in T.elite_list:
  421. # R.coin += random.randint(0, int(3*R.coin_lv(R.rouge_lv)))
  422. R.coin += random.choices([0, 1, 2, 3, 4],
  423. weights=[1, R.coin_lv(R.rouge_lv) - 0.2, R.coin_lv(R.rouge_lv) - 0.4,
  424. R.coin_lv(R.rouge_lv) - 0.6, R.coin_lv(R.rouge_lv) - 0.8], k=1)[0]
  425. R.rouge_exp += random.randint(5, 8)
  426. else:
  427. # R.coin += random.randint(0, int(2*R.coin_lv(R.rouge_lv)))
  428. R.coin += random.choices([0, 1, 2, 3],
  429. weights=[1.6, R.coin_lv(R.rouge_lv) - 0.2, R.coin_lv(R.rouge_lv) - 0.4,
  430. R.coin_lv(R.rouge_lv) - 0.6], k=1)[0]
  431. R.rouge_exp += random.randint(3, 5)
  432. else:
  433. if self.stats_name in T.boss_list:
  434. R.coin += random.randint(int(30 * R.coin_lv(R.rouge_lv)), int(50 * R.coin_lv(R.rouge_lv)))
  435. R.rouge_exp += random.randint(150, 250)
  436. elif self.stats_name in T.elite_list:
  437. R.coin += random.randint(1, 2) * random.choices(
  438. [2, 3, 4],
  439. weights=[
  440. R.coin_lv(R.rouge_lv),
  441. R.coin_lv(R.rouge_lv) - 0.2,
  442. R.coin_lv(R.rouge_lv) - 0.4
  443. ],
  444. k=1)[0]
  445. R.rouge_exp += random.randint(8, 12)
  446. else:
  447. R.coin += random.randint(1, 2) * \
  448. random.choices([2, 3], weights=[R.coin_lv(R.rouge_lv), R.coin_lv(R.rouge_lv) - 0.2], k=1)[
  449. 0]
  450. R.rouge_exp += random.randint(5, 7)
  451. if self.blood_bar_pos != 0:
  452. try:
  453. level.current_level.ui_instances_top.remove(self.blood_bar)
  454. level.current_level.ui_instances_top.remove(self.profile)
  455. level.current_level.boss_blood_bar_dic[self.blood_bar_pos] = 0
  456. except Exception as e:
  457. print(e)
  458. try:
  459. print("血条错误", self.chs_name, self.blood_bar_pos)
  460. except Exception as e:
  461. print(f"npc.py: die(): {e}")
  462. self.hell_plus_death_soldier_spawn()
  463. self.gene_gifts()
  464. self.gene_ghost()
  465. def hell_plus_death_soldier_spawn(self):
  466. if self.chs_name in ["RogueEdenAGhostWizard", "RogueEdenAGhost"]:
  467. return
  468. if not self.isSpawned and not self.isTreasure and R.difficulty == "Hell+" and random.randint(0, 100) >= 97:
  469. n_name = random.choice(["RogueEdenAGhostWizard", "RogueEdenAGhost"])
  470. an_npc = Npc(n_name, (self.x, self.y))
  471. Protect3s(an_npc, an_npc.skill_instances)
  472. level.current_level.npcs.append(an_npc)
  473. Skill21018(an_npc, level.current_level.me, level.current_level.skill_instances)
  474. def gene_gifts(self):
  475. if not self.boss_mode:
  476. return
  477. if self.stats_name not in ["RougeTreasureDestinyWizard", "RogueForestAWizardKing"]:
  478. return
  479. cl = level.current_level
  480. gx = int((cl.map_x_pos + 1) / 40 - 1)
  481. gy = int((cl.map_y_pos + 1) / 40 - 1)
  482. x_range = sorted(range(max(0, self.x - 2), min(self.x + 3, gx)), key=lambda k: abs(k))
  483. y_range = sorted(range(max(0, self.y - 2), min(self.y + 3, gy)), key=lambda k: abs(k))
  484. gift = ""
  485. prob = 0
  486. diff_add = {"Easy": 0, "Normal": 0.03, "Hell": 0.06, "Hell+": 0.08}
  487. if self.stats_name == "RougeTreasureDestinyWizard":
  488. print("RougeTreasureDestinyWizard")
  489. gift = "item_bomb26001"
  490. prob = 0.03 + diff_add[R.difficulty]
  491. if self.stats_name == "RogueForestAWizardKing":
  492. print("RogueForestAWizardKing")
  493. gift = "item_bomb26002"
  494. prob = 0.03 + diff_add[R.difficulty]
  495. if prob - random.uniform(0, 1) > 0:
  496. grids = []
  497. for x in x_range:
  498. for y in y_range:
  499. if (x, y) not in cl.obstacle_instances.keys() and (x, y) not in cl.item_instances.keys() and len(
  500. cl.get_bomb_instance(x, y)) == 0:
  501. grids.append((x, y))
  502. if len(grids) >= 3:
  503. break
  504. if len(grids) >= 3:
  505. break
  506. if len(grids) == 0:
  507. print("哦豁没位置")
  508. return
  509. random.shuffle(grids)
  510. point = grids[0]
  511. try:
  512. print(gift)
  513. an_item = ItemInstance(self.x, self.y, level.current_level.item_instances, item.get_item(gift))
  514. an_item.throw_to(point[0], point[1], an_item.get_direction(point[0], point[1]))
  515. except Exception as e:
  516. print(e)
  517. def gene_ghost(self):
  518. if self.death is None:
  519. return
  520. for gene in self.death:
  521. if "ghost" in gene.keys():
  522. # 生成幽灵Ghost,指定生存时间,速度,接触伤害(自身无敌 + 穿墙穿泡、无视区域)
  523. level.current_level.ghosts.append(
  524. Ghost(gene["name"], (self.x, self.y), gene["ghost"]["time"],
  525. gene["ghost"]["speed"], gene["ghost"]["contact"]))
  526. elif "prob" in gene.keys():
  527. if float(gene["prob"]) - random.uniform(0, 1) > 0:
  528. # 生成普通Npc,并给予无敌防护
  529. an_npc = Npc(gene["name"], (self.x, self.y))
  530. Protect3s(an_npc, an_npc.skill_instances)
  531. level.current_level.npcs.append(an_npc)
  532. if "inherit" in gene.keys():
  533. an_npc.remain_blood = self.remain_blood
  534. break
  535. else:
  536. pass
  537. else:
  538. # 生成普通Npc,并给予无敌防护
  539. an_npc = Npc(gene["name"], (self.x, self.y))
  540. Protect3s(an_npc, an_npc.skill_instances)
  541. level.current_level.npcs.append(an_npc)
  542. if "inherit" in gene.keys():
  543. an_npc.remain_blood = self.remain_blood
  544. def collide_wall(self):
  545. # npc 游走时撞墙 则改变方向
  546. if self.speed >= (5.8 * G.GAME_SQUARE) / 1000:
  547. if self.orientation == "U":
  548. if random.randint(1, 100) >= 20:
  549. self.set_motion("D")
  550. else:
  551. if random.randint(1, 100) >= 40:
  552. self.set_motion("L")
  553. else:
  554. self.set_motion("R")
  555. elif self.orientation == "D":
  556. if random.randint(1, 100) >= 20:
  557. self.set_motion("U")
  558. else:
  559. if random.randint(1, 100) >= 40:
  560. self.set_motion("R")
  561. else:
  562. self.set_motion("L")
  563. elif self.orientation == "L":
  564. if random.randint(1, 100) >= 20:
  565. self.set_motion("R")
  566. else:
  567. if random.randint(1, 100) >= 40:
  568. self.set_motion("D")
  569. else:
  570. self.set_motion("U")
  571. elif self.orientation == "R":
  572. if random.randint(1, 100) >= 20:
  573. self.set_motion("L")
  574. else:
  575. if random.randint(1, 100) >= 40:
  576. self.set_motion("U")
  577. else:
  578. self.set_motion("D")
  579. else:
  580. if self.orientation == "U":
  581. self.set_motion("D")
  582. if self.orientation == "D":
  583. self.set_motion("U")
  584. if self.orientation == "L":
  585. self.set_motion("R")
  586. if self.orientation == "R":
  587. self.set_motion("L")
  588. def try_push(self, direction, offset=(0, 0)):
  589. pass
  590. def try_using_skills(self):
  591. # npc 循环使用技能
  592. if not self.resentful and not self.mocking or self.friendly:
  593. return
  594. # npc 技能时间更新
  595. self.npc_skill_time = pygame.time.get_ticks()
  596. # 每一帧都尝试使用全部技能
  597. for i in range(len(self.skill_names)):
  598. self.use_skill(i)
  599. def use_skill(self, idx: int):
  600. if self.npc_skill_time < self.skill_init_times[idx] or self.skill_remains[idx] == 0:
  601. # 未到初始时间 或技能冷却中 或次数已用完(-1则可以无限使用)
  602. return
  603. name = self.skill_names[idx]
  604. param = self.skill_params[idx]
  605. spawn_order = self.skill_spawn_order[idx]
  606. try:
  607. self.using_skill_map(name, param, spawn_order)
  608. except Exception as e:
  609. print(f"{name}: {e}")
  610. self.skill_remains[idx] -= 1
  611. self.skill_init_times[idx] += self.skill_intervals[idx]
  612. def set_bomb(self):
  613. # npc尝试放置一个糖泡
  614. if self.state != PlayerState.NORMAL:
  615. return
  616. p = (self.x, self.y)
  617. bis = level.current_level.bomb_instances
  618. bs = level.current_level.get_bomb_instance(*p)
  619. if len(bs) > 0: # 当前位置有糖泡
  620. return
  621. BombInstance(*p, bis, self.bomb_skin, 3, 1000)
  622. def set_general_bomb(self, power, damage, skin=False):
  623. # npc尝试放置一个糖泡
  624. if self.state != PlayerState.NORMAL:
  625. return
  626. p = (self.x, self.y)
  627. bis = level.current_level.bomb_instances
  628. bs = level.current_level.get_bomb_instance(*p)
  629. if len(bs) > 0: # 当前位置有糖泡
  630. return
  631. sound_player.play("bomb")
  632. if not skin:
  633. BombInstance(*p, bis, self.bomb_skin, power, damage)
  634. else:
  635. BombInstance(*p, bis, bomb.get_bomb(skin), power, damage)
  636. def set_custom_bomb(self, x, y, power, damage):
  637. # npc尝试放置一个糖泡
  638. if self.state != PlayerState.NORMAL:
  639. return
  640. p = (x, y)
  641. bis = level.current_level.bomb_instances
  642. bs = level.current_level.get_bomb_instance(*p)
  643. if len(bs) > 0: # 当前位置有糖泡
  644. return
  645. BombInstance(*p, bis, self.bomb_skin, power, damage)
  646. def set_restoration(self, to_x, to_y):
  647. self.x = to_x
  648. self.y = to_y
  649. def draw(self, screen: pygame.Surface):
  650. super().draw(screen)
  651. if G.DISPLAY_NPC_NAME_CARD and self.chs_name_show:
  652. self.chs_name_bg.set_alpha(self.temporary_alpha)
  653. screen.blit(self.chs_name_bg, (self.x_pos - 40, self.y_pos - 100))
  654. self.chs_name_text_shadow.set_alpha(self.temporary_alpha)
  655. screen.blit(self.chs_name_text_shadow, (self.x_pos + 3 - self.chs_name_text_half_width, self.y_pos - 95))
  656. self.chs_name_text.set_alpha(self.temporary_alpha)
  657. screen.blit(self.chs_name_text, (self.x_pos + 2 - self.chs_name_text_half_width, self.y_pos - 95))
  658. self.render_energy(screen)
  659. def render_energy(self, screen):
  660. if not self.has_energy:
  661. return
  662. energy_str = str(self.energy_amount)
  663. max_len = len(energy_str)
  664. if max_len > 5:
  665. energy_str = '99999'
  666. x_pos_offset = -5 * max(0, max_len - 1) # 初始偏移量
  667. for i, digit in enumerate(energy_str):
  668. render_digit(screen, int(digit), self.x_pos, self.y_pos, self.temporary_alpha, x_pos_offset + 10 * i)
  669. def using_skill_map(self, skill_name, param, spawn_order):
  670. cl = level.current_level
  671. if skill_name not in skill_map.keys():
  672. return
  673. try:
  674. skill_attributes = skill_map[skill_name]
  675. skill_class = skill_attributes["skill"]
  676. skill_target = skill_attributes.get("target")
  677. skill_target_map = {"me": cl.me, "npcs": cl.npcs, "listed_me": [cl.me]}
  678. final_target = None
  679. if skill_target_map is not None:
  680. final_target = skill_target_map.get(skill_target)
  681. effect_target_map = {"self": self.skill_instances, "cl": cl.skill_instances}
  682. effect_target = effect_target_map.get(skill_attributes.get("effect"))
  683. if skill_attributes.get("old_spawn"):
  684. skill_class(self, final_target, self.skill_spawns, spawn_order, effect_target) # 旧版召唤
  685. elif skill_target is not None:
  686. if not skill_attributes.get("param"):
  687. skill_class(self, final_target, effect_target) # 有目标无参数
  688. else:
  689. skill_class(self, final_target, param, effect_target) # 有目标有参数
  690. else:
  691. if not skill_attributes.get("param"):
  692. skill_class(self, effect_target) # 无目标无参数
  693. else:
  694. skill_class(self, param, effect_target) # 无目标有参数
  695. except Exception as e:
  696. print(f"{skill_name}: {e}")