ghost.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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.character = self.load_character(self.npc_json["character"], color, dict(), True)
  27. self.ghost_time_init = pygame.time.get_ticks()
  28. self.remain_time = 1 if self.time <= 0 else self.time
  29. def update(self):
  30. super().update()
  31. current_time = pygame.time.get_ticks()
  32. self.chase_hero(current_time)
  33. self.contact_damage()
  34. self.update_remain_time(current_time)
  35. def chase_hero(self, current_time):
  36. if self.me.swallow_attack is not None:
  37. self.set_motion()
  38. return
  39. me = (level.current_level.me.x, level.current_level.me.y)
  40. now = (self.x, self.y)
  41. # 如果重新计算路径标志为True则重新追踪玩家
  42. if level.current_level.recal_ghost_paths:
  43. self.chase_path = aStar.cal_path(
  44. now,
  45. me,
  46. (level.current_level.district_square_grid["x1"], level.current_level.district_square_grid["y1"]),
  47. (level.current_level.district_square_grid["x2"], level.current_level.district_square_grid["y2"]),
  48. True
  49. )
  50. if now in self.chase_path.keys():
  51. next = self.chase_path[now]
  52. if next is None:
  53. self.set_motion()
  54. elif next[0] > self.x:
  55. self.set_motion("R")
  56. elif next[1] < self.y:
  57. self.set_motion("U")
  58. elif next[0] < self.x:
  59. self.set_motion("L")
  60. elif next[1] > self.y:
  61. self.set_motion("D")
  62. else:
  63. self.set_motion()
  64. def contact_damage(self):
  65. # 检查npc与人物的接触伤害
  66. if level.current_level.me.x == self.x and level.current_level.me.y == self.y:
  67. level.current_level.me.try_damage(self.contact, "C")
  68. def update_remain_time(self, current_time):
  69. if self.time <= 0:
  70. # 如果区域计时或永存,则不计算剩余时间
  71. return
  72. self.remain_time = self.time - (current_time - self.ghost_time_init)
  73. def try_push(self, direction, offset=(0, 0)):
  74. pass
  75. def set_restoration(self, to_x, to_y):
  76. self.x = to_x
  77. self.y = to_y
  78. def draw(self, screen: pygame.Surface):
  79. super().draw(screen)