From fd161dc26bb674229bc02886f993ed53ed597b85 Mon Sep 17 00:00:00 2001 From: Robert Garrett Date: Mon, 22 Dec 2025 10:45:02 -0600 Subject: [PATCH] Wire in the magic physics vars as a Resource I don't know what to call these, so they're "misc. parameters" for now. --- src/birdoids/mod.rs | 46 +++++++++++++++++++++++++++++++-------------- src/main.rs | 3 ++- 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/src/birdoids/mod.rs b/src/birdoids/mod.rs index 808fb0eb..20b098bc 100644 --- a/src/birdoids/mod.rs +++ b/src/birdoids/mod.rs @@ -10,11 +10,6 @@ use bevy_inspector_egui::{InspectorOptions, prelude::ReflectInspectorOptions}; const BACKGROUND_COLOR: Color = Color::srgb(0.4, 0.4, 0.4); const PLAYERBOID_COLOR: Color = Color::srgb(1.0, 0.0, 0.0); -const TURN_FACTOR: f32 = 1.0; - -const SPACEBRAKES_COEFFICIENT: f32 = 0.5; -const LOW_SPEED_THRESHOLD: f32 = 50.0; -const HIGH_SPEED_THRESHOLD: f32 = 200.0; pub struct BoidsPlugin; @@ -29,6 +24,8 @@ impl Plugin for BoidsPlugin { .insert_resource(ClearColor(BACKGROUND_COLOR)) .insert_resource(FlockingParameters::new()) .register_type::() + .insert_resource(MiscParams::new()) + .register_type::() .add_systems(Startup, (spawn_camera, spawn_boids)) .add_systems( FixedUpdate, @@ -65,6 +62,26 @@ impl FlockingParameters { } } +#[derive(InspectorOptions, Reflect, Resource, Debug, Clone, Copy)] +#[reflect(Resource, InspectorOptions)] +pub(crate) struct MiscParams { + turn_factor: f32, + spacebrakes: f32, + minimum_speed: f32, + maximum_speed: f32, +} + +impl MiscParams { + pub(crate) fn new() -> Self { + Self { + turn_factor: 1.0, + spacebrakes: 0.5, + minimum_speed: 50.0, + maximum_speed: 200.0, + } + } +} + #[derive(Component)] #[require(Velocity, Force, TrackedByKdTree)] pub(crate) struct Boid; @@ -110,12 +127,12 @@ fn spawn_boids( /// Controls the boid's minimum and maximum speed according to a low- and /// high-threshold. Boids moving too slow are sped up, and boids moving too /// fast are slowed down. -fn speed_controller(mut mobs: Query<(&Velocity, &mut Force), With>) { +fn speed_controller(mut mobs: Query<(&Velocity, &mut Force), With>, params: Res) { for (vel, mut impulse) in &mut mobs { - if vel.0.length() < LOW_SPEED_THRESHOLD { - impulse.0 += vel.0 * SPACEBRAKES_COEFFICIENT; - } else if vel.0.length() > HIGH_SPEED_THRESHOLD { - impulse.0 += -vel.0 * SPACEBRAKES_COEFFICIENT; + if vel.0.length() < params.minimum_speed { + impulse.0 += vel.0 * params.spacebrakes; + } else if vel.0.length() > params.maximum_speed { + impulse.0 += -vel.0 * params.spacebrakes; } } } @@ -123,21 +140,22 @@ fn speed_controller(mut mobs: Query<(&Velocity, &mut Force), With>) { fn turn_if_edge( mut query: Query<(&mut Transform, &mut Velocity), With>, window: Query<&Window>, + params: Res, ) { if let Ok(window) = window.single() { let (width, height) = (window.resolution.width(), window.resolution.height()); for (transform, mut velocity) in &mut query { let boid_pos = transform.translation.xy(); if boid_pos.x <= -width / 2. + 50. { - velocity.x += TURN_FACTOR; + velocity.x += params.turn_factor; } else if boid_pos.x >= width / 2. - 50. { - velocity.x -= TURN_FACTOR; + velocity.x -= params.turn_factor; } if boid_pos.y <= -height / 2. + 50. { - velocity.y += TURN_FACTOR; + velocity.y += params.turn_factor; } else if boid_pos.y >= height / 2. - 50. { - velocity.y -= TURN_FACTOR; + velocity.y -= params.turn_factor; } } } else { diff --git a/src/main.rs b/src/main.rs index b897622b..a88267e4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,7 +10,7 @@ use bevy_inspector_egui::{ use birdoids::BoidsPlugin; use debug_plugin::BoidsDebugPlugin; -use crate::birdoids::FlockingParameters; +use crate::birdoids::{FlockingParameters, MiscParams}; fn main() { App::new() @@ -26,5 +26,6 @@ fn main() { .add_plugins(BoidsPlugin) .add_plugins(EguiPlugin::default()) .add_plugins(ResourceInspectorPlugin::::new()) // TODO: monitor only the flocking params resource (once it exists) + .add_plugins(ResourceInspectorPlugin::::new()) .run(); }