WTS/game/scripts/doll/chibi.rpy

351 lines
12 KiB
Plaintext
Raw Normal View History

2022-05-16 23:48:22 +00:00
init 5 python:
class DollChibi(renpy.Displayable):
2022-05-16 23:48:22 +00:00
def __init__(self, name, pose=None, layer="screens", zorder=12, zoom=0.28, *args, **properties):
2022-05-16 23:48:22 +00:00
super(DollChibi, self).__init__(**properties)
self.name = name
self.pose = pose
self.idle = "stand"
2022-05-16 23:48:22 +00:00
self.layer = layer
self.zorder = zorder
self.zoom = zoom
self.pos = (0,0)
self.xzoom = 1
self.char = eval(name)
self.poses = {}
self.animation = None
2022-05-16 23:48:22 +00:00
# Animation
self.anim_frames = None
self.anim_sprites = None
self.anim_prev_sprites = None
2022-05-16 23:48:22 +00:00
self.anim_speed = 1.0
self.anim_fps = 8.0
self.anim_trans = None
self.anim_interval = None
self.anim_interval_total = None
# ATL
self.atl_time = 0
self.atl_time_total = 0
self.atl_partial = None
self.atl_looping = False
self.atl_pause = False
self.atl_at_list = []
2022-05-16 23:48:22 +00:00
def register_pose(self, pose, frames):
self.poses[pose] = frames
print(f"Registered \"{pose}\" pose for {self.name}")
@functools.cache
def build_image(self, hash, pose, frame):
states = self.char.states
items = [v[0] for v in states.values() if v[0] and v[2]]
sprites = []
for i in items:
subpath = posixpath.join("chibi", pose, str(frame))
sprites.extend(i.build_image(i._hash, subpath))
masks = [sprites.pop(sprites.index(x)) for x in sprites if x[0] == "mask"]
sprites.sort(key=itemgetter(2))
masks.sort(key=itemgetter(2))
back_sprites = [x[1] for x in sprites if x[2] < 0]
#Apply alpha mask
for m in masks:
_, mask, mask_zorder = m
for i, s in enumerate(sprites):
_, sprite, sprite_zorder = s
if i < 1 or mask_zorder > sprite_zorder:
continue
masked = AlphaMask(Fixed(*(x[1] for x in sprites[:i]), fit_first=True), mask)
sprites = sprites[i:]
sprites.insert(0, (None, masked, mask_zorder))
break
sprites = back_sprites + [x[1] for x in sprites]
return Fixed(*sprites, fit_first=True)
2022-05-16 23:48:22 +00:00
def build_animation(self):
pose = self.pose
frames = self.poses[pose]
sprites = [self.build_image(self.char._hash, pose, i) for i in range(frames)]
2022-05-16 23:48:22 +00:00
if frames > 1:
interval = self.anim_speed / self.anim_fps
else:
interval = (365.25 * 86400.0) # About one year
self.anim_frames = frames
self.anim_interval = interval
self.anim_interval_total = (frames * interval)
self.anim_sprites = sprites
self.anim_prev_sprites = [ sprites[-1] ] + sprites[:-1]
2022-05-16 23:48:22 +00:00
def render(self, width, height, st, at):
# Animation Renderer
t = st % self.anim_interval_total
trans = self.anim_trans
interval = self.anim_interval
# Trigger event after animation time elapses
if st > self.atl_time_total:
renpy.timeout(0)
for image, prev in zip(self.anim_sprites, self.anim_prev_sprites):
2022-05-16 23:48:22 +00:00
if t < interval:
if not renpy.game.less_updates:
renpy.redraw(self, interval - t)
if trans and st >= interval:
image = trans(old_widget=prev, new_widget=image)
im = renpy.render(image, width, height, t, at)
width, height = im.get_size()
rv = renpy.Render(width, height)
rv.blit(im, (0, 0))
return rv
else:
t = t - interval
def event(self, ev, x, y, st):
# Determine pose change if show time exceeds animation time.
# Looping animations ignore this check because they are rebuilt every loop.
if renpy.in_rollback():
# We don't want to use 'raise renpy.IgnoreEvent' because it blocks events in other displayables, including interfaces
return
elif renpy.is_skipping():
self.stop()
return
elif ((not self.atl_looping and st > self.atl_time_total)
or (ev.type == pygame.MOUSEBUTTONUP and ev.button == 1)
or (ev.type == pygame.KEYDOWN and ev.key in [pygame.K_RETURN, pygame.K_SPACE, pygame.K_KP_ENTER, pygame.K_SELECT])):
self.stop()
# def per_interact(self):
# # Handle interrupt events
# if (renpy.is_skipping() or self.atl_pause) and not self.pose == self.idle:
# self.stop()
2022-05-16 23:48:22 +00:00
def set_pose(self, pose):
if self.pose == pose:
return
self.pose = pose
self.update()
def move(self, path, speed=1.0, pause=True, loop=False, warper="linear", at_list=[], pose="walk", repeat=None, wrap=True, reverse=True):
2022-05-16 23:48:22 +00:00
"""Makes chibi move"""
self.atl_looping = loop
self.atl_pause = pause
self.atl_at_list = at_list
2022-05-16 23:48:22 +00:00
if isinstance(path, tuple):
path = [path]
# If 'A' position is not supplied for A -> B movement, use last known position instead.
if len(path) < 2:
path = [self.pos] + path
# If reverse and loop/repeat is True, and pathing has more than two entries,
# use complex looping by joining the reversed paths
elif reverse and (loop or repeat):
# Reverses the list, strips last entry from the reverse list, and finally joins them.
path += path[1::-1]
if repeat:
path *= repeat
if wrap:
# Wrap pathing back to starting position
path.append(path[0])
2022-05-16 23:48:22 +00:00
self.set_pose(pose)
# Note: Warper names and their count can change over time,
# so it's easier to just evaluate the input.
# List of available warpers:
# https://www.renpy.org/doc/html/atl.html?#warpers
warper = eval(f"_warper.{warper}")
2022-05-16 23:48:22 +00:00
distances = []
times = []
if loop:
# Append first position as last to create a looped path.
path.append(path[0])
# Calculate distances and timings using euclidean distance algorithm.
for xy1, xy2 in zip(path, path[1:]):
x1, y1 = xy1
x2, y2 = xy2
distance = math.hypot(x2 - x1, y2 - y1)
time = distance / (100.0 * speed)
distances.append(distance)
times.append(time)
# Calculate total ATL time required to reach the destination
total_time = sum(times)
self.atl_time_total = total_time
# Recalculate animation intervals when necessary, including speed factors.
frames = self.anim_frames
if frames > 1:
interval = (self.anim_speed / self.anim_fps) / speed
interval_total = (frames * interval)
self.anim_interval = interval
self.anim_interval_total = interval_total
# renpy.partial allows us to pass arguments into a transform function.
partial = renpy.partial(self.move_atl, path, times, loop, warper)
self.atl_partial = partial
transform = Transform(function=partial, anchor=(0.5, 1.0))
self.show(transform, at_list, self.layer, self.zorder)
2022-05-16 23:48:22 +00:00
if pause:
if loop:
renpy.pause(None)
else:
renpy.pause(total_time)
2022-05-16 23:48:22 +00:00
return (distances, times)
def stop(self):
# Freezes the animation
path, _, _, _ = self.atl_partial.args
pos = path[-1]
xzoom = -1 if (path[-1] > path[-2]) else 1
zpos = self.zorder + (1.0 / (pos[1] / config.screen_height))
self.pos = pos
self.xzoom = xzoom
self.atl_time = 0
self.atl_time_total = 0
self.set_pose(self.idle)
transform = Transform(pos=pos, zoom=self.zoom, xzoom=xzoom, anchor=(0.5, 1.0))
self.show(transform, self.atl_at_list, self.layer, zpos)
2022-05-16 23:48:22 +00:00
def move_atl(self, path, times, loop, warper, trans, st, at):
"""Animations are time based, so each segment will happen at a specific frame time."""
if self.atl_time_total == 0:
# Stops updating the animation
return None
2022-05-16 23:48:22 +00:00
if loop:
timer = st % self.atl_time_total
else:
timer = st
if timer > self.atl_time_total:
return None
internal_time = 0
current_segment = 0
# TODO: This loop feels unnecessary, need to find a better way.
for i, t in enumerate(times):
if (internal_time + t) > timer:
current_segment = i
break
internal_time += t
segment_time = (timer - internal_time) / times[current_segment]
next_segment = current_segment + 1
# Adjust XY position
trans.pos = renpy.atl.interpolate(warper(segment_time), path[current_segment], path[next_segment], renpy.atl.PROPERTIES["pos"])
self.pos = trans.pos
# Adjust X zoom based on target X position
# 1 = Facing Right, -1 = Facing Left
trans.xzoom = 1 if (path[current_segment][0] > path[next_segment][0]) else -1
2022-05-16 23:48:22 +00:00
self.xzoom = trans.xzoom
# Adjust zoom
trans.zoom = self.zoom
# Adjust Z position based on Y axis
# TODO: Add room support with bottom, middle, and top vanishing points.
# room_scale = 0.5
# zpos1 = ((path[current_segment][1] / 600.0) * 1000.0) * room_scale
# zpos2 = ((path[next_segment][1] / 600.0) * 1000.0) * room_scale
# trans.zpos = renpy.atl.interpolate(warper(segment_time), zpos1, zpos2, renpy.atl.PROPERTIES["zpos"])
# self.zpos = trans.zpos
# TODO: Using zorders is suboptimal and expensive, using 3D staging would be preferable.
zpos = self.zorder + (1.0 / (self.pos[1] / config.screen_height))
renpy.change_zorder(self.layer, self.name, zpos)
2022-05-16 23:48:22 +00:00
return 0
def show(self, transform, at_list, layer, zorder):
2022-05-16 23:48:22 +00:00
# The safest way to restart the transform is to rebuild it.
# Other methods proved to be too finicky...
image = At(self, transform) # IMPORTANT: Enable perspective and gl_depth for 3D staging
if not renpy.is_init_phase():
renpy.show(name=self.name, what=image, at_list=at_list, layer=layer, zorder=zorder)
def hide(self):
if not renpy.is_init_phase():
renpy.hide(name=self.name)
def update(self):
self.build_animation()
renpy.redraw(self, 0)
@property
def image(self):
return self
2022-05-16 23:48:22 +00:00
init offset = 5
default hooch_chibi = DollChibi(name="hooch", doll=hooch)
default cho_chibi_new = DollChibi(name="cho", doll=cho)
default ton_chibi_new = DollChibi(name="tonks", doll=tonks)
label chibitest:
$ ton_chibi_new.register_pose("stand", 1)
$ ton_chibi_new.register_pose("walk", 8)
"Rollback block"
$ renpy.block_rollback()
"repeat 3"
$ ton_chibi_new.move(path=[(500, 421), (650, 521), (800, 421)], repeat=3)
"loop"
$ ton_chibi_new.move(path=[(500, 421), (650, 521), (800, 421)], loop=True)
"doll synchronisation"
$ ton_chibi_new.move(path=[(500, 421), (650, 521), (800, 421)], loop=True, pause=False)
ton "Default" ("base", "base", "base", "mid", hair="default")
ton "Angry" ("angry", "narrow", "angry", "mid", hair="angry")
ton "Scared" ("scream", "wide", "worried", "mid", hair="scared")
"End"
$ ton_chibi_new.hide()
hide tonks_main
jump main_room_menu