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