diff --git a/Cargo.toml b/Cargo.toml index 9380864..e973aa9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,19 +9,12 @@ keywords = ["bevy", "floating-origin", "large-scale", "space"] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -bevy = { version = "0.10", default_features = false } -bevy_polyline = { version = "0.6", optional = true } +bevy = { version = "0.11", default_features = false } [dev-dependencies] -bevy = { version = "0.10", default_features = false, features = [ - "bevy_text", - "bevy_ui", - "bevy_render", - "bevy_winit", - "x11", -] } -bevy_framepace = "0.12" +bevy = "0.11" +bevy_framepace = "0.13" [features] default = ["debug"] -debug = ["bevy_polyline"] +debug = ["bevy/bevy_gizmos"] diff --git a/examples/debug.rs b/examples/debug.rs index 866d019..7a2a23e 100644 --- a/examples/debug.rs +++ b/examples/debug.rs @@ -5,13 +5,14 @@ use big_space::{FloatingOrigin, GridCell}; fn main() { App::new() - .add_plugins(DefaultPlugins.build().disable::()) - .add_plugin(big_space::FloatingOriginPlugin::::new(0.5, 0.01)) - .add_plugin(big_space::debug::FloatingOriginDebugPlugin::::default()) + .add_plugins(( + DefaultPlugins.build().disable::(), + big_space::FloatingOriginPlugin::::new(0.5, 0.01), + big_space::debug::FloatingOriginDebugPlugin::::default(), + )) .insert_resource(ClearColor(Color::BLACK)) - .add_startup_system(setup) - .add_system(movement) - .add_system(rotation) + .add_systems(Startup, setup) + .add_systems(Update, (movement, rotation)) .run() } diff --git a/examples/demo.rs b/examples/demo.rs index 8391a89..d1d3efb 100644 --- a/examples/demo.rs +++ b/examples/demo.rs @@ -11,16 +11,16 @@ use big_space::{ fn main() { App::new() - .add_plugins(DefaultPlugins.build().disable::()) - .add_plugin(big_space::FloatingOriginPlugin::::default()) - .add_plugin(big_space::debug::FloatingOriginDebugPlugin::::default()) - .add_plugin(big_space::camera::CameraControllerPlugin::::default()) - .add_plugin(bevy_framepace::FramepacePlugin) + .add_plugins(( + DefaultPlugins.build().disable::(), + big_space::FloatingOriginPlugin::::default(), + big_space::debug::FloatingOriginDebugPlugin::::default(), + big_space::camera::CameraControllerPlugin::::default(), + bevy_framepace::FramepacePlugin, + )) .insert_resource(ClearColor(Color::BLACK)) - .add_startup_system(setup) - .add_system(cursor_grab_system) - .add_system(ui_text_system) - .add_startup_system(ui_setup) + .add_systems(Startup, (setup, ui_setup)) + .add_systems(Update, (cursor_grab_system, ui_text_system)) .run() } @@ -101,11 +101,8 @@ fn ui_setup(mut commands: Commands, asset_server: Res) { .with_text_alignment(TextAlignment::Left) .with_style(Style { position_type: PositionType::Absolute, - position: UiRect { - top: Val::Px(10.0), - left: Val::Px(10.0), - ..default() - }, + top: Val::Px(10.0), + left: Val::Px(10.0), ..default() }), BigSpaceDebugText, diff --git a/examples/error.rs b/examples/error.rs index 3eca887..2cbd1ac 100644 --- a/examples/error.rs +++ b/examples/error.rs @@ -10,12 +10,12 @@ use big_space::{FloatingOrigin, FloatingSpatialBundle, GridCell}; fn main() { App::new() - .add_plugins(DefaultPlugins.build().disable::()) - .add_plugin(big_space::FloatingOriginPlugin::::default()) - .add_startup_system(setup_scene) - .add_startup_system(setup_ui) - .add_system(rotator_system) - .add_system(toggle_plugin) + .add_plugins(( + DefaultPlugins.build().disable::(), + big_space::FloatingOriginPlugin::::default(), + )) + .add_systems(Startup, (setup_scene, setup_ui)) + .add_systems(Update, (rotator_system, toggle_plugin)) .run() } diff --git a/src/camera.rs b/src/camera.rs index 6100e19..fcb420a 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -18,14 +18,14 @@ pub struct CameraControllerPlugin(PhantomData

); impl Plugin for CameraControllerPlugin

{ fn build(&self, app: &mut App) { app.init_resource::().add_systems( + PostUpdate, ( default_camera_inputs .before(camera_controller::

) .run_if(|input: Res| !input.defaults_disabled), nearest_objects.before(camera_controller::

), camera_controller::

.before(TransformSystem::TransformPropagate), - ) - .in_base_set(CoreSet::PostUpdate), + ), ); } } @@ -148,10 +148,14 @@ pub fn default_camera_inputs( keyboard.pressed(KeyCode::A).then(|| cam.right -= 1.0); keyboard.pressed(KeyCode::D).then(|| cam.right += 1.0); keyboard.pressed(KeyCode::Space).then(|| cam.up += 1.0); - keyboard.pressed(KeyCode::LControl).then(|| cam.up -= 1.0); + keyboard + .pressed(KeyCode::ControlLeft) + .then(|| cam.up -= 1.0); keyboard.pressed(KeyCode::Q).then(|| cam.roll += 1.0); keyboard.pressed(KeyCode::E).then(|| cam.roll -= 1.0); - keyboard.pressed(KeyCode::LShift).then(|| cam.boost = true); + keyboard + .pressed(KeyCode::ShiftLeft) + .then(|| cam.boost = true); if let Some(total_mouse_motion) = mouse_move.iter().map(|e| e.delta).reduce(|sum, i| sum + i) { cam.pitch += total_mouse_motion.y as f64 * -0.1; cam.yaw += total_mouse_motion.x as f64 * -0.1; diff --git a/src/debug.rs b/src/debug.rs index 8dad6cb..d07d859 100644 --- a/src/debug.rs +++ b/src/debug.rs @@ -3,7 +3,6 @@ use std::marker::PhantomData; use bevy::{prelude::*, utils::HashMap}; -use bevy_polyline::prelude::*; use crate::{precision::GridPrecision, FloatingOrigin, FloatingOriginSettings, GridCell}; @@ -12,144 +11,44 @@ use crate::{precision::GridPrecision, FloatingOrigin, FloatingOriginSettings, Gr pub struct FloatingOriginDebugPlugin(PhantomData

); impl Plugin for FloatingOriginDebugPlugin

{ fn build(&self, app: &mut App) { - app.add_plugin(bevy_polyline::PolylinePlugin) - .add_system(build_cube.in_base_set(CoreSet::Update)) - .add_system( - update_debug_bounds::

- .in_base_set(CoreSet::PostUpdate) - .after(crate::recenter_transform_on_grid::

) - .before(crate::update_global_from_grid::

), - ); + app.add_systems( + PostUpdate, + update_debug_bounds::

+ .after(crate::recenter_transform_on_grid::

) + .before(crate::update_global_from_grid::

), + ); } } -/// Marks entities that are used to render grid cell bounds. -#[derive(Component, Reflect)] -pub struct DebugBounds; - -/// A resource that holds the handles to use for the debug bound polylines. -#[derive(Resource, Reflect)] -pub struct CubePolyline { - polyline: Handle, - material: Handle, - origin_matl: Handle, -} - /// Update the rendered debug bounds to only highlight occupied [`GridCell`]s. [`DebugBounds`] are /// spawned or hidden as needed. pub fn update_debug_bounds( - mut commands: Commands, - cube_polyline: Res, - occupied_cells: Query<(&GridCell

, Option<&FloatingOrigin>), Without>, - mut debug_bounds: Query< - ( - &mut GridCell

, - &mut Handle, - &mut Handle, - &mut Visibility, - ), - With, - >, -) { - let mut occupied_cells = HashMap::from_iter(occupied_cells.iter()).into_iter(); - - for (mut cell, mut polyline, mut matl, mut visibility) in &mut debug_bounds { - if cube_polyline.is_changed() { - *polyline = cube_polyline.polyline.clone(); - } - if let Some((occupied_cell, has_origin)) = occupied_cells.next() { - *visibility = Visibility::Visible; - *cell = *occupied_cell; - if has_origin.is_some() { - *matl = cube_polyline.origin_matl.clone(); - } else { - *matl = cube_polyline.material.clone(); - } - } else { - // If there are more debug bounds than occupied cells, hide the extras. - *visibility = Visibility::Hidden; - } - } - - // If there are still occupied cells but no more debug bounds, we need to spawn more. - for (occupied_cell, has_origin) in occupied_cells { - let material = if has_origin.is_some() { - cube_polyline.origin_matl.clone() - } else { - cube_polyline.material.clone() - }; - commands.spawn(( - SpatialBundle::default(), - cube_polyline.polyline.clone(), - material, - occupied_cell.to_owned(), - DebugBounds, - )); - } -} - -/// Construct a polyline to match the [`FloatingOriginSettings`]. -pub fn build_cube( + mut gizmos: Gizmos, settings: Res, - mut commands: Commands, - mut polyline_materials: ResMut>, - mut polylines: ResMut>, + occupied_cells: Query<(&GridCell

, Option<&FloatingOrigin>)>, + origin_cells: Query<&GridCell

, With>, ) { - if !settings.is_changed() { - return; + let mut cells = HashMap::<_, (GridCell

, bool)>::new(); + let origin_cell = origin_cells.single(); + + for (cell, this_is_origin) in occupied_cells.iter() { + let (_, current_is_origin) = cells + .entry((cell.x, cell.y, cell.z)) + .or_insert((*cell, this_is_origin.is_some())); + + *current_is_origin |= this_is_origin.is_some(); } - let s = settings.grid_edge_length / 2.001; - - /* - (2)-----(3) Y - | \ | \ | - | (1)-----(0) MAX o---X - | | | | \ - MIN (6)--|--(7) | Z - \ | \ | - (5)-----(4) - */ - - let indices = [ - 0, 1, 1, 2, 2, 3, 3, 0, // Top ring - 4, 5, 5, 6, 6, 7, 7, 4, // Bottom ring - 0, 4, 8, 1, 5, 8, 2, 6, 8, 3, 7, // Verticals (8's are NaNs) - ]; - - let vertices = [ - Vec3::new(s, s, s), - Vec3::new(-s, s, s), - Vec3::new(-s, s, -s), - Vec3::new(s, s, -s), - Vec3::new(s, -s, s), - Vec3::new(-s, -s, s), - Vec3::new(-s, -s, -s), - Vec3::new(s, -s, -s), - Vec3::NAN, - ]; - - let polyline = polylines.add(Polyline { - vertices: indices.map(|i| vertices[i]).into(), - }); - - let material = polyline_materials.add(PolylineMaterial { - width: 1.5, - color: Color::rgb(2.0, 0.0, 0.0), - perspective: false, - ..Default::default() - }); - - let origin_matl = polyline_materials.add(PolylineMaterial { - width: 1.5, - color: Color::rgb(0.0, 0.0, 2.0), - perspective: false, - ..Default::default() - }); - - commands.insert_resource(CubePolyline { - polyline, - material, - origin_matl, - }) + for (cell, has_origin) in cells.values() { + let cell = cell - origin_cell; + gizmos.cuboid( + Transform::from_translation(settings.grid_position(&cell, &Transform::IDENTITY)) + .with_scale(Vec3::splat(settings.grid_edge_length)), + if *has_origin { + Color::BLUE + } else { + Color::GREEN + }, + ) + } } diff --git a/src/lib.rs b/src/lib.rs index 0206c0d..33e279d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -86,7 +86,7 @@ #![allow(clippy::type_complexity)] #![deny(missing_docs)] -use bevy::{math::DVec3, prelude::*, transform::TransformSystem}; +use bevy::{math::DVec3, prelude::*, reflect::TypePath, transform::TransformSystem}; use propagation::propagate_transforms; use std::marker::PhantomData; @@ -129,7 +129,7 @@ impl FloatingOriginPlugin

{ } } -impl Plugin for FloatingOriginPlugin

{ +impl Plugin for FloatingOriginPlugin

{ fn build(&self, app: &mut App) { app.insert_resource(FloatingOriginSettings::new( self.grid_edge_length, @@ -138,15 +138,9 @@ impl Plugin for FloatingOriginPlugin

{ .register_type::() .register_type::() .register_type::>() - .add_plugin(ValidParentCheckPlugin::::default()) - .configure_set(TransformSystem::TransformPropagate.in_base_set(CoreSet::PostUpdate)) - .edit_schedule(CoreSchedule::Startup, |schedule| { - schedule.configure_set( - TransformSystem::TransformPropagate.in_base_set(StartupSet::PostStartup), - ); - }) - // add transform systems to startup so the first update is "correct" - .add_startup_systems( + .add_plugins(ValidParentCheckPlugin::::default()) + .add_systems( + PostStartup, ( recenter_transform_on_grid::

, sync_simple_transforms::

@@ -160,6 +154,7 @@ impl Plugin for FloatingOriginPlugin

{ .in_set(TransformSystem::TransformPropagate), ) .add_systems( + PostUpdate, ( recenter_transform_on_grid::

, sync_simple_transforms::