toid blossom

This commit is contained in:
Joe Ardent 2023-11-25 11:24:43 -08:00
parent 5175604649
commit 5e64325eb6
2 changed files with 22 additions and 23 deletions

View file

@ -1,9 +1,14 @@
use argh::FromArgs; use argh::FromArgs;
use bevy::prelude::*; use bevy::prelude::*;
const SPEED: f32 = 20.0; const SPEED: f32 = 1.0;
const SPEED_DIFF_RANGE: f32 = 0.08; // +/- 8% const SPEED_DIFF_RANGE: f32 = 0.08; // +/- 8%
const RADIUS: f32 = 1_000.0;
pub type Point = [f32; 3];
pub type Toint = rstar::primitives::GeomWithData<Point, Entity>;
#[derive(Debug, FromArgs, Resource)] #[derive(Debug, FromArgs, Resource)]
/// Toid Watching /// Toid Watching
pub struct Config { pub struct Config {
@ -12,24 +17,10 @@ pub struct Config {
pub toids: usize, pub toids: usize,
} }
pub type Point = (f32, f32, f32); #[derive(Resource, Deref, DerefMut, Default)]
pub type Toint = rstar::primitives::GeomWithData<Point, Entity>;
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<Toint>); pub struct Index(pub rstar::RTree<Toint>);
#[derive(Resource, Deref, DerefMut)] #[derive(Resource, Deref, DerefMut, Default)]
pub struct Positions(pub std::collections::HashMap<Entity, Vec3>); pub struct Positions(pub std::collections::HashMap<Entity, Vec3>);
#[derive(Component, Debug, Clone, Deref, DerefMut, Default)] #[derive(Component, Debug, Clone, Deref, DerefMut, Default)]
@ -41,6 +32,7 @@ pub struct Velocity(Vec3);
#[derive(Component)] #[derive(Component)]
pub struct Toid { pub struct Toid {
pub speed: f32, pub speed: f32,
pub buddies: usize,
} }
pub fn turkey_time( pub fn turkey_time(
@ -51,9 +43,10 @@ pub fn turkey_time(
let speed_diff = r.gen_range(-SPEED_DIFF_RANGE..=SPEED_DIFF_RANGE); let speed_diff = r.gen_range(-SPEED_DIFF_RANGE..=SPEED_DIFF_RANGE);
let speed = SPEED + (SPEED * speed_diff); let speed = SPEED + (SPEED * speed_diff);
let vel = unit_vec(r) * speed; let vel = unit_vec(r) * speed;
let buddies = r.gen_range(6..=8);
commands commands
.spawn(SpatialBundle::default()) .spawn(SpatialBundle::default())
.insert((Velocity(vel), Buddies::default(), Toid { speed })) .insert((Velocity(vel), Buddies::default(), Toid { speed, buddies }))
.with_children(|t| { .with_children(|t| {
t.spawn(SceneBundle { t.spawn(SceneBundle {
scene: scene.to_owned(), scene: scene.to_owned(),
@ -83,8 +76,9 @@ pub fn update_pos(
) { ) {
let dt = time.delta_seconds(); let dt = time.delta_seconds();
for (mut xform, vel, _entity) in toids.iter_mut() { for (mut xform, vel, _entity) in toids.iter_mut() {
let vel = vel.0; xform.translation += vel.0 * dt;
xform.translation += vel * dt; let look_at = xform.translation + vel.0;
xform.look_at(look_at, Vec3::Y);
} }
} }
@ -111,7 +105,7 @@ pub fn update_buddies(mut toids: Query<(&Transform, Entity, &mut Buddies)>, inde
// util // util
//-************************************************************************ //-************************************************************************
pub fn unit_vec(r: &mut impl rand::prelude::Rng) -> Vec3 { pub fn unit_vec(r: &mut impl rand::Rng) -> Vec3 {
let mut x1: f32 = 0.0; let mut x1: f32 = 0.0;
let mut x2: f32 = 0.0; let mut x2: f32 = 0.0;
let mut ssum = std::f32::MAX; let mut ssum = std::f32::MAX;

View file

@ -8,16 +8,21 @@ fn main() {
let config: audubon::Config = argh::from_env(); let config: audubon::Config = argh::from_env();
App::new() App::new()
.add_plugins(DefaultPlugins) .add_plugins(DefaultPlugins)
.insert_resource(Index(rstar::RTree::new()))
.insert_resource(config) .insert_resource(config)
.add_systems(Startup, setup) .add_systems(Startup, setup)
.add_systems( .add_systems(
Update, Update,
( (
update_vel,
add_gizmos, add_gizmos,
update_pos,
update_buddies,
rotate_camera, rotate_camera,
update_config, update_config,
bevy::window::close_on_esc, bevy::window::close_on_esc,
), )
.chain(),
) )
.run(); .run();
} }
@ -32,7 +37,7 @@ fn setup(
let rand = &mut rand::thread_rng(); let rand = &mut rand::thread_rng();
commands.spawn(Camera3dBundle { commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(0., 1.5, 6.).looking_at(Vec3::ZERO, Vec3::Y), transform: Transform::from_xyz(0., 3.5, 10.).looking_at(Vec3::ZERO, Vec3::Y),
..default() ..default()
}); });
// plane // plane