use argh::FromArgs; use bevy::prelude::*; // toid const SPEED: f32 = 10.0; const SPEED_DIFF_RANGE: f32 = 0.08; // +/- 8% // how far from origin before really wanting to come back const RADIUS: f32 = 800.0; // how close to try to stay to your buddies const BUDDY_RADIUS: f32 = 15.0; const MIN_ALTITUDE: f32 = 1.5; pub type Point = [f32; 3]; pub type Toint = rstar::primitives::GeomWithData; #[derive(Debug, FromArgs, Resource)] /// Toid Watching pub struct Config { /// how many Toids to spawn #[argh(option, short = 't', default = "100")] pub toids: usize, } #[derive(Resource, Deref, DerefMut, Default)] pub struct Index(pub rstar::RTree); #[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 buddies: usize, } 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; let buddies = r.gen_range(6..=8); let spatial_bundle = SpatialBundle { transform: Transform::from_xyz(0.0, MIN_ALTITUDE + 0.1, 0.0), ..Default::default() }; commands .spawn(spatial_bundle) .insert((Velocity(vel), Buddies::default(), Toid { speed, buddies })) .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