she moves!

This commit is contained in:
Zynh Ludwig 2024-09-01 02:24:03 -07:00
parent f42d8bffd5
commit e41ff1cf61

View file

@ -14,25 +14,32 @@ fn main() {
.init_resource::<BoidMesh>() .init_resource::<BoidMesh>()
.add_plugins(DefaultPlugins) .add_plugins(DefaultPlugins)
.add_systems(Startup, (build_boid_mesh, setup).chain()) .add_systems(Startup, (build_boid_mesh, setup).chain())
.add_systems(Update, move_boids)
.run(); .run();
} }
#[derive(Resource, Default)] #[derive(Resource, Default)]
struct BoidMesh(Mesh2dHandle); struct BoidMesh(Mesh2dHandle);
#[derive(Component)] #[derive(Component, Default)]
struct Boid; struct Boid;
#[derive(Component)] #[derive(Component, Default)]
struct Velocity(Vec2); struct Velocity(Vec3);
#[derive(Bundle)] #[derive(Bundle, Default)]
struct BoidBundle { struct BoidBundle {
mesh: MaterialMesh2dBundle<ColorMaterial>, mesh: MaterialMesh2dBundle<ColorMaterial>,
velocity: Velocity, velocity: Velocity,
boid: Boid, boid: Boid,
} }
fn move_boids(mut boids: Query<(&mut Transform, &Velocity), With<Boid>>) {
boids.iter_mut().for_each(|(mut transform, velocity)| {
transform.translation += velocity.0;
});
}
fn build_boid_mesh(mut meshes: ResMut<Assets<Mesh>>, mut boid_mesh: ResMut<BoidMesh>) { fn build_boid_mesh(mut meshes: ResMut<Assets<Mesh>>, mut boid_mesh: ResMut<BoidMesh>) {
let mesh = Mesh2dHandle(meshes.add(make_boid_mesh())); let mesh = Mesh2dHandle(meshes.add(make_boid_mesh()));
boid_mesh.0 = mesh; boid_mesh.0 = mesh;
@ -47,10 +54,14 @@ fn setup(
let boid_color = materials.add(Color::from(tailwind::NEUTRAL_50)); let boid_color = materials.add(Color::from(tailwind::NEUTRAL_50));
commands.spawn(MaterialMesh2dBundle { commands.spawn(BoidBundle {
mesh: boid_mesh.0.clone(), mesh: MaterialMesh2dBundle {
material: boid_color, mesh: boid_mesh.0.clone(),
transform: Transform::from_scale(Vec3::splat(2.5)), material: boid_color,
transform: Transform::from_scale(Vec3::splat(2.5)),
..default()
},
velocity: Velocity(Vec3::Y),
..default() ..default()
}); });
} }