WTS/game/scripts/doll/body.rpy

83 lines
2.4 KiB
Plaintext
Raw Normal View History

2022-05-16 23:48:22 +00:00
init python:
class DollBody(DollMethods):
layer_types = {
# Body class has no use for layer types
}
2022-05-16 23:48:22 +00:00
layer_modifiers = {
"zorder": None,
}
def __init__(self, obj):
2022-05-16 23:48:22 +00:00
self.char = obj
self.layers = None
self.hue = HueMatrix(0)
self.zorder = 0
2022-05-16 23:48:22 +00:00
def get_layers(self):
path = os.path.join("characters", self.char.name, self.char.pose, "body")
2022-05-16 23:48:22 +00:00
extensions = self.extensions
modifiers = self.layer_modifiers
2022-05-16 23:48:22 +00:00
layers = {}
for f in renpy.list_files():
fp, fn = os.path.split(f)
fn, ext = os.path.splitext(fn)
2022-05-16 23:48:22 +00:00
if not fp == path or not ext in extensions:
continue
2022-05-16 23:48:22 +00:00
_, *tails = fn.rsplit("_")
2022-05-16 23:48:22 +00:00
zorder = self.zorder
2022-05-16 23:48:22 +00:00
if tails:
lmodifier, *tails = tails
2022-05-16 23:48:22 +00:00
if not lmodifier in modifiers:
print("Invalid modifier for file: {}".format(f))
continue
zorder_mod = modifiers.get(lmodifier)
zorder = (zorder + int(zorder_mod)) if lmodifier != "zorder" else int(tails[-1])
2022-05-16 23:48:22 +00:00
layers.setdefault(fn, [f, zorder])
2022-05-16 23:48:22 +00:00
return layers
2022-05-16 23:48:22 +00:00
@functools.cache
def build_image(self, matrix=None):
self.layers = layers = self.layers or self.get_layers()
sprites = []
2022-05-16 23:48:22 +00:00
if matrix is None:
matrix = self.hue
2022-05-16 23:48:22 +00:00
processors = {
"default": lambda file: Transform(Image(file), matrixcolor=matrix),
}
2022-05-16 23:48:22 +00:00
for identifier, (file, zorder) in layers.items():
processor = processors.get(identifier, processors["default"])
processed_file = processor(file)
sprites.append((identifier, processed_file, zorder))
2022-05-16 23:48:22 +00:00
return sprites
2022-05-16 23:48:22 +00:00
@property
def image(self):
if not renpy.is_skipping():
if not self._image_cached:
self._image_cached = True
2022-05-16 23:48:22 +00:00
sprites = self.build_image(self.hue)
sprites.sort(key=itemgetter(2))
sprites = [x[1] for x in sprites]
2022-05-16 23:48:22 +00:00
self._image = Fixed(*sprites, fit_first=True)
return self._image
2022-05-16 23:48:22 +00:00
def rebuild_image(self):
self._image_cached = False