Skip to content

Commit

Permalink
refactor: populated, single
Browse files Browse the repository at this point in the history
  • Loading branch information
mrchantey committed Nov 9, 2024
1 parent 5997659 commit 39c38e6
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 46 deletions.
9 changes: 3 additions & 6 deletions crates/beet_examples/src/components/camera_distance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,21 @@ impl CameraDistance {

pub fn camera_distance(
mut resize: EventReader<WindowResized>,
main_window: Query<&Window, With<PrimaryWindow>>,
main_window: Single<&Window, With<PrimaryWindow>>,
camera_added: Query<
(),
(
With<CameraDistance>,
Or<(Added<Camera>, Added<CameraDistance>)>,
),
>,
mut query: Query<(&mut Transform, &Projection, &CameraDistance)>,
mut query: Populated<(&mut Transform, &Projection, &CameraDistance)>,
) {
if camera_added.iter().count() == 0 && resize.read().count() == 0 {
return;
}
let Ok(window) = main_window.get_single() else {
return;
};

let aspect_ratio = window.width() as f32 / window.height() as f32;
let aspect_ratio = main_window.width() as f32 / main_window.height() as f32;

for (mut transform, projection, camera_distance) in query.iter_mut() {
let Projection::Perspective(perspective) = projection else {
Expand Down
2 changes: 1 addition & 1 deletion crates/beet_examples/src/components/collectable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const TURNS_PER_SECOND: f32 = 0.5;

pub fn rotate_collectables(
time: Res<Time>,
mut query: Query<&mut Transform, With<Collectable>>,
mut query: Populated<&mut Transform, With<Collectable>>,
) {
for mut transform in query.iter_mut() {
let angle = time.delta_secs() * TAU * TURNS_PER_SECOND;
Expand Down
35 changes: 11 additions & 24 deletions crates/beet_examples/src/components/follow_cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,13 @@ use bevy::prelude::*;
pub struct FollowCursor2d;

pub fn follow_cursor_2d(
camera_query: Query<(&Camera, &GlobalTransform)>,
mut cursor_query: Query<&mut Transform, With<FollowCursor2d>>,
windows: Query<&Window>,
camera_query: Single<(&Camera, &GlobalTransform)>,
windows: Single<&Window>,
mut cursor_query: Populated<&mut Transform, With<FollowCursor2d>>,
) {
let Ok((camera, camera_transform)) = camera_query.get_single() else {
return;
};
let (camera, camera_transform) = *camera_query;

let Some(cursor_position) = windows
.get_single()
.ok()
.map(|w| w.cursor_position())
.flatten()
else {
let Some(cursor_position) = windows.cursor_position() else {
return;
};

Expand Down Expand Up @@ -80,22 +73,16 @@ impl Default for FollowCursor3d {
}

pub fn follow_cursor_3d(
camera_query: Query<(&Camera, &GlobalTransform)>,
mut cursor_query: Query<(&mut Transform, &FollowCursor3d)>,
windows: Query<&Window>,
camera_query: Single<(&Camera, &GlobalTransform)>,
windows: Single<&Window>,
mut cursor_query: Populated<(&mut Transform, &FollowCursor3d)>,
) {
let Ok((camera, camera_transform)) = camera_query.get_single() else {
return;
};
let (camera, camera_transform) = *camera_query;

let Some(cursor_position) = windows
.get_single()
.ok()
.map(|w| w.cursor_position())
.flatten()
else {
let Some(cursor_position) = windows.cursor_position() else {
return;
};

let Ok(ray) = camera.viewport_to_world(camera_transform, cursor_position)
else {
return;
Expand Down
2 changes: 1 addition & 1 deletion crates/beet_examples/src/components/randomize_position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl Default for RandomizePosition {

pub fn randomize_position(
mut commands: Commands,
mut query: Query<
mut query: Populated<
(Entity, &mut Transform, &RandomizePosition),
Added<RandomizePosition>,
>,
Expand Down
4 changes: 2 additions & 2 deletions crates/beet_examples/src/components/wrap_around.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl WrapAround {

pub fn wrap_around(
wrap: Res<WrapAround>,
mut query: Query<&mut Transform, Changed<Transform>>,
mut query: Populated<&mut Transform, Changed<Transform>>,
) {
for mut transform in query.iter_mut() {
if transform.translation.x > wrap.half_extents.x {
Expand Down Expand Up @@ -62,7 +62,7 @@ pub fn wrap_around(

pub fn update_wrap_around(
mut wrap_around: ResMut<WrapAround>,
windows: Query<&Window, Changed<Window>>,
windows: Populated<&Window, Changed<Window>>,
) {
for window in windows.iter() {
wrap_around.set_if_neq(WrapAround::from_window_size(window.size()));
Expand Down
2 changes: 1 addition & 1 deletion crates/beet_flow/examples/derive_action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use bevy::prelude::*;
#[global_observers(log_name_on_run, log_name_on_run)]
struct LogOnRun(pub String);

fn log_name_on_run(trigger: Trigger<OnRun>, query: Query<&LogOnRun>) {
fn log_name_on_run(trigger: Trigger<OnRun>, query: Populated<&LogOnRun>) {
let name = query.get(trigger.entity()).map(|n| n.0.as_str()).unwrap();
println!("log_name_on_run: {name}");
}
Expand Down
6 changes: 3 additions & 3 deletions crates/beet_flow/examples/derive_action_generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ struct LogOnRun<T: 'static + Send + Sync + ToString>(pub T);

fn log_on_run<T: 'static + Send + Sync + ToString>(
trigger: Trigger<OnRun>,
query: Query<&LogOnRun<T>>,
query: Populated<&LogOnRun<T>>,
) {
let name = query
.get(trigger.entity())
Expand All @@ -16,11 +16,11 @@ fn log_on_run<T: 'static + Send + Sync + ToString>(
println!("log_on_run: {name}");
}

fn log_name_on_run(trigger: Trigger<OnRun>, query: Query<&Name>) {
fn log_name_on_run(trigger: Trigger<OnRun>, query: Populated<&Name>) {
let name = query
.get(trigger.entity())
.map(|n| n.as_str())
.unwrap_or("");
.unwrap_or("unnamed");
println!("log_name_on_run: {name}");
}

Expand Down
5 changes: 2 additions & 3 deletions crates/beet_flow/examples/derive_action_systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ use bevy::prelude::*;
#[category(ActionCategory::Agent)]
struct LogOnRun(pub String);

fn log_on_run(query: Query<&LogOnRun, Added<Running>>) {
let name = &query.single().0;
println!("log_on_run: {name}");
fn log_on_run(query: Single<&LogOnRun, Added<Running>>) {
println!("log_on_run: {}", &query.0);
}

fn main() {
Expand Down
2 changes: 1 addition & 1 deletion crates/beet_flow/examples/observer_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub struct Event2;

fn main() {
let mut world = World::new();
world.add_observer(|_trigger: Trigger<Event2>, query: Query<&Foo>| {
world.add_observer(|_trigger: Trigger<Event2>, query: Populated<&Foo>| {
println!("Event2 triggered, num components: {}", query.iter().len());
});
world.add_observer(|_trigger: Trigger<Event1>, mut commands: Commands| {
Expand Down
8 changes: 4 additions & 4 deletions crates/beet_flow/examples/observers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ enum OnRunResult {
fn sequence_start(
trigger: Trigger<OnRun>,
mut commands: Commands,
query: Query<&Children>,
query: Populated<&Children>,
) {
if let Ok(children) = query.get(trigger.entity()) {
if let Some(first_child) = children.iter().next() {
Expand All @@ -28,7 +28,7 @@ fn sequence_start(
fn sequence_next(
trigger: Trigger<OnChildResult>,
mut commands: Commands,
query: Query<&Children>,
query: Populated<&Children>,
) {
if trigger.event().result == OnRunResult::Failure {
commands.trigger_targets(OnRunResult::Failure, trigger.entity());
Expand All @@ -48,7 +48,7 @@ fn sequence_next(
}
}

fn log_on_run(trigger: Trigger<OnRun>, names: Query<&Name>) {
fn log_on_run(trigger: Trigger<OnRun>, names: Populated<&Name>) {
let name = names
.get(trigger.entity())
.map(|n| n.as_str())
Expand All @@ -59,7 +59,7 @@ fn log_on_run(trigger: Trigger<OnRun>, names: Query<&Name>) {
fn succeed_on_run(
trigger: Trigger<OnRun>,
mut commands: Commands,
parents: Query<&Parent>,
parents: Populated<&Parent>,
) {
commands.trigger_targets(OnRunResult::Success, trigger.entity());
if let Some(parent) = parents.get(trigger.entity()).ok() {
Expand Down

0 comments on commit 39c38e6

Please sign in to comment.