effect_instance.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. import enum
  2. import pygame
  3. from game.algo import blender
  4. from game.const import game as G
  5. from game.effect import effect
  6. from game.frame import magic
  7. from game.level import level
  8. from game.sound import sound_player
  9. from game.file_loader.fileLoader import load_json, load_png
  10. class EffectInstance:
  11. def __init__(self, effect_name: str, user, follow: bool, effect_instances):
  12. super().__init__()
  13. self.user = user
  14. self.follow = follow
  15. self.effect_instances = effect_instances
  16. self.x = user.x
  17. self.y = user.y
  18. self.x_pos = self.x * G.GAME_SQUARE + G.HALF_GAME_SQUARE
  19. self.y_pos = self.y * G.GAME_SQUARE + G.HALF_GAME_SQUARE
  20. self.state = EffectState.NORMAL
  21. self.effect_time_init = pygame.time.get_ticks() # effect初始时间
  22. self.effect_time_max = 0 # effect最大时间
  23. self.effect_frame_timer = list() # 每个effect帧计时器(在0~interval之间循环)
  24. self.effect_frame_idx = list() # 每个effect帧索引
  25. self.effect = effect.get_effect(effect_name)
  26. for i in range(len(self.effect["magics"])):
  27. self.effect_frame_idx.append(0) # 为每一个effect图层创建一个idx索引
  28. self.effect_frame_timer.append(self.effect_time_init) # 为每一个magic_instance创建一个计时器
  29. self.effect_instances.append(self)
  30. def set_xy(self, x, y):
  31. self.x = x
  32. self.y = y
  33. self.x_pos = x * G.GAME_SQUARE + G.HALF_GAME_SQUARE
  34. self.y_pos = y * G.GAME_SQUARE + G.HALF_GAME_SQUARE
  35. def set_xy_pos(self, x_pos, y_pos):
  36. self.x_pos = x_pos
  37. self.y_pos = y_pos
  38. self.x = x_pos // G.GAME_SQUARE
  39. self.y = y_pos // G.GAME_SQUARE
  40. def set_max_time(self, max_time):
  41. for i in range(len(self.effect["magics"])):
  42. self.effect["magics"][i]["max_time"] = max_time
  43. def update(self):
  44. if self.state == EffectState.DEAD:
  45. self.effect_instances.remove(self)
  46. return
  47. if self.follow:
  48. self.x = self.user.x
  49. self.y = self.user.y
  50. self.x_pos = self.user.x_pos
  51. self.y_pos = self.user.y_pos
  52. current_time = pygame.time.get_ticks()
  53. self.update_frame(current_time)
  54. def update_frame(self, current_time):
  55. finished_num = 0
  56. for i in range(len(self.effect["magics"])):
  57. a_magic_instance = self.effect["magics"][i]
  58. repeat = a_magic_instance["repeat"]
  59. interval = a_magic_instance["interval"]
  60. frames = a_magic_instance["frames"]
  61. if current_time - self.effect_frame_timer[i] > interval:
  62. self.effect_frame_timer[i] = current_time
  63. self.effect_frame_idx[i] += 1
  64. if self.effect_frame_idx[i] >= len(frames): # 如果magic_instance播放完了
  65. if not repeat:
  66. finished_num += 1
  67. continue # 不重复 则不再播放
  68. self.effect_frame_idx[i] = 0 # 重复 则从头播放
  69. if finished_num == len(self.effect["magics"]):
  70. self.state = EffectState.DEAD
  71. def draw(self, screen: pygame.Surface):
  72. if self.state == EffectState.DEAD:
  73. return
  74. for i in range(len(self.effect["magics"])):
  75. a_magic_instance = self.effect["magics"][i]
  76. if "max_time" in a_magic_instance.keys():
  77. if pygame.time.get_ticks() - self.effect_time_init > a_magic_instance["max_time"]:
  78. continue # 对于超时的magic直接跳过
  79. frames = a_magic_instance["frames"]
  80. if self.effect_frame_idx[i] >= len(frames):
  81. continue # 对于播放完的magic,直接跳过
  82. frame = frames[self.effect_frame_idx[i]]
  83. cx = self.x_pos + frame.cx + a_magic_instance["cx"]
  84. cy = self.y_pos + frame.cy + a_magic_instance["cy"]
  85. if a_magic_instance["special_flag"] < 0:
  86. blender.blit(frame.image, frame.alpha, screen, (int(cx), int(cy)), a_magic_instance["special_flag"])
  87. else:
  88. screen.blit(frame.image, (cx, cy), special_flags=a_magic_instance["special_flag"])
  89. # BLENDMODE_NONE = 0
  90. # BLEND_ADD = 1
  91. # BLEND_SUB = 2
  92. # BLEND_MIN = 4
  93. # BLEND_MAX = 5
  94. # BLEND_PREMULTIPLIED = 17
  95. class EffectState(enum.Enum):
  96. NORMAL = 0
  97. DEAD = -1
  98. class ReadyGo:
  99. def __init__(self, effect_instances):
  100. self.effect_instances = effect_instances
  101. self.state = EffectState.NORMAL
  102. self.ready = pygame.image.load(G.GAME_PATH + "\\" + magic.MAGIC_IMG_ROOT + '/' + "magic0135_0_0.png").convert_alpha()
  103. self.go = pygame.image.load(G.GAME_PATH + "\\" + magic.MAGIC_IMG_ROOT + '/' + "magic0136_0_0.png").convert_alpha()
  104. self.line = pygame.image.load(G.GAME_PATH + "\\" + magic.MAGIC_IMG_ROOT + '/' + "magic0137_0_0.png").convert_alpha()
  105. self.frame_idx = -1
  106. self.init_time = pygame.time.get_ticks()
  107. self.current_time = pygame.time.get_ticks()
  108. self.time_interval = self.current_time
  109. self.ready_x_pos = (-600, -554, -508, -462, -416, -370, -324, -278, -232, -186, -140, -94, -48, -2, 44, 50, 40, 30, 20, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
  110. self.ready_cx = 120
  111. self.ready_cy = 200
  112. self.ready_alpha = (255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 251, 247, 243, 239, 235, 231, 227, 223, 219, 215, 211, 207, 203, 199, 195, 191, 187, 183, 179, 175, 171, 167, 163, 159, 155, 151, 147, 143, 139, 135, 131, 127, 123, 119, 115, 111, 107, 103, 99, 95, 91, 87, 83, 79, 75, 71, 67, 63, 59, 55, 51, 47, 43, 39, 35, 31, 27, 23, 19, 15, 0)
  113. self.go_x_pos = (-600, -600, -600, -600, -600, -600, -600, -600, -600, -600, -600, -600, -600, -600, -600, -600, -600, -600, -600, -600, -600, -600, -600, -600, -600, -600, -600, -600, -600, -600, -600, -600, -600, -600, -600, -600, -600, -600, -600, -600, -600, -600, -600, -600, -600, -600, -600, -600, -600, -600, -600, -554, -508, -462, -416, -370, -324, -278, -232, -186, -140, -94, -48, -2, 44, 50, 40, 30, 20, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
  114. self.go_cx = 200
  115. self.go_cy = 200
  116. self.go_alpha = (255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 230, 205, 180, 155, 130, 105, 80, 55, 30, 0)
  117. self.line_x_pos = (-600, -560, -520, -480, -440, -400, -360, -320, -280, -240, -200, -160, -120, -80, -40, 0, 24, 48, 72, 96, 120, 144, 168, 192, 216, 240, 264, 288, 312, 336, 360, 384, 408, 432, 456, 480, 504, 528, 552, 576, 600, 624, 648, 672, 696, 720, 720, 720, 720, 720, -600, -560, -520, -480, -440, -400, -360, -320, -280, -240, -200, -160, -120, -80, -40, 0, 24, 48, 72, 96, 120, 144, 168, 192, 216, 240, 264, 288, 312, 336, 360, 384, 408, 432, 456, 480, 504, 528, 552, 576, 600, 624, 648, 672, 696, 720, 720, 720, 720, 720, 720)
  118. self.line_cx = 160
  119. self.line_cy = 200
  120. self.line_alpha = (255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 245, 235, 225, 215, 205, 195, 185, 175, 165, 155, 145, 135, 125, 115, 105, 95, 85, 75, 65, 55, 45, 35, 25, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 245, 235, 225, 215, 205, 195, 185, 175, 165, 155, 145, 135, 125, 115, 105, 95, 85, 75, 65, 55, 45, 35, 25, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
  121. sound_player.play("ready_go")
  122. self.effect_instances.append(self)
  123. def update(self):
  124. if self.state == EffectState.DEAD:
  125. self.effect_instances.remove(self)
  126. return
  127. self.current_time = pygame.time.get_ticks()
  128. self.frame_idx = (self.current_time - self.init_time) // 18
  129. if level.current_level.score_board is not None:
  130. level.current_level.score_board.redraw_trigger = True
  131. if self.frame_idx >= 100:
  132. self.state = EffectState.DEAD
  133. return
  134. def draw(self, screen: pygame.Surface):
  135. self.ready.set_alpha(self.ready_alpha[self.frame_idx])
  136. screen.blit(self.ready, (self.ready_x_pos[self.frame_idx] + self.ready_cx, self.ready_cy))
  137. self.go.set_alpha(self.go_alpha[self.frame_idx])
  138. screen.blit(self.go, (self.go_x_pos[self.frame_idx] + self.go_cx, self.go_cy))
  139. self.line.set_alpha(self.line_alpha[self.frame_idx])
  140. screen.blit(self.line, (self.line_x_pos[self.frame_idx] + self.line_cx, self.line_cy), special_flags=1)
  141. class DistrictAlarm:
  142. def __init__(self, effect_instances):
  143. self.effect_instances = effect_instances
  144. self.img_alpha = (64, 128, 192, 255, 192, 128, 64)
  145. self.frame_idx = -1
  146. self.frame_timer = pygame.time.get_ticks()
  147. self.district_alarm_img = pygame.image.load(G.GAME_PATH + "\\" + "res/img/magic/magic0193_0_0.png")
  148. self.district_alarm_imgs: pygame.Surface = None
  149. self.load()
  150. def load(self):
  151. cl = level.current_level
  152. cl.district_alarming = True
  153. x1 = cl.district_square["x1"]
  154. x2 = cl.district_square["x2"]
  155. y1 = cl.district_square["y1"]
  156. y2 = cl.district_square["y2"]
  157. x_range = range(int((x2 - x1) / G.GAME_SQUARE) + 1)
  158. y_range = range(int((y2 - y1) / G.GAME_SQUARE) + 1)
  159. self.district_alarm_imgs = pygame.Surface((x2 - x1 + G.GAME_SQUARE, y2 - y1 + G.GAME_SQUARE), pygame.SRCALPHA, 32).convert_alpha()
  160. for x in x_range:
  161. for y in y_range:
  162. self.district_alarm_imgs.blit(self.district_alarm_img, (x * G.GAME_SQUARE, y * G.GAME_SQUARE))
  163. self.effect_instances.append(self)
  164. def update(self):
  165. current_time = pygame.time.get_ticks()
  166. if current_time - self.frame_timer > 50:
  167. self.frame_idx += 1
  168. self.frame_timer = current_time
  169. if self.frame_idx >= 7:
  170. level.current_level.district_alarming = False
  171. self.effect_instances.remove(self)
  172. def draw(self, screen: pygame.Surface):
  173. cl = level.current_level
  174. self.district_alarm_imgs.set_alpha(self.img_alpha[self.frame_idx])
  175. screen.blit(
  176. self.district_alarm_imgs,
  177. (cl.district_square["x1"] - G.HALF_GAME_SQUARE, cl.district_square["y1"] - G.HALF_GAME_SQUARE)
  178. )
  179. class Point:
  180. def __init__(self, x, y):
  181. self.x = x
  182. self.y = y
  183. class EnvironmentEffect:
  184. def __init__(self, effect_instances, effect_name):
  185. self.effect_instances = effect_instances
  186. self.frame_index = 0
  187. self.state = EffectState.NORMAL
  188. self.last_frame_time = pygame.time.get_ticks()
  189. self.effect_name = effect_name
  190. self.current_frame = None
  191. effect_json = load_json("game/environment" + '/' + self.effect_name + ".json")
  192. self.rain_images = [pygame.image.load("res/img/environment/" + img).convert_alpha() for img in effect_json["IMG"]]
  193. self.cx = effect_json["CX"]
  194. self.cy = effect_json["CY"]
  195. self.interval = effect_json["INTERVAL"]
  196. self.num_frames = len(effect_json["IMG"])
  197. self.effect_instances.append(self)
  198. def update(self):
  199. current_time = pygame.time.get_ticks()
  200. if current_time - self.last_frame_time >= self.interval:
  201. if self.frame_index + 1 >= self.num_frames:
  202. self.frame_index = 0
  203. else:
  204. self.frame_index += 1
  205. self.last_frame_time = current_time
  206. if len(G.ENVIRONMENT_EFFECT) <= 0:
  207. self.state = EffectState.DEAD
  208. self.effect_instances.remove(self)
  209. return
  210. def draw(self, screen: pygame.Surface):
  211. self.current_frame = self.rain_images[self.frame_index]
  212. cx = self.cx[self.frame_index]
  213. cy = self.cy[self.frame_index]
  214. screen.blit(self.current_frame, (cx, cy))