use argh::FromArgs; use bevy::prelude::*; const SPEED: f32 = 20.0; const SPEED_DIFF_RANGE: f32 = 0.08; // +/- 8% #[derive(Debug, FromArgs, Resource)] /// Toid Watching pub struct Config { /// how many Toids to spawn #[argh(option, short = 't', default = "100")] pub toids: usize, } pub type Point = (f32, f32, f32); pub type Toint = rstar::primitives::GeomWithData; pub trait Pointable { fn to_point(&self) -> Point; } impl Pointable for Vec3 { fn to_point(&self) -> Point { (self.x, self.y, self.z) } } #[derive(Resource, Deref, DerefMut)] pub struct Index(pub rstar::RTree); #[derive(Resource, Deref, DerefMut)] pub struct Positions(pub std::collections::HashMap); #[derive(Component, Debug, Clone, Deref, DerefMut, Default)] pub struct Buddies(Vec); #[derive(Component, Debug, Clone, Deref, DerefMut, Default)] pub struct Velocity(Vec3); #[derive(Component)] pub struct Toid { pub speed: f32, } pub fn turkey_time( commands: &mut Commands, scene: &Handle, r: &mut impl rand::prelude::Rng, ) -> Entity { let speed_diff = r.gen_range(-SPEED_DIFF_RANGE..=SPEED_DIFF_RANGE); let speed = SPEED + (SPEED * speed_diff); let vel = unit_vec(r) * speed; commands .spawn(SpatialBundle::default()) .insert((Velocity(vel), Buddies::default(), Toid { speed })) .with_children(|t| { t.spawn(SceneBundle { scene: scene.to_owned(), ..Default::default() }) .insert(Transform::from_rotation(Quat::from_axis_angle( Vec3::Y, -std::f32::consts::FRAC_PI_2, ))); }) .id() } pub fn add_gizmos(mut gizmos: Gizmos, toids: Query<(&Transform, Entity), With>) { let gizmos = &mut gizmos; for (pos, _entity) in toids.iter() { let nudge = pos.up() * 0.15; let rpos = pos.translation + nudge; gizmos.ray(rpos, pos.forward(), Color::RED); } } pub fn update_pos( mut toids: Query<(&mut Transform, &Velocity, Entity)>, time: Res