refine queries

This commit is contained in:
Franz Dietrich 2022-08-09 22:37:26 +02:00
parent 92458b55e5
commit 1d1787b8c0

View File

@ -26,6 +26,7 @@ impl Plugin for TurtlePlugin {
pub struct Turtle { pub struct Turtle {
colors: Colors, colors: Colors,
commands: TurtleCommands, commands: TurtleCommands,
name: Name,
} }
impl Default for Turtle { impl Default for Turtle {
@ -58,6 +59,7 @@ impl Default for Turtle {
TurtleCommand::Right(Angle(150.)), TurtleCommand::Right(Angle(150.)),
TurtleCommand::Forward(Length(100.)), TurtleCommand::Forward(Length(100.)),
]), ]),
name: Name::new("Turtle"),
} }
} }
} }
@ -202,18 +204,20 @@ fn setup(mut commands: Commands) {
}, },
Transform::identity(), Transform::identity(),
)) ))
.insert(animator); .insert(animator)
.insert(TurtleShape);
} }
fn draw_lines( fn draw_lines(
mut commands: Commands, mut commands: Commands,
tcmd: Query<&TurtleCommands>, tcmd: Query<&TurtleCommands>,
mut query_event: EventReader<TweenCompleted>, mut query_event: EventReader<TweenCompleted>, // TODO: howto attach only to the right event?
) { ) {
for ev in query_event.iter() { for ev in query_event.iter() {
let index = ev.user_data; let index = ev.user_data;
let tcmd = tcmd.get_single().unwrap();
let t = tcmd.lines.get(index as usize).unwrap(); for t in tcmd.iter() {
let t = t.lines.get(index as usize).unwrap();
match t { match t {
TurtleGraphElement::TurtleLine { start, end } => { TurtleGraphElement::TurtleLine { start, end } => {
commands.spawn_bundle(GeometryBuilder::build_as( commands.spawn_bundle(GeometryBuilder::build_as(
@ -229,13 +233,11 @@ fn draw_lines(
} }
} }
} }
}
/// The sprite is animated by changing its translation depending on the time that has passed since
/// the last frame.
fn keypresses( fn keypresses(
//time: Res<Time>,
keys: Res<Input<KeyCode>>, keys: Res<Input<KeyCode>>,
mut qry: Query<&mut Animator<Transform>>, mut qry: Query<&mut Animator<Transform>, With<TurtleShape>>,
mut tcmd: Query<&mut TurtleCommands>, mut tcmd: Query<&mut TurtleCommands>,
) { ) {
if keys.just_pressed(KeyCode::W) { if keys.just_pressed(KeyCode::W) {