bevy 0.11

This commit is contained in:
Aevyrie 2023-07-17 22:49:24 -07:00
parent 1a1f7ebc39
commit 6847936f1a
No known key found for this signature in database
GPG Key ID: ADE96E69CCF36AA0
7 changed files with 72 additions and 183 deletions

View File

@ -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"]

View File

@ -5,13 +5,14 @@ use big_space::{FloatingOrigin, GridCell};
fn main() {
App::new()
.add_plugins(DefaultPlugins.build().disable::<TransformPlugin>())
.add_plugin(big_space::FloatingOriginPlugin::<i64>::new(0.5, 0.01))
.add_plugin(big_space::debug::FloatingOriginDebugPlugin::<i64>::default())
.add_plugins((
DefaultPlugins.build().disable::<TransformPlugin>(),
big_space::FloatingOriginPlugin::<i64>::new(0.5, 0.01),
big_space::debug::FloatingOriginDebugPlugin::<i64>::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()
}

View File

@ -11,16 +11,16 @@ use big_space::{
fn main() {
App::new()
.add_plugins(DefaultPlugins.build().disable::<TransformPlugin>())
.add_plugin(big_space::FloatingOriginPlugin::<i128>::default())
.add_plugin(big_space::debug::FloatingOriginDebugPlugin::<i128>::default())
.add_plugin(big_space::camera::CameraControllerPlugin::<i128>::default())
.add_plugin(bevy_framepace::FramepacePlugin)
.add_plugins((
DefaultPlugins.build().disable::<TransformPlugin>(),
big_space::FloatingOriginPlugin::<i128>::default(),
big_space::debug::FloatingOriginDebugPlugin::<i128>::default(),
big_space::camera::CameraControllerPlugin::<i128>::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<AssetServer>) {
.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,

View File

@ -10,12 +10,12 @@ use big_space::{FloatingOrigin, FloatingSpatialBundle, GridCell};
fn main() {
App::new()
.add_plugins(DefaultPlugins.build().disable::<TransformPlugin>())
.add_plugin(big_space::FloatingOriginPlugin::<i64>::default())
.add_startup_system(setup_scene)
.add_startup_system(setup_ui)
.add_system(rotator_system)
.add_system(toggle_plugin)
.add_plugins((
DefaultPlugins.build().disable::<TransformPlugin>(),
big_space::FloatingOriginPlugin::<i64>::default(),
))
.add_systems(Startup, (setup_scene, setup_ui))
.add_systems(Update, (rotator_system, toggle_plugin))
.run()
}

View File

@ -18,14 +18,14 @@ pub struct CameraControllerPlugin<P: GridPrecision>(PhantomData<P>);
impl<P: GridPrecision> Plugin for CameraControllerPlugin<P> {
fn build(&self, app: &mut App) {
app.init_resource::<CameraInput>().add_systems(
PostUpdate,
(
default_camera_inputs
.before(camera_controller::<P>)
.run_if(|input: Res<CameraInput>| !input.defaults_disabled),
nearest_objects.before(camera_controller::<P>),
camera_controller::<P>.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;

View File

@ -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<P: GridPrecision>(PhantomData<P>);
impl<P: GridPrecision> Plugin for FloatingOriginDebugPlugin<P> {
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::<P>
.in_base_set(CoreSet::PostUpdate)
.after(crate::recenter_transform_on_grid::<P>)
.before(crate::update_global_from_grid::<P>),
);
app.add_systems(
PostUpdate,
update_debug_bounds::<P>
.after(crate::recenter_transform_on_grid::<P>)
.before(crate::update_global_from_grid::<P>),
);
}
}
/// 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<Polyline>,
material: Handle<PolylineMaterial>,
origin_matl: Handle<PolylineMaterial>,
}
/// Update the rendered debug bounds to only highlight occupied [`GridCell`]s. [`DebugBounds`] are
/// spawned or hidden as needed.
pub fn update_debug_bounds<P: GridPrecision>(
mut commands: Commands,
cube_polyline: Res<CubePolyline>,
occupied_cells: Query<(&GridCell<P>, Option<&FloatingOrigin>), Without<DebugBounds>>,
mut debug_bounds: Query<
(
&mut GridCell<P>,
&mut Handle<Polyline>,
&mut Handle<PolylineMaterial>,
&mut Visibility,
),
With<DebugBounds>,
>,
) {
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<FloatingOriginSettings>,
mut commands: Commands,
mut polyline_materials: ResMut<Assets<PolylineMaterial>>,
mut polylines: ResMut<Assets<Polyline>>,
occupied_cells: Query<(&GridCell<P>, Option<&FloatingOrigin>)>,
origin_cells: Query<&GridCell<P>, With<FloatingOrigin>>,
) {
if !settings.is_changed() {
return;
let mut cells = HashMap::<_, (GridCell<P>, 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
},
)
}
}

View File

@ -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<P: GridPrecision> FloatingOriginPlugin<P> {
}
}
impl<P: GridPrecision> Plugin for FloatingOriginPlugin<P> {
impl<P: GridPrecision + Reflect + FromReflect + TypePath> Plugin for FloatingOriginPlugin<P> {
fn build(&self, app: &mut App) {
app.insert_resource(FloatingOriginSettings::new(
self.grid_edge_length,
@ -138,15 +138,9 @@ impl<P: GridPrecision> Plugin for FloatingOriginPlugin<P> {
.register_type::<Transform>()
.register_type::<GlobalTransform>()
.register_type::<GridCell<P>>()
.add_plugin(ValidParentCheckPlugin::<GlobalTransform>::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::<GlobalTransform>::default())
.add_systems(
PostStartup,
(
recenter_transform_on_grid::<P>,
sync_simple_transforms::<P>
@ -160,6 +154,7 @@ impl<P: GridPrecision> Plugin for FloatingOriginPlugin<P> {
.in_set(TransformSystem::TransformPropagate),
)
.add_systems(
PostUpdate,
(
recenter_transform_on_grid::<P>,
sync_simple_transforms::<P>