diff --git a/Cargo.toml b/Cargo.toml index 7e1c668..8754687 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,7 @@ features = [ "bevy_color", "bevy_render", "bevy_winit", + "bevy_gizmos", "multi_threaded", "x11", "wayland", diff --git a/src/main.rs b/src/main.rs index 86ea2f5..bca759d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +#![allow(clippy::type_complexity)] // #![allow(unused)] mod acceleration; @@ -21,6 +22,7 @@ fn main() { Update, ( build_local_cache, + (seperation_gizmos), (seperation, cohesion, alignment), accelerate, move_boids, @@ -31,6 +33,9 @@ fn main() { .run(); } +#[derive(Component, Default)] +struct DrawGizmos; + #[derive(Component, Default)] struct Boid; @@ -82,7 +87,7 @@ fn cohesion( accel.0 = Vec2::ZERO; let mut count = 0; - for transform in other_boids.iter_many(&local.0[..]) { + for transform in other_boids.iter_many(&local.0) { accel.0 += transform.translation.xy(); count += 1; @@ -93,6 +98,22 @@ fn cohesion( }) } +fn seperation_gizmos( + boids: Query<(&Transform, &LocalCache), (With, With)>, + other_boids: Query<&Transform, With>, + mut gizmos: Gizmos, +) { + boids.iter().for_each(|(transform, local)| { + other_boids.iter_many(&local.0).for_each(|other_transform| { + gizmos.arrow_2d( + transform.translation.xy(), + other_transform.translation.xy(), + tailwind::ROSE_500, + ); + }) + }); +} + fn seperation( mut boids: Query<(&Transform, &LocalCache, &mut SeperationAcceleration), With>, other_boids: Query<&Transform, With>, @@ -110,7 +131,7 @@ fn alignment( .for_each(|(velocity, local, mut accel)| { accel.0 = Vec2::ZERO; let mut count = 0; - for other_velocity in other_boids.iter_many(&local.0[..]) { + for other_velocity in other_boids.iter_many(&local.0) { accel.0 += other_velocity.0; count += 1; @@ -179,7 +200,7 @@ fn setup( let lower_height = -half_height; let upper_height = half_height; - for _ in 0..1000 { + for i in 0..1000 { use std::f32::consts::TAU; let x = small_rng.gen_range(lower_width..=upper_width); @@ -187,7 +208,7 @@ fn setup( let angle = small_rng.gen_range(-TAU..=TAU); - commands.spawn(BoidBundle { + let mut entity_commands = commands.spawn(BoidBundle { mesh: MaterialMesh2dBundle { mesh: boid_mesh.0.clone(), material: boid_color.clone(), @@ -198,6 +219,10 @@ fn setup( velocity: Velocity(Vec2::from_angle(angle) * 200.), ..default() }); + + if i == 0 { + entity_commands.insert(DrawGizmos); + } } commands.spawn(Camera2dBundle::default());