character.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import pygame
  2. from game.algo import blender
  3. from game.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. 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 = load_png(CHARACTER_IMG_ROOT + type + '/' + frames["IMG"][i])
  47. # img = pygame.image.load(CHARACTER_IMG_ROOT + type + '/' + frames["IMG"][i]).convert_alpha()
  48. if is_ghost:
  49. img.fill((192, 192, 192), special_flags=pygame.BLEND_SUB)
  50. pygame.surfarray.pixels_alpha(img)[...] = pygame.surfarray.pixels_alpha(img)[...] * 0.5
  51. if component in CHARACTER_COMPONENTS_MASKED:
  52. # 使用叠加的方式进行染色
  53. img = blender.color_overlay(img, color, True)
  54. cx = frames["CX"][i]
  55. cy = frames["CY"][i]
  56. a_color[orient][component].append(Frame(img, cx, cy))
  57. if orient == "LOSE":
  58. continue
  59. for component in DECORATION_CATEGORIES:
  60. if component in decorations.keys():
  61. # 删除已有的xxx和xxx_m
  62. if component in a_color[orient]:
  63. del a_color[orient][component]
  64. if component + "_m" in a_color[orient]:
  65. del a_color[orient][component + "_m"]
  66. if component == "Cladorn":
  67. if "Cloth" in a_color[orient]:
  68. del a_color[orient]["Cloth"]
  69. if "Cloth_m" in a_color[orient]:
  70. del a_color[orient]["Cloth_m"]
  71. # 读取新的decoration
  72. load_decoration_img(component, a_color, orient, decorations)
  73. return a_color
  74. def load_decoration_img(category: str, a_color, orient, decorations: dict):
  75. a_color[orient][category] = list()
  76. frames = decorations[category][orient]
  77. # 对于每一帧 加载图片
  78. size = len(frames["IMG"])
  79. type = category.lower()
  80. for i in range(size):
  81. # print(CHARACTER_IMG_ROOT + type + '/' + frames["IMG"][i])
  82. img = pygame.image.load(CHARACTER_IMG_ROOT + type + '/' + frames["IMG"][i]).convert_alpha()
  83. cx = frames["CX"][i]
  84. cy = frames["CY"][i]
  85. a_color[orient][category].append(Frame(img, cx, cy))
  86. class Frame:
  87. def __init__(self, image: pygame.Surface, cx, cy):
  88. self.image = image
  89. self.cx = cx
  90. self.cy = cy