use mover for moonflower

This commit is contained in:
Patrick Moessler 2023-02-23 00:19:20 +01:00
parent fb2b888193
commit 5324cfb186
4 changed files with 38 additions and 33 deletions

View file

@ -1,6 +1,7 @@
from typing import Optional
import pygame as pg import pygame as pg
from util.transform import Mover from util.transform import PositionGenerator, transform_static
class Effect(pg.sprite.Sprite): class Effect(pg.sprite.Sprite):
@ -17,7 +18,11 @@ class Effect(pg.sprite.Sprite):
class MovingEffect(Effect): class MovingEffect(Effect):
def __init__( def __init__(
self, image: pg.Surface, rect: pg.Rect, mover: Mover, *groups: pg.sprite.Group self,
image: pg.Surface,
rect: pg.Rect,
mover: Optional[PositionGenerator] = None,
*groups: pg.sprite.Group
) -> None: ) -> None:
super().__init__(image, rect, *groups) super().__init__(image, rect, *groups)
self.mover = mover self.mover = mover or transform_static(rect.center)

View file

@ -1,12 +1,12 @@
from typing import Any, Tuple from typing import Any, Optional, Tuple
import pygame as pg import pygame as pg
from effects.effect import Effect from effects.effect import MovingEffect
from util.color import Colors from util.color import Colors
from util.transform import transform_bounce
import random
import math import math
from typing import Union, Generator from typing import Union, Generator
from util.transform import PositionGenerator
def calc_radii(size, f): def calc_radii(size, f):
r_c = size / (5 * f + 2) r_c = size / (5 * f + 2)
@ -14,7 +14,7 @@ def calc_radii(size, f):
return r_c, r_a return r_c, r_a
class Moonflower(Effect): class Moonflower(MovingEffect):
def __init__( def __init__(
self, self,
bounds: pg.Rect, bounds: pg.Rect,
@ -22,28 +22,20 @@ class Moonflower(Effect):
Union[pg.Color, Generator[pg.Color, None, None]], Union[pg.Color, Generator[pg.Color, None, None]],
Union[pg.Color, Generator[pg.Color, None, None]], Union[pg.Color, Generator[pg.Color, None, None]],
], ],
mover: Optional[PositionGenerator] = None,
beat_color: bool = False, beat_color: bool = False,
size: int = 100, size: int = 100,
velocity: Tuple[int, int] = (1, 10),
rot_speed: float = 5, rot_speed: float = 5,
outer: int = 5, outer: int = 5,
x_factor: Tuple[float, float] = (0.1, 1),
y_factor: Tuple[float, float] = (0.1, 1),
*groups: pg.sprite.Group *groups: pg.sprite.Group
) -> None: ) -> None:
self.min_velocity = velocity[0]
self.max_velocity = velocity[1]
self.rot_speed = rot_speed self.rot_speed = rot_speed
self.rotation = 0.0 self.rotation = 0.0
self.velocity = random.randint(self.min_velocity, self.max_velocity)
# self.ticks = random.randint(0, 360)
self.colors = colors self.colors = colors
self.beat_color = beat_color self.beat_color = beat_color
self.o_count = outer self.o_count = outer
self.o_f = 1 / math.sin(math.pi / self.o_count) self.o_f = 1 / math.sin(math.pi / self.o_count)
self.spot_radius, self.o_radius = calc_radii(size, self.o_f) self.spot_radius, self.o_radius = calc_radii(size, self.o_f)
self.i_radius = (self.o_radius - self.spot_radius) * 0.5 self.i_radius = (self.o_radius - self.spot_radius) * 0.5
@ -58,13 +50,14 @@ class Moonflower(Effect):
size, size,
size, size,
), ),
*groups mover,
*groups,
) )
self.bounds = bounds self.bounds = bounds
self.bouncer = transform_bounce( # self.bouncer = transform_bounce(
bounds=bounds, velocity=velocity, x_factor=x_factor, y_factor=y_factor, on_beat_random_phase=True # bounds=bounds, velocity=velocity, x_factor=x_factor, y_factor=y_factor, on_beat_random_phase=True
) # )
next(self.bouncer) next(self.mover)
self.o_color = ( self.o_color = (
self.colors[0] self.colors[0]
if isinstance(self.colors[0], pg.Color) if isinstance(self.colors[0], pg.Color)
@ -78,7 +71,7 @@ class Moonflower(Effect):
self.update(is_beat=False) self.update(is_beat=False)
def update(self, *args: Any, **kwargs: Any) -> None: def update(self, *args: Any, **kwargs: Any) -> None:
self.rect.center = self.bouncer.send((self.rect.size, kwargs["is_beat"])) self.rect.center = self.mover.send((self.rect.size, kwargs["is_beat"]))
self.image.fill(Colors.Black) self.image.fill(Colors.Black)
@ -106,5 +99,3 @@ class Moonflower(Effect):
) )
self.rotation += self.rot_speed self.rotation += self.rot_speed
# self.ticks += int(self.velocity / 180 * math.pi)
self.velocity = random.randint(self.min_velocity, self.max_velocity)

View file

@ -17,6 +17,7 @@ from effects.rotatingpoly import RotatingPoly
from effects.scanreticle import ScanReticle from effects.scanreticle import ScanReticle
from effects.spiro import Spiro from effects.spiro import Spiro
from effects.starfield import Starfield from effects.starfield import Starfield
from util.transform import transform_bounce
class Presets: class Presets:
@ -124,10 +125,13 @@ class Presets:
beat_color=self.beat_reactive, beat_color=self.beat_reactive,
size=self.bounds.height // 4, size=self.bounds.height // 4,
outer=5, outer=5,
velocity=(1, 1),
rot_speed=1.5, rot_speed=1.5,
x_factor=(1, 1), mover=transform_bounce(
y_factor=(2.2, 2.2), bounds=self.bounds,
velocity=(0.5, 1.5),
x_factor=(0.5, 1.5),
y_factor=(0.5, 1.5),
),
) )
] ]

View file

@ -1,12 +1,16 @@
import math import math
import random import random
from typing import Callable, Generator, Optional, Tuple from typing import Generator, Optional, Tuple
import pygame as pg import pygame as pg
PositionGenerator = Generator[ PositionGenerator = Generator[
Tuple[int, int], Tuple[Tuple[int, int], Optional[bool]], None Tuple[int, int], Tuple[Tuple[int, int], Optional[bool]], None
] ]
Mover = Callable[(...), PositionGenerator]
def transform_static(position: Tuple[int, int]) -> PositionGenerator:
while True:
_ = yield (position)
def transform_bounce( def transform_bounce(
@ -14,7 +18,7 @@ def transform_bounce(
velocity: Tuple[float, float], velocity: Tuple[float, float],
x_factor: Tuple[float, float], x_factor: Tuple[float, float],
y_factor: Tuple[float, float], y_factor: Tuple[float, float],
on_beat_random_phase: bool = False, on_beat_random_phase: int = 0,
) -> PositionGenerator: ) -> PositionGenerator:
min_velocity = velocity[0] min_velocity = velocity[0]
max_velocity = velocity[1] max_velocity = velocity[1]
@ -36,9 +40,10 @@ def transform_bounce(
+ bounds.centery + bounds.centery
) )
phase += current_velocity / 180 * math.pi inc = current_velocity
if on_beat_random_phase and is_beat: if on_beat_random_phase and is_beat:
phase += random.randrange(0, 360) inc += random.randrange(0, on_beat_random_phase)
phase += inc / 180 * math.pi
def transform_oscillate( def transform_oscillate(