import math
from pybevy.prelude import *
@component
class AnimateTranslation(Component):
@component
class AnimateRotation(Component):
@component
class AnimateScale(Component):
def setup(commands: Commands) -> None:
commands.spawn(Camera2d())
text_font = TextFont.from_font_size(50.0)
commands.spawn(
Text2d("translation"),
text_font,
TextLayout(justify=Justify.Center),
TextColor.WHITE,
Transform.from_xyz(-400.0, 100.0, 0.0),
AnimateTranslation(),
)
commands.spawn(
Text2d("rotation"),
text_font,
TextLayout(justify=Justify.Center),
TextColor(Color.srgb(1.0, 0.8, 0.2)), Transform.from_xyz(0.0, 100.0, 0.0),
AnimateRotation(),
)
commands.spawn(
Text2d("scale"),
text_font,
TextLayout(justify=Justify.Center),
TextColor(Color.srgb(0.5, 1.0, 0.5)), Transform.from_xyz(400.0, 100.0, 0.0),
AnimateScale(),
)
smaller_font = TextFont.from_font_size(35.0)
commands.spawn(
Text2d("This text has\nFontSmoothing.NoSmoothing\nAnd Justify.Center"),
smaller_font,
TextLayout(justify=Justify.Center),
TextColor(Color.srgb(1.0, 0.5, 1.0)), Transform.from_xyz(-400.0, -150.0, 0.0),
)
commands.spawn(
Text2d("Multi-line text\nLeft justified\nWith custom color"),
TextFont.from_font_size(30.0),
TextLayout(justify=Justify.Left),
TextColor(Color.srgb(0.3, 0.8, 1.0)), Transform.from_xyz(200.0, -150.0, 0.0),
)
def animate_translation(
time: Res[Time],
transforms: Query[Mut[Transform], With[tuple[Text2d, AnimateTranslation]]],
) -> None:
t = time.elapsed_secs()
for transform in transforms:
transform.translation.x = 100.0 * math.sin(t) - 400.0
transform.translation.y = 100.0 * math.cos(t) + 100.0
def animate_rotation(
time: Res[Time],
transforms: Query[Mut[Transform], With[tuple[Text2d, AnimateRotation]]],
) -> None:
t = time.elapsed_secs()
for transform in transforms:
transform.rotation = Quat.from_rotation_z(math.cos(t))
def animate_scale(
time: Res[Time],
transforms: Query[Mut[Transform], With[tuple[Text2d, AnimateScale]]],
) -> None:
t = time.elapsed_secs()
for transform in transforms:
scale = (math.sin(t) + 1.1) * 2.0
transform.scale = Vec3.splat(scale)
@entrypoint
def main(app: App) -> App:
return (
app.add_plugins(DefaultPlugins)
.add_systems(Startup, setup)
.add_systems(Update, (animate_translation, animate_rotation, animate_scale))
)
if __name__ == "__main__":
main().run()