effect_instance.py 12 KB

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