diff --git a/effects/effect.py b/effects/effect.py index d5f0f2b..1c25936 100644 --- a/effects/effect.py +++ b/effects/effect.py @@ -71,6 +71,41 @@ def transform_bounce( size_x, size_y = yield (pos_x, pos_y) +def transform_oscillate( + bounds: pg.Rect, + period: int, + initial_pos: Tuple[int, int] = (-1, -1), +) -> Generator[Tuple[int, int], Tuple[int, int], None]: + pos_x = float(initial_pos[0] if initial_pos[0] > 0 else bounds.left) + pos_y = float(initial_pos[1] if initial_pos[1] > 0 else bounds.top) + direction = "+" + + size_x, size_y = yield (bounds.left, bounds.top) + + while True: + range_x = bounds.width - size_x + range_y = bounds.height - size_y + + inc_x = range_x / period + inc_y = range_y / period + + if direction == "+": + pos_x = pg.math.clamp(pos_x + inc_x, 0, range_x) + pos_y = pg.math.clamp(pos_y + inc_y, 0, range_y) + else: + pos_x = pg.math.clamp(pos_x - inc_x, 0, range_x) + pos_y = pg.math.clamp(pos_y - inc_y, 0, range_y) + + if (inc_x and (pos_x > range_x - inc_x)) or ( + inc_y and (pos_y > range_y - inc_y) + ): + direction = "-" + elif (inc_x and (pos_x < inc_x)) or (inc_y and (pos_y < inc_y)): + direction = "+" + + size_x, size_y = yield (int(pos_x), int(pos_y)) + + class Effect(pg.sprite.Sprite): def __init__( self, image: pg.Surface, rect: pg.Rect, *groups: pg.sprite.Group