pet.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import json
  2. import random
  3. import os
  4. import pygame
  5. from game.algo import aStar
  6. from game.const import color as C
  7. from game.const import game as G
  8. from game.frame import bomb, item
  9. from game.level import level
  10. from game.sound import sound_player
  11. from game.sprite.bomb_instance import BombInstance
  12. from game.sprite.ghost import Ghost
  13. from game.sprite.hero import Hero
  14. from game.sprite.item_instance import ItemInstance
  15. from game.sprite.player import Player, PlayerState
  16. from game.sprite.throwable import Throwable
  17. from game.file_loader.fileLoader import load_json, load_png
  18. import logging
  19. from datetime import datetime
  20. class Pet(Player, Throwable):
  21. def __init__(self, npc_name, xy, color=C.CHARACTER_RED):
  22. super(Pet, self).__init__(npc_name, xy, color)
  23. self.npc_json = None
  24. self.chs_name = None # Npc中文名
  25. self.stats_name = npc_name
  26. self.idx_motion = {0: "R", 1: "U", 2: "L", 3: "D"}
  27. self.contact = 0 # Npc接触伤害
  28. self.resent_dist = None # Npc仇恨范围(几格以内)
  29. self.resentful = False # Npc玩家仇恨
  30. self.mocking = False # Npc受到嘲笑药水的作用暂时仇恨
  31. self.friendly = False # Npc受到玫瑰药水的作用暂时解除仇恨
  32. self.refresh = 0
  33. self.is_ghost = True # 幽灵模式
  34. self.fake_blood = 0
  35. self.energy_amount = 0 # 充能格数
  36. self.max_energy_amount = 0
  37. self.has_energy = False
  38. self.wall_walking = True # 穿墙
  39. self.flying = True # 飞行(免疫墙体减速)
  40. self.sliding = False # 踩技能滑行中
  41. self.sliding_direction = None # 踩技能后滑的方向
  42. self.witch_protected_remove = False # 巫师无防护状态
  43. self.fire_load_time = 0
  44. self.slime = False
  45. self.last_in_water = 0
  46. self.slime_time = 0
  47. self.free_chasing = False
  48. self.chasing_point = (1,1)
  49. self.imitating = False
  50. self.combo_num = 1
  51. self.is_combo_target = False
  52. self.bomb_shield_num = 0 # 防糖泡电磁护盾
  53. self.bomb_shield_protect = False
  54. self.is_target = False
  55. self.light = 0 # 鬼屋模式发光大小 0不发光,1发光20像素,2发光40像素 最大9
  56. self.sharing_damage = False
  57. self.obstacle_can_push = False
  58. self.npc_time_init = None # Npc初始化时刻
  59. self.wander_random = 10 # Npc在无仇恨时的闲逛时间
  60. self.district_locked = True
  61. self.chase_path = None # Npc追踪路径
  62. self.npc_skill_time = pygame.time.get_ticks() # npc施放技能时的时刻 设为成员变量是为了统一多个技能的时间
  63. self.chs_name_bg = load_png("res/img/ui/game/misc172.png") # Npc姓名牌背景
  64. self.blood_bar = None
  65. self.blood_bar_pos = 0
  66. self.profile = None
  67. self.chain = False
  68. self.chase_rad = 2
  69. self.chs_name_font = pygame.font.Font("res/font/simsun.ttc", 13) # Npc姓名牌字体
  70. self.chs_name_text = None # Npc姓名牌前景文字
  71. self.chs_name_text_shadow = None # Npc姓名牌背景文字
  72. self.chs_name_text_half_width = None # Npc姓名牌文字的半宽 用于居中显示文字
  73. self.chs_name_show = True # 是否显示名牌
  74. self.bomb_skin = bomb.get_bomb("bomb1")
  75. self.gifts = None # 爆装备
  76. self.death = None # 死亡状态
  77. self.last_motion = 0
  78. self.last_random = 10
  79. self.energy_num = None
  80. self.load_npc(npc_name, color)
  81. def load_npc(self, npc_name, color):
  82. # 加载npc的json文件
  83. self.npc_json = load_json("game/pet/" + npc_name + ".json")
  84. self.chs_name = self.npc_json["chs_name"]
  85. self.blood = self.npc_json["blood"]
  86. self.light = self.npc_json.get("light",self.light)
  87. if "self_damage_blood" in self.npc_json.keys():
  88. self.self_damage_blood = self.npc_json["self_damage_blood"]
  89. self.speed = (max(2.5, self.npc_json["speed"]) * G.GAME_SQUARE) / 1000
  90. self.contact = self.npc_json["contact"]
  91. self.defense = self.npc_json["defense"]
  92. if "slime" in self.npc_json.keys():
  93. self.slime = self.npc_json["slime"]
  94. if "gifts" in self.npc_json.keys():
  95. self.gifts = self.npc_json["gifts"]
  96. if "death" in self.npc_json.keys():
  97. self.death = self.npc_json["death"]
  98. if "show_name" in self.npc_json.keys():
  99. self.chs_name_show = self.npc_json["show_name"]
  100. if "light" in self.npc_json.keys():
  101. self.light = self.npc_json["light"]
  102. if "chain" in self.npc_json.keys():
  103. self.chain = self.npc_json["chain"]
  104. if "chase_rad" in self.npc_json.keys():
  105. self.chase_rad = self.npc_json["chase_rad"]
  106. self.resent_dist = self.npc_json["resent_dist"] * G.GAME_SQUARE # 仇恨距离(像素)
  107. for n, s in enumerate(self.npc_json["skills"]):
  108. self.skill_names.append(s["name"])
  109. self.skill_init_times.append(s["init"])
  110. self.skill_intervals.append(s["interval"])
  111. self.skill_remains.append(s["max"])
  112. self.skill_params.append(s["params"] if "params" in s else [])
  113. self.skill_params_order.append(n)
  114. self.skill_spawns.append(s["spawns"] if "spawns" in s else [])
  115. self.skill_spawn_order.append(s["spawn_order"] if "spawn_order" in s else n)
  116. self.remain_blood = self.blood
  117. self.chase_path = dict()
  118. self.chs_name_text = self.chs_name_font.render(self.chs_name, True, (255, 128, 192))
  119. self.chs_name_text_shadow = self.chs_name_font.render(self.chs_name, True, (0, 0, 0))
  120. self.chs_name_text_half_width = self.chs_name_text.get_width() // 2
  121. self.character = self.load_character(self.npc_json["character"], color, dict())
  122. self.npc_time_init = pygame.time.get_ticks()
  123. def update(self):
  124. super().update()
  125. current_time = pygame.time.get_ticks()
  126. # self.update_npc_blood_display(current_time)
  127. self.chase_hero()
  128. # self.try_using_skills()
  129. def update_npc_blood_display(self, current_time):
  130. if G.DISPLAY_NPC_NAME_CARD and G.DISPLAY_NPC_BLOOD:
  131. self.chs_name_text = self.chs_name_font.render(str(self.remain_blood-self.fake_blood), True, (255, 128, 192))
  132. self.chs_name_text_shadow = self.chs_name_font.render(str(self.remain_blood-self.fake_blood), True, (0, 0, 0))
  133. self.chs_name_text_half_width = self.chs_name_text.get_width() // 2
  134. else:
  135. self.chs_name_text = self.chs_name_font.render(self.chs_name, True, (255, 128, 192))
  136. self.chs_name_text_shadow = self.chs_name_font.render(self.chs_name, True, (0, 0, 0))
  137. self.chs_name_text_half_width = self.chs_name_text.get_width() // 2
  138. def chase_hero(self):
  139. me = (level.current_level.me.x, level.current_level.me.y)
  140. if abs(self.x - me[0]) + abs(self.y - me[1]) <= self.chase_rad:
  141. self.set_motion()
  142. return
  143. now = (self.x, self.y)
  144. # 追踪玩家
  145. self.chase_path = aStar.cal_path(
  146. now, me,
  147. (level.current_level.district_square_grid["x1"], level.current_level.district_square_grid["y1"]),
  148. (level.current_level.district_square_grid["x2"], level.current_level.district_square_grid["y2"]),
  149. is_ghost=self.is_ghost
  150. )
  151. if now in self.chase_path.keys():
  152. next = self.chase_path[now]
  153. if next is None:
  154. self.set_motion()
  155. elif next[0] > self.x:
  156. self.set_motion("R")
  157. elif next[1] < self.y:
  158. self.set_motion("U")
  159. elif next[0] < self.x:
  160. self.set_motion("L")
  161. elif next[1] > self.y:
  162. self.set_motion("D")
  163. else:
  164. self.set_motion()
  165. def half_body_damage(self, point, cl, current_time):
  166. self.try_damage(cl.grid_damage_blood[point])
  167. def die(self):
  168. super().die()
  169. sound_player.play("npc_dead")
  170. def try_push(self, direction, offset=(0, 0)):
  171. pass
  172. def try_using_skills(self):
  173. pass
  174. def use_skill(self, idx: int):
  175. cl = level.current_level
  176. if self.npc_skill_time < self.skill_init_times[idx] or self.skill_remains[idx] == 0:
  177. # 未到初始时间 或技能冷却中 或次数已用完(-1则可以无限使用)
  178. return
  179. def set_restoration(self, to_x, to_y):
  180. self.x = to_x
  181. self.y = to_y
  182. def draw(self, screen: pygame.Surface):
  183. super().draw(screen)
  184. if G.DISPLAY_NPC_NAME_CARD and self.chs_name_show:
  185. self.chs_name_text_shadow.set_alpha(self.temporary_alpha)
  186. screen.blit(self.chs_name_text_shadow, (self.x_pos + 3 - self.chs_name_text_half_width, self.y_pos - 75))
  187. self.chs_name_text.set_alpha(self.temporary_alpha)
  188. screen.blit(self.chs_name_text, (self.x_pos + 2 - self.chs_name_text_half_width, self.y_pos - 75))
  189. if self.chain:
  190. 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)