diff --git a/src/main.rs b/src/main.rs index e8b0dde..5e5609f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,9 @@ -#![allow(unused)] +// #![allow(unused)] mod boid_mesh; mod boundary; -use bevy::{color::palettes::tailwind, prelude::*, reflect::List, sprite::MaterialMesh2dBundle}; +use bevy::{color::palettes::tailwind, prelude::*, sprite::MaterialMesh2dBundle}; use boid_mesh::BoidMesh; use rand::{Rng, SeedableRng}; @@ -15,8 +15,10 @@ fn main() { .add_systems( Update, ( + build_local_cache, (seperation, cohesion, alignment), - (accelerate, move_boids).chain(), + accelerate, + move_boids, boundary::boid_border_teleport, ) .chain(), @@ -51,7 +53,8 @@ struct BoidBundle { mesh: MaterialMesh2dBundle, velocity: Velocity, boid: Boid, - cache: BoidAccelerationBundle, + accel_cache: BoidAccelerationBundle, + local_cache: LocalCache, } #[derive(Component, Default)] @@ -80,11 +83,29 @@ fn build_local_cache( }); } -fn seperation(mut boids: Query<(Entity, &Transform, &mut SeperationAcceleration), With>) {} +fn seperation( + mut boids: Query<(&Transform, &LocalCache, &mut SeperationAcceleration), With>, +) { +} -fn cohesion(mut boids: Query<(Entity, &Transform, &mut CohesionAcceleration), With>) {} +fn cohesion(mut boids: Query<(&Transform, &LocalCache, &mut CohesionAcceleration), With>) {} -fn alignment(mut boids: Query<(Entity, &Transform, &mut AlignmentAcceleration), With>) {} +fn alignment( + mut boids: Query<(&LocalCache, &mut AlignmentAcceleration), With>, + other_boids: Query<&Velocity, With>, +) { + boids.par_iter_mut().for_each(|(local, mut accel)| { + accel.0 = Vec2::ZERO; + let mut count = 0; + for velocity in other_boids.iter_many(&local.0[..]) { + accel.0 += velocity.0; + + count += 1; + } + + accel.0 /= count as f32; + }); +} fn accelerate( mut boids: Query< @@ -105,9 +126,11 @@ fn accelerate( vel.0 += coh.0 * time.delta_seconds(); vel.0 += ali.0 * time.delta_seconds(); + vel.0 = vel.0.clamp_length_max(200.); + sep.0 *= 0.; - sep.0 *= 0.; - sep.0 *= 0.; + coh.0 *= 0.; + ali.0 *= 0.; }); } @@ -137,7 +160,7 @@ fn setup( let lower_height = -half_height; let upper_height = half_height; - for i in 0..1000 { + for _ in 0..1000 { use std::f32::consts::TAU; let x = small_rng.gen_range(lower_width..=upper_width);