character.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import pygame
  2. from client_local.algo import blender
  3. from client_local.file_loader.fileLoader import load_json, load_png
  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. decoration_key = tuple(sorted(
  20. (component, config.get("NAME", component))
  21. for component, config in decorations.items()
  22. ))
  23. cache_key = (color, decoration_key)
  24. if name not in characters.keys():
  25. characters[name] = dict()
  26. if cache_key not in characters[name]:
  27. a_color = load_color(character_json, color, decorations, is_ghost)
  28. characters[name][cache_key] = a_color
  29. return characters[name][cache_key]
  30. # 通过color常量和body_json创建body序列,用于生成get_body的二级键的值
  31. def load_color(character_json, color, decorations: dict, is_ghost):
  32. a_color = {}
  33. # 遍历朝向
  34. for orient in CHARACTER_ORIENTS:
  35. if orient not in character_json.keys():
  36. # 如果character没有这个方向,则跳过不加载
  37. continue
  38. a_color[orient] = dict()
  39. a_color[orient]["Cx"] = character_json[orient]["Cx"] # 获取每个朝向的x偏移
  40. a_color[orient]["Cy"] = character_json[orient]["Cy"] # 获取每个朝向的y偏移
  41. # 遍历身体部件
  42. for component in CHARACTER_COMPONENTS["D"]:
  43. if component in character_json[orient].keys():
  44. a_color[orient][component] = list()
  45. type = component.lower()
  46. frames = character_json[orient][component]
  47. # 对于每一帧 加载图片
  48. size = len(character_json[orient][component]["IMG"])
  49. for i in range(size):
  50. # print(CHARACTER_IMG_ROOT + type + '/' + frames["IMG"][i])
  51. img = load_png(CHARACTER_IMG_ROOT + type + '/' + frames["IMG"][i])
  52. # img = pygame.image.load(CHARACTER_IMG_ROOT + type + '/' + frames["IMG"][i]).convert_alpha()
  53. if is_ghost:
  54. img.fill((192, 192, 192), special_flags=pygame.BLEND_SUB)
  55. pygame.surfarray.pixels_alpha(img)[...] = pygame.surfarray.pixels_alpha(img)[...] * 0.5
  56. if component in CHARACTER_COMPONENTS_MASKED:
  57. # 使用叠加的方式进行染色
  58. img = blender.color_overlay(img, color, True)
  59. cx = frames["CX"][i]
  60. cy = frames["CY"][i]
  61. a_color[orient][component].append(Frame(img, cx, cy))
  62. if orient == "LOSE":
  63. continue
  64. for component in DECORATION_CATEGORIES:
  65. if component in decorations.keys():
  66. # 删除已有的xxx和xxx_m
  67. if component in a_color[orient]:
  68. del a_color[orient][component]
  69. if component + "_m" in a_color[orient]:
  70. del a_color[orient][component + "_m"]
  71. if component == "Cladorn":
  72. if "Cloth" in a_color[orient]:
  73. del a_color[orient]["Cloth"]
  74. if "Cloth_m" in a_color[orient]:
  75. del a_color[orient]["Cloth_m"]
  76. # 读取新的decoration
  77. load_decoration_img(component, a_color, orient, decorations)
  78. return a_color
  79. def load_decoration_img(category: str, a_color, orient, decorations: dict):
  80. a_color[orient][category] = list()
  81. frames = decorations[category][orient]
  82. # 对于每一帧 加载图片
  83. size = len(frames["IMG"])
  84. type = category.lower()
  85. for i in range(size):
  86. # print(CHARACTER_IMG_ROOT + type + '/' + frames["IMG"][i])
  87. img = pygame.image.load(CHARACTER_IMG_ROOT + type + '/' + frames["IMG"][i]).convert_alpha()
  88. cx = frames["CX"][i]
  89. cy = frames["CY"][i]
  90. a_color[orient][category].append(Frame(img, cx, cy))
  91. class Frame:
  92. def __init__(self, image: pygame.Surface, cx, cy):
  93. self.image = image
  94. self.cx = cx
  95. self.cy = cy