boid mesh setup

main
Zynh Ludwig 2024-09-01 02:14:22 -07:00
parent 5f9998b98d
commit f42d8bffd5
1 changed files with 20 additions and 8 deletions

View File

@ -11,33 +11,45 @@ use std::f32::consts::FRAC_PI_2;
fn main() { fn main() {
App::new() App::new()
.init_resource::<BoidMesh>()
.add_plugins(DefaultPlugins) .add_plugins(DefaultPlugins)
.add_systems(Startup, setup) .add_systems(Startup, (build_boid_mesh, setup).chain())
.run(); .run();
} }
#[derive(Resource, Default)]
struct BoidMesh(Mesh2dHandle);
#[derive(Component)] #[derive(Component)]
struct Boid; struct Boid;
#[derive(Component)]
struct Velocity(Vec2);
#[derive(Bundle)]
struct BoidBundle { struct BoidBundle {
mesh: MaterialMesh2dBundle<ColorMaterial>, mesh: MaterialMesh2dBundle<ColorMaterial>,
position: TransformBundle, velocity: Velocity,
velocity: Vec2, boid: Boid,
}
fn build_boid_mesh(mut meshes: ResMut<Assets<Mesh>>, mut boid_mesh: ResMut<BoidMesh>) {
let mesh = Mesh2dHandle(meshes.add(make_boid_mesh()));
boid_mesh.0 = mesh;
} }
fn setup( fn setup(
mut commands: Commands, mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>, mut materials: ResMut<Assets<ColorMaterial>>,
boid_mesh: Res<BoidMesh>,
) { ) {
commands.spawn(Camera2dBundle::default()); 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 { commands.spawn(MaterialMesh2dBundle {
mesh: Mesh2dHandle(meshes.add(boid_mesh)), mesh: boid_mesh.0.clone(),
material: materials.add(boid_color), material: boid_color,
transform: Transform::from_scale(Vec3::splat(2.5)), transform: Transform::from_scale(Vec3::splat(2.5)),
..default() ..default()
}); });