ghost.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import json
  2. import pygame
  3. from client_local.algo import aStar
  4. from client_local.const import game as G
  5. from client_local.const import color as C
  6. from client_local.level import level
  7. from client_local.sprite.player import Player
  8. from client_local.file_loader.fileLoader import load_json, load_png
  9. # 死亡npc的
  10. class Ghost(Player):
  11. def __init__(self, npc_name, xy, time, speed, contact, color=C.CHARACTER_RED):
  12. super(Ghost, self).__init__(npc_name, xy, color)
  13. self.npc_json = None
  14. self.ghost_time_init = 0
  15. self.time = int(time) # 总存活时间
  16. self.remain_time = 0 # 剩余存活时间
  17. self.speed = speed * G.GAME_SQUARE / 1000 # 移动速度
  18. self.contact = int(contact) # 接触伤害
  19. self.me = level.current_level.me
  20. self.chase_path = dict()
  21. self.load_ghost(npc_name, color)
  22. self.wall_walking = True
  23. level.current_level.recal_ghost_paths = True
  24. def load_ghost(self, npc_name, color):
  25. self.npc_json = load_json("game/npc/" + npc_name + ".json")
  26. self.color = C.resolve_character_colour(self.npc_json.get("colour"), color)
  27. self.character = self.load_character(self.npc_json["character"], self.color, dict(), True)
  28. self.ghost_time_init = pygame.time.get_ticks()
  29. self.remain_time = 1 if self.time <= 0 else self.time
  30. def update(self):
  31. super().update()
  32. current_time = pygame.time.get_ticks()
  33. self.chase_hero(current_time)
  34. self.contact_damage()
  35. self.update_remain_time(current_time)
  36. def chase_hero(self, current_time):
  37. if self.me.swallow_attack is not None:
  38. self.set_motion()
  39. return
  40. me = (level.current_level.me.x, level.current_level.me.y)
  41. now = (self.x, self.y)
  42. # 如果重新计算路径标志为True则重新追踪玩家
  43. if level.current_level.recal_ghost_paths:
  44. self.chase_path = aStar.cal_path(
  45. now,
  46. me,
  47. (level.current_level.district_square_grid["x1"], level.current_level.district_square_grid["y1"]),
  48. (level.current_level.district_square_grid["x2"], level.current_level.district_square_grid["y2"]),
  49. True
  50. )
  51. if now in self.chase_path.keys():
  52. next = self.chase_path[now]
  53. if next is None:
  54. self.set_motion()
  55. elif next[0] > self.x:
  56. self.set_motion("R")
  57. elif next[1] < self.y:
  58. self.set_motion("U")
  59. elif next[0] < self.x:
  60. self.set_motion("L")
  61. elif next[1] > self.y:
  62. self.set_motion("D")
  63. else:
  64. self.set_motion()
  65. def contact_damage(self):
  66. # 检查npc与人物的接触伤害
  67. if level.current_level.me.x == self.x and level.current_level.me.y == self.y:
  68. level.current_level.me.try_damage(self.contact, "C")
  69. def update_remain_time(self, current_time):
  70. if self.time <= 0:
  71. # 如果区域计时或永存,则不计算剩余时间
  72. return
  73. self.remain_time = self.time - (current_time - self.ghost_time_init)
  74. def try_push(self, direction, offset=(0, 0)):
  75. pass
  76. def set_restoration(self, to_x, to_y):
  77. self.x = to_x
  78. self.y = to_y
  79. def draw(self, screen: pygame.Surface):
  80. super().draw(screen)