New DebugUI plugin
This plugin is meant to have some add-ons to interact with the simulation to provide insight. This first change adds a cursor under the mouse with the intention that it's circle can be used as a reticule for targeting boids. Hovering over a cluster and getting their CoM or velocities, for example.
This commit is contained in:
50
src/debug_plugin.rs
Normal file
50
src/debug_plugin.rs
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
use bevy::{prelude::*, sprite::MaterialMesh2dBundle, window::PrimaryWindow};
|
||||||
|
|
||||||
|
pub struct BoidsDebugPlugin;
|
||||||
|
|
||||||
|
impl Plugin for BoidsDebugPlugin {
|
||||||
|
fn build(&self, app: &mut App) {
|
||||||
|
app.add_systems(Startup, setup)
|
||||||
|
.add_systems(FixedUpdate, update_cursor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn setup(
|
||||||
|
mut commands: Commands,
|
||||||
|
mut meshes: ResMut<Assets<Mesh>>,
|
||||||
|
mut materials: ResMut<Assets<ColorMaterial>>,
|
||||||
|
) {
|
||||||
|
commands.spawn((
|
||||||
|
Cursor,
|
||||||
|
MaterialMesh2dBundle {
|
||||||
|
mesh: meshes.add(Annulus::new(9.5, 10.0)).into(),
|
||||||
|
material: materials.add(Color::srgb(0.0, 0.0, 0.0)),
|
||||||
|
..default()
|
||||||
|
},
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Component)]
|
||||||
|
struct Cursor;
|
||||||
|
|
||||||
|
fn update_cursor(
|
||||||
|
window: Query<&Window, With<PrimaryWindow>>,
|
||||||
|
camera: Query<(&Camera, &GlobalTransform)>,
|
||||||
|
mut cursor_query: Query<&mut Transform, With<Cursor>>,
|
||||||
|
) {
|
||||||
|
// I'm trusting that only one thing has the `Cursor` component
|
||||||
|
// It's defined here in this module, so that *should* be the case...
|
||||||
|
let win = window.get_single().unwrap();
|
||||||
|
let (cam, cam_transform) = camera.get_single().unwrap();
|
||||||
|
// the cursor might not be on the window. Only adjust position when it is.
|
||||||
|
if let Some(cursor_pos_in_window) = win.cursor_position() {
|
||||||
|
// transform the window position into world space
|
||||||
|
// I'm trusting that this viewport actually displays this camera
|
||||||
|
// (or at least that the coordinates can map correctly)
|
||||||
|
let cursor_in_world = cam
|
||||||
|
.viewport_to_world_2d(cam_transform, cursor_pos_in_window)
|
||||||
|
.unwrap();
|
||||||
|
let mut cursor = cursor_query.get_single_mut().unwrap();
|
||||||
|
cursor.translation = cursor_in_world.extend(0.0);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,12 +1,15 @@
|
|||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
|
||||||
mod birdoids_plugin;
|
mod birdoids_plugin;
|
||||||
|
mod debug_plugin;
|
||||||
|
|
||||||
use birdoids_plugin::BoidsPlugin;
|
use birdoids_plugin::BoidsPlugin;
|
||||||
|
use debug_plugin::BoidsDebugPlugin;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
App::new()
|
App::new()
|
||||||
.add_plugins(DefaultPlugins)
|
.add_plugins(DefaultPlugins)
|
||||||
|
.add_plugins(BoidsDebugPlugin)
|
||||||
.add_plugins(BoidsPlugin)
|
.add_plugins(BoidsPlugin)
|
||||||
.run();
|
.run();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user