wanderer.py 6.1 KB

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