Skip to content

Commit

Permalink
movetowards example changes
Browse files Browse the repository at this point in the history
  • Loading branch information
laundmo committed Jul 6, 2024
1 parent 6f52049 commit 302b966
Showing 1 changed file with 49 additions and 23 deletions.
72 changes: 49 additions & 23 deletions examples/movetowards.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
use bevy::{log::LogPlugin, prelude::*, window::PrimaryWindow};
use bevy::{
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
ecs::query,
math::vec2,
prelude::*,
time::common_conditions::on_timer,
window::PrimaryWindow,
};
use bevy_spatial::{
kdtree::KDTree3, AutomaticUpdate, SpatialAccess, SpatialStructure, TransformMode,
};
Expand All @@ -12,16 +19,24 @@ struct MoveTowards;

fn main() {
App::new()
.add_plugins(DefaultPlugins.build().disable::<LogPlugin>())
.add_plugins(DefaultPlugins)
.add_plugins(
AutomaticUpdate::<NearestNeighbour>::new()
.with_spatial_ds(SpatialStructure::KDTree3)
.with_frequency(Duration::from_secs(1))
.with_transform(TransformMode::Transform),
)
.add_plugins(FrameTimeDiagnosticsPlugin::default())
.add_plugins(LogDiagnosticsPlugin::default())
.add_systems(Startup, setup)
.add_systems(Update, mouseclick)
.add_systems(Update, move_to)
.add_systems(
Update,
(
mouseclick,
log_num.run_if(on_timer(Duration::from_secs_f32(0.5))),
move_to,
),
)
.run();
}

Expand Down Expand Up @@ -58,26 +73,33 @@ fn mouseclick(
) {
let win = window.single();
let (cam, cam_t) = cam.single();
if mouse_input.just_pressed(MouseButton::Left) {
if mouse_input.pressed(MouseButton::Left) {
if let Some(pos) = win.cursor_position() {
commands.spawn((
MoveTowards,
SpriteBundle {
sprite: Sprite {
color: Color::srgb(0.15, 0.15, 1.0),
custom_size: Some(Vec2::new(10.0, 10.0)),
..default()
},
transform: Transform {
translation: cam
.viewport_to_world_2d(cam_t, pos)
.unwrap_or(Vec2::ZERO)
.extend(0.0),
..default()
},
..default()
},
));
for xoff in -1..=1 {
for yoff in -1..=1 {
commands.spawn((
MoveTowards,
SpriteBundle {
sprite: Sprite {
color: Color::srgb(0.15, 0.15, 1.0),
custom_size: Some(Vec2::new(10.0, 10.0)),
..default()
},
transform: Transform {
translation: cam
.viewport_to_world_2d(
cam_t,
pos + vec2(xoff as f32 * 16., yoff as f32 * 16.),
)
.unwrap_or(Vec2::ZERO)
.extend(0.0),
..default()
},
..default()
},
));
}
}
}
}
}
Expand All @@ -94,3 +116,7 @@ fn move_to(
}
}
}

fn log_num(query: Query<(), With<MoveTowards>>) {
info!(target: "blue", amount = query.iter().len())
}

0 comments on commit 302b966

Please sign in to comment.