wanderer.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import json
  2. import pygame
  3. import random
  4. from client_local.algo import aStar
  5. from client_local.const import game as G
  6. from client_local.const import color as C
  7. from client_local.level import level
  8. from client_local.sound import sound_player
  9. from client_local.sprite.npc import Npc
  10. from client_local.sprite.player import Player, PlayerState
  11. from client_local.file_loader.fileLoader import load_json, load_png
  12. # 常驻地图的怪
  13. class Wanderer(Npc):
  14. def __init__(self, npc_name, xy, color=C.CHARACTER_RED, patrol_path=None):
  15. super(Wanderer, self).__init__(npc_name, xy, color, patrol_path=patrol_path or None)
  16. def update(self):
  17. super(Npc, self).update()
  18. if not self.is_dead:
  19. current_time = pygame.time.get_ticks()
  20. self.update_npc_blood_display(current_time)
  21. if self.has_patrol:
  22. cl = level.current_level
  23. self.patrol(((0, 0), (cl.map_x - 1, cl.map_y - 1)))
  24. else:
  25. self.wander_and_detect(current_time)
  26. self.chase_hero()
  27. self.contact_damage()
  28. self.try_using_skills()
  29. self.throw()
  30. def wander_and_detect(self, current_time):
  31. me = level.current_level.me
  32. swallow = me.swallow_attack
  33. if ((self.resentful or self.mocking) and not self.friendly
  34. and (swallow is None or (not swallow.require_aggro and swallow.user is not self))):
  35. return
  36. if current_time - self.npc_time_init < 300:
  37. return
  38. if (current_time - self.last_motion) >= 50 * self.last_random:
  39. if self.speed >= (5.8 * G.GAME_SQUARE) / 1000:
  40. self.last_random = random.randint(5, 15)
  41. if 0 <= (current_time % 1200) <= 300 or (random.randint(1, 100) >= 85):
  42. self.last_motion = current_time
  43. if random.randint(1, 100) >= 25:
  44. motion = self.idx_motion[random.randint(0, 3)]
  45. self.set_motion(motion)
  46. else:
  47. self.set_motion(self.orientation)
  48. else:
  49. self.set_motion(self.orientation)
  50. else:
  51. self.set_motion(self.orientation)
  52. if swallow is not None and swallow.user is self:
  53. return
  54. # 技能初始时间更新
  55. for i in range(len(self.skill_names)):
  56. # 游荡状态下,技能初始时间永远等于当前时刻加init值
  57. self.skill_init_times[i] = self.npc_json["skills"][i].get("init", 0) + current_time
  58. self.is_skill_init = True
  59. if me.state == PlayerState.NORMAL:
  60. 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():
  61. # 如果玩家活着且距离小于临界值 且不被障碍阻碍视野 则产生玩家仇恨
  62. self.resentful = True
  63. level.current_level.recal_npc_paths = True
  64. def chase_hero(self):
  65. if (not self.resentful and not self.mocking) or self.friendly or self.imitating or (self.speed == 0 and self.original_speed == 0):
  66. return
  67. hero = level.current_level.me
  68. if hero.swallow_attack is not None and not (self.free_chasing or self.chasing_monster):
  69. if hero.swallow_attack.user is not self:
  70. self.set_motion()
  71. return
  72. me = (hero.x, hero.y)
  73. if self.free_chasing:
  74. me = self.chasing_point
  75. if self.chasing_monster and self.chasing_monster_target is not None:
  76. me = (self.chasing_monster_target.x, self.chasing_monster_target.y)
  77. # 玩家在糖泡中 定身Npc
  78. 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:
  79. self.set_motion()
  80. return
  81. now = (self.x, self.y)
  82. # 如果重新计算路径标志为True则重新追踪玩家
  83. if level.current_level.recal_npc_paths:
  84. self.chase_path = aStar.cal_path(
  85. now, me,
  86. (max(0, level.current_level.me.x - 15), max(0, level.current_level.me.y - 15)),
  87. (min(G.CURRENT_MAP_WIDTH-1, level.current_level.me.x + 15), min(G.CURRENT_MAP_HEIGHT-1, level.current_level.me.y + 15)),
  88. is_ghost=self.is_ghost
  89. )
  90. if now in self.chase_path.keys():
  91. _next = self.chase_path[now]
  92. if _next is None:
  93. self.set_motion()
  94. elif _next[0] > self.x:
  95. self.set_motion("R")
  96. elif _next[1] < self.y:
  97. self.set_motion("U")
  98. elif _next[0] < self.x:
  99. self.set_motion("L")
  100. elif _next[1] > self.y:
  101. self.set_motion("D")
  102. else:
  103. self.set_motion()
  104. def die(self):
  105. super(Npc, self).die()
  106. sound_player.play("npc_dead")
  107. if self.blood_bar_pos != 0:
  108. try:
  109. level.current_level.ui_instances_top.remove(self.blood_bar)
  110. level.current_level.ui_instances_top.remove(self.profile)
  111. level.current_level.boss_blood_bar_dic[self.blood_bar_pos] = 0
  112. except Exception as e:
  113. print(e)
  114. try:
  115. print("血条错误", self.chs_name, self.blood_bar_pos)
  116. except Exception as e:
  117. print(f"npc.py: die(): {e}")
  118. self.is_dead = True
  119. def die_quietly(self):
  120. super(Npc, self).die()
  121. if self.blood_bar_pos != 0:
  122. try:
  123. level.current_level.ui_instances_top.remove(self.blood_bar)
  124. level.current_level.ui_instances_top.remove(self.profile)
  125. level.current_level.boss_blood_bar_dic[self.blood_bar_pos] = 0
  126. except:
  127. try:
  128. print("血条错误", self.chs_name, self.blood_bar_pos)
  129. except:
  130. pass
  131. self.is_dead = True
  132. self.remain_blood = -1
  133. def handle_direction_movement(self, direction, speed):
  134. right, right_grid, top, top_grid, left, left_grid, bottom, bottom_grid = self.get_ruld_block()
  135. cl = level.current_level
  136. block = cl.block
  137. if direction == "R":
  138. self.movement_right(False, speed, top, bottom, right, block, top_grid, bottom_grid, cl)
  139. elif direction == "U":
  140. self.movement_up(False, speed, top, left, right, block, left_grid, right_grid, cl)
  141. elif direction == "L":
  142. self.movement_left(False, speed, top, bottom, left, block, top_grid, bottom_grid, cl)
  143. elif direction == "D":
  144. self.movement_down(False, speed, bottom, left, right, block, left_grid, right_grid, cl)