diff --git a/src/main.rs b/src/main.rs index 082ce2f..b54b8a3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,33 +11,45 @@ use std::f32::consts::FRAC_PI_2; fn main() { App::new() + .init_resource::() .add_plugins(DefaultPlugins) - .add_systems(Startup, setup) + .add_systems(Startup, (build_boid_mesh, setup).chain()) .run(); } +#[derive(Resource, Default)] +struct BoidMesh(Mesh2dHandle); + #[derive(Component)] struct Boid; +#[derive(Component)] +struct Velocity(Vec2); + +#[derive(Bundle)] struct BoidBundle { mesh: MaterialMesh2dBundle, - position: TransformBundle, - velocity: Vec2, + velocity: Velocity, + boid: Boid, +} + +fn build_boid_mesh(mut meshes: ResMut>, mut boid_mesh: ResMut) { + let mesh = Mesh2dHandle(meshes.add(make_boid_mesh())); + boid_mesh.0 = mesh; } fn setup( mut commands: Commands, - mut meshes: ResMut>, mut materials: ResMut>, + boid_mesh: Res, ) { commands.spawn(Camera2dBundle::default()); - let boid_mesh = make_boid_mesh(); + let boid_color = materials.add(Color::from(tailwind::NEUTRAL_50)); - let boid_color = Color::from(tailwind::NEUTRAL_50); commands.spawn(MaterialMesh2dBundle { - mesh: Mesh2dHandle(meshes.add(boid_mesh)), - material: materials.add(boid_color), + mesh: boid_mesh.0.clone(), + material: boid_color, transform: Transform::from_scale(Vec3::splat(2.5)), ..default() });