from pybevy.prelude import *
@component
class Movable(Component):
@resource
class MovementSettings(Resource):
def __init__(self):
self.spawn = Vec3.ZERO
self.max_distance = 5.0
self.speed = 2.0
def setup(
commands: Commands,
meshes: ResMut[Assets[Mesh]],
materials: ResMut[Assets[StandardMaterial]],
) -> None:
entity_spawn = Vec3.ZERO
commands.spawn(
Mesh3d(meshes.add(Cuboid().mesh())),
MeshMaterial3d(materials.add(StandardMaterial(base_color=Color.WHITE))),
Transform.from_translation(entity_spawn),
Movable(),
)
commands.spawn(Camera3d(), Transform.from_xyz(0.0, 10.0, 20.0).looking_at(entity_spawn, Vec3.Y))
commands.spawn(DirectionalLight(), Transform.from_xyz(3.0, 3.0, 3.0).looking_at(Vec3.ZERO, Vec3.Y))
def move_cube(
cubes: Query[Mut[Transform], With[Movable]],
settings: ResMut[MovementSettings],
time: Res[Time],
) -> None:
for transform in cubes:
distance = (settings.spawn - transform.translation).length()
if distance > settings.max_distance:
settings.speed *= -1.0
direction = transform.local_x()
transform.translation += direction * settings.speed * time.delta_secs()
@entrypoint
def main(app: App) -> App:
return (
app.add_plugins(DefaultPlugins)
.insert_resource(MovementSettings())
.add_systems(Startup, setup)
.add_systems(Update, move_cube)
)
if __name__ == "__main__":
main().run()