she moves!
This commit is contained in:
parent
f42d8bffd5
commit
e41ff1cf61
1 changed files with 19 additions and 8 deletions
27
src/main.rs
27
src/main.rs
|
@ -14,25 +14,32 @@ fn main() {
|
|||
.init_resource::<BoidMesh>()
|
||||
.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<ColorMaterial>,
|
||||
velocity: Velocity,
|
||||
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>) {
|
||||
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()
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue