character.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import pygame
  2. from game.algo import blender
  3. import game.const.game as G
  4. CHARACTER_IMG_ROOT = "res/img/"
  5. CHARACTER_ORIENTS = ("STAND_R", "STAND_U", "STAND_L", "STAND_D", "R", "U", "L", "D", "LOSE")
  6. CHARACTER_COMPONENTS = {
  7. "R": ("Body", "Body_m", "Foot", "Leg", "Leg_m", "Cloth", "Cloth_m", "Cladorn", "Face", "Hair", "Hair_m", "Eye", "Ear", "Mouth", "Cap", "Cap_m", "Fhadorn", "Npack", "Npack_m", "Fpack", "Thadorn"),
  8. "U": ("Body", "Body_m", "Foot", "Leg", "Leg_m", "Cloth", "Cloth_m", "Cladorn", "Face", "Eye", "Ear", "Mouth", "Hair", "Hair_m", "Cap", "Cap_m", "Fhadorn", "Npack", "Npack_m", "Fpack", "Thadorn"),
  9. "L": ("Thadorn", "Body", "Body_m", "Foot", "Leg", "Leg_m", "Cloth", "Cloth_m", "Cladorn", "Npack", "Npack_m", "Face", "Hair", "Hair_m", "Eye", "Ear", "Mouth", "Cap", "Cap_m", "Fhadorn", "Fpack"),
  10. "D": ("Fpack", "Npack", "Npack_m", "Body", "Body_m", "Foot", "Leg", "Leg_m", "Cloth", "Cloth_m", "Cladorn", "Face", "Hair", "Hair_m", "Eye", "Ear", "Mouth", "Cap", "Cap_m", "Fhadorn", "Thadorn")
  11. }
  12. CHARACTER_COMPONENTS_MASKED = ("Body_m", "Cloth_m", "Hair_m", "Leg_m", "Npack_m", "Cap_m")
  13. DECORATION_CATEGORIES = ("Cap", "Hair", "Eye", "Ear", "Mouth", "Cladorn", "Fpack", "Npack", "Thadorn", "Footprint")
  14. characters = {}
  15. # 通过name和color获取body序列。
  16. # name是一级键,取自body_json的NAME;color是二级键,取自color常量
  17. def get_character(character_json, color, decorations: dict, is_ghost=False):
  18. name = character_json["NAME"] + "_ghost" if is_ghost else character_json["NAME"]
  19. if name not in characters.keys():
  20. characters[name] = dict()
  21. if color not in characters[name]:
  22. a_color = load_color(character_json, color, decorations, is_ghost)
  23. characters[name][color] = a_color
  24. return characters[name][color]
  25. # 通过color常量和body_json创建body序列,用于生成get_body的二级键的值
  26. def load_color(character_json, color, decorations: dict, is_ghost):
  27. a_color = {}
  28. # 遍历朝向
  29. for orient in CHARACTER_ORIENTS:
  30. if orient not in character_json.keys():
  31. # 如果character没有这个方向,则跳过不加载
  32. continue
  33. a_color[orient] = dict()
  34. a_color[orient]["Cx"] = character_json[orient]["Cx"] # 获取每个朝向的x偏移
  35. a_color[orient]["Cy"] = character_json[orient]["Cy"] # 获取每个朝向的y偏移
  36. # 遍历身体部件
  37. for component in CHARACTER_COMPONENTS["D"]:
  38. if component in character_json[orient].keys():
  39. a_color[orient][component] = list()
  40. type = component.lower()
  41. frames = character_json[orient][component]
  42. # 对于每一帧 加载图片
  43. size = len(character_json[orient][component]["IMG"])
  44. for i in range(size):
  45. # print(CHARACTER_IMG_ROOT + type + '/' + frames["IMG"][i])
  46. img = pygame.image.load(G.GAME_PATH + "\\" + CHARACTER_IMG_ROOT + type + '/' + frames["IMG"][i]).convert_alpha()
  47. if is_ghost:
  48. img.fill((192, 192, 192), special_flags=pygame.BLEND_SUB)
  49. pygame.surfarray.pixels_alpha(img)[...] = pygame.surfarray.pixels_alpha(img)[...] * 0.5
  50. if component in CHARACTER_COMPONENTS_MASKED:
  51. # 使用叠加的方式进行染色
  52. img = blender.color_overlay(img, color, True)
  53. cx = frames["CX"][i]
  54. cy = frames["CY"][i]
  55. a_color[orient][component].append(Frame(img, cx, cy))
  56. if orient == "LOSE":
  57. continue
  58. for component in DECORATION_CATEGORIES:
  59. if component in decorations.keys():
  60. # 删除已有的xxx和xxx_m
  61. if component in a_color[orient]:
  62. del a_color[orient][component]
  63. if component + "_m" in a_color[orient]:
  64. del a_color[orient][component + "_m"]
  65. if component == "Cladorn":
  66. if "Cloth" in a_color[orient]:
  67. del a_color[orient]["Cloth"]
  68. if "Cloth_m" in a_color[orient]:
  69. del a_color[orient]["Cloth_m"]
  70. # 读取新的decoration
  71. load_decoration_img(component, a_color, orient, decorations)
  72. return a_color
  73. def load_decoration_img(category: str, a_color, orient, decorations: dict):
  74. a_color[orient][category] = list()
  75. frames = decorations[category][orient]
  76. # 对于每一帧 加载图片
  77. size = len(frames["IMG"])
  78. type = category.lower()
  79. for i in range(size):
  80. # print(CHARACTER_IMG_ROOT + type + '/' + frames["IMG"][i])
  81. img = pygame.image.load(G.GAME_PATH + "\\" + CHARACTER_IMG_ROOT + type + '/' + frames["IMG"][i]).convert_alpha()
  82. cx = frames["CX"][i]
  83. cy = frames["CY"][i]
  84. a_color[orient][category].append(Frame(img, cx, cy))
  85. class Frame:
  86. def __init__(self, image: pygame.Surface, cx, cy):
  87. self.image = image
  88. self.cx = cx
  89. self.cy = cy